18 #ifdef ZRTOS_ARCH__BYTE_ALIGNMENT 19 #define ZRTOS_MALLOC__BYTE_ALIGNMENT ZRTOS_ARCH__BYTE_ALIGNMENT 21 #define ZRTOS_MALLOC__BYTE_ALIGNMENT ZRTOS_TYPES__BYTE_ALIGNMENT 29 #ifndef ZRTOS_MALLOC__CFG_DISABLE_FREE 39 #define ZRTOS_MALLOC__GET_REQUIRED_SIZE(type,count) \ 40 ( sizeof(zrtos_malloc_internal_t) \ 43 sizeof(zrtos_malloc_heap_chunk_t) \ 44 + zrtos_types__ceil_size_to_alignment(sizeof(type)) \ 50 #define ZRTOS_MALLOC__GLOBAL_HEAP_INIT(name) \ 53 ,sizeof(name) / sizeof(name[0]) \ 57 zrtos_debug__memset( \ 58 zrtos_types__ptr_add(name,sizeof(zrtos_malloc_internal_t)) \ 60 ,(sizeof(name) / sizeof(name[0])) \ 61 -sizeof(zrtos_malloc_internal_t) \ 65 #define ZRTOS_MALLOC__GLOBAL_HEAP(name,len) \ 66 zrtos_malloc_t name[ \ 68 ] __attribute__((aligned(ZRTOS_MALLOC__BYTE_ALIGNMENT))); \ 70 ZRTOS_ASSERT__STATIC(len >= sizeof(zrtos_malloc_internal_t)); \ 72 void *kmalloc(size_t length){ \ 73 return zrtos_malloc__malloc( \ 79 void kfree(void *ptr){ \ 80 zrtos_malloc__free(ptr); \ 83 #define ZRTOS_MALLOC__INIT(name,len) \ 84 zrtos_malloc_t name[ \ 86 ] __attribute__((aligned(ZRTOS_MALLOC__BYTE_ALIGNMENT))); \ 88 ZRTOS_ASSERT__STATIC(len >= sizeof(zrtos_malloc_internal_t)); \ 92 ,sizeof(name) / sizeof(name[0]) \ 103 zrtos_debug__memset(thiz_->
ptr,0xFF,length);
111 #ifndef ZRTOS_MALLOC__CFG_DISABLE_FREE 121 while(chunk != last){
122 if((chunk->
length & 1) == 0
123 && (chunk->
length >> 1) == length){
143 #ifndef ZRTOS_MALLOC__CFG_DISABLE_FREE 148 bool has_free_space = (thiz_->
length 156 if(aligned_length > (SIZE_MAX>>1)){
161 }
else if(has_free_space){
163 chunk->
length = (aligned_length << 1) | 1;
164 thiz_->
ptr += total_length;
173 zrtos_debug__memset(chunk,pattern++,aligned_length);
182 bool has_free_space = (thiz_->
length 192 thiz_->
ptr += aligned_length;
197 zrtos_debug__memset(ret,pattern++,aligned_length);
204 #ifndef ZRTOS_MALLOC__CFG_DISABLE_FREE 211 return (chunk->
length >> 1);
221 #ifndef ZRTOS_MALLOC__CFG_DISABLE_FREE 230 zrtos_debug__memset(ptr,0xFF,(chunk->
length >> 1));
#define ZRTOS_CAST(type, value)
#define ZRTOS_DEBUG__CODE(code)
bool zrtos_malloc__init(zrtos_malloc_t *thiz, size_t length)
#define ZRTOS_MALLOC__BYTE_ALIGNMENT
size_t zrtos_malloc__get_length(void *ptr)
void * zrtos_malloc__malloc(zrtos_malloc_t *thiz, size_t length)
void zrtos_malloc__free(void *ptr)
struct _zrtos_malloc_heap_chunk_t zrtos_malloc_heap_chunk_t
void * zrtos_types__ptr_add(void *ptr, size_t byte_len)
size_t zrtos_types__ptr_get_byte_distance(void *bigger, void *smaller)
static zrtos_malloc_heap_chunk_t * zrtos_malloc__get_free_chunk(zrtos_malloc_t *thiz, size_t length)
struct _zrtos_malloc_t zrtos_malloc_t
size_t zrtos_types__ceil_size_to_alignment(size_t len)
struct _zrtos_malloc_internal_t zrtos_malloc_internal_t