Skip to content

Commit

Permalink
Do not iterate over heap blocks in GC_get_memory_use
Browse files Browse the repository at this point in the history
GC_get_memory_use() return the total memory use (in bytes) by all
allocated blocks, but this is equal to GC_get_heap_size() minus
GC_get_free_bytes().

* include/gc/gc.h (GC_get_memory_use): Refine comment.
* misc.c (block_add_size): Remove static function definition.
* misc.c (GC_get_memory_use): Add assertion that
GC_heapsize>=GC_large_free_bytes; set bytes to
GC_heapsize-GC_large_free_bytes instead of calling
GC_apply_to_all_blocks(block_add_size).
  • Loading branch information
ivmai committed Jul 29, 2024
1 parent 14cf9b1 commit 6d37227
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
5 changes: 3 additions & 2 deletions include/gc/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -919,8 +919,9 @@ GC_API size_t GC_CALL GC_get_prof_stats(struct GC_prof_stats_s *,
/* typically to avoid data race on multiprocessors. */
GC_API size_t GC_CALL GC_get_size_map_at(int i);

/* Count total memory use (in bytes) by all allocated blocks. Acquires */
/* the allocator lock in the reader mode. */
/* Return the total memory use (in bytes) by all allocated blocks. */
/* The result is equal to GC_get_heap_size() - GC_get_free_bytes(). */
/* Acquires the allocator lock in the reader mode. */
GC_API GC_word GC_CALL GC_get_memory_use(void);

/* Disable garbage collection. Even GC_gcollect calls will be */
Expand Down
15 changes: 3 additions & 12 deletions misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2483,22 +2483,13 @@ GC_API void * GC_CALL GC_do_blocking(GC_fn_type fn, void * client_data)
}
#endif /* !NO_DEBUGGING */

static void GC_CALLBACK block_add_size(struct hblk *h, void *pbytes)
{
const hdr *hhdr = HDR(h);

*(word *)pbytes += (hhdr -> hb_sz + HBLKSIZE-1) & ~(HBLKSIZE-1);
# if defined(CPPCHECK)
GC_noop1_ptr(h);
# endif
}

GC_API GC_word GC_CALL GC_get_memory_use(void)
{
word bytes = 0;
word bytes;

READER_LOCK();
GC_apply_to_all_blocks(block_add_size, &bytes);
GC_ASSERT(GC_heapsize >= GC_large_free_bytes);
bytes = GC_heapsize - GC_large_free_bytes;
READER_UNLOCK();
return bytes;
}
Expand Down

0 comments on commit 6d37227

Please sign in to comment.