Mercurial > molko
annotate libcore/core/alloc.h @ 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 |
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 #ifndef MOLKO_ALLOC_H |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
20 #define MOLKO_ALLOC_H |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
21 |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
22 /** |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
23 * \file alloc.h |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
24 * \brief Custom allocators. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
25 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
26 * This module controls how the API should allocate memory. Although dynamic |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
27 * allocation isn't much used in the core API, it is used in few places where |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
28 * static arrays would not fit (e.g. loading maps). |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
29 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
30 * To change the allocator, simply modify the global allocator object. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
31 */ |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
32 |
228
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
33 #include <stdbool.h> |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
34 #include <stddef.h> |
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 #include "util.h" |
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 /** |
228
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
39 * \brief Default size to allocate in struct alloc_pool. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
40 * \warning Must be a power of 2. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
41 */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
42 #define ALLOC_POOL_INIT_DEFAULT 32 |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
43 |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
44 /** |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
45 * \brief Global allocator strategy. |
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 struct allocator { |
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 * (+) Allocate some data. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
50 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
51 * The default implementation uses malloc and calls \ref panic in case |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
52 * of failure. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
53 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
54 * \pre size != 0 |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
55 * \param size the size to alloc |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
56 * \return A pointer to the allocated region (NULL is allowed). |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
57 */ |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
58 void *(*alloc)(size_t size); |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
59 |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
60 /** |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
61 * (+) Realloc a region. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
62 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
63 * The default implementation uses malloc and calls \ref panic in case |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
64 * of failure. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
65 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
66 * \pre size != 0 |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
67 * \param ptr the old region |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
68 * \param size the new size |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
69 * \return A pointer to the allocated region (NULL is allowed). |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
70 */ |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
71 void *(*realloc)(void *ptr, size_t size); |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
72 |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
73 /** |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
74 * (+) Free resources. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
75 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
76 * The default implementation calls standard C free function. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
77 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
78 * \param ptr the region (may be NULL) |
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 void (*free)(void *ptr); |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
81 }; |
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 /** |
228
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
84 * \brief Pool allocator. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
85 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
86 * This small structure is a helper to reallocate data each time a new slot is |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
87 * requested. It allocates twice as the current storage when size exceeds |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
88 * capacity. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
89 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
90 * It uses realloc mechanism to upgrade the new storage space so pointers |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
91 * returned must not be referenced directly. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
92 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
93 * It is designed in mind to help allocating resources locally that may be |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
94 * referenced in another module without having to manage an array from the user |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
95 * code. Because it is designed for this responsability only it only supports |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
96 * insertion. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
97 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
98 * The initial capacity is controlled by the ALLOC_POOL_INIT_DEFAULT macro and |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
99 * **must** be a power of two. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
100 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
101 * A custom finalizer function can be set to finalize each individual object if |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
102 * necessary. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
103 */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
104 struct alloc_pool { |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
105 void *data; /*!< (+?) Pointer to the region. */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
106 size_t elemsize; /*!< (-) Size of individual element. */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
107 size_t size; /*!< (-) Number of items in array. */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
108 size_t capacity; /*!< (-) Current capacity. */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
109 |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
110 /** |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
111 * (+?) Optional finalizer. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
112 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
113 * This function will be invoked for every element when \ref |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
114 * alloc_pool_finish is called. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
115 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
116 * \param data the element to finalize |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
117 */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
118 void (*finalizer)(void *data); |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
119 }; |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
120 |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
121 /** |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
122 * \brief Global allocator object. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
123 */ |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
124 extern struct allocator allocator; |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
125 |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
126 /** |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
127 * Shortcut for allocator->alloc. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
128 * |
227
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
129 * \pre size != 0 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
130 * \param size the amount of bytes to allocate |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
131 * \return The result of allocator->alloc. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
132 */ |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
133 void * |
227
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
134 alloc_new(size_t size); |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
135 |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
136 /** |
227
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
137 * Similar to alloc_new but zero-initialize the memory. |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
138 * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
139 * \pre size != 0 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
140 * \param size the amount of bytes to allocate |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
141 * \return The result of allocator->alloc. |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
142 */ |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
143 void * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
144 alloc_new0(size_t size); |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
145 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
146 /** |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
147 * Shortcut for allocator->alloc but specialized for arrays. |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
148 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
149 * \pre n != 0 && size != 0 |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
150 * \param n the number of objects to allocate |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
151 * \param size the size of each object |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
152 * \return The result of allocator->alloc. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
153 */ |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
154 void * |
227
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
155 alloc_array(size_t n, size_t size); |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
156 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
157 /** |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
158 * Similar to alloc_array but zero-initialize the memory. |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
159 * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
160 * \pre n != 0 && size != 0 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
161 * \param n the number of objects to allocate |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
162 * \param size the size of each object |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
163 * \return The result of allocator->alloc. |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
164 */ |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
165 void * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
166 alloc_array0(size_t n, size_t size); |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
167 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
168 /** |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
169 * Shortcut for allocator->realloc. |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
170 * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
171 * \param ptr the old pointer (maybe NULL) |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
172 * \param amount the new amount (maybe 0) |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
173 * \return The result of allocator->realloc |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
174 */ |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
175 void * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
176 alloc_renew(void *ptr, size_t amount); |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
177 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
178 /** |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
179 * Shortcut for allocator->realloc but specialized for arrays. |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
180 * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
181 * \param ptr the old pointer (maybe NULL) |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
182 * \param n the number of element |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
183 * \param size the size of each object |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
184 * \return The result of allocator->realloc |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
185 */ |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
186 void * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
187 alloc_rearray(void *ptr, size_t n, size_t size); |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
188 |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
189 /** |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
190 * Duplicate region pointer by ptr. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
191 * |
228
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
192 * This function calls \ref alloc_new to allocate memory. |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
193 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
194 * \pre ptr != NULL |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
195 * \param ptr the pointer |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
196 * \param size the size of the memory to copy |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
197 * \return The result of allocator->alloc filled with a copy of ptr. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
198 */ |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
199 void * |
226
dd7c8d4321a3
misc: miscellaneous cleanups
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
200 alloc_dup(const void *ptr, size_t size); |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
201 |
228
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
202 /** |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
203 * Initialize the pool. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
204 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
205 * This will effectively create a initial storage according to |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
206 * ALLOC_POOL_INIT_DEFAULT. |
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 * This function effectively use the global allocator object to initialize the |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
209 * array. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
210 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
211 * \pre pool != NULL |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
212 * \pre elemsize != 0 |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
213 * \param pool the pool to initialize |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
214 * \param elemsize size of each individual element |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
215 * \param finalizer a finalizer to use when clearing the pool. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
216 * \return True if allocation suceedeed. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
217 */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
218 bool |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
219 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
|
220 |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
221 /** |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
222 * Request a new slot from the pool. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
223 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
224 * If the current size has reached the capacity, it will be doubled in that case |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
225 * it is possible that all previous pointer may be invalidated. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
226 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
227 * This function effectively use the global allocator object to realloc the |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
228 * array. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
229 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
230 * \pre pool != NULL |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
231 * \param pool the pool |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
232 * \return The pointer to the region. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
233 */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
234 void * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
235 alloc_pool_new(struct alloc_pool *pool); |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
236 |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
237 /** |
237
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
238 * Get the value at the given index. |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
239 * |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
240 * \pre pool != NULL |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
241 * \pre index < pool->size |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
242 * \param pool the pool |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
243 * \param index the object index |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
244 * \return The pointer to the region. |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
245 */ |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
246 void * |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
247 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
|
248 |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
249 /** |
228
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
250 * Finalize the pool and all individual element if a finalizer is set. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
251 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
252 * You must call \ref alloc_pool_init again before reusing it. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
253 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
254 * \pre pool != NULL |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
255 * \param pool the pool to clear |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
256 */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
257 void |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
258 alloc_pool_finish(struct alloc_pool *pool); |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
259 |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
260 #endif /* !MOLKO_ALLOC_H */ |