ChibiOS/RT  5.1.0
chheap.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
3 
4  This file is part of ChibiOS.
5 
6  ChibiOS is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  ChibiOS is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /**
21  * @file chheap.h
22  * @brief Heaps macros and structures.
23  *
24  * @addtogroup heaps
25  * @{
26  */
27 
28 #ifndef CHHEAP_H
29 #define CHHEAP_H
30 
31 #if !defined(CH_CFG_USE_HEAP)
32 #define CH_CFG_USE_HEAP FALSE
33 #endif
34 
35 #if (CH_CFG_USE_HEAP == TRUE) || defined(__DOXYGEN__)
36 
37 /*===========================================================================*/
38 /* Module constants. */
39 /*===========================================================================*/
40 
41 /**
42  * @brief Minimum alignment used for heap.
43  * @note Cannot use the sizeof operator in this macro.
44  */
45 #if (SIZEOF_PTR == 4) || defined(__DOXYGEN__)
46 #define CH_HEAP_ALIGNMENT 8U
47 #elif (SIZEOF_PTR == 2)
48 #define CH_HEAP_ALIGNMENT 4U
49 #else
50 #error "unsupported pointer size"
51 #endif
52 
53 /*===========================================================================*/
54 /* Module pre-compile time settings. */
55 /*===========================================================================*/
56 
57 /*===========================================================================*/
58 /* Derived constants and error checks. */
59 /*===========================================================================*/
60 
61 #if CH_CFG_USE_MEMCORE == FALSE
62 #error "CH_CFG_USE_HEAP requires CH_CFG_USE_MEMCORE"
63 #endif
64 
65 #if (CH_CFG_USE_MUTEXES == FALSE) && (CH_CFG_USE_SEMAPHORES == FALSE)
66 #error "CH_CFG_USE_HEAP requires CH_CFG_USE_MUTEXES and/or CH_CFG_USE_SEMAPHORES"
67 #endif
68 
69 /*===========================================================================*/
70 /* Module data structures and types. */
71 /*===========================================================================*/
72 
73 /**
74  * @brief Type of a memory heap.
75  */
76 typedef struct memory_heap memory_heap_t;
77 
78 /**
79  * @brief Type of a memory heap header.
80  */
81 typedef union heap_header heap_header_t;
82 
83 /**
84  * @brief Memory heap block header.
85  */
86 union heap_header {
87  struct {
88  heap_header_t *next; /**< @brief Next block in free list. */
89  size_t pages; /**< @brief Size of the area in pages. */
90  } free;
91  struct {
92  memory_heap_t *heap; /**< @brief Block owner heap. */
93  size_t size; /**< @brief Size of the area in bytes. */
94  } used;
95 };
96 
97 /**
98  * @brief Structure describing a memory heap.
99  */
100 struct memory_heap {
101  memgetfunc2_t provider; /**< @brief Memory blocks provider for
102  this heap. */
103  heap_header_t header; /**< @brief Free blocks list header. */
104 #if CH_CFG_USE_MUTEXES == TRUE
105  mutex_t mtx; /**< @brief Heap access mutex. */
106 #else
107  semaphore_t sem; /**< @brief Heap access semaphore. */
108 #endif
109 };
110 
111 /*===========================================================================*/
112 /* Module macros. */
113 /*===========================================================================*/
114 
115 /**
116  * @brief Allocation of an aligned static heap buffer.
117  */
118 #define CH_HEAP_AREA(name, size) \
119  ALIGNED_VAR(CH_HEAP_ALIGNMENT) \
120  uint8_t name[MEM_ALIGN_NEXT((size), CH_HEAP_ALIGNMENT)]
121 
122 /*===========================================================================*/
123 /* External declarations. */
124 /*===========================================================================*/
125 
126 #ifdef __cplusplus
127 extern "C" {
128 #endif
129  void _heap_init(void);
130  void chHeapObjectInit(memory_heap_t *heapp, void *buf, size_t size);
131  void *chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align);
132  void chHeapFree(void *p);
133  size_t chHeapStatus(memory_heap_t *heapp, size_t *totalp, size_t *largestp);
134 #ifdef __cplusplus
135 }
136 #endif
137 
138 /*===========================================================================*/
139 /* Module inline functions. */
140 /*===========================================================================*/
141 
142 /**
143  * @brief Allocates a block of memory from the heap by using the first-fit
144  * algorithm.
145  * @details The allocated block is guaranteed to be properly aligned for a
146  * pointer data type.
147  *
148  * @param[in] heapp pointer to a heap descriptor or @p NULL in order to
149  * access the default heap.
150  * @param[in] size the size of the block to be allocated. Note that the
151  * allocated block may be a bit bigger than the requested
152  * size for alignment and fragmentation reasons.
153  * @return A pointer to the allocated block.
154  * @retval NULL if the block cannot be allocated.
155  *
156  * @api
157  */
158 static inline void *chHeapAlloc(memory_heap_t *heapp, size_t size) {
159 
160  return chHeapAllocAligned(heapp, size, CH_HEAP_ALIGNMENT);
161 }
162 
163 /**
164  * @brief Returns the size of an allocated block.
165  * @note The returned value is the requested size, the real size is the
166  * same value aligned to the next @p CH_HEAP_ALIGNMENT multiple.
167  *
168  * @param[in] p pointer to the memory block
169  * @return Size of the block.
170  *
171  * @api
172  */
173 static inline size_t chHeapGetSize(const void *p) {
174 
175  return ((heap_header_t *)p - 1U)->used.size;
176 }
177 
178 #endif /* CH_CFG_USE_HEAP == TRUE */
179 
180 #endif /* CHHEAP_H */
181 
182 /** @} */
static size_t chHeapGetSize(const void *p)
Returns the size of an allocated block.
Definition: chheap.h:173
Memory heap block header.
Definition: chheap.h:86
memgetfunc2_t provider
Memory blocks provider for this heap.
Definition: chheap.h:101
heap_header_t header
Free blocks list header.
Definition: chheap.h:103
void chHeapFree(void *p)
Frees a previously allocated memory block.
Definition: chheap.c:293
#define CH_HEAP_ALIGNMENT
Minimum alignment used for heap.
Definition: chheap.h:46
Mutex structure.
Definition: chmtx.h:57
void chHeapObjectInit(memory_heap_t *heapp, void *buf, size_t size)
Initializes a memory heap from a static memory area.
Definition: chheap.c:131
mutex_t mtx
Heap access mutex.
Definition: chheap.h:105
size_t chHeapStatus(memory_heap_t *heapp, size_t *totalp, size_t *largestp)
Reports the heap status.
Definition: chheap.c:357
size_t pages
Size of the area in pages.
Definition: chheap.h:89
size_t size
Size of the area in bytes.
Definition: chheap.h:93
Semaphore structure.
Definition: chsem.h:52
heap_header_t * next
Next block in free list.
Definition: chheap.h:88
memory_heap_t * heap
Block owner heap.
Definition: chheap.h:92
static void * chHeapAlloc(memory_heap_t *heapp, size_t size)
Allocates a block of memory from the heap by using the first-fit algorithm.
Definition: chheap.h:158
void * chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align)
Allocates a block of memory from the heap by using the first-fit algorithm.
Definition: chheap.c:172
Structure describing a memory heap.
Definition: chheap.h:100
void *(* memgetfunc2_t)(size_t size, unsigned align, size_t offset)
Enhanced memory get function.
Definition: chmemcore.h:80
void _heap_init(void)
Initializes the default heap.
Definition: chheap.c:107