1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43336 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2013-10-17 09:51:41 +00:00
parent cf0106827d
commit ee4da732e3
2 changed files with 24 additions and 7 deletions

View file

@ -1,3 +1,9 @@
Thu Oct 17 18:50:08 2013 Koichi Sasada <ko1@atdot.net>
* 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@atdot.net>
* string.c (STR_HEAP_SIZE): includes TERM_LEN(str).

25
gc.c
View file

@ -173,6 +173,9 @@ static ruby_gc_params_t initial_params = {
#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, void *ptr, size_t new_size, size_t old_size
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
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);