Skip to content

Commit

Permalink
Add upb_Arena_SpaceAllocated function
Browse files Browse the repository at this point in the history
Ref: protocolbuffers/protobuf#10291

Ruby types defined though native extensions should register
a function that report their memory footprint in bytes.

This feature is used by various memory profiling tools.
  • Loading branch information
byroot committed Sep 13, 2022
1 parent 3f4f7ab commit f127673
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions upb/arena.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ static upb_Arena* arena_findroot(upb_Arena* a) {
return a;
}

size_t upb_Arena_SpaceAllocated(upb_Arena* arena) {
arena = arena_findroot(arena);
size_t memsize = 0;

mem_block* block = arena->freelist;

while (block) {
memsize += sizeof(mem_block) + block->size;
block = block->next;
}

return memsize;
}

uint32_t upb_Arena_DebugRefCount(upb_Arena* arena) {
return arena_findroot(arena)->refcount;
}

static void upb_Arena_addblock(upb_Arena* a, upb_Arena* root, void* ptr,
size_t size) {
mem_block* block = ptr;
Expand Down
2 changes: 2 additions & 0 deletions upb/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ void upb_Arena_Free(upb_Arena* a);
bool upb_Arena_AddCleanup(upb_Arena* a, void* ud, upb_CleanupFunc* func);
bool upb_Arena_Fuse(upb_Arena* a, upb_Arena* b);
void* _upb_Arena_SlowMalloc(upb_Arena* a, size_t size);
size_t upb_Arena_SpaceAllocated(upb_Arena* arena);
uint32_t upb_Arena_DebugRefCount(upb_Arena* arena);

UPB_INLINE upb_alloc* upb_Arena_Alloc(upb_Arena* a) { return (upb_alloc*)a; }

Expand Down

0 comments on commit f127673

Please sign in to comment.