27 */ |
27 */ |
28 |
28 |
29 #include "cx/mempool.h" |
29 #include "cx/mempool.h" |
30 |
30 |
31 #include <string.h> |
31 #include <string.h> |
|
32 #include <errno.h> |
32 |
33 |
33 struct cx_mempool_memory_s { |
34 struct cx_mempool_memory_s { |
34 /** The destructor. */ |
35 /** The destructor. */ |
35 cx_destructor_func destructor; |
36 cx_destructor_func destructor; |
36 /** The actual memory. */ |
37 /** The actual memory. */ |
43 ) { |
44 ) { |
44 struct cx_mempool_s *pool = p; |
45 struct cx_mempool_s *pool = p; |
45 |
46 |
46 if (pool->size >= pool->capacity) { |
47 if (pool->size >= pool->capacity) { |
47 size_t newcap = pool->capacity - (pool->capacity % 16) + 16; |
48 size_t newcap = pool->capacity - (pool->capacity % 16) + 16; |
48 struct cx_mempool_memory_s **newdata = realloc(pool->data, newcap*sizeof(struct cx_mempool_memory_s*)); |
49 size_t newmsize; |
|
50 if (pool->capacity > newcap || cx_szmul(newcap, |
|
51 sizeof(struct cx_mempool_memory_s*), &newmsize)) { |
|
52 errno = EOVERFLOW; |
|
53 return NULL; |
|
54 } |
|
55 struct cx_mempool_memory_s **newdata = realloc(pool->data, newmsize); |
49 if (newdata == NULL) { |
56 if (newdata == NULL) { |
50 return NULL; |
57 return NULL; |
51 } |
58 } |
52 pool->data = newdata; |
59 pool->data = newdata; |
53 pool->capacity = newcap; |
60 pool->capacity = newcap; |
202 size_t capacity, |
210 size_t capacity, |
203 cx_destructor_func destr |
211 cx_destructor_func destr |
204 ) { |
212 ) { |
205 size_t poolsize; |
213 size_t poolsize; |
206 if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) { |
214 if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) { |
|
215 errno = EOVERFLOW; |
207 return NULL; |
216 return NULL; |
208 } |
217 } |
209 |
218 |
210 struct cx_mempool_s *pool = |
219 struct cx_mempool_s *pool = |
211 malloc(sizeof(struct cx_mempool_s)); |
220 malloc(sizeof(struct cx_mempool_s)); |