Revision: 3901 http://trac.macosforge.org/projects/ruby/changeset/3901 Author: lsansonetti@apple.com Date: 2010-04-01 15:10:02 -0700 (Thu, 01 Apr 2010) Log Message: ----------- added xmalloc_ptrs() function and use it when it's necessary Modified Paths: -------------- MacRuby/trunk/array.c MacRuby/trunk/array.h MacRuby/trunk/compiler.cpp MacRuby/trunk/dispatcher.cpp MacRuby/trunk/gc.c MacRuby/trunk/include/ruby/defines.h MacRuby/trunk/vm.cpp MacRuby/trunk/vm.h Modified: MacRuby/trunk/array.c =================================================================== --- MacRuby/trunk/array.c 2010-04-01 21:58:39 UTC (rev 3900) +++ MacRuby/trunk/array.c 2010-04-01 22:10:02 UTC (rev 3901) @@ -45,11 +45,16 @@ if (rary->cap > 0) { newlen *= 2; } - VALUE *new_elements = xrealloc(rary->elements, - sizeof(VALUE) * newlen); - if (new_elements != rary->elements) { - GC_WB(&rary->elements, new_elements); + if (rary->elements == NULL) { + GC_WB(&rary->elements, xmalloc_ptrs(sizeof(VALUE) * newlen)); } + else { + VALUE *new_elements = xrealloc(rary->elements, + sizeof(VALUE) * newlen); + if (new_elements != rary->elements) { + GC_WB(&rary->elements, new_elements); + } + } rary->cap = newlen; } } Modified: MacRuby/trunk/array.h =================================================================== --- MacRuby/trunk/array.h 2010-04-01 21:58:39 UTC (rev 3900) +++ MacRuby/trunk/array.h 2010-04-01 22:10:02 UTC (rev 3901) @@ -1,5 +1,5 @@ /* - * MacRuby Hash. + * MacRuby Array. * * This file is covered by the Ruby license. See COPYING for more details. * Modified: MacRuby/trunk/compiler.cpp =================================================================== --- MacRuby/trunk/compiler.cpp 2010-04-01 21:58:39 UTC (rev 3900) +++ MacRuby/trunk/compiler.cpp 2010-04-01 22:10:02 UTC (rev 3901) @@ -6677,7 +6677,7 @@ va_list ar; va_start(ar, argc); - VALUE *data = (VALUE *)xmalloc(argc * sizeof(VALUE)); + VALUE *data = (VALUE *)xmalloc_ptrs(argc * sizeof(VALUE)); for (int i = 0; i < argc; ++i) { VALUE field = va_arg(ar, VALUE); GC_WB(&data[i], field); Modified: MacRuby/trunk/dispatcher.cpp =================================================================== --- MacRuby/trunk/dispatcher.cpp 2010-04-01 21:58:39 UTC (rev 3900) +++ MacRuby/trunk/dispatcher.cpp 2010-04-01 22:10:02 UTC (rev 3901) @@ -78,7 +78,7 @@ IMP pimp, const rb_vm_arity_t &arity, int argc, const VALUE *argv) { if ((arity.real != argc) || (arity.max == -1)) { - VALUE *new_argv = (VALUE *)xmalloc(sizeof(VALUE) * arity.real); + VALUE *new_argv = (VALUE *)xmalloc_ptrs(sizeof(VALUE) * arity.real); __rb_vm_fix_args(argv, new_argv, arity, argc); argv = new_argv; argc = arity.real; @@ -123,7 +123,7 @@ int argc, const VALUE *argv) { if ((arity.real != argc) || (arity.max == -1)) { - VALUE *new_argv = (VALUE *)xmalloc(sizeof(VALUE) * arity.real); + VALUE *new_argv = (VALUE *)xmalloc_ptrs(sizeof(VALUE) * arity.real); __rb_vm_fix_args(argv, new_argv, arity, argc); argv = new_argv; argc = arity.real; @@ -350,7 +350,7 @@ GET_VM()->set_method_missing_reason(call_status); - VALUE *new_argv = (VALUE *)xmalloc(sizeof(VALUE) * (argc + 1)); + VALUE *new_argv = (VALUE *)xmalloc_ptrs(sizeof(VALUE) * (argc + 1)); char buf[100]; int n = snprintf(buf, sizeof buf, "%s", sel_getName(sel)); @@ -993,7 +993,7 @@ int count = RARRAY_LEN(ary); if (real_argc + count >= argv_size) { const size_t new_argv_size = real_argc + count + 100; - VALUE *new_argv = (VALUE *)xmalloc(sizeof(VALUE) + VALUE *new_argv = (VALUE *)xmalloc_ptrs(sizeof(VALUE) * new_argv_size); memcpy(new_argv, argv, sizeof(VALUE) * argv_size); argv = new_argv; @@ -1007,7 +1007,7 @@ else { if (real_argc >= argv_size) { const size_t new_argv_size = real_argc + 100; - VALUE *new_argv = (VALUE *)xmalloc(sizeof(VALUE) + VALUE *new_argv = (VALUE *)xmalloc_ptrs(sizeof(VALUE) * new_argv_size); memcpy(new_argv, argv, sizeof(VALUE) * argv_size); argv = new_argv; @@ -1513,7 +1513,7 @@ && (arity.min > 1 || (arity.min == 1 && arity.min != arity.max))) { // Expand the array. long ary_len = RARRAY_LEN(argv[0]); - new_argv = (VALUE *)xmalloc(sizeof(VALUE) * ary_len); + new_argv = (VALUE *)xmalloc_ptrs(sizeof(VALUE) * ary_len); for (int i = 0; i < ary_len; i++) { new_argv[i] = RARRAY_AT(argv[0], i); } @@ -1533,7 +1533,7 @@ else { new_argc = argc; } - new_argv = (VALUE *)xmalloc(sizeof(VALUE) * new_argc); + new_argv = (VALUE *)xmalloc_ptrs(sizeof(VALUE) * new_argc); for (int i = 0; i < new_argc; i++) { new_argv[i] = i < argc ? argv[i] : Qnil; } Modified: MacRuby/trunk/gc.c =================================================================== --- MacRuby/trunk/gc.c 2010-04-01 21:58:39 UTC (rev 3900) +++ MacRuby/trunk/gc.c 2010-04-01 22:10:02 UTC (rev 3901) @@ -109,11 +109,9 @@ exit(1); } -void * -ruby_xmalloc(size_t size) +static inline void * +ruby_xmalloc_memory(size_t size, int type) { - void *mem; - if (size < 0) { rb_raise(rb_eNoMemError, "negative allocation size (or too big)"); } @@ -124,16 +122,32 @@ rb_objc_no_gc_error(); } - mem = auto_zone_allocate_object(__auto_zone, size, - AUTO_MEMORY_SCANNED, 0, 0); + void *mem = auto_zone_allocate_object(__auto_zone, size, type, 0, 0); if (mem == NULL) { rb_memerror(); } - return mem; } void * +ruby_xmalloc(size_t size) +{ + return ruby_xmalloc_memory(size, AUTO_MEMORY_SCANNED); +} + +void * +ruby_xmalloc_ptrs(size_t size) +{ + int type; +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070 + type = AUTO_MEMORY_ALL_POINTERS; +#else + type = AUTO_MEMORY_SCANNED; +#endif + return ruby_xmalloc_memory(size, type); +} + +void * ruby_xmalloc2(size_t n, size_t size) { size_t len = size * n; @@ -146,20 +160,14 @@ void * ruby_xcalloc(size_t n, size_t size) { - void *mem; - - mem = ruby_xmalloc2(n, size); + void *mem = ruby_xmalloc2(n, size); memset(mem, 0, n * size); - return mem; } - void * ruby_xrealloc(void *ptr, size_t size) { - void *mem; - if (size < 0) { rb_raise(rb_eArgError, "negative re-allocation size"); } @@ -171,21 +179,18 @@ } #if MAC_OS_X_VERSION_MAX_ALLOWED < 1060 - { - size_t old_size = malloc_size(ptr); - if (old_size >= size) { - return ptr; - } - mem = ruby_xmalloc(size); - if (mem == NULL) { - rb_memerror(); - } - auto_zone_write_barrier_memmove(__auto_zone, mem, ptr, old_size); - xfree(ptr); + size_t old_size = malloc_size(ptr); + if (old_size >= size) { + return ptr; } + void *mem = ruby_xmalloc(size); + if (mem == NULL) { + rb_memerror(); + } + auto_zone_write_barrier_memmove(__auto_zone, mem, ptr, old_size); + xfree(ptr); #else - mem = malloc_zone_realloc(__auto_zone, ptr, size); - + void *mem = malloc_zone_realloc(__auto_zone, ptr, size); if (mem == NULL) { rb_memerror(); } @@ -213,7 +218,6 @@ } } - /* * call-seq: * GC.enable => true or false Modified: MacRuby/trunk/include/ruby/defines.h =================================================================== --- MacRuby/trunk/include/ruby/defines.h 2010-04-01 21:58:39 UTC (rev 3900) +++ MacRuby/trunk/include/ruby/defines.h 2010-04-01 22:10:02 UTC (rev 3901) @@ -50,6 +50,7 @@ #endif #define xmalloc ruby_xmalloc +#define xmalloc_ptrs ruby_xmalloc_ptrs #define xmalloc2 ruby_xmalloc2 #define xcalloc ruby_xcalloc #define xrealloc ruby_xrealloc @@ -57,6 +58,7 @@ #define xfree ruby_xfree void *xmalloc(size_t); +void *xmalloc_ptrs(size_t); void *xmalloc2(size_t,size_t); void *xcalloc(size_t,size_t); void *xrealloc(void*,size_t); Modified: MacRuby/trunk/vm.cpp =================================================================== --- MacRuby/trunk/vm.cpp 2010-04-01 21:58:39 UTC (rev 3900) +++ MacRuby/trunk/vm.cpp 2010-04-01 22:10:02 UTC (rev 3901) @@ -4660,7 +4660,7 @@ if (argc > 0) { t->argc = argc; - GC_WB(&t->argv, xmalloc(sizeof(VALUE) * argc)); + GC_WB(&t->argv, xmalloc_ptrs(sizeof(VALUE) * argc)); for (int i = 0; i < argc; i++) { GC_WB(&t->argv[i], argv[i]); } Modified: MacRuby/trunk/vm.h =================================================================== --- MacRuby/trunk/vm.h 2010-04-01 21:58:39 UTC (rev 3900) +++ MacRuby/trunk/vm.h 2010-04-01 22:10:02 UTC (rev 3901) @@ -329,11 +329,11 @@ rb_vm_regrow_robject_slots(struct RObject *obj, unsigned int new_num_slot) { unsigned int i; - VALUE *new_slots = (VALUE *)xmalloc(sizeof(VALUE) * (new_num_slot + 1)); - for (i = 0; i < obj->num_slots; i++) { - GC_WB(&new_slots[i], obj->slots[i]); + VALUE *new_slots = (VALUE *)xrealloc(obj->slots, + sizeof(VALUE) * (new_num_slot + 1)); + if (new_slots != obj->slots) { + GC_WB(&obj->slots, new_slots); } - GC_WB(&obj->slots, new_slots); for (i = obj->num_slots; i <= new_num_slot; i++) { obj->slots[i] = Qundef; } @@ -436,7 +436,7 @@ int num_slots = 10; obj = (struct RObject *)rb_objc_newobj(sizeof(struct RObject)); - GC_WB(&obj->slots, xmalloc(num_slots * sizeof(VALUE))); + GC_WB(&obj->slots, xmalloc_ptrs(num_slots * sizeof(VALUE))); OBJSETUP(obj, klass, T_OBJECT); @@ -447,7 +447,6 @@ for (i = 0; i < num_slots; i++) { ROBJECT(obj)->slots[i] = Qundef; } - return (VALUE)obj; }