src/server/ucx/mempool.c

changeset 25
5dee29c7c530
parent 15
cff9c4101dd7
child 31
280250e45ba6
equal deleted inserted replaced
24:1a7853a4257e 25:5dee29c7c530
48 pool->size = newcap; 48 pool->size = newcap;
49 return EXIT_SUCCESS; 49 return EXIT_SUCCESS;
50 } 50 }
51 } 51 }
52 52
53 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { 53 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
54 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); 54 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
55 if (mem == NULL) return NULL; 55 if (mem == NULL) return NULL;
56 56
57 if (pool->ndata >= pool->size) { 57 if (pool->ndata >= pool->size) {
58 ucx_mempool_chcap(pool, pool->size + 16); 58 ucx_mempool_chcap(pool, pool->size + 16);
63 pool->ndata++; 63 pool->ndata++;
64 64
65 return &mem->c; 65 return &mem->c;
66 } 66 }
67 67
68 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { 68 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
69 void *ptr = ucx_mempool_malloc(pool, nelem*elsize); 69 void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
70 if(ptr == NULL) { 70 if(ptr == NULL) {
71 return NULL; 71 return NULL;
72 } 72 }
73 memset(ptr, 0, nelem * elsize); 73 memset(ptr, 0, nelem * elsize);
74 return ptr; 74 return ptr;
75 } 75 }
76 76
77 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { 77 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
78 void *mem = ((char*)ptr) - sizeof(ucx_destructor); 78 void *mem = ((char*)ptr) - sizeof(ucx_destructor);
79 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); 79 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
80 if (newm == NULL) return NULL; 80 if (newm == NULL) return NULL;
81 if (mem != newm) { 81 if (mem != newm) {
82 for(int i=0;i<pool->ndata;i++) { 82 for(int i=0;i<pool->ndata;i++) {

mercurial