void* malloc(size_t size) { if(pthread_equal(pthread_self(), main_thread) && size > 0 && size <= MAX_CHUNK_SIZE) { size = ROUNDUP_SIZE(size); void* result = allocate_memory(size); if(result != NULL) { return result; } } pthread_mutex_lock(&dlmalloc_mutex); void* result = dlmalloc(size); pthread_mutex_unlock(&dlmalloc_mutex); return result; }
static void *allocNBytes(CICcontext *context, int size) { void *result; if (context->pass == 1) { /* The first pass * A more sophisticated scheme could reduce the number of mallocs. */ CICmallocs *mallocs = (CICmallocs *)sysCalloc(1, sizeof(CICmallocs) + size); if (mallocs == 0) JAVA_ERROR(context, "out of memory"); result = (void *)(mallocs + 1); mallocs->next = context->pass1.mallocs; ROUNDUP_SIZE(size); if (context->in_clinit) context->clinit_size += size; else context->malloc_size += size; context->pass1.mallocs = mallocs; } else { /* The second pass */ #define ALLOC_BLOCK(ptr,buf,sizelimit) \ result = (ptr); \ ROUNDUP_SIZE(size); \ (ptr) += (size); \ sysAssert((ptr) <= (buf) + (sizelimit)) if (context->in_clinit) { ALLOC_BLOCK(context->pass2.clinit_ptr, context->pass2.clinit_buffer, context->clinit_size); } else { ALLOC_BLOCK(context->pass2.malloc_ptr, context->pass2.malloc_buffer, context->malloc_size); } } return result; }