+ if ( (dyld::gLibSystemHelpers != NULL) && dyld::gProcessInfo->libSystemInitialized ) {
+ void* p = dyld::gLibSystemHelpers->malloc(size);
+ //dyld::log("malloc(%lu) => %p from libSystem\n", size, p);
+ return p;
+ }
+ else {
+ if ( size > DYLD_POOL_CHUNK_SIZE ) {
+ dyld::log("dyld malloc overflow: size=%zu\n", size);
+ exit(1);
+ }
+ size = (size+sizeof(void*)-1) & (-sizeof(void*)); // pointer align
+ uint8_t* result = currentPool->current;
+ currentPool->current += size;
+ if ( currentPool->current > currentPool->end ) {
+ vm_address_t addr = 0;
+ kern_return_t r = vm_allocate(mach_task_self(), &addr, DYLD_POOL_CHUNK_SIZE, VM_FLAGS_ANYWHERE);
+ if ( r != KERN_SUCCESS ) {
+ dyld::log("out of address space for dyld memory pool\n");
+ exit(1);
+ }
+ dyld_static_pool* newPool = (dyld_static_pool*)addr;
+ newPool->previousPool = NULL;
+ newPool->current = newPool->pool;
+ newPool->end = (uint8_t*)(addr + DYLD_POOL_CHUNK_SIZE);
+ newPool->previousPool = currentPool;
+ currentPool = newPool;
+ if ( (currentPool->current + size) > currentPool->end ) {
+ dyld::log("dyld memory pool exhausted: size=%lu\n", size);
+ exit(1);
+ }
+ result = currentPool->current;
+ currentPool->current += size;
+ }
+ //dyld::log("%p = malloc(%3lu) from pool %p, free space = %lu\n", result, size, currentPool, (long)(currentPool->end - currentPool->current));
+ return result;