From 67f8e2523ed2bac0a89011b01225b43908ae18ec Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Thu, 9 Jul 2015 00:49:31 +0300 Subject: [PATCH] Add API function to calculate total memory in use by all GC blocks (Apply part of commit db2b9f1 from 'racket_gc' branch.) * include/gc.h (GC_get_memory_use): New API function declaration. * misc.c (get_size, GC_get_memory_use): New function. --- include/gc.h | 2 ++ misc.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/gc.h b/include/gc.h index 1f509561..996586b2 100644 --- a/include/gc.h +++ b/include/gc.h @@ -724,6 +724,8 @@ GC_API size_t GC_CALL GC_get_prof_stats(struct GC_prof_stats_s *, size_t /* stats_sz */); #endif +GC_API long GC_get_memory_use(void); + /* Disable garbage collection. Even GC_gcollect calls will be */ /* ineffective. */ GC_API void GC_CALL GC_disable(void); diff --git a/misc.c b/misc.c index e67a76f1..55d078c8 100644 --- a/misc.c +++ b/misc.c @@ -2014,6 +2014,25 @@ GC_API void * GC_CALL GC_do_blocking(GC_fn_type fn, void * client_data) } #endif /* !NO_DEBUGGING */ +static void get_size(struct hblk *h, word lptr) +{ + hdr *hhdr = HDR(h); + long bytes = WORDS_TO_BYTES(hhdr->hb_sz); + + bytes += HBLKSIZE-1; + bytes &= ~(HBLKSIZE-1); + + *(long *)lptr += bytes; +} +long GC_get_memory_use() +{ + long c = 0; + LOCK(); + GC_apply_to_all_blocks(get_size, (word)&c); + UNLOCK(); + return c; +} + /* Getter functions for the public Read-only variables. */ /* GC_get_gc_no() is unsynchronized and should be typically called */ -- 2.40.0