]> granicus.if.org Git - gc/commitdiff
Code refactoring, add test and document GC_get_memory_use
authorIvan Maidanski <ivmai@mail.ru>
Thu, 9 Jul 2015 21:29:43 +0000 (00:29 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Thu, 9 Jul 2015 21:29:43 +0000 (00:29 +0300)
* include/gc.h (GC_get_memory_use): Add comment.
* include/gc.h (GC_get_memory_use): Decorate with GC_CALL; change
return type from long to size_t.
* misc.c (GC_get_memory_use): Likewise.
* misc.c (get_size): Rename to block_add_size.
* misc.c (get_size): Rename lptr parameter to pbytes; code refactoring.
* misc.c (GC_get_memory_use): Rename "c" local variable to "bytes";
add DCL_LOCK_STATE.
* tests/test.c (check_heap_stats): Call GC_get_memory_use (and print
returned value).

include/gc.h
misc.c
tests/test.c

index 996586b2a199d82c2feb5e04f68974cda5578631..ad5da16700fe9a77a2b592bd125896de977c63f9 100644 (file)
@@ -724,7 +724,9 @@ 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);
+/* Count total memory use in bytes by all allocated blocks.  Acquires   */
+/* the lock.                                                            */
+GC_API size_t GC_CALL GC_get_memory_use(void);
 
 /* Disable garbage collection.  Even GC_gcollect calls will be          */
 /* ineffective.                                                         */
diff --git a/misc.c b/misc.c
index 55d078c8c773930379aefbf8d8c1318ea81e1f4e..344d7b83b304df6cecd7dfe70bf23a9e2594b349 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -2014,23 +2014,22 @@ 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)
+static void block_add_size(struct hblk *h, word pbytes)
 {
   hdr *hhdr = HDR(h);
-  long bytes = WORDS_TO_BYTES(hhdr->hb_sz);
-
-  bytes += HBLKSIZE-1;
-  bytes &= ~(HBLKSIZE-1);
-
-  *(long *)lptr += bytes;
+  *(word *)pbytes += (WORDS_TO_BYTES(hhdr->hb_sz) + (HBLKSIZE - 1))
+                        & ~(HBLKSIZE - 1);
 }
-long GC_get_memory_use()
+
+GC_API size_t GC_CALL GC_get_memory_use(void)
 {
-  long c = 0;
+  word bytes = 0;
+  DCL_LOCK_STATE;
+
   LOCK();
-  GC_apply_to_all_blocks(get_size, (word)&c);
+  GC_apply_to_all_blocks(block_add_size, (word)(&bytes));
   UNLOCK();
-  return c;
+  return (size_t)bytes;
 }
 
 /* Getter functions for the public Read-only variables.                 */
index 7d91d2dd68e6292194af9d0fee70e52cfc77045c..9607bddf591239f5f8ca161471b629ce165fc0a4 100644 (file)
@@ -1519,6 +1519,8 @@ void check_heap_stats(void)
 #   endif
     GC_printf("Total number of bytes allocated is %lu\n",
                   (unsigned long)GC_get_total_bytes());
+    GC_printf("Total memory use by allocated blocks is %lu bytes\n",
+              (unsigned long)GC_get_memory_use());
     GC_printf("Final heap size is %lu bytes\n",
                   (unsigned long)GC_get_heap_size());
     if (GC_get_total_bytes() < n_tests *