1 /**
2  * @file lv_mem.h
3  *
4  */
5 
6 #ifndef LV_MEM_H
7 #define LV_MEM_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "../lv_conf_internal.h"
17 
18 #include <stdint.h>
19 #include <stddef.h>
20 #include <string.h>
21 
22 #include "lv_types.h"
23 
24 /*********************
25  *      DEFINES
26  *********************/
27 
28 /**********************
29  *      TYPEDEFS
30  **********************/
31 
32 /**
33  * Heap information structure.
34  */
35 typedef struct {
36     uint32_t total_size; /**< Total heap size*/
37     uint32_t free_cnt;
38     uint32_t free_size; /**< Size of available memory*/
39     uint32_t free_biggest_size;
40     uint32_t used_cnt;
41     uint32_t max_used; /**< Max size of Heap memory used*/
42     uint8_t used_pct; /**< Percentage used*/
43     uint8_t frag_pct; /**< Amount of fragmentation*/
44 } lv_mem_monitor_t;
45 
46 typedef struct {
47     void * p;
48     uint16_t size;
49     uint8_t used : 1;
50 } lv_mem_buf_t;
51 
52 typedef lv_mem_buf_t lv_mem_buf_arr_t[LV_MEM_BUF_MAX_NUM];
53 
54 /**********************
55  * GLOBAL PROTOTYPES
56  **********************/
57 
58 /**
59  * Initialize the dyn_mem module (work memory and other variables)
60  */
61 void lv_mem_init(void);
62 
63 /**
64  * Clean up the memory buffer which frees all the allocated memories.
65  * @note It work only if `LV_MEM_CUSTOM == 0`
66  */
67 void lv_mem_deinit(void);
68 
69 /**
70  * Allocate a memory dynamically
71  * @param size size of the memory to allocate in bytes
72  * @return pointer to the allocated memory
73  */
74 void * lv_mem_alloc(size_t size);
75 
76 /**
77  * Free an allocated data
78  * @param data pointer to an allocated memory
79  */
80 void lv_mem_free(void * data);
81 
82 /**
83  * Reallocate a memory with a new size. The old content will be kept.
84  * @param data pointer to an allocated memory.
85  * Its content will be copied to the new memory block and freed
86  * @param new_size the desired new size in byte
87  * @return pointer to the new memory, NULL on failure
88  */
89 void * lv_mem_realloc(void * data_p, size_t new_size);
90 
91 /**
92  *
93  * @return
94  */
95 lv_res_t lv_mem_test(void);
96 
97 /**
98  * Give information about the work memory of dynamic allocation
99  * @param mon_p pointer to a lv_mem_monitor_t variable,
100  *              the result of the analysis will be stored here
101  */
102 void lv_mem_monitor(lv_mem_monitor_t * mon_p);
103 
104 
105 /**
106  * Get a temporal buffer with the given size.
107  * @param size the required size
108  */
109 void * lv_mem_buf_get(uint32_t size);
110 
111 /**
112  * Release a memory buffer
113  * @param p buffer to release
114  */
115 void lv_mem_buf_release(void * p);
116 
117 /**
118  * Free all memory buffers
119  */
120 void lv_mem_buf_free_all(void);
121 
122 //! @cond Doxygen_Suppress
123 
124 #if LV_MEMCPY_MEMSET_STD
125 
126 /**
127  * Wrapper for the standard memcpy
128  * @param dst pointer to the destination buffer
129  * @param src pointer to the source buffer
130  * @param len number of byte to copy
131  */
lv_memcpy(void * dst,const void * src,size_t len)132 static inline void * lv_memcpy(void * dst, const void * src, size_t len)
133 {
134     return memcpy(dst, src, len);
135 }
136 
137 /**
138  * Wrapper for the standard memcpy
139  * @param dst pointer to the destination buffer
140  * @param src pointer to the source buffer
141  * @param len number of byte to copy
142  */
lv_memcpy_small(void * dst,const void * src,size_t len)143 static inline void * lv_memcpy_small(void * dst, const void * src, size_t len)
144 {
145     return memcpy(dst, src, len);
146 }
147 
148 /**
149  * Wrapper for the standard memset
150  * @param dst pointer to the destination buffer
151  * @param v value to set [0..255]
152  * @param len number of byte to set
153  */
lv_memset(void * dst,uint8_t v,size_t len)154 static inline void lv_memset(void * dst, uint8_t v, size_t len)
155 {
156     memset(dst, v, len);
157 }
158 
159 /**
160  * Wrapper for the standard memset with fixed 0x00 value
161  * @param dst pointer to the destination buffer
162  * @param len number of byte to set
163  */
lv_memset_00(void * dst,size_t len)164 static inline void lv_memset_00(void * dst, size_t len)
165 {
166     memset(dst, 0x00, len);
167 }
168 
169 /**
170  * Wrapper for the standard memset with fixed 0xFF value
171  * @param dst pointer to the destination buffer
172  * @param len number of byte to set
173  */
lv_memset_ff(void * dst,size_t len)174 static inline void lv_memset_ff(void * dst, size_t len)
175 {
176     memset(dst, 0xFF, len);
177 }
178 
179 #else
180 /**
181  * Same as `memcpy` but optimized for 4 byte operation.
182  * @param dst pointer to the destination buffer
183  * @param src pointer to the source buffer
184  * @param len number of byte to copy
185  */
186 void * /* LV_ATTRIBUTE_FAST_MEM */ lv_memcpy(void * dst, const void * src, size_t len);
187 
188 /**
189  * Same as `memcpy` but optimized to copy only a few bytes.
190  * @param dst pointer to the destination buffer
191  * @param src pointer to the source buffer
192  * @param len number of byte to copy
193  */
lv_memcpy_small(void * dst,const void * src,size_t len)194 static inline void * LV_ATTRIBUTE_FAST_MEM lv_memcpy_small(void * dst, const void * src, size_t len)
195 {
196     uint8_t * d8 = (uint8_t *)dst;
197     const uint8_t * s8 = (const uint8_t *)src;
198 
199     while(len) {
200         *d8 = *s8;
201         d8++;
202         s8++;
203         len--;
204     }
205 
206     return dst;
207 }
208 
209 /**
210  * Same as `memset` but optimized for 4 byte operation.
211  * @param dst pointer to the destination buffer
212  * @param v value to set [0..255]
213  * @param len number of byte to set
214  */
215 void /* LV_ATTRIBUTE_FAST_MEM */ lv_memset(void * dst, uint8_t v, size_t len);
216 
217 /**
218  * Same as `memset(dst, 0x00, len)` but optimized for 4 byte operation.
219  * @param dst pointer to the destination buffer
220  * @param len number of byte to set
221  */
222 void /* LV_ATTRIBUTE_FAST_MEM */ lv_memset_00(void * dst, size_t len);
223 
224 /**
225  * Same as `memset(dst, 0xFF, len)` but optimized for 4 byte operation.
226  * @param dst pointer to the destination buffer
227  * @param len number of byte to set
228  */
229 void /* LV_ATTRIBUTE_FAST_MEM */ lv_memset_ff(void * dst, size_t len);
230 
231 //! @endcond
232 
233 #endif
234 
235 /**********************
236  *      MACROS
237  **********************/
238 
239 #ifdef __cplusplus
240 } /*extern "C"*/
241 #endif
242 
243 #endif /*LV_MEM_H*/
244