src/basic_mempool.c

Wed, 08 Feb 2023 18:56:58 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 08 Feb 2023 18:56:58 +0100
changeset 646
dfd0403ff8b6
parent 572
f0f99dd06d9f
child 650
77021e06b1a8
permissions
-rw-r--r--

add pointer swap utility

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
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/basic_mempool.h"
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 <stdint.h>
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include <string.h>
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 #define of_chk_(n) if (SIZE_MAX - sizeof(cx_destructor_func) < (n)) return NULL
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 /** Internal structure for denoting pooled memory. */
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 typedef struct {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 /** The destructor. */
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 cx_destructor_func destructor;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 /**
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 * Access to the first byte of the polled memory.
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 */
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 char c;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 } cx_basic_mempool_memory;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 static int cx_basic_mempool_chcap(
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 struct cx_basic_mempool_s *pool,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 size_t newcap
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 ) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 if (newcap < pool->ndata) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 return 1;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 size_t newcapsz;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 if (cx_szmul(newcap, sizeof(void *), &newcapsz)) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 return 1;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 void **data = realloc(pool->data, newcapsz);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 if (data) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 pool->data = data;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 pool->size = newcap;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 return 0;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 } else {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 return 1;
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 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 void *cx_malloc_basic_mempool(
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 void *data,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 size_t n
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 of_chk_(n);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 struct cx_basic_mempool_s *pool = data;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 if (pool->ndata >= pool->size) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 size_t newcap = pool->size * 2;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 if (newcap < pool->size || cx_basic_mempool_chcap(pool, newcap)) {
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 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82
572
f0f99dd06d9f #201 - remove dangerous allocator config
Mike Becker <universe@uap-core.de>
parents: 571
diff changeset
83 cx_basic_mempool_memory *mem = malloc(sizeof(cx_destructor_func) + n);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 if (mem == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 mem->destructor = NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 pool->data[pool->ndata] = mem;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 pool->ndata++;
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 return &(mem->c);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 void *cx_calloc_basic_mempool(
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 void *data,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 size_t nelem,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 size_t elsize
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 ) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 size_t msz;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 if (cx_szmul(nelem, elsize, &msz)) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 void *ptr = cx_malloc_basic_mempool(data, msz);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 if (ptr == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 memset(ptr, 0, nelem * elsize);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 return ptr;
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
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 void *cx_realloc_basic_mempool(
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 void *data,
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 size_t n
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 ) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 of_chk_(n);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 struct cx_basic_mempool_s *pool = data;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 char *mem = ((char *) ptr) - sizeof(cx_destructor_func);
572
f0f99dd06d9f #201 - remove dangerous allocator config
Mike Becker <universe@uap-core.de>
parents: 571
diff changeset
121 char *newm = (char *) realloc(mem, n + sizeof(cx_destructor_func));
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 if (newm == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 if (mem != newm) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 cx_for_n(i, pool->ndata) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 if (pool->data[i] == mem) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 pool->data[i] = newm;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 return newm + sizeof(cx_destructor_func);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 abort();
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 } else {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 return newm + sizeof(cx_destructor_func);
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
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 void cx_free_basic_mempool(
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 void *data,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 void *ptr
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 ) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 struct cx_basic_mempool_s *pool = data;
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 cx_basic_mempool_memory *mem = (cx_basic_mempool_memory *)
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 ((char *) ptr - sizeof(cx_destructor_func));
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 cx_for_n(i, pool->ndata) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 if (mem == pool->data[i]) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 if (mem->destructor != NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 mem->destructor(&(mem->c));
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 }
572
f0f99dd06d9f #201 - remove dangerous allocator config
Mike Becker <universe@uap-core.de>
parents: 571
diff changeset
151 free(mem);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 size_t last_index = pool->ndata - 1;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 if (i != last_index) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 pool->data[i] = pool->data[last_index];
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 pool->data[last_index] = NULL;
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 pool->ndata--;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 return;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 }
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 abort();
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 }
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 void cx_basic_mempool_destroy(CxMempool *p) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 struct cx_basic_mempool_s *pool = (struct cx_basic_mempool_s *) p;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 cx_basic_mempool_memory *mem;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 cx_for_n(i, pool->ndata) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 mem = (cx_basic_mempool_memory *) pool->data[i];
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 if (mem) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 if (mem->destructor) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 mem->destructor(&(mem->c));
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 }
572
f0f99dd06d9f #201 - remove dangerous allocator config
Mike Becker <universe@uap-core.de>
parents: 571
diff changeset
173 free(mem);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 free(pool->data);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 free((void *) p->allocator);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 free(pool);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 void cx_basic_mempool_set_destr(
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 __attribute__((__unused__)) CxMempool *pool,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 void *ptr,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 cx_destructor_func func
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 ) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 static cx_allocator_class cx_basic_mempool_allocator_class = {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 cx_malloc_basic_mempool,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 cx_realloc_basic_mempool,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 cx_calloc_basic_mempool,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 cx_free_basic_mempool
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 };
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 static cx_mempool_class cx_basic_mempool_class = {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 cx_basic_mempool_destroy,
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 cx_basic_mempool_set_destr,
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
572
f0f99dd06d9f #201 - remove dangerous allocator config
Mike Becker <universe@uap-core.de>
parents: 571
diff changeset
201 CxMempool *cxBasicMempoolCreate(size_t capacity) {
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 size_t poolsize;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 if (cx_szmul(capacity, sizeof(void *), &poolsize)) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 struct cx_basic_mempool_s *pool =
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 malloc(sizeof(struct cx_basic_mempool_s));
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 if (pool == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 if (!provided_allocator) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 free(pool);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 return NULL;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 provided_allocator->cl = &cx_basic_mempool_allocator_class;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 provided_allocator->data = pool;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 pool->base.cl = &cx_basic_mempool_class;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 pool->base.allocator = provided_allocator;
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 pool->data = malloc(poolsize);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 if (pool->data == NULL) {
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 free(provided_allocator);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 free(pool);
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 return NULL;
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
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 pool->ndata = 0;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 pool->size = capacity;
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 return (CxMempool *) pool;
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 }

mercurial