51 sizeof(struct cx_mempool_memory_s*), &newmsize)) { |
51 sizeof(struct cx_mempool_memory_s*), &newmsize)) { |
52 errno = EOVERFLOW; |
52 errno = EOVERFLOW; |
53 return NULL; |
53 return NULL; |
54 } |
54 } |
55 struct cx_mempool_memory_s **newdata = realloc(pool->data, newmsize); |
55 struct cx_mempool_memory_s **newdata = realloc(pool->data, newmsize); |
56 if (newdata == NULL) { |
56 if (newdata == NULL) return NULL; |
57 return NULL; |
|
58 } |
|
59 pool->data = newdata; |
57 pool->data = newdata; |
60 pool->capacity = newcap; |
58 pool->capacity = newcap; |
61 } |
59 } |
62 |
60 |
63 struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n); |
61 struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n); |
64 if (mem == NULL) { |
62 if (mem == NULL) return NULL; |
65 return NULL; |
|
66 } |
|
67 |
63 |
68 mem->destructor = pool->auto_destr; |
64 mem->destructor = pool->auto_destr; |
69 pool->data[pool->size] = mem; |
65 pool->data[pool->size] = mem; |
70 pool->size++; |
66 pool->size++; |
71 |
67 |
81 if (cx_szmul(nelem, elsize, &msz)) { |
77 if (cx_szmul(nelem, elsize, &msz)) { |
82 errno = EOVERFLOW; |
78 errno = EOVERFLOW; |
83 return NULL; |
79 return NULL; |
84 } |
80 } |
85 void *ptr = cx_mempool_malloc(p, msz); |
81 void *ptr = cx_mempool_malloc(p, msz); |
86 if (ptr == NULL) { |
82 if (ptr == NULL) return NULL; |
87 return NULL; |
|
88 } |
|
89 memset(ptr, 0, nelem * elsize); |
83 memset(ptr, 0, nelem * elsize); |
90 return ptr; |
84 return ptr; |
91 } |
85 } |
92 |
86 |
93 static void *cx_mempool_realloc( |
87 static void *cx_mempool_realloc( |
99 |
93 |
100 struct cx_mempool_memory_s *mem, *newm; |
94 struct cx_mempool_memory_s *mem, *newm; |
101 mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func)); |
95 mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func)); |
102 newm = realloc(mem, n + sizeof(cx_destructor_func)); |
96 newm = realloc(mem, n + sizeof(cx_destructor_func)); |
103 |
97 |
104 if (newm == NULL) { |
98 if (newm == NULL) return NULL; |
105 return NULL; |
|
106 } |
|
107 if (mem != newm) { |
99 if (mem != newm) { |
108 for (size_t i = 0; i < pool->size; i++) { |
100 for (size_t i = 0; i < pool->size; i++) { |
109 if (pool->data[i] == mem) { |
101 if (pool->data[i] == mem) { |
110 pool->data[i] = newm; |
102 pool->data[i] = newm; |
111 return ((char*)newm) + sizeof(cx_destructor_func); |
103 return ((char*)newm) + sizeof(cx_destructor_func); |
112 } |
104 } |
113 } |
105 } |
114 abort(); |
106 abort(); // LCOV_EXCL_LINE |
115 } else { |
107 } else { |
116 return ptr; |
108 return ptr; |
117 } |
109 } |
118 } |
110 } |
119 |
111 |
216 return NULL; |
208 return NULL; |
217 } |
209 } |
218 |
210 |
219 struct cx_mempool_s *pool = |
211 struct cx_mempool_s *pool = |
220 malloc(sizeof(struct cx_mempool_s)); |
212 malloc(sizeof(struct cx_mempool_s)); |
221 if (pool == NULL) { |
213 if (pool == NULL) return NULL; |
222 return NULL; |
|
223 } |
|
224 |
214 |
225 CxAllocator *provided_allocator = malloc(sizeof(CxAllocator)); |
215 CxAllocator *provided_allocator = malloc(sizeof(CxAllocator)); |
226 if (provided_allocator == NULL) { |
216 if (provided_allocator == NULL) { // LCOV_EXCL_START |
227 free(pool); |
217 free(pool); |
228 return NULL; |
218 return NULL; |
229 } |
219 } // LCOV_EXCL_STOP |
230 provided_allocator->cl = &cx_mempool_allocator_class; |
220 provided_allocator->cl = &cx_mempool_allocator_class; |
231 provided_allocator->data = pool; |
221 provided_allocator->data = pool; |
232 |
222 |
233 pool->allocator = provided_allocator; |
223 pool->allocator = provided_allocator; |
234 |
224 |
235 pool->data = malloc(poolsize); |
225 pool->data = malloc(poolsize); |
236 if (pool->data == NULL) { |
226 if (pool->data == NULL) { // LCOV_EXCL_START |
237 free(provided_allocator); |
227 free(provided_allocator); |
238 free(pool); |
228 free(pool); |
239 return NULL; |
229 return NULL; |
240 } |
230 } // LCOV_EXCL_STOP |
241 |
231 |
242 pool->size = 0; |
232 pool->size = 0; |
243 pool->capacity = capacity; |
233 pool->capacity = capacity; |
244 pool->auto_destr = destr; |
234 pool->auto_destr = destr; |
245 |
235 |
246 return (CxMempool *) pool; |
236 return pool; |
247 } |
237 } |