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