123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #ifndef CHMEMHEAPS_H
- #define CHMEMHEAPS_H
- #if (CH_CFG_USE_HEAP == TRUE) || defined(__DOXYGEN__)
- #if (SIZEOF_PTR == 4) || defined(__DOXYGEN__)
- #define CH_HEAP_ALIGNMENT 8U
- #elif (SIZEOF_PTR == 2)
- #define CH_HEAP_ALIGNMENT 4U
- #else
- #error "unsupported pointer size"
- #endif
- #if CH_CFG_USE_MEMCORE == FALSE
- #error "CH_CFG_USE_HEAP requires CH_CFG_USE_MEMCORE"
- #endif
- #if (CH_CFG_USE_MUTEXES == FALSE) && (CH_CFG_USE_SEMAPHORES == FALSE)
- #error "CH_CFG_USE_HEAP requires CH_CFG_USE_MUTEXES and/or CH_CFG_USE_SEMAPHORES"
- #endif
- typedef struct memory_heap memory_heap_t;
- typedef union heap_header heap_header_t;
- union heap_header {
- struct {
- heap_header_t *next;
- size_t pages;
- } free;
- struct {
- memory_heap_t *heap;
- size_t size;
- } used;
- };
- struct memory_heap {
- memgetfunc2_t provider;
- heap_header_t header;
- #if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
- mutex_t mtx;
- #else
- semaphore_t sem;
- #endif
- };
- #define CH_HEAP_AREA(name, size) \
- ALIGNED_VAR(CH_HEAP_ALIGNMENT) \
- uint8_t name[MEM_ALIGN_NEXT((size), CH_HEAP_ALIGNMENT)]
- #ifdef __cplusplus
- extern "C" {
- #endif
- void _heap_init(void);
- void chHeapObjectInit(memory_heap_t *heapp, void *buf, size_t size);
- void *chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align);
- void chHeapFree(void *p);
- size_t chHeapStatus(memory_heap_t *heapp, size_t *totalp, size_t *largestp);
- #ifdef __cplusplus
- }
- #endif
- static inline void *chHeapAlloc(memory_heap_t *heapp, size_t size) {
- return chHeapAllocAligned(heapp, size, CH_HEAP_ALIGNMENT);
- }
- static inline size_t chHeapGetSize(const void *p) {
- return ((heap_header_t *)p - 1U)->used.size;
- }
- #endif
- #endif
|