picox  0.1
xstack_allocator.h
[詳解]
1 
50 /*
51  * License: MIT license
52  * Copyright (c) <2015> <MaskedW [maskedw00@gmail.com]>
53  *
54  * Permission is hereby granted, free of charge, to any person
55  * obtaining a copy of this software and associated documentation
56  * files (the "Software"), to deal in the Software without
57  * restriction, including without limitation the rights to use, copy,
58  * modify, merge, publish, distribute, sublicense, and/or sell copies
59  * of the Software, and to permit persons to whom the Software is
60  * furnished to do so, subject to the following conditions:
61  *
62  * The above copyright notice and this permission notice shall be
63  * included in all copies or substantial portions of the Software.
64  *
65  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
66  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
67  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
68  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
69  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
70  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
71  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
72  * SOFTWARE.
73  */
74 
75 
76 #ifndef picox_allocator_xstack_allocator_h_
77 #define picox_allocator_xstack_allocator_h_
78 
79 
80 #include <picox/core/xcore.h>
81 
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
87 
90 typedef struct XStackAllocator
91 {
93  uint8_t* heap;
94  uint8_t* begin;
95  uint8_t* end;
96  size_t capacity;
97  size_t alignment;
98  bool growth_upward;
100 
101 
118 void xsalloc_init(XStackAllocator* self, void* heap, size_t size, size_t alignment);
119 
120 
126 void* xsalloc_allocate(XStackAllocator* self, size_t size);
127 
128 
131 void xsalloc_clear(XStackAllocator* self);
132 
133 
140 void xsalloc_rewind(XStackAllocator* self, void* begin, void* end);
141 
142 
145 static inline bool
147 {
148  X_ASSERT(self);
149  return self->growth_upward;
150 }
151 
152 
155 static inline void
157 {
158  X_ASSERT(self);
159  self->growth_upward = growth_upward;
160 }
161 
162 
165 static inline size_t
167 {
168  X_ASSERT(self);
169  return self->end - self->begin;
170 }
171 
172 
175 static inline size_t
177 {
178  X_ASSERT(self);
179  return self->capacity;
180 }
181 
182 
185 static inline size_t
187 {
188  X_ASSERT(self);
189  return self->alignment;
190 }
191 
192 
195 static inline uint8_t*
197 {
198  X_ASSERT(self);
199  return self->heap;
200 }
201 
202 
205 static inline uint8_t*
207 {
208  X_ASSERT(self);
209  return self->begin;
210 }
211 
212 
215 static inline uint8_t*
217 {
218  X_ASSERT(self);
219  return self->end;
220 }
221 
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 
227 
228 #endif // picox_allocator_xsalloc_h_
static size_t xsalloc_reserve(const XStackAllocator *self)
Definition: xstack_allocator.h:166
static size_t xsalloc_capacity(const XStackAllocator *self)
Definition: xstack_allocator.h:176
static uint8_t * xsalloc_heap(const XStackAllocator *self)
Definition: xstack_allocator.h:196
Definition: xstack_allocator.h:90
static void xsalloc_set_growth_direction(XStackAllocator *self, bool growth_upward)
Definition: xstack_allocator.h:156
static uint8_t * xsalloc_end(const XStackAllocator *self)
Definition: xstack_allocator.h:216
static size_t xsalloc_alignment(const XStackAllocator *self)
Definition: xstack_allocator.h:186
void xsalloc_init(XStackAllocator *self, void *heap, size_t size, size_t alignment)
Definition: xstack_allocator.c:60
static uint8_t * xsalloc_bedin(const XStackAllocator *self)
Definition: xstack_allocator.h:206
uint8_t * heap
privatesection
Definition: xstack_allocator.h:93
void * xsalloc_allocate(XStackAllocator *self, size_t size)
Definition: xstack_allocator.c:88
void xsalloc_rewind(XStackAllocator *self, void *begin, void *end)
Definition: xstack_allocator.c:123
static bool xsalloc_growth_direction(XStackAllocator *self)
Definition: xstack_allocator.h:146
void xsalloc_clear(XStackAllocator *self)
Definition: xstack_allocator.c:114