agileRTOS (zrtos)  Version 0.8.0 (ghostbuster)
malloc.h File Reference
#include <zrtos/assert.h>
#include <zrtos/types.h>
#include <zrtos/cast.h>
#include <zrtos/debug.h>
Include dependency graph for malloc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  _zrtos_malloc_t
 
struct  _zrtos_malloc_heap_chunk_t
 
struct  _zrtos_malloc_internal_t
 

Macros

#define ZRTOS_MALLOC__BYTE_ALIGNMENT   ZRTOS_TYPES__BYTE_ALIGNMENT
 
#define ZRTOS_MALLOC__GET_REQUIRED_SIZE(type, count)
 
#define ZRTOS_MALLOC__GLOBAL_HEAP_INIT(name)
 
#define ZRTOS_MALLOC__GLOBAL_HEAP(name, len)
 
#define ZRTOS_MALLOC__INIT(name, len)
 

Typedefs

typedef struct _zrtos_malloc_t zrtos_malloc_t
 
typedef struct _zrtos_malloc_heap_chunk_t zrtos_malloc_heap_chunk_t
 
typedef struct _zrtos_malloc_internal_t zrtos_malloc_internal_t
 

Functions

bool zrtos_malloc__init (zrtos_malloc_t *thiz, size_t length)
 
static zrtos_malloc_heap_chunk_tzrtos_malloc__get_free_chunk (zrtos_malloc_t *thiz, size_t length)
 
void * zrtos_malloc__malloc (zrtos_malloc_t *thiz, size_t length)
 
size_t zrtos_malloc__get_length (void *ptr)
 
void zrtos_malloc__free (void *ptr)
 

Macro Definition Documentation

◆ ZRTOS_MALLOC__BYTE_ALIGNMENT

#define ZRTOS_MALLOC__BYTE_ALIGNMENT   ZRTOS_TYPES__BYTE_ALIGNMENT

Definition at line 21 of file malloc.h.

◆ ZRTOS_MALLOC__GET_REQUIRED_SIZE

#define ZRTOS_MALLOC__GET_REQUIRED_SIZE (   type,
  count 
)
Value:
+ ( \
( \
) \
* (count) \
) \
)
size_t zrtos_types__ceil_size_to_alignment(size_t len)
Definition: types.h:59
struct _zrtos_malloc_internal_t zrtos_malloc_internal_t

Definition at line 39 of file malloc.h.

◆ ZRTOS_MALLOC__GLOBAL_HEAP

#define ZRTOS_MALLOC__GLOBAL_HEAP (   name,
  len 
)
Value:
(len) \
] __attribute__((aligned(ZRTOS_MALLOC__BYTE_ALIGNMENT))); \
\
void *kmalloc(size_t length){ \
name \
,length \
); \
} \
\
void kfree(void *ptr){ \
zrtos_malloc__free(ptr); \
}
#define ZRTOS_ASSERT__STATIC(cond)
Definition: assert.h:25
#define ZRTOS_MALLOC__BYTE_ALIGNMENT
Definition: malloc.h:21
void * zrtos_malloc__malloc(zrtos_malloc_t *thiz, size_t length)
Definition: malloc.h:142

Definition at line 65 of file malloc.h.

◆ ZRTOS_MALLOC__GLOBAL_HEAP_INIT

#define ZRTOS_MALLOC__GLOBAL_HEAP_INIT (   name)
Value:
name \
,sizeof(name) / sizeof(name[0]) \
); \
zrtos_debug__memset( \
,0xFF \
,(sizeof(name) / sizeof(name[0])) \
); \
});
#define ZRTOS_DEBUG__CODE(code)
Definition: debug.h:47
bool zrtos_malloc__init(zrtos_malloc_t *thiz, size_t length)
Definition: malloc.h:95
void * zrtos_types__ptr_add(void *ptr, size_t byte_len)
Definition: types.h:35

Definition at line 50 of file malloc.h.

◆ ZRTOS_MALLOC__INIT

#define ZRTOS_MALLOC__INIT (   name,
  len 
)
Value:
(len) \
] __attribute__((aligned(ZRTOS_MALLOC__BYTE_ALIGNMENT))); \
name \
,sizeof(name) / sizeof(name[0]) \
)
#define ZRTOS_ASSERT__STATIC(cond)
Definition: assert.h:25
bool zrtos_malloc__init(zrtos_malloc_t *thiz, size_t length)
Definition: malloc.h:95
#define ZRTOS_MALLOC__BYTE_ALIGNMENT
Definition: malloc.h:21

Definition at line 83 of file malloc.h.

Typedef Documentation

◆ zrtos_malloc_heap_chunk_t

◆ zrtos_malloc_internal_t

◆ zrtos_malloc_t

Function Documentation

◆ zrtos_malloc__free()

void zrtos_malloc__free ( void *  ptr)

Definition at line 220 of file malloc.h.

