picox  0.1
xpico_allocator.h
[詳解]
1 
28 /*
29  * License: MIT license
30  * Copyright (c) <2015> <MaskedW [maskedw00@gmail.com]>
31  *
32  * Permission is hereby granted, free of charge, to any person
33  * obtaining a copy of this software and associated documentation
34  * files (the "Software"), to deal in the Software without
35  * restriction, including without limitation the rights to use, copy,
36  * modify, merge, publish, distribute, sublicense, and/or sell copies
37  * of the Software, and to permit persons to whom the Software is
38  * furnished to do so, subject to the following conditions:
39  *
40  * The above copyright notice and this permission notice shall be
41  * included in all copies or substantial portions of the Software.
42  *
43  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
44  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
46  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
47  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
48  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
49  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
50  * SOFTWARE.
51  */
52 
53 
54 #ifndef picox_allocator_xpico_allocator_h_
55 #define picox_allocator_xpico_allocator_h_
56 
57 
58 #include <picox/core/xcore.h>
59 
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 
68 typedef struct XPicoAllocator
69 {
71  uint8_t* heap;
72  uint8_t* top;
73  size_t capacity;
74  size_t reserve;
75  size_t alignment;
76  size_t max_used;
77  bool ownmemory;
79 
80 
108 bool xpalloc_init(XPicoAllocator* self, void* heap, size_t size, size_t alignment);
109 
110 
113 void xpalloc_deinit(XPicoAllocator* self);
114 
115 
121 void* xpalloc_allocate(XPicoAllocator* self, size_t size);
122 
123 
126 void* xpalloc_reallocate(XPicoAllocator* self, void* old_mem, size_t size);
127 
128 
129 
137 void xpalloc_deallocate(XPicoAllocator* self, void* ptr);
138 
139 
142 void xpalloc_clear(XPicoAllocator* self);
143 
144 
147 static inline uint8_t*
149 {
150  X_ASSERT(self);
151  return self->heap;
152 }
153 
154 
157 static inline size_t
159 {
160  X_ASSERT(self);
161  return self->reserve;
162 }
163 
164 
167 static inline size_t
169 {
170  X_ASSERT(self);
171  return self->capacity;
172 }
173 
176 static inline size_t
178 {
179  X_ASSERT(self);
180  return self->max_used;
181 }
182 
183 
186 size_t xpalloc_allocation_overhead(const XPicoAllocator* self, size_t n);
187 
188 
195 typedef void (*XPicoAllocatorWalker)(const uint8_t* chunk, size_t size, void* user);
196 
197 
205 void xpalloc_walk_heap(const XPicoAllocator* self, XPicoAllocatorWalker walker, void* user);
206 
207 
210 bool xpalloc_is_owner(const XPicoAllocator* self, const void* ptr);
211 
212 
213 #ifdef __cplusplus
214 }
215 #endif
216 
217 
218 #endif // picox_allocator_xpalloc_h_
static size_t xpalloc_reserve(const XPicoAllocator *self)
Definition: xpico_allocator.h:158
size_t xpalloc_allocation_overhead(const XPicoAllocator *self, size_t n)
Definition: xpico_allocator.c:213
static size_t xpalloc_max_used(const XPicoAllocator *self)
Definition: xpico_allocator.h:177
void xpalloc_clear(XPicoAllocator *self)
Definition: xpico_allocator.c:199
static size_t xpalloc_capacity(const XPicoAllocator *self)
Definition: xpico_allocator.h:168
void xpalloc_deinit(XPicoAllocator *self)
Definition: xpico_allocator.c:101
void * xpalloc_allocate(XPicoAllocator *self, size_t size)
Definition: xpico_allocator.c:113
void * xpalloc_reallocate(XPicoAllocator *self, void *old_mem, size_t size)
Definition: xpico_allocator.c:144
static uint8_t * xpalloc_heap(const XPicoAllocator *self)
Definition: xpico_allocator.h:148
bool xpalloc_is_owner(const XPicoAllocator *self, const void *ptr)
Definition: xpico_allocator.c:235
void xpalloc_walk_heap(const XPicoAllocator *self, XPicoAllocatorWalker walker, void *user)
Definition: xpico_allocator.c:221
void xpalloc_deallocate(XPicoAllocator *self, void *ptr)
Definition: xpico_allocator.c:179
uint8_t * heap
privatesection
Definition: xpico_allocator.h:71
Definition: xpico_allocator.h:68
bool xpalloc_init(XPicoAllocator *self, void *heap, size_t size, size_t alignment)
Definition: xpico_allocator.c:62
void(* XPicoAllocatorWalker)(const uint8_t *chunk, size_t size, void *user)
Definition: xpico_allocator.h:195