APM:Libraries
malloc.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) Siddharth Bharat Purohit 2017
3  * This file is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the
5  * Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This file is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 /*
17  wrappers for allocation functions
18 
19  Relies on linker wrap options
20 
21  Note that not all functions that have been wrapped are implemented
22  here. The others are wrapped to ensure the function is not used
23  without an implementation. If we need them then we can implement as
24  needed.
25  */
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <hal.h>
30 #include <chheap.h>
31 #include <stdarg.h>
32 
33 #define MIN_ALIGNMENT 8
34 
35 #if defined(CCM_RAM_SIZE)
36 #ifndef CCM_BASE_ADDRESS
37 #define CCM_BASE_ADDRESS 0x10000000
38 #endif
39 static memory_heap_t ccm_heap;
40 static bool ccm_heap_initialised = false;
41 #endif
42 
43 void *malloc_ccm(size_t size)
44 {
45  void *p = NULL;
46 #if defined(CCM_RAM_SIZE)
47  if (!ccm_heap_initialised) {
48  ccm_heap_initialised = true;
49  chHeapObjectInit(&ccm_heap, (void *)CCM_BASE_ADDRESS, CCM_RAM_SIZE*1024);
50  }
51  p = chHeapAllocAligned(&ccm_heap, size, CH_HEAP_ALIGNMENT);
52  if (p != NULL) {
53  memset(p, 0, size);
54  }
55 #else
56  (void)size;
57 #endif
58  return p;
59 }
60 
61 void *malloc(size_t size)
62 {
63  if (size == 0) {
64  return NULL;
65  }
66  void *p = chHeapAllocAligned(NULL, size, MIN_ALIGNMENT);
67  if (p) {
68  memset(p, 0, size);
69  } else {
70  // fall back to CCM memory when main memory full
71  p = malloc_ccm(size);
72  }
73  return p;
74 }
75 
76 void *calloc(size_t nmemb, size_t size)
77 {
78  return malloc(nmemb * size);
79 }
80 
81 void free(void *ptr)
82 {
83  if(ptr != NULL) {
84  chHeapFree(ptr);
85  }
86 }
87 
88 /*
89  return total available memory in bytes
90  */
91 size_t mem_available(void)
92 {
93  size_t totalp = 0;
94  // get memory available on main heap
95  chHeapStatus(NULL, &totalp, NULL);
96 
97  // we also need to add in memory that is not yet allocated to the heap
98  totalp += chCoreGetStatusX();
99 
100 #if defined(CCM_RAM_SIZE)
101  size_t ccm_available = 0;
102  chHeapStatus(&ccm_heap, &ccm_available, NULL);
103  totalp += ccm_available;
104 #endif
105 
106  return totalp;
107 }
void * calloc(size_t nmemb, size_t size)
Definition: malloc.c:76
void * malloc_ccm(size_t size)
Definition: malloc.c:43
#define MIN_ALIGNMENT
Definition: malloc.c:33
size_t mem_available(void)
Definition: malloc.c:91
void * malloc(size_t size)
Definition: malloc.c:61
#define NULL
Definition: hal_types.h:59
void free(void *ptr)
Definition: malloc.c:81