SingingCat 0
application
multiram.c
1#include "main-header.h"
2#include "umm_malloc.h"
3
18static struct mpm MULTI_RAM;
19extern char _estack; // from linker script
20extern char _Minimum_Stack_Size; // from linker script
21
22void sc_fancy_allocator_init();
27 memset(&MULTI_RAM, 0, sizeof(MULTI_RAM));
28 MULTI_RAM.max_commands = sizeof(MULTI_RAM.commem) / sizeof(MULTI_RAM.commem[0]);
29 sc_fancy_allocator_init();
30}
31struct mpm *get_mpm() {
32 return &MULTI_RAM;
33}
38void *mpm_reserve(int bytes) {
39 if ((MULTI_RAM.dalloc) || (sizeof(MULTI_RAM.dynbuf) <= bytes)) {
40 return NULL;
41 }
42 MULTI_RAM.dalloc = 1;
43 return MULTI_RAM.dynbuf;
44}
48void mpm_free(void *mem) {
49 if (mem != MULTI_RAM.dynbuf) {
50 printf("inv mem free (%p vs %p)\n", mem, MULTI_RAM.dynbuf);
51 return;
52 }
53 MULTI_RAM.dalloc = 0;
54}
55
56int is_stack(void *adr) {
57 void *es = &_estack;
58 void *mss = &_Minimum_Stack_Size;
59
60 if ((adr <= es) && (adr >= (void *)(es - mss))) {
61 return 1;
62 }
63 return 0;
64}
65
66/***********************************************************************************
67* alternative, more dynamic allocator
68***********************************************************************************/
69
70
71// in bytes
72#define MEMAREA_SIZE 4096
73static uint8_t MEMAREA[MEMAREA_SIZE];
74
75void *UMM_MALLOC_CFG_HEAP_ADDR;
76uint32_t UMM_MALLOC_CFG_HEAP_SIZE;
77
78void sc_fancy_allocator_init() {
79 umm_init_heap(&MEMAREA, MEMAREA_SIZE);
80}
81void *sc_realloc(void *ptr, size_t size) {
82 return umm_realloc(ptr, size);
83}
84
85void sc_free(void *ptr) {
86 umm_free(ptr);
87}
void * mpm_reserve(int bytes)
Definition: multiram.c:38
void mpm_free(void *mem)
Definition: multiram.c:48
void multi_mem_init()
called very early on, even before printf works
Definition: multiram.c:26
Definition: multiram.h:6