diff -r 5dbef9e07376 -r 11dffb40cd91 ucx/mempool.c
--- a/ucx/mempool.c Fri Aug 16 12:41:30 2013 +0200
+++ b/ucx/mempool.c Sat Aug 17 12:04:04 2013 +0200
@@ -36,13 +36,23 @@
#include "mempool.h"
+/** Capsule for destructible memory chunks. */
typedef struct {
+ /** The destructor for the memory chunk. */
ucx_destructor destructor;
+ /**
+ * First byte of the memory chunk.
+ * Note, that the address &c
is also the address
+ * of the whole memory chunk.
+ */
char c;
} ucx_memchunk;
+/** Capsule for data and its destructor. */
typedef struct {
+ /** The destructor for the data. */
ucx_destructor destructor;
+ /** A pointer to the data. */
void *ptr;
} ucx_regdestr;
@@ -80,16 +90,18 @@
}
void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
+ if (pool->ndata >= pool->size) {
+ // The hard coded 16 is documented for this function and ucx_mempool_new
+ if (ucx_mempool_chcap(pool, pool->size + 16) == EXIT_FAILURE) {
+ return NULL;
+ }
+ }
+
ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
if (!mem) {
return NULL;
}
- if (pool->ndata >= pool->size) {
- // The hard coded 16 is documented for this function and ucx_mempool_new
- ucx_mempool_chcap(pool, pool->size + 16);
- }
-
mem->destructor = NULL;
pool->data[pool->ndata] = mem;
pool->ndata++;
@@ -121,7 +133,7 @@
}
fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
(intptr_t)ptr, (intptr_t)pool);
- exit(1);
+ exit(EXIT_FAILURE);
} else {
return newm + sizeof(ucx_destructor);
}
@@ -132,12 +144,13 @@
for(size_t i=0 ; indata ; i++) {
if(chunk == pool->data[i]) {
if(chunk->destructor != NULL) {
- chunk->destructor(&chunk->c);
+ chunk->destructor(&(chunk->c));
}
free(chunk);
size_t last_index = pool->ndata - 1;
if(i != last_index) {
pool->data[i] = pool->data[last_index];
+ pool->data[last_index] = NULL;
}
pool->ndata--;
return;