220  {
221 #ifndef ZRTOS_MALLOC__CFG_DISABLE_FREE
224  ,ptr
225  );
226  --chunk;
227  chunk->length &= ~((size_t)1);
228 
230  zrtos_debug__memset(ptr,0xFF,(chunk->length >> 1));
231  });
232 #endif
233 }
#define ZRTOS_CAST(type, value)
Definition: cast.h:18
#define ZRTOS_DEBUG__CODE(code)
Definition: debug.h:47
uint16_t size_t
Definition: types.h:22
Here is the caller graph for this function:

◆ zrtos_malloc__get_free_chunk()

static zrtos_malloc_heap_chunk_t* zrtos_malloc__get_free_chunk ( zrtos_malloc_t thiz,
size_t  length 
)
static

Definition at line 112 of file malloc.h.

115  {
119  length <<= 1;
120 
121  while(chunk != last){
122  if((chunk->length & 1) == 0
123  && (chunk->length >> 1) == length){
124  return chunk;
125  }
127  chunk
128  ,sizeof(zrtos_malloc_heap_chunk_t) + (chunk->length>>1)
129  );
130  }
131 
132  return 0;
133 }
void * zrtos_types__ptr_add(void *ptr, size_t byte_len)
Definition: types.h:35
Here is the call graph for this function:
Here is the caller graph for this function:

◆ zrtos_malloc__get_length()

size_t zrtos_malloc__get_length ( void *  ptr)

Definition at line 205 of file malloc.h.

205  {
208  ,ptr
209  );
210  --chunk;
211  return (chunk->length >> 1);
212 }
#define ZRTOS_CAST(type, value)
Definition: cast.h:18
Here is the caller graph for this function:

◆ zrtos_malloc__init()

bool zrtos_malloc__init ( zrtos_malloc_t thiz,
size_t  length 
)

Definition at line 95 of file malloc.h.

95  {
97  bool ret = false;
98  if(length >= sizeof(zrtos_malloc_internal_t)){
100  thiz_->length = length - sizeof(zrtos_malloc_internal_t);
101 
103  zrtos_debug__memset(thiz_->ptr,0xFF,length);
104  });
105 
106  ret = true;
107  }
108  return ret;
109 }
#define ZRTOS_DEBUG__CODE(code)
Definition: debug.h:47
void * zrtos_types__ptr_add(void *ptr, size_t byte_len)
Definition: types.h:35
struct _zrtos_malloc_internal_t zrtos_malloc_internal_t
static uint8_t
Definition: mcp2515.h:159
Here is the call graph for this function:

◆ zrtos_malloc__malloc()

void* zrtos_malloc__malloc ( zrtos_malloc_t thiz,
size_t  length 
)

Definition at line 142 of file malloc.h.

142  {
143 #ifndef ZRTOS_MALLOC__CFG_DISABLE_FREE
145  zrtos_malloc_heap_chunk_t *chunk = 0;
146  size_t aligned_length = zrtos_types__ceil_size_to_alignment(length);
147  size_t total_length = sizeof(zrtos_malloc_heap_chunk_t) + aligned_length;
148  bool has_free_space = (thiz_->length
150  thiz_->ptr
151  ,(thiz_ + 1)
152  ))
153  >= total_length
154  ;
155 
156  if(aligned_length > (SIZE_MAX>>1)){
157  //out of bounds
158  goto L_OUT;
159  }else if((chunk = zrtos_malloc__get_free_chunk(thiz,aligned_length))){
160  chunk->length |= 1;
161  }else if(has_free_space){
162  chunk = (zrtos_malloc_heap_chunk_t*)thiz_->ptr;
163  chunk->length = (aligned_length << 1) | 1;
164  thiz_->ptr += total_length;
165  }else{
166  goto L_OUT;
167  }
168 
169  chunk++;
170 
172  static uint8_t pattern = 0x20;
173  zrtos_debug__memset(chunk,pattern++,aligned_length);
174  });
175 
176 L_OUT:
177  return chunk;
178 #else
180  void *ret = 0;
181  size_t aligned_length = zrtos_types__ceil_size_to_alignment(length);
182  bool has_free_space = (thiz_->length
184  thiz_->ptr
185  ,thiz_
186  ))
187  >= aligned_length
188  ;
189 
190  if(has_free_space){
191  ret = thiz_->ptr;
192  thiz_->ptr += aligned_length;
193  }
194 
196  static uint8_t pattern = 0x20;
197  zrtos_debug__memset(ret,pattern++,aligned_length);
198  });
199 
200  return ret;
201 #endif
202 }
#define ZRTOS_DEBUG__CODE(code)
Definition: debug.h:47
struct _zrtos_malloc_heap_chunk_t zrtos_malloc_heap_chunk_t
size_t zrtos_types__ptr_get_byte_distance(void *bigger, void *smaller)
Definition: types.h:43
static zrtos_malloc_heap_chunk_t * zrtos_malloc__get_free_chunk(zrtos_malloc_t *thiz, size_t length)
Definition: malloc.h:112
size_t zrtos_types__ceil_size_to_alignment(size_t len)
Definition: types.h:59
static uint8_t
Definition: mcp2515.h:159
Here is the call graph for this function:
Here is the caller graph for this function: