Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tlsf_malloc_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Alexandre Bustico, Gautier Hattenberger
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
28 #include <ch.h>
29 #include <tlsf.h>
31 
32 
34  tlsf_t tlsf;
35  mutex_t *mtx;
36 };
37 
38 
39 #ifdef HEAP_CCM
40 #define HEAP_CCM_BUFFER HEAP_CCM ## _buffer
41 #define HEAP_CCM_MTX HEAP_CCM ## _mtx
42 static uint8_t HEAP_CCM_BUFFER[HEAP_CCM_SIZE] __attribute__((section(HEAP_CCM_SECTION), aligned(8))) ;
43 static MUTEX_DECL(HEAP_CCM_MTX);
45 #endif
46 
47 #ifdef HEAP_SRAM
48 #define HEAP_SRAM_BUFFER HEAP_SRAM ## _buffer
49 #define HEAP_SRAM_MTX HEAP_SRAM ## _mtx
50 static uint8_t HEAP_SRAM_BUFFER[HEAP_SRAM_SIZE] __attribute__((section(HEAP_SRAM_SECTION), aligned(8))) ;
51 static MUTEX_DECL(HEAP_SRAM_MTX);
52 tlsf_memory_heap_t HEAP_SRAM;
53 #endif
54 
55 #ifdef HEAP_EXTERN
56 #define HEAP_EXTERN_BUFFER HEAP_EXTERN ## _buffer
57 #define HEAP_EXTERN_MTX HEAP_EXTERN ## _mtx
58 static uint8_t HEAP_EXTERN_BUFFER[HEAP_EXTERN_SIZE] __attribute__((section(HEAP_EXTERN_SECTION), aligned(8))) ;
59 static MUTEX_DECL(HEAP_EXTERN_MTX);
60 tlsf_memory_heap_t HEAP_EXTERN;
61 #endif
62 
63 static void stat_tlsf_walker(void *ptr, size_t size, int used, void *user);
64 
65 #if defined RTOS_DEBUG && RTOS_DEBUG == 1
66 static void error_cb(const char *msg)
67 {
68  chSysHalt(msg);
69 }
70 #else
71 #define error_cb NULL
72 #endif
73 
74 void tlsf_init_heaps(void)
75 {
76 #ifdef HEAP_CCM
77  HEAP_CCM.mtx = &HEAP_CCM_MTX;
78  HEAP_CCM.tlsf = tlsf_create_with_pool(HEAP_CCM_BUFFER, HEAP_CCM_SIZE, error_cb);
79 #endif
80 #ifdef HEAP_SRAM
81  HEAP_SRAM.mtx = &HEAP_SRAM_MTX;
82  HEAP_SRAM.tlsf = tlsf_create_with_pool(HEAP_SRAM_BUFFER, HEAP_SRAM_SIZE, error_cb);
83 #endif
84 #ifdef HEAP_EXTERN
85  HEAP_EXTERN.mtx = &HEAP_EXTERN_MTX;
86  HEAP_EXTERN.tlsf = tlsf_create_with_pool(HEAP_EXTERN_BUFFER, HEAP_EXTERN_SIZE, error_cb);
87 #endif
88 }
89 
91 {
92  return heap->tlsf;
93 }
94 
95 void *tlsf_malloc_r(tlsf_memory_heap_t *heap, size_t bytes)
96 {
97  chMtxLock(heap->mtx);
98  void *ret = tlsf_malloc(heap->tlsf, bytes);
99  chMtxUnlock(heap->mtx);
100  return ret;
101 }
102 
103 void *tlsf_memalign_r(tlsf_memory_heap_t *heap, size_t align, size_t bytes)
104 {
105  chMtxLock(heap->mtx);
106  void *ret = tlsf_memalign(heap->tlsf, align, bytes);
107  chMtxUnlock(heap->mtx);
108  return ret;
109 }
110 
111 void *tlsf_realloc_r(tlsf_memory_heap_t *heap, void *ptr, size_t bytes)
112 {
113  chMtxLock(heap->mtx);
114  void *ret = tlsf_realloc(heap->tlsf, ptr, bytes);
115  chMtxUnlock(heap->mtx);
116  return ret;
117 }
118 
119 void tlsf_free_r(tlsf_memory_heap_t *heap, void *ptr)
120 {
121  chMtxLock(heap->mtx);
122  tlsf_free(heap->tlsf, ptr);
123  chMtxUnlock(heap->mtx);
124 }
125 
126 
127 void tlsf_stat_r(tlsf_memory_heap_t *heap, struct tlsf_stat_t *stat)
128 {
129  stat->mused = stat->mfree = 0;
130  chMtxLock(heap->mtx);
131  tlsf_walk_pool(tlsf_get_pool(heap->tlsf), &stat_tlsf_walker, stat);
132  chMtxUnlock(heap->mtx);
133 }
134 
135 
136 /* Returns nonzero if any internal consistency check fails. */
138 {
139  int ret = 0;
140  chMtxLock(heap->mtx);
141  ret = tlsf_check(heap->tlsf);
142  if (ret == 0) {
143  ret = tlsf_check_pool(tlsf_get_pool(heap->tlsf));
144  }
145 
146  chMtxUnlock(heap->mtx);
147  return ret;
148 }
149 
150 
151 static void stat_tlsf_walker(void *ptr, size_t size, int used, void *user)
152 {
153  (void) ptr;
154  struct tlsf_stat_t *tstat = (struct tlsf_stat_t *) user;
155  if (used) {
156  tstat->mused += size;
157  } else {
158  tstat->mfree += size;
159  }
160 }
161 
size_t mfree
free memory available
Definition: tlsf_malloc.h:56
void * tlsf_memalign_r(tlsf_memory_heap_t *heap, size_t align, size_t bytes)
void * tlsf_get_heap_addr(const tlsf_memory_heap_t *heap)
#define error_cb
#define HEAP_CCM
void tlsf_free_r(tlsf_memory_heap_t *heap, void *ptr)
void tlsf_stat_r(tlsf_memory_heap_t *heap, struct tlsf_stat_t *stat)
#define HEAP_CCM_SIZE
size_t mused
used memory
Definition: tlsf_malloc.h:57
Dynamic memory allocation based on TLSF library.
void * tlsf_realloc_r(tlsf_memory_heap_t *heap, void *ptr, size_t bytes)
#define HEAP_CCM_SECTION
int tlsf_check_r(tlsf_memory_heap_t *heap)
unsigned char uint8_t
Definition: types.h:14
static void stat_tlsf_walker(void *ptr, size_t size, int used, void *user)
static MUTEX_DECL(sys_time_mtx)
uint8_t msg[10]
Buffer used for general comunication over SPI (out buffer)
void * tlsf_malloc_r(tlsf_memory_heap_t *heap, size_t bytes)
void tlsf_init_heaps(void)