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> |
8 #include <errno.h> |
32 #ifdef __cplusplus |
|
33 #define __STDC_FORMAT_MACROS |
|
34 #endif |
|
35 #include <inttypes.h> |
9 |
36 |
10 #include "mempool.h" |
37 #include "mempool.h" |
11 |
38 |
12 typedef struct { |
39 typedef struct { |
13 ucx_destructor destructor; |
40 ucx_destructor destructor; |
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 == NULL) return NULL; |
30 |
57 |
31 pool->data = malloc(n * sizeof(void*)); |
58 pool->data = (void**) malloc(n * sizeof(void*)); |
32 if (pool->data == NULL) { |
59 if (pool->data == NULL) { |
33 free(pool); |
60 free(pool); |
34 return NULL; |
61 return NULL; |
35 } |
62 } |
36 |
63 |
77 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { |
104 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { |
78 char *mem = ((char*)ptr) - sizeof(ucx_destructor); |
105 char *mem = ((char*)ptr) - sizeof(ucx_destructor); |
79 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); |
106 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); |
80 if (newm == NULL) return NULL; |
107 if (newm == NULL) return NULL; |
81 if (mem != newm) { |
108 if (mem != newm) { |
82 for(int i=0;i<pool->ndata;i++) { |
109 for(size_t i=0 ; i < pool->ndata ; i++) { |
83 if(pool->data[i] == mem) { |
110 if(pool->data[i] == mem) { |
84 pool->data[i] = newm; |
111 pool->data[i] = newm; |
85 return newm + sizeof(ucx_destructor); |
112 return newm + sizeof(ucx_destructor); |
86 } |
113 } |
87 } |
114 } |
88 fprintf(stderr, "FATAL: %8x not in mpool %8x\n", ptr, pool); |
115 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
|
116 (intptr_t)ptr, (intptr_t)pool); |
89 exit(1); |
117 exit(1); |
90 } else { |
118 } else { |
91 return newm + sizeof(ucx_destructor); |
119 return newm + sizeof(ucx_destructor); |
92 } |
120 } |
93 } |
121 } |
94 |
122 |
95 void ucx_mempool_free(UcxMempool *pool) { |
123 void ucx_mempool_free(UcxMempool *pool) { |
96 ucx_memchunk *chunk; |
124 ucx_memchunk *chunk; |
97 for(int i=0;i<pool->ndata;i++) { |
125 for(size_t i=0 ; i<pool->ndata ; i++) { |
98 chunk = (ucx_memchunk*) pool->data[i]; |
126 chunk = (ucx_memchunk*) pool->data[i]; |
99 if(chunk->destructor != NULL) { |
127 if(chunk->destructor != NULL) { |
100 chunk->destructor(&chunk->c); |
128 chunk->destructor(&chunk->c); |
101 } |
129 } |
102 free(chunk); |
130 free(chunk); |