mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
42 lines
1.2 KiB
Plaintext
42 lines
1.2 KiB
Plaintext
(defmodule Debug
|
|
|
|
(defdynamic sanitize-addresses []
|
|
(add-cflag "-fsanitize=address"))
|
|
|
|
(register memory-balance (Fn [] Long))
|
|
(register reset-memory-balance! (Fn [] ()))
|
|
(register log-memory-balance! (Fn [Bool] ()))
|
|
|
|
(defmacro memory-logged [form]
|
|
(list 'do
|
|
'(Debug.log-memory-balance! true)
|
|
form
|
|
'(Debug.log-memory-balance! false)))
|
|
|
|
(defmacro assert-balanced [form]
|
|
(list 'let '[balance (Debug.memory-balance)]
|
|
(list 'do
|
|
(list 'let []
|
|
form)
|
|
'(if (= balance (Debug.memory-balance))
|
|
()
|
|
(do (IO.println &(fmt "Invalid memory balance: %d" (Debug.memory-balance)))
|
|
(System.exit 1)))
|
|
())))
|
|
|
|
(defn trace [x]
|
|
(do
|
|
(IO.println &(str &x))
|
|
x))
|
|
|
|
)
|
|
|
|
(defmacro assert [expr]
|
|
(list 'if (list '= true expr)
|
|
()
|
|
(list 'do
|
|
(list 'IO.println (list 'ref
|
|
(list 'String.append (list 'String.copy "Assertion failed ")
|
|
(list 'String.copy (source-location)))))
|
|
'(System.exit 1))))
|