src/ucx/mempool.c

Sun, 15 Sep 2024 09:47:36 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 15 Sep 2024 09:47:36 +0200
changeset 558
0e79e17c70e2
parent 504
c094afcdfb27
permissions
-rw-r--r--

improve sessionhandler trace logging

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

mercurial