Mercurial > molko
annotate libmlk-core/core/alloc.h @ 252:95c2c4a72410
core: add alloc_set to set SDL allocators as well
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 01 Dec 2020 21:53:23 +0100 |
parents | 71b3b7036de7 |
children | c4da052c0def |
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 |
243
71b3b7036de7
misc: lot of cleanups,
David Demelier <markand@malikania.fr>
parents:
237
diff
changeset
|
19 #ifndef MOLKO_CORE_ALLOC_H |
71b3b7036de7
misc: lot of cleanups,
David Demelier <markand@malikania.fr>
parents:
237
diff
changeset
|
20 #define MOLKO_CORE_ALLOC_H |
182
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 /** |
252
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
45 * \brief Allocator functions. |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
46 */ |
252
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
47 struct alloc_funcs { |
182
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 /** |
252
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
122 * Set custom allocator functions. |
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
123 * |
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
124 * The alloc_funcs struct must stay valid until the program is closed. |
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
125 * |
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
126 * \pre funcs != NULL |
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
127 * \pre funcs->alloc != NULL |
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
128 * \pre funcs->realloc != NULL |
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
129 * \pre funcs->free != NULL |
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
130 * \param funcs the functions |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
131 */ |
252
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
132 void |
95c2c4a72410
core: add alloc_set to set SDL allocators as well
David Demelier <markand@malikania.fr>
parents:
243
diff
changeset
|
133 alloc_set(const struct alloc_funcs *funcs); |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
134 |
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 * Shortcut for allocator->alloc. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
137 * |
227
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
138 * \pre size != 0 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
139 * \param size the amount of bytes to allocate |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
140 * \return The result of allocator->alloc. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
141 */ |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
142 void * |
227
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
143 alloc_new(size_t size); |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
144 |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
145 /** |
227
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
146 * Similar to alloc_new but zero-initialize the memory. |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
147 * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
148 * \pre size != 0 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
149 * \param size the amount of bytes to allocate |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
150 * \return The result of allocator->alloc. |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
151 */ |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
152 void * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
153 alloc_new0(size_t size); |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
154 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
155 /** |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
156 * Shortcut for allocator->alloc but specialized for arrays. |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
157 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
158 * \pre n != 0 && size != 0 |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
159 * \param n the number of objects to allocate |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
160 * \param size the size of each object |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
161 * \return The result of allocator->alloc. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
162 */ |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
163 void * |
227
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
164 alloc_array(size_t n, size_t size); |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
165 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
166 /** |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
167 * Similar to alloc_array but zero-initialize the memory. |
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 * \pre n != 0 && size != 0 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
170 * \param n the number of objects to allocate |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
171 * \param size the size of each object |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
172 * \return The result of allocator->alloc. |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
173 */ |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
174 void * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
175 alloc_array0(size_t n, size_t size); |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
176 |
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 * Shortcut for allocator->realloc. |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
179 * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
180 * \param ptr the old pointer (maybe NULL) |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
181 * \param amount the new amount (maybe 0) |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
182 * \return The result of allocator->realloc |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
183 */ |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
184 void * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
185 alloc_renew(void *ptr, size_t amount); |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
186 |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
187 /** |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
188 * Shortcut for allocator->realloc but specialized for arrays. |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
189 * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
190 * \param ptr the old pointer (maybe NULL) |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
191 * \param n the number of element |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
192 * \param size the size of each object |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
193 * \return The result of allocator->realloc |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
194 */ |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
195 void * |
befa2e855d3b
core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents:
226
diff
changeset
|
196 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
|
197 |
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 * Duplicate region pointer by ptr. |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
200 * |
228
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
201 * 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
|
202 * |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
203 * \pre ptr != NULL |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
204 * \param ptr the pointer |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
205 * \param size the size of the memory to copy |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
206 * \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
|
207 */ |
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
208 void * |
226
dd7c8d4321a3
misc: miscellaneous cleanups
David Demelier <markand@malikania.fr>
parents:
182
diff
changeset
|
209 alloc_dup(const void *ptr, size_t size); |
182
f6497ec74b49
core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
210 |
228
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
211 /** |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
212 * Initialize the pool. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
213 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
214 * 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
|
215 * ALLOC_POOL_INIT_DEFAULT. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
216 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
217 * 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
|
218 * array. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
219 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
220 * \pre pool != NULL |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
221 * \pre elemsize != 0 |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
222 * \param pool the pool to initialize |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
223 * \param elemsize size of each individual element |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
224 * \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
|
225 * \return True if allocation suceedeed. |
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 bool |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
228 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
|
229 |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
230 /** |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
231 * Request a new slot from the pool. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
232 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
233 * 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
|
234 * 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
|
235 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
236 * 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
|
237 * array. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
238 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
239 * \pre pool != NULL |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
240 * \param pool the pool |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
241 * \return The pointer to the region. |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
242 */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
243 void * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
244 alloc_pool_new(struct alloc_pool *pool); |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
245 |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
246 /** |
237
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
247 * Get the value at the given 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 * \pre pool != NULL |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
250 * \pre index < pool->size |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
251 * \param pool the pool |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
252 * \param index the object index |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
253 * \return The pointer to the region. |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
254 */ |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
255 void * |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
256 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
|
257 |
1bf5bd306bb0
core: add alloc_pool_get function
David Demelier <markand@malikania.fr>
parents:
228
diff
changeset
|
258 /** |
228
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
259 * 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
|
260 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
261 * 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
|
262 * |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
263 * \pre pool != NULL |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
264 * \param pool the pool to clear |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
265 */ |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
266 void |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
267 alloc_pool_finish(struct alloc_pool *pool); |
2734223d3daf
core: add a alloc_pool module
David Demelier <markand@malikania.fr>
parents:
227
diff
changeset
|
268 |
243
71b3b7036de7
misc: lot of cleanups,
David Demelier <markand@malikania.fr>
parents:
237
diff
changeset
|
269 #endif /* !MOLKO_CORE_ALLOC_H */ |