src/mempool.c

Wed, 25 Dec 2024 19:33:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 25 Dec 2024 19:33:04 +0100
changeset 1054
fb1076ead56f
parent 1040
1ecf4dbbc60c
permissions
-rw-r--r--

add implementation for creating new json values

relates to #527

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>
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
32 #include <errno.h>
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
34 struct cx_mempool_memory_s {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 /** The destructor. */
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 cx_destructor_func destructor;
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
37 /** The actual memory. */
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
38 char c[];
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
39 };
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
41 static void *cx_mempool_malloc(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
42 void *p,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
43 size_t n
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 ) {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
45 struct cx_mempool_s *pool = p;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
47 if (pool->size >= pool->capacity) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
48 size_t newcap = pool->capacity - (pool->capacity % 16) + 16;
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
49 size_t newmsize;
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
50 if (pool->capacity > newcap || cx_szmul(newcap,
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
51 sizeof(struct cx_mempool_memory_s*), &newmsize)) {
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
52 errno = EOVERFLOW;
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
53 return NULL;
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
54 }
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
55 struct cx_mempool_memory_s **newdata = realloc(pool->data, newmsize);
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
56 if (newdata == NULL) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
57 return NULL;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
58 }
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
59 pool->data = newdata;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
60 pool->capacity = newcap;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
63 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
64 if (mem == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 return NULL;
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 mem->destructor = pool->auto_destr;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
69 pool->data[pool->size] = mem;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
70 pool->size++;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
72 return mem->c;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
75 static void *cx_mempool_calloc(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
76 void *p,
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 size_t nelem,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 size_t elsize
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 ) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 size_t msz;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 if (cx_szmul(nelem, elsize, &msz)) {
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
82 errno = EOVERFLOW;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 return NULL;
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 void *ptr = cx_mempool_malloc(p, msz);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 if (ptr == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 memset(ptr, 0, nelem * elsize);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 return ptr;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
93 static void *cx_mempool_realloc(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
94 void *p,
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 void *ptr,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 size_t n
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 ) {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
98 struct cx_mempool_s *pool = p;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
100 struct cx_mempool_memory_s *mem, *newm;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
101 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
102 newm = realloc(mem, n + sizeof(cx_destructor_func));
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
103
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 if (newm == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 if (mem != newm) {
962
cd418898af5c remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents: 947
diff changeset
108 for (size_t i = 0; i < pool->size; i++) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 if (pool->data[i] == mem) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 pool->data[i] = newm;
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
111 return ((char*)newm) + sizeof(cx_destructor_func);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 abort();
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 } else {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
116 return ptr;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
120 static void cx_mempool_free(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
121 void *p,
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 void *ptr
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 ) {
947
2a47b4a5c216 fix cx_mempool_free crash if ptr is null
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 727
diff changeset
124 if (!ptr) return;
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
125 struct cx_mempool_s *pool = p;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
127 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
128 ((char *) ptr - sizeof(cx_destructor_func));
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
129
962
cd418898af5c remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents: 947
diff changeset
130 for (size_t i = 0; i < pool->size; i++) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 if (mem == pool->data[i]) {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
132 if (mem->destructor) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
133 mem->destructor(mem->c);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 }
572
f0f99dd06d9f #201 - remove dangerous allocator config
Mike Becker <universe@uap-core.de>
parents: 571
diff changeset
135 free(mem);
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
136 size_t last_index = pool->size - 1;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 if (i != last_index) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 pool->data[i] = pool->data[last_index];
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 pool->data[last_index] = NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 }
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
141 pool->size--;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 return;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 abort();
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
148 void cxMempoolFree(CxMempool *pool) {
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 970
diff changeset
149 if (pool == NULL) return;
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
150 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
151 for (size_t i = 0; i < pool->size; i++) {
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
152 mem = pool->data[i];
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
153 if (mem->destructor) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
154 mem->destructor(mem->c);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 }
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
156 free(mem);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 free(pool->data);
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
159 free((void*) pool->allocator);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 free(pool);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
163 void cxMempoolSetDestructor(
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 void *ptr,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 cx_destructor_func func
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 ) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 970
diff changeset
170 void cxMempoolRemoveDestructor(void *ptr) {
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 970
diff changeset
171 *(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
172 }
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 970
diff changeset
173
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
174 struct cx_mempool_foreign_mem_s {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
175 cx_destructor_func destr;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
176 void* mem;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 };
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
179 static void cx_mempool_destr_foreign_mem(void* ptr) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
180 struct cx_mempool_foreign_mem_s *fm = ptr;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
181 fm->destr(fm->mem);
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
182 }
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
183
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
184 int cxMempoolRegister(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
185 CxMempool *pool,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
186 void *memory,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
187 cx_destructor_func destr
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
188 ) {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
189 struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
190 pool,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
191 sizeof(struct cx_mempool_foreign_mem_s)
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 if (fm == NULL) return 1;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
194
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
195 fm->mem = memory;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
196 fm->destr = destr;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
197 *(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
198
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
199 return 0;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
200 }
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
201
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
202 static cx_allocator_class cx_mempool_allocator_class = {
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
203 cx_mempool_malloc,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
204 cx_mempool_realloc,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
205 cx_mempool_calloc,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
206 cx_mempool_free
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 };
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
209 CxMempool *cxMempoolCreate(
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
210 size_t capacity,
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
211 cx_destructor_func destr
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
212 ) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 size_t poolsize;
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
214 if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) {
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
215 errno = EOVERFLOW;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
219 struct cx_mempool_s *pool =
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
220 malloc(sizeof(struct cx_mempool_s));
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 if (pool == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
226 if (provided_allocator == NULL) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 free(pool);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 }
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
230 provided_allocator->cl = &cx_mempool_allocator_class;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 provided_allocator->data = pool;
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->allocator = provided_allocator;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 pool->data = malloc(poolsize);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 if (pool->data == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237 free(provided_allocator);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 free(pool);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241
727
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
242 pool->size = 0;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
243 pool->capacity = capacity;
d92a59f5d261 improve mempool implementation
Mike Becker <universe@uap-core.de>
parents: 650
diff changeset
244 pool->auto_destr = destr;
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 return (CxMempool *) pool;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 }

mercurial