ucx/mempool.c

changeset 2
eeb50c534497
parent 0
1f419bd32da1
child 124
80609f9675f1
equal deleted inserted replaced
1:eb5269000bc8 2:eeb50c534497
73 return NULL; 73 return NULL;
74 } 74 }
75 75
76 pool->ndata = 0; 76 pool->ndata = 0;
77 pool->size = n; 77 pool->size = n;
78
79 UcxAllocator *allocator = (UcxAllocator*)malloc(sizeof(UcxAllocator));
80 if(!allocator) {
81 free(pool->data);
82 free(pool);
83 return NULL;
84 }
85 allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc;
86 allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc;
87 allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc;
88 allocator->free = (ucx_allocator_free)ucx_mempool_free;
89 allocator->pool = pool;
90 pool->allocator = allocator;
91
78 return pool; 92 return pool;
79 } 93 }
80 94
81 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { 95 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
82 void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); 96 void **data = (void**) realloc(pool->data, newcap*sizeof(void*));
171 } 185 }
172 free(chunk); 186 free(chunk);
173 } 187 }
174 } 188 }
175 free(pool->data); 189 free(pool->data);
190 free(pool->allocator);
176 free(pool); 191 free(pool);
177 } 192 }
178 193
179 void ucx_mempool_set_destr(void *ptr, ucx_destructor func) { 194 void ucx_mempool_set_destr(void *ptr, ucx_destructor func) {
180 *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func; 195 *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func;
187 rd->destructor = destr; 202 rd->destructor = destr;
188 rd->ptr = ptr; 203 rd->ptr = ptr;
189 ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); 204 ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
190 } 205 }
191 206
192 UcxAllocator* ucx_mempool_allocator(UcxMempool *pool) {
193 UcxAllocator *allocator = (UcxAllocator*)ucx_mempool_malloc(
194 pool, sizeof(UcxAllocator));
195 if(!allocator) {
196 return NULL;
197 }
198 allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc;
199 allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc;
200 allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc;
201 allocator->free = (ucx_allocator_free)ucx_mempool_free;
202 allocator->pool = pool;
203 return allocator;
204 }

mercurial