src/server/ucx/mempool.c

Mon, 09 Sep 2013 12:15:54 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 09 Sep 2013 12:15:54 +0200
changeset 93
95b77e842db3
parent 88
73b3485e96f1
permissions
-rw-r--r--

fixed solaris build

15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
5 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
6 * Redistribution and use in source and binary forms, with or without
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
7 * modification, are permitted provided that the following conditions are met:
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
8 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
10 * notice, this list of conditions and the following disclaimer.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
11 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
14 * documentation and/or other materials provided with the distribution.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
15 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdlib.h>
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <string.h>
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdio.h>
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
32 #ifdef __cplusplus
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
33 #define __STDC_FORMAT_MACROS
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
34 #endif
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
35 #include <inttypes.h>
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include "mempool.h"
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 typedef struct {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 ucx_destructor destructor;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 char c;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 } ucx_memchunk;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 typedef struct {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 ucx_destructor destructor;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 void *ptr;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 } ucx_regdestr;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 void ucx_mempool_shared_destr(void* ptr) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 ucx_regdestr *rd = (ucx_regdestr*)ptr;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 rd->destructor(rd->ptr);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 UcxMempool *ucx_mempool_new(size_t n) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 if (pool == NULL) return NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
58 pool->data = (void**) malloc(n * sizeof(void*));
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 if (pool->data == NULL) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 free(pool);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 return NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 pool->ndata = 0;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 pool->size = n;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 return pool;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
70 void **data = (void**) realloc(pool->data, newcap*sizeof(void*));
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 if (data == NULL) {
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
72 return 1;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 } else {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 pool->data = data;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 pool->size = newcap;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 return EXIT_SUCCESS;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
80 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 if (mem == NULL) return NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 if (pool->ndata >= pool->size) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 ucx_mempool_chcap(pool, pool->size + 16);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 mem->destructor = NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 pool->data[pool->ndata] = mem;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 pool->ndata++;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 return &mem->c;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
95 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 if(ptr == NULL) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 return NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 memset(ptr, 0, nelem * elsize);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 return ptr;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
104 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
105 char *mem = ((char*)ptr) - sizeof(ucx_destructor);
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 if (newm == NULL) return NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 if (mem != newm) {
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
109 for(size_t i=0 ; i < pool->ndata ; i++) {
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 if(pool->data[i] == mem) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 pool->data[i] = newm;
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
112 return newm + sizeof(ucx_destructor);
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 }
88
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
115 fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
116 (intptr_t)ptr, (intptr_t)pool);
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 exit(1);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 } else {
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
119 return newm + sizeof(ucx_destructor);
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122
88
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
123 void ucx_mempool_free(UcxMempool *pool, void *ptr) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
124 ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor));
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
125 for(size_t i=0 ; i<pool->ndata ; i++) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
126 if(chunk == pool->data[i]) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
127 if(chunk->destructor != NULL) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
128 chunk->destructor(&chunk->c);
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
129 }
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
130 free(chunk);
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
131 size_t last_index = pool->ndata - 1;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
132 if(i != last_index) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
133 pool->data[i] = pool->data[last_index];
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
134 }
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
135 pool->ndata--;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
136 return;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
137 }
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
138 }
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
139 fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
140 (intptr_t)ptr, (intptr_t)pool);
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
141 exit(1);
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
142 }
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
143
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
144 void ucx_mempool_destroy(UcxMempool *pool) {
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 ucx_memchunk *chunk;
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
146 for(size_t i=0 ; i<pool->ndata ; i++) {
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 chunk = (ucx_memchunk*) pool->data[i];
88
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
148 if(chunk) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
149 if(chunk->destructor != NULL) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
150 chunk->destructor(&chunk->c);
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
151 }
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
152 free(chunk);
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 free(pool->data);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 free(pool);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 void ucx_mempool_set_destr(void *ptr, ucx_destructor func) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc(
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 pool,
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 sizeof(ucx_regdestr));
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 rd->destructor = destr;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 rd->ptr = ptr;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 }
88
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
171
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
172 UcxAllocator* ucx_mempool_allocator(UcxMempool *pool) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
173 UcxAllocator *allocator = (UcxAllocator*)ucx_mempool_malloc(
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
174 pool, sizeof(UcxAllocator));
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
175 if(!allocator) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
176 return NULL;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
177 }
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
178 allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
179 allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
180 allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
181 allocator->free = (ucx_allocator_free)ucx_mempool_free;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
182 allocator->pool = pool;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
183 return allocator;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
184 }

mercurial