Numworks Epsilon  1.4.1
Graphing Calculator Operating System
malloc.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <string.h>
3 #include <assert.h>
4 #include <private/memconfig.h>
5 
6 #if LIBA_LOG_DYNAMIC_MEMORY
7 #include <ion/log.h>
8 #endif
9 
10 extern char _heap_start;
11 extern char _heap_end;
12 
14  .nHeap = 0
15 };
16 
17 // Memsys headers cannot be included easily so we rewrite them here
18 int memsys5Init(void *NotUsed);
19 void memsys5FreeUnsafe(void *pOld);
20 void * memsys5MallocUnsafe(int nByte);
21 void * memsys5Realloc(void *pPrior, int nBytes);
22 int memsys5Roundup(int n);
23 
24 static void configure_heap() {
27  HeapConfig.mnReq = 1;
28  HeapConfig.bMemstat = 0;
29  HeapConfig.xLog = 0;
30  memsys5Init(0);
31 }
32 
33 void free(void *ptr) {
34 #if LIBA_LOG_DYNAMIC_MEMORY
35  ion_log_print_string("FREE-");
36  ion_log_print_integer((uint32_t)ptr);
37  ion_log_print_string("\n");
38 #endif
39  if (ptr != NULL) {
40  memsys5FreeUnsafe(ptr);
41  }
42 }
43 
44 void * malloc(size_t size) {
45  void * p = NULL;
46  if (HeapConfig.nHeap == 0) {
47  configure_heap();
48  }
49  if (size > 0) {
51  }
52 #if LIBA_LOG_DYNAMIC_MEMORY
53  ion_log_print_string("MALLOC-");
54  ion_log_print_integer((uint32_t)p);
55  ion_log_print_string("-");
56  ion_log_print_integer((uint32_t)size);
57  ion_log_print_string("\n");
58 #endif
59  /* If the process could not find enough space in the memory dedicated to
60  * dynamic allocation, p is NULL. The conventional malloc just return NULL
61  * without crashing. However, for debuging purposes, we abort the process
62  * in that case to easily spot memory leaking. */
63  assert(p != NULL);
64  return p;
65 }
66 
67 void * realloc(void *ptr, size_t size) {
68  return memsys5Realloc(ptr, memsys5Roundup(size));
69 }
#define assert(e)
Definition: assert.h:9
void * pHeap
Definition: memconfig.h:10
void * malloc(size_t size)
Definition: malloc.c:44
void free(void *ptr)
Definition: malloc.c:33
void * memsys5Realloc(void *pPrior, int nBytes)
void * memsys5MallocUnsafe(int nByte)
char _heap_start
Definition: boot.cpp:10
void memsys5FreeUnsafe(void *pOld)
unsigned int uint32_t
Definition: stdint.h:6
#define NULL
Definition: stddef.h:4
void(* xLog)(void *, int, const char *)
Definition: memconfig.h:13
void * realloc(void *ptr, size_t size)
Definition: malloc.c:67
int memsys5Init(void *NotUsed)
heap_config_t HeapConfig
Definition: malloc.c:13
char _heap_end
Definition: boot.cpp:11
int memsys5Roundup(int n)