src/mempool.c

changeset 270
3d80d425543b
parent 259
2f5dea574a75
equal deleted inserted replaced
269:591473851c95 270:3d80d425543b
63 ucx_regdestr *rd = (ucx_regdestr*)ptr; 63 ucx_regdestr *rd = (ucx_regdestr*)ptr;
64 rd->destructor(rd->ptr); 64 rd->destructor(rd->ptr);
65 } 65 }
66 66
67 UcxMempool *ucx_mempool_new(size_t n) { 67 UcxMempool *ucx_mempool_new(size_t n) {
68 size_t poolsz;
69 if(ucx_szmul(n, sizeof(void*), &poolsz)) {
70 return NULL;
71 }
72
68 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); 73 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
69 if (!pool) { 74 if (!pool) {
70 return NULL; 75 return NULL;
71 } 76 }
72 77
73 pool->data = (void**) malloc(n * sizeof(void*)); 78 pool->data = (void**) malloc(poolsz);
74 if (pool->data == NULL) { 79 if (pool->data == NULL) {
75 free(pool); 80 free(pool);
76 return NULL; 81 return NULL;
77 } 82 }
78 83
98 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { 103 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
99 if (newcap < pool->ndata) { 104 if (newcap < pool->ndata) {
100 return 1; 105 return 1;
101 } 106 }
102 107
103 void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); 108 size_t newcapsz;
109 if(ucx_szmul(newcap, sizeof(void*), &newcapsz)) {
110 return 1;
111 }
112
113 void **data = (void**) realloc(pool->data, newcapsz);
104 if (data) { 114 if (data) {
105 pool->data = data; 115 pool->data = data;
106 pool->size = newcap; 116 pool->size = newcap;
107 return 0; 117 return 0;
108 } else { 118 } else {
109 return 1; 119 return 1;
110 } 120 }
111 } 121 }
112 122
113 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { 123 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
124 if(((size_t)-1) - sizeof(ucx_destructor) < n) {
125 return NULL;
126 }
127
114 if (pool->ndata >= pool->size) { 128 if (pool->ndata >= pool->size) {
115 size_t newcap = pool->size*2; 129 size_t newcap = pool->size*2;
116 if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) { 130 if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) {
117 return NULL; 131 return NULL;
118 } 132 }
130 144
131 return &(mem->c); 145 return &(mem->c);
132 } 146 }
133 147
134 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { 148 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
135 void *ptr = ucx_mempool_malloc(pool, nelem*elsize); 149 size_t msz;
150 if(ucx_szmul(nelem, elsize, &msz)) {
151 return NULL;
152 }
153
154 void *ptr = ucx_mempool_malloc(pool, msz);
136 if (!ptr) { 155 if (!ptr) {
137 return NULL; 156 return NULL;
138 } 157 }
139 memset(ptr, 0, nelem * elsize); 158 memset(ptr, 0, nelem * elsize);
140 return ptr; 159 return ptr;
141 } 160 }
142 161
143 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { 162 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
163 if(((size_t)-1) - sizeof(ucx_destructor) < n) {
164 return NULL;
165 }
166
144 char *mem = ((char*)ptr) - sizeof(ucx_destructor); 167 char *mem = ((char*)ptr) - sizeof(ucx_destructor);
145 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); 168 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
146 if (!newm) { 169 if (!newm) {
147 return NULL; 170 return NULL;
148 } 171 }

mercurial