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 Aug 10, 2022
1 parent 3f4f7ab commit 1a70918
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions upb/arena.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ static upb_Arena* arena_findroot(upb_Arena* a) {
return a;
}

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

mem_block* block = arena->freelist;

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

return memsize;
}

static void upb_Arena_addblock(upb_Arena* a, upb_Arena* root, void* ptr,
size_t size) {
mem_block* block = ptr;
Expand Down
1 change: 1 addition & 0 deletions upb/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ 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);

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

Expand Down

0 comments on commit 1a70918

Please sign in to comment.