null+****@clear*****
null+****@clear*****
2011年 1月 10日 (月) 17:44:27 JST
Kouhei Sutou 2011-01-10 08:44:27 +0000 (Mon, 10 Jan 2011) New Revision: bd5cedfc97c4a17dfb0115fd9e393ebcb58bcfcb Log: memory-debug: support memory allocation expansion by realloc. Modified files: lib/ctx.c Modified: lib/ctx.c (+25 -3) =================================================================== --- lib/ctx.c 2011-01-10 07:55:53 +0000 (3e0d869) +++ lib/ctx.c 2011-01-10 08:44:27 +0000 (3dc608e) @@ -204,6 +204,8 @@ grn_alloc_info_set_backtrace(char *buffer, size_t size) rest--; } free(symbols); + } else { + buffer[0] = '\0'; } # undef N_TRACE_LEVEL } @@ -220,7 +222,6 @@ grn_alloc_info_add(void *address) new_alloc_info = malloc(sizeof(grn_alloc_info)); new_alloc_info->address = address; new_alloc_info->freed = GRN_FALSE; - new_alloc_info->alloc_backtrace[0] = '\0'; grn_alloc_info_set_backtrace(new_alloc_info->alloc_backtrace, sizeof(new_alloc_info->alloc_backtrace)); new_alloc_info->next = ctx->impl->alloc_info; @@ -228,6 +229,25 @@ grn_alloc_info_add(void *address) } inline static void +grn_alloc_info_change(void *old_address, void *new_address) +{ + grn_ctx *ctx; + grn_alloc_info *alloc_info; + + ctx = &grn_gctx; + if (!ctx->impl) { return; } + + alloc_info = ctx->impl->alloc_info; + for (; alloc_info; alloc_info = alloc_info->next) { + if (alloc_info->address == old_address) { + alloc_info->address = new_address; + grn_alloc_info_set_backtrace(alloc_info->alloc_backtrace, + sizeof(alloc_info->alloc_backtrace)); + } + } +} + +inline static void grn_alloc_info_dump(grn_ctx *ctx) { int i = 0; @@ -269,7 +289,6 @@ grn_alloc_info_check(void *address) alloc_info->free_backtrace); } else { alloc_info->freed = GRN_TRUE; - alloc_info->free_backtrace[0] = '\0'; grn_alloc_info_set_backtrace(alloc_info->free_backtrace, sizeof(alloc_info->free_backtrace)); } @@ -298,6 +317,7 @@ grn_alloc_info_free(grn_ctx *ctx) #else /* ENABLE_MEMORY_DEBUG */ # define grn_alloc_info_add(address) +# define grn_alloc_info_change(old_address, new_address) # define grn_alloc_info_check(address) # define grn_alloc_info_dump(ctx) # define grn_alloc_info_free(ctx) @@ -2162,7 +2182,9 @@ grn_realloc_default(grn_ctx *ctx, void *ptr, size_t size, const char* file, int return NULL; } } - if (!ptr) { + if (ptr) { + grn_alloc_info_change(ptr, res); + } else { GRN_ADD_ALLOC_COUNT(1); grn_alloc_info_add(res); }