src/mempool.c

changeset 1040
1ecf4dbbc60c
parent 993
b642eca4b956
equal deleted inserted replaced
1039:ec62453fc8a6 1040:1ecf4dbbc60c
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;
70 size_t nelem, 77 size_t nelem,
71 size_t elsize 78 size_t elsize
72 ) { 79 ) {
73 size_t msz; 80 size_t msz;
74 if (cx_szmul(nelem, elsize, &msz)) { 81 if (cx_szmul(nelem, elsize, &msz)) {
82 errno = EOVERFLOW;
75 return NULL; 83 return NULL;
76 } 84 }
77 void *ptr = cx_mempool_malloc(p, msz); 85 void *ptr = cx_mempool_malloc(p, msz);
78 if (ptr == NULL) { 86 if (ptr == NULL) {
79 return NULL; 87 return NULL;
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));

mercurial