ruby-changes:31257
From: ko1 <ko1@a...>
Date: Thu, 17 Oct 2013 18:51:46 +0900 (JST)
Subject: [ruby-changes:31257] ko1:r43336 (trunk): * gc.c (CALC_EXACT_MALLOC_SIZE_CHECK_OLD_SIZE): introduced.
ko1 2013-10-17 18:51:41 +0900 (Thu, 17 Oct 2013) New Revision: 43336 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43336 Log: * gc.c (CALC_EXACT_MALLOC_SIZE_CHECK_OLD_SIZE): introduced. This macro enable checker compare with allocated memory and declared old_size of sized_xfree and sized_xrealloc. Modified files: trunk/ChangeLog trunk/gc.c Index: ChangeLog =================================================================== --- ChangeLog (revision 43335) +++ ChangeLog (revision 43336) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Oct 17 18:50:08 2013 Koichi Sasada <ko1@a...> + + * gc.c (CALC_EXACT_MALLOC_SIZE_CHECK_OLD_SIZE): introduced. + This macro enable checker compare with allocated memory and + declared old_size of sized_xfree and sized_xrealloc. + Thu Oct 17 18:45:41 2013 Koichi Sasada <ko1@a...> * string.c (STR_HEAP_SIZE): includes TERM_LEN(str). Index: gc.c =================================================================== --- gc.c (revision 43335) +++ gc.c (revision 43336) @@ -173,6 +173,9 @@ static ruby_gc_params_t initial_params = https://github.com/ruby/ruby/blob/trunk/gc.c#L173 #ifndef CALC_EXACT_MALLOC_SIZE #define CALC_EXACT_MALLOC_SIZE 0 #endif +#ifndef CALC_EXACT_MALLOC_SIZE_CHECK_OLD_SIZE +#define CALC_EXACT_MALLOC_SIZE_CHECK_OLD_SIZE 0 +#endif typedef enum { GPR_FLAG_NONE = 0x000, @@ -4970,16 +4973,20 @@ vm_xrealloc(rb_objspace_t *objspace, voi https://github.com/ruby/ruby/blob/trunk/gc.c#L4973 vm_malloc_increase(objspace, new_size, old_size, FALSE); #if CALC_EXACT_MALLOC_SIZE - size += sizeof(size_t); + new_size += sizeof(size_t); ptr = (size_t *)ptr - 1; cem_oldsize = ((size_t *)ptr)[0]; + + if (CALC_EXACT_MALLOC_SIZE_CHECK_OLD_SIZE && old_size > 0 && cem_oldsize - sizeof(size_t) != old_size) { + fprintf(stderr, "vm_xrealloc: old_size mismatch: expected %d, but %d\n", (int)(cem_oldsize-sizeof(size_t)), (int)old_size); + } #endif TRY_WITH_GC(mem = realloc(ptr, new_size)); #if CALC_EXACT_MALLOC_SIZE - ATOMIC_SIZE_ADD(objspace->malloc_params.allocated_size, size - cem_oldsize); - ((size_t *)mem)[0] = size; + ATOMIC_SIZE_ADD(objspace->malloc_params.allocated_size, new_size - cem_oldsize); + ((size_t *)mem)[0] = new_size; mem = (size_t *)mem + 1; #endif @@ -4990,13 +4997,17 @@ static void https://github.com/ruby/ruby/blob/trunk/gc.c#L4997 vm_xfree(rb_objspace_t *objspace, void *ptr, size_t old_size) { #if CALC_EXACT_MALLOC_SIZE - size_t size; + size_t cem_oldsize; ptr = ((size_t *)ptr) - 1; - size = ((size_t*)ptr)[0]; - if (size) { - ATOMIC_SIZE_SUB(objspace->malloc_params.allocated_size, size); + cem_oldsize = ((size_t*)ptr)[0]; + if (cem_oldsize) { + ATOMIC_SIZE_SUB(objspace->malloc_params.allocated_size, cem_oldsize); ATOMIC_SIZE_DEC(objspace->malloc_params.allocations); } + + if (CALC_EXACT_MALLOC_SIZE_CHECK_OLD_SIZE && old_size > 0 && cem_oldsize - sizeof(size_t) != old_size) { + fprintf(stderr, "vm_xfree: old_size mismatch: expected %d, but %d\n", (int)(cem_oldsize-sizeof(size_t)), (int)old_size); + } #endif vm_malloc_increase(objspace, 0, old_size, FALSE); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/