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