#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <mach/mach.h>
#include <sys/mman.h>
extern "C" void* __dso_handle;
#include "dyld.h"
#include "dyldLibSystemInterface.h"
#if __LP64__
#define DYLD_POOL_CHUNK_SIZE 200*1024
#else
#define DYLD_POOL_CHUNK_SIZE 150*1024
#endif
struct dyld_static_pool {
dyld_static_pool* previousPool;
uint8_t* current;
uint8_t* end;
uint8_t pool[1];
};
static uint8_t initialPoolContent[DYLD_POOL_CHUNK_SIZE] __attribute__((__aligned__(16)));
static dyld_static_pool initialPool = { NULL, initialPoolContent, &initialPoolContent[DYLD_POOL_CHUNK_SIZE] };
static dyld_static_pool* currentPool = &initialPool;
void* malloc(size_t size)
{
if ( (dyld::gLibSystemHelpers != NULL) && dyld::gProcessInfo->libSystemInitialized ) {
void* p = dyld::gLibSystemHelpers->malloc(size);
return p;
}
else {
if ( size > DYLD_POOL_CHUNK_SIZE ) {
dyld::log("dyld malloc overflow: size=%lu\n", size);
dyld::halt("dyld malloc overflow\n");
}
size = (size+sizeof(void*)-1) & (-sizeof(void*)); 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::halt("out of address space for dyld memory pool\n");
}
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);
dyld::halt("dyld memory pool exhausted\n");
}
result = currentPool->current;
currentPool->current += size;
}
return result;
}
}
void free(void* ptr)
{
if ( (dyld::gLibSystemHelpers != NULL) && ((ptr < &__dso_handle) || (ptr >= &initialPoolContent[DYLD_POOL_CHUNK_SIZE])) ) {
for (dyld_static_pool* p = currentPool; p != NULL; p = p->previousPool) {
if ( (p->pool <= ptr) && (ptr < p->end) ) {
return;
}
}
return dyld::gLibSystemHelpers->free(ptr);
}
else {
}
}
void* calloc(size_t count, size_t size)
{
size_t total = count * size;
if ( total/count != size ) {
dyld::log("dyld calloc overflow: count=%zu, size=%zu\n", count, size);
dyld::halt("dyld calloc overflow");
}
if ( dyld::gLibSystemHelpers != NULL ) {
void* result = dyld::gLibSystemHelpers->malloc(total);
if ( result != NULL )
bzero(result, total);
return result;
}
else {
return malloc(total);
}
}
void* realloc(void *ptr, size_t size)
{
void* result = malloc(size);
memcpy(result, ptr, size);
return result;
}
extern "C" int _malloc_lock;
int _malloc_lock = 0;
int vm_alloc(vm_address_t* addr, vm_size_t size, uint32_t flags)
{
if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 12) ) {
return dyld::gLibSystemHelpers->vm_alloc(mach_task_self(), addr, size, flags);
}
else {
return ::vm_allocate(mach_task_self(), addr, size, flags);
}
}
void* xmmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset)
{
if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 12) ) {
return dyld::gLibSystemHelpers->mmap(addr, len, prot, flags, fd, offset);
}
else {
return ::mmap(addr, len, prot, flags, fd, offset);
}
}