picox  0.1
xfixed_allocator.h
[詳解]
1 
52 /*
53  * License: MIT license
54  * Copyright (c) <2015> <MaskedW [maskedw00@gmail.com]>
55  *
56  * Permission is hereby granted, free of charge, to any person
57  * obtaining a copy of self software and associated documentation
58  * files (the "Software"), to deal in the Software without
59  * restriction, including without limitation the rights to use, copy,
60  * modify, merge, publish, distribute, sublicense, and/or sell copies
61  * of the Software, and to permit persons to whom the Software is
62  * furnished to do so, subject to the following conditions:
63  *
64  * The above copyright notice and self permission notice shall be
65  * included in all copies or substantial portions of the Software.
66  *
67  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
68  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
69  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
70  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
71  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
72  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
73  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
74  * SOFTWARE.
75  */
76 
77 
78 #ifndef picox_allocator_xfixed_allocator_h_
79 #define picox_allocator_xfixed_allocator_h_
80 
81 
82 #include <picox/core/xcore.h>
83 
84 
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 
89 
92 typedef struct XFixedAllocator
93 {
95  uint8_t* heap;
96  uint8_t* top;
97  uint8_t* next;
98  size_t block_size;
99  size_t num_blocks;
100  size_t remain_blocks;
101  size_t alignment;
103 
104 
105 #define XFALLOC_MIN_ALIGNMENT (X_ALIGN_OF(size_t))
106 
107 
126 void xfalloc_init(XFixedAllocator* self, void* heap, size_t heap_size, size_t block_size);
127 
128 
131 void* xfalloc_allocate(XFixedAllocator* self);
132 
133 
138 void xfalloc_deallocate(XFixedAllocator* self, void* ptr);
139 
140 
143 void xfalloc_clear(XFixedAllocator* self);
144 
145 
148 static inline void*
150 {
151  X_ASSERT(self);
152  return self->heap;
153 }
154 
155 
158 static inline size_t
160 {
161  X_ASSERT(self);
162  return self->block_size;
163 }
164 
165 
168 static inline size_t
170 {
171  X_ASSERT(self);
172  return self->num_blocks;
173 }
174 
175 
178 static inline size_t
180 {
181  X_ASSERT(self);
182  return self->remain_blocks;
183 }
184 
185 
186 #ifdef __cplusplus
187 }
188 #endif
189 
190 
191 #endif // picox_allocator_xfixed_allocator_h_
void * xfalloc_allocate(XFixedAllocator *self)
Definition: xfixed_allocator.c:89
uint8_t * heap
privatesection
Definition: xfixed_allocator.h:95
Definition: xfixed_allocator.h:92
void xfalloc_clear(XFixedAllocator *self)
Definition: xfixed_allocator.c:80
static void * xfalloc_heap(const XFixedAllocator *self)
Definition: xfixed_allocator.h:149
static size_t xfalloc_block_size(const XFixedAllocator *self)
Definition: xfixed_allocator.h:159
static size_t xfalloc_num_blocks(const XFixedAllocator *self)
Definition: xfixed_allocator.h:169
static size_t xfalloc_remain_blocks(const XFixedAllocator *self)
Definition: xfixed_allocator.h:179
void xfalloc_deallocate(XFixedAllocator *self, void *ptr)
Definition: xfixed_allocator.c:106
void xfalloc_init(XFixedAllocator *self, void *heap, size_t heap_size, size_t block_size)
Definition: xfixed_allocator.c:46