// Reallocate storage in Arena. void *Arena::Arealloc(void* old_ptr, size_t old_size, size_t new_size) { assert(new_size >= 0, "bad size"); if (new_size == 0) return NULL; #ifdef ASSERT if (UseMallocOnly) { // always allocate a new object (otherwise we'll free this one twice) char* copy = (char*)Amalloc(new_size); size_t n = MIN2(old_size, new_size); if (n > 0) memcpy(copy, old_ptr, n); return copy; } #endif char *c_old = (char*)old_ptr; // Handy name // Stupid fast special case if( new_size <= old_size ) { // Shrink in-place if( c_old+old_size == _hwm) // Attempt to free the excess bytes _hwm = c_old+new_size; // Adjust hwm return c_old; } // make sure that new_size is legal size_t corrected_new_size = ARENA_ALIGN(new_size); // See if we can resize in-place if( (c_old+old_size == _hwm) && // Adjusting recent thing (c_old+corrected_new_size <= _max) ) { // Still fits where it sits _hwm = c_old+corrected_new_size; // Adjust hwm return c_old; // Return old pointer } // Oops, got to relocate guts void *new_ptr = Amalloc(new_size); memcpy( new_ptr, c_old, old_size ); Afree(c_old,old_size); // Mostly done to keep stats accurate return new_ptr; }
//------------------------------realloc---------------------------------------- // Reallocate storage in Arena. void *Arena::Arealloc( void *old_ptr, size_t old_size, size_t new_size ) { char *c_old = (char*)old_ptr; // Handy name // Stupid fast special case if( new_size <= old_size ) { // Shrink in-place if( c_old+old_size == _hwm) // Attempt to free the excess bytes _hwm = c_old+new_size; // Adjust hwm return c_old; } // See if we can resize in-place if( (c_old+old_size == _hwm) && // Adjusting recent thing (c_old+new_size <= _max) ) { // Still fits where it sits _hwm = c_old+new_size; // Adjust hwm return c_old; // Return old pointer } // Oops, got to relocate guts void *new_ptr = Amalloc(new_size); memcpy( new_ptr, c_old, old_size ); Afree(c_old,old_size); // Mostly done to keep stats accurate return new_ptr; }
//------------------------------calloc----------------------------------------- // Allocate zeroed storage in Arena void *Arena::Acalloc( size_t items, size_t x ) { size_t z = items*x; // Total size needed void *ptr = Amalloc(z); // Get space memset( ptr, 0, z ); // Zap space return ptr; // Return space }