void *realloc(void *ptr, size_t size){ if (!ptr) { return malloc(size); } if (valid_address(ptr)) { size = align_pointer(size); block_t block = get_block(ptr); if (size <= block->size && BLOCK_SIZE_MIN <= block->size - size) { split_block(block, size); } else { if (block->next && block->next->free && size <= BLOCK_SIZE + block->size + block->next->size) { //merge möglich merge_block(block); if (BLOCK_SIZE_MIN <= block->size - size) { split_block(block, size); } } else { //real malloc void *newptr = malloc(size); if (!newptr) { perror("error at realloc"); return NULL; } block_t newblock = get_block(newptr); copy_block(block, newblock); free(block); return newptr; } } return ptr; } return 0; }
/* Same as malloc, but sets error variable _mm_error when fails. Returns a 16-byte aligned pointer */ void* MikMod_malloc(size_t size) { #if defined __MACH__ || defined __QNXNTO__ void *d = malloc(size); if(!d) { _mm_errno = MMERR_OUT_OF_MEMORY; if(_mm_errorhandler) { _mm_errorhandler(); } } return d; #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE) void * d = _aligned_malloc(size, ALIGN_STRIDE); if (d) { ZeroMemory(d, size); return d; } else { _mm_errno = MMERR_OUT_OF_MEMORY; if(_mm_errorhandler) { _mm_errorhandler(); } } return 0; #else void *d = calloc(1, size + ALIGN_STRIDE + sizeof(void*)); if(!d) { _mm_errno = MMERR_OUT_OF_MEMORY; if(_mm_errorhandler) _mm_errorhandler(); } return align_pointer(d, ALIGN_STRIDE); #endif }
int main (int argc, char *argv[]) { char *p, *q; int size = 100; p = malloc(size + 32); q = align_pointer(p); printf("raw:%d aligned:%d\n", p, q); printf("raw:%p aligned:%p\n", p, q); /* Use q, but remember to free p */ return 0; }
static void update_current_thread_stack (void *start) { int stack_guard = 0; SgenThreadInfo *info = mono_thread_info_current (); info->client_info.stack_start = align_pointer (&stack_guard); g_assert (info->client_info.stack_start); g_assert (info->client_info.stack_start >= info->client_info.stack_start_limit && info->client_info.stack_start < info->client_info.stack_end); #if !defined(MONO_CROSS_COMPILE) && MONO_ARCH_HAS_MONO_CONTEXT MONO_CONTEXT_GET_CURRENT (info->client_info.ctx); #else g_error ("Sgen STW requires a working mono-context"); #endif if (mono_gc_get_gc_callbacks ()->thread_suspend_func) mono_gc_get_gc_callbacks ()->thread_suspend_func (info->client_info.runtime_data, NULL, &info->client_info.ctx); }
void* MikMod_realloc(void *data, size_t size) { if (data) { #if defined __MACH__ void *d = realloc(data, size); if (d) { return d; } return 0; #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE) //return _aligned_realloc(data, size, ALIGN_STRIDE); return realloc(data, size); #else char *newPtr = (char *)realloc(get_pointer(data), size + ALIGN_STRIDE + sizeof(void*)); return align_pointer(newPtr, ALIGN_STRIDE); #endif } return MikMod_malloc(size); }
void* MikMod_realloc(void *data, size_t size) { if (data) { #if defined __MACH__ || defined __QNXNTO__ void *d = realloc(data, size); if(!d) { _mm_errno = MMERR_OUT_OF_MEMORY; if(_mm_errorhandler) { _mm_errorhandler(); } } return d; #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE) return _aligned_realloc(data, size, ALIGN_STRIDE); #else unsigned char *newPtr = (unsigned char *)realloc(get_pointer(data), size + ALIGN_STRIDE + sizeof(void*)); return align_pointer((char*)newPtr, ALIGN_STRIDE); #endif } return MikMod_malloc(size); }
void *malloc(size_t size){ sum_allocs++; sum_alloc_size += size; num_allocated++; block_t current, last; size = align_pointer(size); if (base) { /* First find a block */ last = base; current = find_free_block(&last, size); if (current) { //there is a fitting free block if (BLOCK_SIZE_MIN <= current->size - size){ // we can split the block split_block(current, size); } current->free=0; } else { //there are no fitting blocks current = new_block(last, size); if (!current){ // error perror("error at !current"); return NULL; } } } else { //first malloc (base == NULL) current = new_block(NULL, size); if (!current){ perror("error at !current"); return(NULL); } base = current; } return current->raw; }
static void update_current_thread_stack (void *start) { int stack_guard = 0; #if !defined(USE_MONO_CTX) void *reg_ptr = cur_thread_regs; #endif SgenThreadInfo *info = mono_thread_info_current (); info->stack_start = align_pointer (&stack_guard); g_assert (info->stack_start >= info->stack_start_limit && info->stack_start < info->stack_end); #ifdef USE_MONO_CTX MONO_CONTEXT_GET_CURRENT (cur_thread_ctx); memcpy (&info->ctx, &cur_thread_ctx, sizeof (MonoContext)); if (mono_gc_get_gc_callbacks ()->thread_suspend_func) mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, NULL, &info->ctx); #else ARCH_STORE_REGS (reg_ptr); memcpy (&info->regs, reg_ptr, sizeof (info->regs)); if (mono_gc_get_gc_callbacks ()->thread_suspend_func) mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, NULL, NULL); #endif }
/* Same as calloc, but sets error variable _mm_error when fails */ void* MikMod_calloc(size_t nitems,size_t size) { void *d; if(!(d=calloc(nitems,size))) { _mm_errno = MMERR_OUT_OF_MEMORY; if(_mm_errorhandler) _mm_errorhandler(); } return d; #if 0 #if defined __MACH__ void *d = calloc(nitems, size); if (d) { return d; } return 0; #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE) void * d = _aligned_malloc(size * nitems, ALIGN_STRIDE); if (d) { ZeroMemory(d, size * nitems); return d; } return 0; #else void *d = calloc(nitems, size + ALIGN_STRIDE + sizeof(void*)); if(!d) { _mm_errno = MMERR_OUT_OF_MEMORY; if(_mm_errorhandler) _mm_errorhandler(); } return align_pointer(d, ALIGN_STRIDE); #endif #endif }
/* Note that memory allocated with this function can never be freed, because the start address of the block allocated is lost. */ void * __gmp_allocate_func_aligned (size_t bytes, size_t align) { return align_pointer ((*__gmp_allocate_func) (bytes + align-1), align); }