mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
More uniform solution for memory logging.
This commit is contained in:
parent
7296b83264
commit
6d5bd6a74d
@ -5,13 +5,13 @@
|
||||
|
||||
(register memory-balance (Fn [] Long))
|
||||
(register reset-memory-balance! (Fn [] ()))
|
||||
(register log-memory-balance Bool)
|
||||
(register log-memory-balance! (Fn [Bool] ()))
|
||||
|
||||
(defmacro memory-logged [form]
|
||||
(list 'do
|
||||
'(set! &Debug.log-memory-balance true)
|
||||
'(Debug.log-memory-balance! true)
|
||||
form
|
||||
'(set! &Debug.log-memory-balance false)))
|
||||
'(Debug.log-memory-balance! false)))
|
||||
|
||||
(defn trace [x]
|
||||
(do
|
||||
|
17
core/core.h
17
core/core.h
@ -15,11 +15,11 @@ typedef char* string;
|
||||
|
||||
#ifdef LOG_MEMORY
|
||||
long malloc_balance_counter = 0;
|
||||
bool Debug_log_MINUS_memory_MINUS_balance = false;
|
||||
bool log_memory_balance = false;
|
||||
|
||||
void *logged_malloc(size_t size) {
|
||||
void *ptr = malloc(size);
|
||||
if(Debug_log_MINUS_memory_MINUS_balance) {
|
||||
if(log_memory_balance) {
|
||||
printf("MALLOC: %p (%ld bytes)\n", ptr, size);
|
||||
}
|
||||
malloc_balance_counter++;
|
||||
@ -27,7 +27,7 @@ void *logged_malloc(size_t size) {
|
||||
}
|
||||
|
||||
void logged_free(void *ptr) {
|
||||
if(Debug_log_MINUS_memory_MINUS_balance) {
|
||||
if(log_memory_balance) {
|
||||
printf("FREE: %p\n", ptr);
|
||||
}
|
||||
free(ptr);
|
||||
@ -40,6 +40,10 @@ void logged_free(void *ptr) {
|
||||
/* } */
|
||||
}
|
||||
|
||||
void Debug_log_MINUS_memory_MINUS_balance_BANG_(bool value) {
|
||||
log_memory_balance = value;
|
||||
}
|
||||
|
||||
#define CARP_MALLOC(size) logged_malloc(size)
|
||||
#define CARP_FREE(ptr) logged_free(ptr)
|
||||
|
||||
@ -56,8 +60,6 @@ void Debug_reset_MINUS_memory_MINUS_balance_BANG_() {
|
||||
#define CARP_MALLOC(size) malloc(size)
|
||||
#define CARP_FREE(ptr) free(ptr)
|
||||
|
||||
bool Debug_log_MINUS_memory_MINUS_balance;
|
||||
|
||||
long Debug_memory_MINUS_balance() {
|
||||
printf("Error - calling 'memory-balance' without compiling with LOG_MEMORY enabled.\n");
|
||||
exit(1);
|
||||
@ -69,6 +71,11 @@ void Debug_reset_MINUS_memory_MINUS_balance_BANG_() {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void Debug_log_MINUS_memory_MINUS_balance_BANG_(bool value) {
|
||||
printf("Error - calling 'log-memory-balance!' without compiling with LOG_MEMORY enabled.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// Array
|
||||
|
@ -4,21 +4,18 @@
|
||||
(local-include "core.h")
|
||||
(project-set! "echoCompilationCommand" "true")
|
||||
|
||||
;; (defn better-repeat [n inpt]
|
||||
;; (let [str @""]
|
||||
;; (do
|
||||
;; (for [i 0 n]
|
||||
;; (set! &str (append str @inpt)))
|
||||
;; str)))
|
||||
(defn better-repeat [n inpt]
|
||||
(let [str @""]
|
||||
(do
|
||||
(for [i 0 n]
|
||||
(set! &str (append str @inpt)))
|
||||
str)))
|
||||
|
||||
;; (defn main []
|
||||
;; (do
|
||||
;; (Debug.reset-memory-balance!)
|
||||
;; ;;(set! &Debug.log-memory-balance true)
|
||||
;; (let []
|
||||
;; (IO.println &(better-repeat 5 "yeah")))
|
||||
;; (IO.println (refstr (Debug.memory-balance)))
|
||||
;; ))
|
||||
(defn main []
|
||||
(do
|
||||
(let []
|
||||
(IO.println &(better-repeat 5 "yeah")))
|
||||
(IO.println (refstr (Debug.memory-balance)))))
|
||||
|
||||
;;;; GENERIC TYPES
|
||||
;; (deftype (Pair a b) [ma a mb b])
|
||||
|
Loading…
Reference in New Issue
Block a user