src/mempool.c

Tue, 12 Nov 2024 17:34:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 12 Nov 2024 17:34:04 +0100
changeset 987
53e22f112b11
parent 985
68754c7de906
permissions
-rw-r--r--

add configure option for szmul

571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
29 #include "cx/mempool.h"
970
c9b02747cfc5 move szmul to common.h
Mike Becker <universe@uap-core.de>
parents: 962
diff changeset
30
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include <string.h>
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
33 struct cx_mempool_memory_s {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 /** The destructor. */
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 cx_destructor_func destructor;
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
36 /** The actual memory. */
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
37 char c[];
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
38 };
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
40 static void *cx_mempool_malloc(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
41 void *p,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
42 size_t n
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 ) {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
44 struct cx_mempool_s *pool = p;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
46 if (pool->size >= pool->capacity) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
47 size_t newcap = pool->capacity - (pool->capacity % 16) + 16;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
48 struct cx_mempool_memory_s **newdata = realloc(pool->data, newcap*sizeof(struct cx_mempool_memory_s*));
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
49 if (newdata == NULL) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
50 return NULL;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
51 }
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
52 pool->data = newdata;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
53 pool->capacity = newcap;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
56 struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 if (mem == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
61 mem->destructor = pool->auto_destr;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
62 pool->data[pool->size] = mem;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
63 pool->size++;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
65 return mem->c;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
68 static void *cx_mempool_calloc(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
69 void *p,
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 size_t nelem,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 size_t elsize
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 ) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 size_t msz;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 if (cx_szmul(nelem, elsize, &msz)) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 }
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
77 void *ptr = cx_mempool_malloc(p, msz);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 if (ptr == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 memset(ptr, 0, nelem * elsize);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 return ptr;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
85 static void *cx_mempool_realloc(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
86 void *p,
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 void *ptr,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 size_t n
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 ) {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
90 struct cx_mempool_s *pool = p;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
92 struct cx_mempool_memory_s *mem, *newm;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
93 mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func));
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
94 newm = realloc(mem, n + sizeof(cx_destructor_func));
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
95
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 if (newm == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 if (mem != newm) {
962
cd418898af5c remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents: 947
diff changeset
100 for (size_t i = 0; i < pool->size; i++) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 if (pool->data[i] == mem) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 pool->data[i] = newm;
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
103 return ((char*)newm) + sizeof(cx_destructor_func);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 abort();
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 } else {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
108 return ptr;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
112 static void cx_mempool_free(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
113 void *p,
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 void *ptr
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 ) {
947
2a47b4a5c216 fix cx_mempool_free crash if ptr is null
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 727
diff changeset
116 if (!ptr) return;
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
117 struct cx_mempool_s *pool = p;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
119 struct cx_mempool_memory_s *mem = (struct cx_mempool_memory_s *)
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 ((char *) ptr - sizeof(cx_destructor_func));
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
121
962
cd418898af5c remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents: 947
diff changeset
122 for (size_t i = 0; i < pool->size; i++) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 if (mem == pool->data[i]) {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
124 if (mem->destructor) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
125 mem->destructor(mem->c);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 }
572
f0f99dd06d9f #201 - remove dangerous allocator config
Mike Becker <universe@uap-core.de>
parents: 571
diff changeset
127 free(mem);
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
128 size_t last_index = pool->size - 1;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 if (i != last_index) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 pool->data[i] = pool->data[last_index];
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 pool->data[last_index] = NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 }
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
133 pool->size--;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 return;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 abort();
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
140 void cxMempoolDestroy(CxMempool *pool) {
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 970
diff changeset
141 if (pool == NULL) return;
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
142 struct cx_mempool_memory_s *mem;
962
cd418898af5c remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents: 947
diff changeset
143 for (size_t i = 0; i < pool->size; i++) {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
144 mem = pool->data[i];
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
145 if (mem->destructor) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
146 mem->destructor(mem->c);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 }
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
148 free(mem);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 free(pool->data);
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
151 free((void*) pool->allocator);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 free(pool);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
155 void cxMempoolSetDestructor(
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 void *ptr,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 cx_destructor_func func
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 ) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 970
diff changeset
162 void cxMempoolRemoveDestructor(void *ptr) {
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 970
diff changeset
163 *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = NULL;
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 970
diff changeset
164 }
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 970
diff changeset
165
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
166 struct cx_mempool_foreign_mem_s {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
167 cx_destructor_func destr;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
168 void* mem;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 };
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
171 static void cx_mempool_destr_foreign_mem(void* ptr) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
172 struct cx_mempool_foreign_mem_s *fm = ptr;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
173 fm->destr(fm->mem);
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
174 }
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
175
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
176 int cxMempoolRegister(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
177 CxMempool *pool,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
178 void *memory,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
179 cx_destructor_func destr
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
180 ) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
181 struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
182 pool,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
183 sizeof(struct cx_mempool_foreign_mem_s)
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
184 );
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
185 if (fm == NULL) return 1;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
186
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
187 fm->mem = memory;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
188 fm->destr = destr;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
189 *(cx_destructor_func *) ((char *) fm - sizeof(cx_destructor_func)) = cx_mempool_destr_foreign_mem;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
190
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
191 return 0;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
192 }
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
193
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
194 static cx_allocator_class cx_mempool_allocator_class = {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
195 cx_mempool_malloc,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
196 cx_mempool_realloc,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
197 cx_mempool_calloc,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
198 cx_mempool_free
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 };
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
201 CxMempool *cxMempoolCreate(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
202 size_t capacity,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
203 cx_destructor_func destr
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
204 ) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 size_t poolsize;
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
206 if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
210 struct cx_mempool_s *pool =
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
211 malloc(sizeof(struct cx_mempool_s));
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 if (pool == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
217 if (provided_allocator == NULL) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 free(pool);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 }
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
221 provided_allocator->cl = &cx_mempool_allocator_class;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 provided_allocator->data = pool;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
224 pool->allocator = provided_allocator;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 pool->data = malloc(poolsize);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 if (pool->data == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 free(provided_allocator);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 free(pool);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
233 pool->size = 0;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
234 pool->capacity = capacity;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
235 pool->auto_destr = destr;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237 return (CxMempool *) pool;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 }

mercurial