Skip to content

Commit e680e2e

Browse files
committed
Debug: keep track of how buffer chunks were allocated.
1 parent d6ab69a commit e680e2e

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

ext/msgpack/buffer.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ void msgpack_buffer_init(msgpack_buffer_t* b)
6666
static void _msgpack_buffer_chunk_destroy(msgpack_buffer_chunk_t* c)
6767
{
6868
if(c->mem != NULL) {
69-
if(!msgpack_rmem_free(&s_rmem, c->mem)) {
69+
if(c->rmem) {
70+
if(!msgpack_rmem_free(&s_rmem, c->mem)) {
71+
rb_bug("Failed to free an rmem pointer, memory leak?");
72+
}
73+
} else {
7074
xfree(c->mem);
7175
}
7276
/* no needs to update rmem_owner because chunks will not be
@@ -349,6 +353,8 @@ static inline void* _msgpack_buffer_chunk_malloc(
349353
size_t required_size, size_t* allocated_size)
350354
{
351355
if(required_size <= MSGPACK_RMEM_PAGE_SIZE) {
356+
c->rmem = true;
357+
352358
if((size_t)(b->rmem_end - b->rmem_last) < required_size) {
353359
/* alloc new rmem page */
354360
*allocated_size = MSGPACK_RMEM_PAGE_SIZE;
@@ -379,6 +385,7 @@ static inline void* _msgpack_buffer_chunk_malloc(
379385
*allocated_size = required_size;
380386
void* mem = xmalloc(required_size);
381387
c->mem = mem;
388+
c->rmem = false;
382389
return mem;
383390
}
384391

ext/msgpack/buffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ struct msgpack_buffer_chunk_t {
7878
void* mem;
7979
msgpack_buffer_chunk_t* next;
8080
VALUE mapped_string; /* RBString or NO_MAPPED_STRING */
81+
bool rmem;
8182
};
8283

8384
union msgpack_buffer_cast_block_t {

ext/msgpack/unpacker.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ void _msgpack_unpacker_init(msgpack_unpacker_t* uk)
8686

8787
static inline void _msgpack_unpacker_free_stack(msgpack_unpacker_stack_t* stack) {
8888
#ifdef UNPACKER_STACK_RMEM
89-
msgpack_rmem_free(&s_stack_rmem, stack->data);
89+
if (!msgpack_rmem_free(&s_stack_rmem, stack->data)) {
90+
rb_bug("Failed to free an rmem pointer, memory leak?");
91+
}
9092
#else
9193
xfree(stack->data);
9294
#endif

0 commit comments

Comments
 (0)