ucx/mempool.c

Mon, 21 Jul 2014 13:18:32 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 21 Jul 2014 13:18:32 +0200
changeset 184
5c0990c95f74
parent 177
11ad03783baf
child 192
1e51558b9d09
permissions
-rw-r--r--

added some string tests

13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
177
11ad03783baf go ahead, nothing to see here
Mike Becker <universe@uap-core.de>
parents: 158
diff changeset
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
5 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
6 * Redistribution and use in source and binary forms, with or without
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
7 * modification, are permitted provided that the following conditions are met:
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
8 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
10 * notice, this list of conditions and the following disclaimer.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
11 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
14 * documentation and/or other materials provided with the distribution.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
15 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdlib.h>
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <string.h>
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdio.h>
95
ecfdc1c4a552 added gnu++11 support
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
32 #ifdef __cplusplus
ecfdc1c4a552 added gnu++11 support
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
33 #define __STDC_FORMAT_MACROS
ecfdc1c4a552 added gnu++11 support
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
34 #endif
75
990734f548ef fixed some compiler warnings
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
35 #include <inttypes.h>
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
17
2e7050c3a18e fix for ultra fail fix
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
37 #include "mempool.h"
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
39 /** Capsule for destructible memory chunks. */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 typedef struct {
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
41 /** The destructor for the memory chunk. */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 ucx_destructor destructor;
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
43 /**
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
44 * First byte of the memory chunk.
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
45 * Note, that the address <code>&amp;c</code> is also the address
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
46 * of the whole memory chunk.
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
47 */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 char c;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 } ucx_memchunk;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
51 /** Capsule for data and its destructor. */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 typedef struct {
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
53 /** The destructor for the data. */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 ucx_destructor destructor;
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
55 /** A pointer to the data. */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 void *ptr;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 } ucx_regdestr;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
131
fc3af16818a3 fixed some warnings when compiling with the sun C++ Compiler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
59 UCX_EXTERN void ucx_mempool_shared_destr(void* ptr) {
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 ucx_regdestr *rd = (ucx_regdestr*)ptr;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 rd->destructor(rd->ptr);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 UcxMempool *ucx_mempool_new(size_t n) {
14
b78e174b6814 Fixed realloc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
65 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
66 if (!pool) {
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
67 return NULL;
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
68 }
15
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
69
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
70 pool->data = (void**) malloc(n * sizeof(void*));
15
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
71 if (pool->data == NULL) {
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
72 free(pool);
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
73 return NULL;
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
74 }
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
75
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 pool->ndata = 0;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 pool->size = n;
158
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
78
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
79 UcxAllocator *allocator = (UcxAllocator*)malloc(sizeof(UcxAllocator));
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
80 if(!allocator) {
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
81 free(pool->data);
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
82 free(pool);
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
83 return NULL;
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
84 }
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
85 allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc;
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
86 allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc;
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
87 allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc;
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
88 allocator->free = (ucx_allocator_free)ucx_mempool_free;
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
89 allocator->pool = pool;
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
90 pool->allocator = allocator;
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
91
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 return pool;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
15
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
95 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
96 void **data = (void**) realloc(pool->data, newcap*sizeof(void*));
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
97 if (data) {
15
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
98 pool->data = data;
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
99 pool->size = newcap;
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
100 return EXIT_SUCCESS;
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
101 } else {
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
102 return EXIT_FAILURE;
15
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
103 }
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
141
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 138
diff changeset
107 if (pool->ndata >= pool->size) {
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 138
diff changeset
108 // The hard coded 16 is documented for this function and ucx_mempool_new
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 138
diff changeset
109 if (ucx_mempool_chcap(pool, pool->size + 16) == EXIT_FAILURE) {
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 138
diff changeset
110 return NULL;
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 138
diff changeset
111 }
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 138
diff changeset
112 }
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 138
diff changeset
113
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
115 if (!mem) {
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
116 return NULL;
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
117 }
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 mem->destructor = NULL;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 pool->data[pool->ndata] = mem;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 pool->ndata++;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
123 return &(mem->c);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
128 if (!ptr) {
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 return NULL;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 memset(ptr, 0, nelem * elsize);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 return ptr;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
136 char *mem = ((char*)ptr) - sizeof(ucx_destructor);
15
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
137 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
138 if (!newm) {
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
139 return NULL;
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
140 }
15
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
141 if (mem != newm) {
95
ecfdc1c4a552 added gnu++11 support
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
142 for(size_t i=0 ; i < pool->ndata ; i++) {
14
b78e174b6814 Fixed realloc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
143 if(pool->data[i] == mem) {
b78e174b6814 Fixed realloc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
144 pool->data[i] = newm;
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
145 return newm + sizeof(ucx_destructor);
14
b78e174b6814 Fixed realloc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
146 }
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 }
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 113
diff changeset
148 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
75
990734f548ef fixed some compiler warnings
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
149 (intptr_t)ptr, (intptr_t)pool);
141
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 138
diff changeset
150 exit(EXIT_FAILURE);
16
b4769e4eb4d1 fixed ultra fail + renamed files from mpool to mempool
Mike Becker <universe@uap-core.de>
parents: 15
diff changeset
151 } else {
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
152 return newm + sizeof(ucx_destructor);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
156 void ucx_mempool_free(UcxMempool *pool, void *ptr) {
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
157 ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor));
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
158 for(size_t i=0 ; i<pool->ndata ; i++) {
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
159 if(chunk == pool->data[i]) {
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
160 if(chunk->destructor != NULL) {
141
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 138
diff changeset
161 chunk->destructor(&(chunk->c));
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
162 }
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
163 free(chunk);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
164 size_t last_index = pool->ndata - 1;
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
165 if(i != last_index) {
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
166 pool->data[i] = pool->data[last_index];
141
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 138
diff changeset
167 pool->data[last_index] = NULL;
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
168 }
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
169 pool->ndata--;
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
170 return;
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
171 }
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
172 }
116
234920008754 some fixes and some documentation
Mike Becker <universe@uap-core.de>
parents: 113
diff changeset
173 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
174 (intptr_t)ptr, (intptr_t)pool);
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
175 exit(EXIT_FAILURE);
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
176 }
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
177
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
178 void ucx_mempool_destroy(UcxMempool *pool) {
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 ucx_memchunk *chunk;
95
ecfdc1c4a552 added gnu++11 support
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
180 for(size_t i=0 ; i<pool->ndata ; i++) {
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 chunk = (ucx_memchunk*) pool->data[i];
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
182 if(chunk) {
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
183 if(chunk->destructor) {
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
184 chunk->destructor(&(chunk->c));
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
185 }
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
186 free(chunk);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 free(pool->data);
158
81d580042da1 Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
190 free(pool->allocator);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 free(pool);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 void ucx_mempool_set_destr(void *ptr, ucx_destructor func) {
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) {
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc(
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 pool,
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 sizeof(ucx_regdestr));
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 rd->destructor = destr;
14
b78e174b6814 Fixed realloc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
203 rd->ptr = ptr;
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 }
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
206

mercurial