int main(int argc, char** argv) { size_t alloc_size = 2642080; size_t size = alloc_size + tlsf_size() + tlsf_pool_overhead() + tlsf_alloc_overhead(); void* memory = ::malloc(size); tlsf_t tlsf = tlsf_create_with_pool(memory,size); tlsf_walk_pool(tlsf_get_pool(tlsf),0,0); std::cout << "try alloc " << std::hex << alloc_size << std::endl; void* ptr = tlsf_malloc(tlsf,alloc_size); assert(ptr); ::free(memory); return 0; }
Allocator::Allocator(void) { impl = new AllocatorImpl; size_t default_size = 10*1024*1024; impl->pools = (next_t*)malloc(default_size); impl->pools->next = 0x0; impl->pools->pool_size = default_size; size_t off = tlsf_size() + tlsf_pool_overhead() + sizeof(next_t); //printf("Generated Memory Pool with '%p'\n", impl->pools); impl->tlsf = tlsf_create_with_pool(((char*)impl->pools)+off, default_size-2*off); //printf("Allocator(%p)\n", impl); }
void Allocator::addMemory(void *v, size_t mem_size) { next_t *n = impl->pools; while(n->next) n = n->next; n->next = (next_t*)v; n->next->next = 0x0; n->next->pool_size = mem_size; //printf("Inserting '%p'\n", v); off_t off = sizeof(next_t) + tlsf_pool_overhead(); void *result = tlsf_add_pool(impl->tlsf, ((char*)n->next)+off, //0x0eadbeef); mem_size-off-sizeof(size_t)); if(!result) printf("FAILED TO INSERT MEMORY POOL\n"); };//{(void)mem_size;};