annotate libcore/core/alloc.c @ 237:1bf5bd306bb0

core: add alloc_pool_get function
author David Demelier <markand@malikania.fr>
date Fri, 27 Nov 2020 13:31:27 +0100
parents 2734223d3daf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
182
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * alloc.h -- custom allocators
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <assert.h>
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
20 #include <errno.h>
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
21 #include <stdlib.h>
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
22 #include <string.h>
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
23
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
24 #include "alloc.h"
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
25 #include "error.h"
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
26 #include "panic.h"
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
27
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
28 static void *
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
29 panic_alloc(size_t size)
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 {
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
31 void *mem;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
32
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
33 if (!(mem = malloc(size)))
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
34 panicf("%s", strerror(errno));
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
35
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
36 return mem;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
37 }
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
38
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 static void *
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
40 panic_realloc(void *ptr, size_t size)
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 {
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
42 void *mem;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
43
188
cbce9143bf87 core: allow realloc of 0 for duktape
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
44 if (!(mem = realloc(ptr, size)) && size != 0)
182
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
45 panicf("%s", strerror(errno));
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
46
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 return mem;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 }
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
49
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 struct allocator allocator = {
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
51 .alloc = panic_alloc,
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
52 .realloc = panic_realloc,
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
53 .free = free
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
54 };
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
55
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
56 void *
227
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
57 alloc_new(size_t size)
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
58 {
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
59 assert(size != 0);
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
60
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
61 return allocator.alloc(size);
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
62 }
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
63
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
64 void *
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
65 alloc_new0(size_t size)
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
66 {
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
67 assert(size != 0);
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
68
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
69 void *ptr;
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
70
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
71 if ((ptr = allocator.alloc(size)))
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
72 memset(ptr, 0, size);
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
73
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
74 return ptr;
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
75 }
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
76
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
77 void *
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
78 alloc_array(size_t n, size_t size)
182
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
79 {
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
80 assert(n != 0);
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
81 assert(size != 0);
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
82
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
83 size_t total = n * size;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
84
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
85 if (total / n != size)
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
86 return errorf("%s", strerror(ENOMEM)), NULL;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
87
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
88 return allocator.alloc(total);
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
89 }
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
90
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
91 void *
227
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
92 alloc_array0(size_t n, size_t size)
182
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
93 {
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 assert(n != 0);
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
95 assert(size != 0);
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
96
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
97 void *mem;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
98 size_t total = n * size;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
99
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
100 if (total / n != size)
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
101 return errorf("%s", strerror(ENOMEM)), NULL;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
102
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
103 if ((mem = allocator.alloc(total)))
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
104 memset(mem, 0, total);
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
105
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
106 return mem;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
107 }
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
108
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
109 void *
227
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
110 alloc_renew(void *ptr, size_t amount)
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
111 {
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
112 return allocator.realloc(ptr, amount);
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
113 }
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
114
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
115 void *
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
116 alloc_rearray(void *ptr, size_t n, size_t size)
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
117 {
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
118 size_t total = n * size;
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
119
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
120 if (total / n != size)
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
121 return errorf("%s", strerror(ENOMEM)), NULL;
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
122
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
123 return allocator.realloc(ptr, total);
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
124 }
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
125
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
126 void *
182
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
127 alloc_dup(const void *ptr, size_t size)
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
128 {
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
129 assert(ptr);
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
130 assert(size != 0);
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
131
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
132 void *mem;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
133
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
134 if ((mem = allocator.alloc(size)))
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
135 memcpy(mem, ptr, size);
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
136
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
137 return mem;
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff changeset
138 }
227
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
139
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
140 char *
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
141 alloc_sdup(const char *src)
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
142 {
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
143 assert(src);
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
144
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
145 char *ret;
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
146 size_t length = strlen(src) + 1;
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
147
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
148 if ((ret = allocator.alloc(length)))
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
149 memcpy(ret, src, length + 1);
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
150
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
151 return ret;
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
152 }
228
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
153
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
154 bool
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
155 alloc_pool_init(struct alloc_pool *pool, size_t elemsize, void (*finalizer)(void *))
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
156 {
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
157 assert(pool);
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
158 assert(elemsize != 0);
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
159
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
160 if (!(pool->data = alloc_array(ALLOC_POOL_INIT_DEFAULT, elemsize)))
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
161 return false;
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
162
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
163 pool->elemsize = elemsize;
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
164 pool->size = 0;
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
165 pool->capacity = ALLOC_POOL_INIT_DEFAULT;
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
166 pool->finalizer = finalizer;
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
167
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
168 return true;
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
169 }
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
170
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
171 void *
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
172 alloc_pool_new(struct alloc_pool *pool)
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
173 {
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
174 assert(pool);
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
175
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
176 if (pool->size >= pool->capacity) {
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
177 void *newptr = alloc_rearray(pool->data, pool->capacity * 2, pool->elemsize);
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
178
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
179 if (!newptr)
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
180 return NULL;
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
181
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
182 pool->data = newptr;
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
183 pool->capacity *= 2;
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
184 }
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
185
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
186 return ((unsigned char *)pool->data) + pool->size++ * pool->elemsize;
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
187 }
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
188
237
1bf5bd306bb0 core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents: 228
diff changeset
189 void *
1bf5bd306bb0 core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents: 228
diff changeset
190 alloc_pool_get(const struct alloc_pool *pool, size_t index)
1bf5bd306bb0 core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents: 228
diff changeset
191 {
1bf5bd306bb0 core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents: 228
diff changeset
192 assert(pool);
1bf5bd306bb0 core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents: 228
diff changeset
193 assert(index < pool->size);
1bf5bd306bb0 core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents: 228
diff changeset
194
1bf5bd306bb0 core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents: 228
diff changeset
195 return ((unsigned char *)pool->data) + index * pool->elemsize;
1bf5bd306bb0 core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents: 228
diff changeset
196 }
1bf5bd306bb0 core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents: 228
diff changeset
197
228
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
198 void
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
199 alloc_pool_finish(struct alloc_pool *pool)
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
200 {
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
201 if (pool->finalizer) {
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
202 unsigned char *tab = pool->data;
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
203
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
204 for (size_t i = 0; i < pool->size; ++i)
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
205 pool->finalizer(tab + i * pool->elemsize);
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
206 }
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
207
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
208 free(pool->data);
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
209 memset(pool, 0, sizeof (*pool));
2734223d3daf core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents: 227
diff changeset
210 }