olaf@13: /* olaf@13: * olaf@13: */ olaf@13: olaf@13: #include olaf@13: #include olaf@13: #include universe@15: #include olaf@13: olaf@13: #include "mpool.h" olaf@13: olaf@13: typedef struct { olaf@13: ucx_destructor destructor; olaf@13: char c; olaf@13: } ucx_memchunk; olaf@13: olaf@13: typedef struct { olaf@13: ucx_destructor destructor; olaf@13: void *ptr; olaf@13: } ucx_regdestr; olaf@13: olaf@13: void ucx_mempool_shared_destr(void* ptr) { olaf@13: ucx_regdestr *rd = (ucx_regdestr*)ptr; olaf@13: rd->destructor(rd->ptr); olaf@13: } olaf@13: olaf@13: UcxMempool *ucx_mempool_new(size_t n) { olaf@14: UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); universe@15: if (pool == NULL) return NULL; universe@15: olaf@13: pool->data = malloc(n * sizeof(void*)); universe@15: if (pool->data == NULL) { universe@15: free(pool); universe@15: return NULL; universe@15: } universe@15: olaf@13: pool->ndata = 0; olaf@13: pool->size = n; olaf@13: return pool; olaf@13: } olaf@13: universe@15: int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { olaf@13: void **data = realloc(pool->data, newcap*sizeof(void*)); universe@15: if (data == NULL) { universe@15: return ENOMEM; universe@15: } else { universe@15: pool->data = data; universe@15: pool->size = newcap; universe@15: return EXIT_SUCCESS; universe@15: } olaf@13: } olaf@13: olaf@13: void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { olaf@13: ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); universe@15: if (mem == NULL) return NULL; olaf@13: olaf@13: if (pool->ndata >= pool->size) { olaf@13: ucx_mempool_chcap(pool, pool->size + 16); universe@15: } olaf@13: olaf@13: mem->destructor = NULL; olaf@13: pool->data[pool->ndata] = mem; olaf@13: pool->ndata++; olaf@13: olaf@13: return &mem->c; olaf@13: } olaf@13: olaf@13: void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { olaf@13: void *ptr = ucx_mempool_malloc(pool, nelem*elsize); olaf@13: if(ptr == NULL) { olaf@13: return NULL; olaf@13: } olaf@13: memset(ptr, 0, nelem * elsize); olaf@13: return ptr; olaf@13: } olaf@13: olaf@13: void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { olaf@13: void *mem = ((char*)ptr) - sizeof(ucx_destructor); universe@15: char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); olaf@14: if (newm == NULL) return NULL; universe@15: if (mem != newm) { olaf@14: for(int i=0;indata;i++) { olaf@14: if(pool->data[i] == mem) { olaf@14: pool->data[i] = newm; olaf@14: break; olaf@14: } olaf@13: } universe@15: fprintf(stderr, "FATAL: %8x not in mpool %8x\n", mem, pool); universe@15: exit(1); olaf@13: } universe@15: return ((char*) newm) + sizeof(ucx_destructor); olaf@13: } olaf@13: olaf@13: void ucx_mempool_free(UcxMempool *pool) { olaf@13: ucx_memchunk *chunk; olaf@13: for(int i=0;indata;i++) { olaf@13: chunk = (ucx_memchunk*) pool->data[i]; olaf@13: if(chunk->destructor != NULL) { olaf@13: chunk->destructor(&chunk->c); olaf@13: } olaf@13: free(chunk); olaf@13: } olaf@13: free(pool->data); olaf@13: free(pool); olaf@13: } olaf@13: olaf@13: void ucx_mempool_set_destr(void *ptr, ucx_destructor func) { olaf@13: *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func; olaf@13: } olaf@13: olaf@13: void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) { olaf@13: ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc( olaf@13: pool, olaf@13: sizeof(ucx_regdestr)); olaf@13: rd->destructor = destr; olaf@14: rd->ptr = ptr; olaf@13: ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); olaf@13: }