]> granicus.if.org Git - gc/commitdiff
Add API function to calculate total memory in use by all GC blocks
authorEli Barzilay <eli@racket-lang.org>
Wed, 8 Jul 2015 21:49:31 +0000 (00:49 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Wed, 8 Jul 2015 21:49:31 +0000 (00:49 +0300)
(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
misc.c

index 1f5095613414fe90c91e63fa3ca5814898d3a0db..996586b2a199d82c2feb5e04f68974cda5578631 100644 (file)
@@ -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 e67a76f17050bb6774d5c6c307515e942510ad51..55d078c8c773930379aefbf8d8c1318ea81e1f4e 100644 (file)
--- 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      */