1 /**
2 ****************************************************************************************
3 *
4 * @file ke_mem.h
5 *
6 * @brief API for the heap management module.
7 *
8 * Copyright (C) RivieraWaves 2009-2015
9 *
10 *
11 ****************************************************************************************
12 */
13
14 #ifndef _KE_MEM_H_
15 #define _KE_MEM_H_
16
17 #include "rwip_config.h" // IP configuration
18 #include <stdint.h> // standard integer
19 #include <stdbool.h> // standard includes
20 #include "ke_config.h" // kernel configuration
21
22 /**
23 ****************************************************************************************
24 * @defgroup MEM Memory
25 * @ingroup KERNEL
26 * @brief Heap management module.
27 *
28 * This module implements heap management functions that allow initializing heap,
29 * allocating and freeing memory.
30 *
31 * @{
32 ****************************************************************************************
33 */
34
35 #if (KE_MEM_RW)
36
37
38 // forward declarations
39 struct mblock_free;
40
41 /**
42 ****************************************************************************************
43 * @brief Heap initialization.
44 *
45 * This function performs the following operations:
46 * - sanity checks
47 * - check memory allocated is at least large enough to hold two block descriptors to hold
48 * start and end
49 * - initialize the first and last descriptors
50 * - save heap into kernel environment variable.
51 *
52 * @param[in] type Memory type.
53 * @param[in|out] heap Heap pointer
54 * @param[in] heap_size Size of the heap
55 *
56 *
57 ****************************************************************************************
58 */
59 void ke_mem_init(uint8_t type, uint8_t* heap, uint16_t heap_size);
60
61 /**
62 ****************************************************************************************
63 * @brief Allocation of a block of memory.
64 *
65 * Allocates a memory block whose size is size; if no memory is available return NULL
66 *
67 * @param[in] size Size of the memory area that need to be allocated.
68 * @param[in] type Type of memory block
69 *
70 * @return A pointer to the allocated memory area.
71 *
72 ****************************************************************************************
73 */
74 void *ke_malloc(uint32_t size, uint8_t type);
75
76
77 /**
78 ****************************************************************************************
79 * @brief Check if it's possible to allocate a block of memory with a specific size.
80 *
81 * @param[in] size Size of the memory area that need to be allocated.
82 * @param[in] type Type of memory block
83 *
84 * @return True if memory block can be allocated, False else.
85 *
86 ****************************************************************************************
87 */
88 bool ke_check_malloc(uint32_t size, uint8_t type);
89
90 /**
91 ****************************************************************************************
92 * @brief Freeing of a block of memory.
93 *
94 * Free the memory area pointed by mem_ptr : mark the block as free and insert it in
95 * the pool of free block.
96 *
97 * @param[in] mem_ptr Pointer to the memory area that need to be freed.
98 *
99 ****************************************************************************************
100 */
101 void ke_free(void *mem_ptr);
102
103
104 /**
105 ****************************************************************************************
106 * @brief Check if current heap is empty or not (not used)
107 *
108 * @param[in] type Type of memory heap block
109 *
110 * @return true if heap not used, false else.
111 ****************************************************************************************
112 */
113 bool ke_mem_is_empty(uint8_t type);
114
115
116
117 /**
118 ****************************************************************************************
119 * @brief Check if current pointer is free or not
120 *
121 * @param[in] mem_ptr pointer to a memory block
122 *
123 * @return true if already free, false else.
124 ****************************************************************************************
125 */
126 bool ke_is_free(void* mem_ptr);
127
128 #if (KE_PROFILING)
129
130 /**
131 ****************************************************************************************
132 * @brief Retrieve memory usage of selected heap.
133 *
134 * @param[in] type Type of memory heap block
135 *
136 * @return current memory usage of current heap.
137 ****************************************************************************************
138 */
139 uint16_t ke_get_mem_usage(uint8_t type);
140
141
142 /**
143 ****************************************************************************************
144 * @brief Retrieve max memory usage of all heap.
145 * This command also resets max measured value.
146 *
147 * @return max memory usage of all heap.
148 ****************************************************************************************
149 */
150 uint32_t ke_get_max_mem_usage(void);
151
152 #endif // (KE_PROFILING)
153
154
155
156
157 #elif (KE_MEM_LINUX)
158 // Wrappers to Linux mem functions here
159
160 #include <linux/slab.h>
161
ke_malloc(uint32_t size)162 __INLINE void *ke_malloc(uint32_t size)
163 {
164 return kmalloc(size, GFP_KERNEL);
165 }
166
ke_free(void * mem_ptr)167 __INLINE void ke_free (void * mem_ptr)
168 {
169 kfree(mem_ptr);
170 }
171
172 #elif (KE_MEM_LIBC)
173 // Wrapper to lib C mem functions here
174 #include <stdlib.h>
175
ke_malloc(uint32_t size)176 __INLINE void *ke_malloc(uint32_t size) { return malloc(size); }
177
ke_free(void * mem_ptr)178 __INLINE void ke_free(void * mem_ptr) { free(mem_ptr); }
179
180 #endif // KE_MEM_RW
181
182 ///@} MEM
183
184 #endif // _KE_MEM_H_
185
186