ucx/mempool.c

changeset 95
ecfdc1c4a552
parent 75
990734f548ef
child 103
08018864fb91
equal deleted inserted replaced
94:57ea041df22f 95:ecfdc1c4a552
3 */ 3 */
4 4
5 #include <stdlib.h> 5 #include <stdlib.h>
6 #include <string.h> 6 #include <string.h>
7 #include <stdio.h> 7 #include <stdio.h>
8 #ifdef __cplusplus
9 #define __STDC_FORMAT_MACROS
10 #endif
8 #include <inttypes.h> 11 #include <inttypes.h>
9 12
10 #include "mempool.h" 13 #include "mempool.h"
11 14
12 typedef struct { 15 typedef struct {
77 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { 80 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
78 char *mem = ((char*)ptr) - sizeof(ucx_destructor); 81 char *mem = ((char*)ptr) - sizeof(ucx_destructor);
79 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); 82 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
80 if (newm == NULL) return NULL; 83 if (newm == NULL) return NULL;
81 if (mem != newm) { 84 if (mem != newm) {
82 for(int i=0;i<pool->ndata;i++) { 85 for(size_t i=0 ; i < pool->ndata ; i++) {
83 if(pool->data[i] == mem) { 86 if(pool->data[i] == mem) {
84 pool->data[i] = newm; 87 pool->data[i] = newm;
85 return newm + sizeof(ucx_destructor); 88 return newm + sizeof(ucx_destructor);
86 } 89 }
87 } 90 }
88 fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08"PRIxPTR"\n", 91 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
89 (intptr_t)ptr, (intptr_t)pool); 92 (intptr_t)ptr, (intptr_t)pool);
90 exit(1); 93 exit(1);
91 } else { 94 } else {
92 return newm + sizeof(ucx_destructor); 95 return newm + sizeof(ucx_destructor);
93 } 96 }
94 } 97 }
95 98
96 void ucx_mempool_free(UcxMempool *pool) { 99 void ucx_mempool_free(UcxMempool *pool) {
97 ucx_memchunk *chunk; 100 ucx_memchunk *chunk;
98 for(int i=0;i<pool->ndata;i++) { 101 for(size_t i=0 ; i<pool->ndata ; i++) {
99 chunk = (ucx_memchunk*) pool->data[i]; 102 chunk = (ucx_memchunk*) pool->data[i];
100 if(chunk->destructor != NULL) { 103 if(chunk->destructor != NULL) {
101 chunk->destructor(&chunk->c); 104 chunk->destructor(&chunk->c);
102 } 105 }
103 free(chunk); 106 free(chunk);

mercurial