src/mempool.c

Tue, 08 Oct 2024 19:52:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 08 Oct 2024 19:52:09 +0200
changeset 922
eabfbe9e2952
parent 727
d92a59f5d261
child 947
2a47b4a5c216
permissions
-rw-r--r--

also report coverage for header files

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"
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "cx/utils.h"
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) {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
100 cx_for_n(i, pool->size) {
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 ) {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
116 struct cx_mempool_s *pool = p;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
118 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
119 ((char *) ptr - sizeof(cx_destructor_func));
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
120
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
121 cx_for_n(i, pool->size) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 if (mem == pool->data[i]) {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
123 if (mem->destructor) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
124 mem->destructor(mem->c);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 }
572
f0f99dd06d9f #201 - remove dangerous allocator config
Mike Becker <universe@uap-core.de>
parents: 571
diff changeset
126 free(mem);
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
127 size_t last_index = pool->size - 1;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 if (i != last_index) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 pool->data[i] = pool->data[last_index];
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 pool->data[last_index] = NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 }
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
132 pool->size--;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 return;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 }
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 abort();
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
139 void cxMempoolDestroy(CxMempool *pool) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
140 struct cx_mempool_memory_s *mem;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
141 cx_for_n(i, pool->size) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
142 mem = pool->data[i];
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
143 if (mem->destructor) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
144 mem->destructor(mem->c);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 }
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
146 free(mem);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 free(pool->data);
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
149 free((void*) pool->allocator);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 free(pool);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
153 void cxMempoolSetDestructor(
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 void *ptr,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 cx_destructor_func func
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 ) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 *(cx_destructor_func *) ((char *) ptr - sizeof(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
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
160 struct cx_mempool_foreign_mem_s {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
161 cx_destructor_func destr;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
162 void* mem;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 };
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
165 static void cx_mempool_destr_foreign_mem(void* ptr) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
166 struct cx_mempool_foreign_mem_s *fm = ptr;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
167 fm->destr(fm->mem);
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
168 }
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
169
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
170 int cxMempoolRegister(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
171 CxMempool *pool,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
172 void *memory,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
173 cx_destructor_func destr
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 struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
176 pool,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
177 sizeof(struct cx_mempool_foreign_mem_s)
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
178 );
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
179 if (fm == NULL) return 1;
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 fm->mem = memory;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
182 fm->destr = destr;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
183 *(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
184
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
185 return 0;
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
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
188 static cx_allocator_class cx_mempool_allocator_class = {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
189 cx_mempool_malloc,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
190 cx_mempool_realloc,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
191 cx_mempool_calloc,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
192 cx_mempool_free
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 };
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
195 CxMempool *cxMempoolCreate(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
196 size_t capacity,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
197 cx_destructor_func destr
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
198 ) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 size_t poolsize;
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
200 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
201 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
204 struct cx_mempool_s *pool =
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
205 malloc(sizeof(struct cx_mempool_s));
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 if (pool == NULL) {
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
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
211 if (provided_allocator == NULL) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 free(pool);
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 }
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
215 provided_allocator->cl = &cx_mempool_allocator_class;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 provided_allocator->data = pool;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
218 pool->allocator = provided_allocator;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 pool->data = malloc(poolsize);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 if (pool->data == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 free(provided_allocator);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 free(pool);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 return NULL;
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
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
227 pool->size = 0;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
228 pool->capacity = capacity;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
229 pool->auto_destr = destr;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 return (CxMempool *) pool;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 }

mercurial