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  * Get a temporal buffer with the given size.
106  * @param size the required size
107  */
108 void * lv_mem_buf_get(uint32_t size);
109 
110 /**
111  * Release a memory buffer
112  * @param p buffer to release
113  */
114 void lv_mem_buf_release(void * p);
115 
116 /**
117  * Free all memory buffers
118  */
119 void lv_mem_buf_free_all(void);
120 
121 //! @cond Doxygen_Suppress
122 
123 #if LV_MEMCPY_MEMSET_STD
124 
125 /**
126  * Wrapper for the standard memcpy
127  * @param dst pointer to the destination buffer
128  * @param src pointer to the source buffer
129  * @param len number of byte to copy
130  */
lv_memcpy(void * dst,const void * src,size_t len)131 static inline void * lv_memcpy(void * dst, const void * src, size_t len)
132 {
133     return memcpy(dst, src, len);
134 }
135 
136 /**
137  * Wrapper for the standard memcpy
138  * @param dst pointer to the destination buffer
139  * @param src pointer to the source buffer
140  * @param len number of byte to copy
141  */
lv_memcpy_small(void * dst,const void * src,size_t len)142 static inline void * lv_memcpy_small(void * dst, const void * src, size_t len)
143 {
144     return memcpy(dst, src, len);
145 }
146 
147 /**
148  * Wrapper for the standard memset
149  * @param dst pointer to the destination buffer
150  * @param v value to set [0..255]
151  * @param len number of byte to set
152  */
lv_memset(void * dst,uint8_t v,size_t len)153 static inline void lv_memset(void * dst, uint8_t v, size_t len)
154 {
155     memset(dst, v, len);
156 }
157 
158 /**
159  * Wrapper for the standard memset with fixed 0x00 value
160  * @param dst pointer to the destination buffer
161  * @param len number of byte to set
162  */
lv_memset_00(void * dst,size_t len)163 static inline void lv_memset_00(void * dst, size_t len)
164 {
165     memset(dst, 0x00, len);
166 }
167 
168 /**
169  * Wrapper for the standard memset with fixed 0xFF value
170  * @param dst pointer to the destination buffer
171  * @param len number of byte to set
172  */
lv_memset_ff(void * dst,size_t len)173 static inline void lv_memset_ff(void * dst, size_t len)
174 {
175     memset(dst, 0xFF, len);
176 }
177 
178 #else
179 /**
180  * Same as `memcpy` but optimized for 4 byte operation.
181  * @param dst pointer to the destination buffer
182  * @param src pointer to the source buffer
183  * @param len number of byte to copy
184  */
185 void * /* LV_ATTRIBUTE_FAST_MEM */ lv_memcpy(void * dst, const void * src, size_t len);
186 
187 /**
188  * Same as `memcpy` but optimized to copy only a few bytes.
189  * @param dst pointer to the destination buffer
190  * @param src pointer to the source buffer
191  * @param len number of byte to copy
192  */
lv_memcpy_small(void * dst,const void * src,size_t len)193 static inline void * LV_ATTRIBUTE_FAST_MEM lv_memcpy_small(void * dst, const void * src, size_t len)
194 {
195     uint8_t * d8 = (uint8_t *)dst;
196     const uint8_t * s8 = (const uint8_t *)src;
197 
198     while(len) {
199         *d8 = *s8;
200         d8++;
201         s8++;
202         len--;
203     }
204 
205     return dst;
206 }
207 
208 /**
209  * Same as `memset` but optimized for 4 byte operation.
210  * @param dst pointer to the destination buffer
211  * @param v value to set [0..255]
212  * @param len number of byte to set
213  */
214 void /* LV_ATTRIBUTE_FAST_MEM */ lv_memset(void * dst, uint8_t v, size_t len);
215 
216 /**
217  * Same as `memset(dst, 0x00, len)` but optimized for 4 byte operation.
218  * @param dst pointer to the destination buffer
219  * @param len number of byte to set
220  */
221 void /* LV_ATTRIBUTE_FAST_MEM */ lv_memset_00(void * dst, size_t len);
222 
223 /**
224  * Same as `memset(dst, 0xFF, len)` but optimized for 4 byte operation.
225  * @param dst pointer to the destination buffer
226  * @param len number of byte to set
227  */
228 void /* LV_ATTRIBUTE_FAST_MEM */ lv_memset_ff(void * dst, size_t len);
229 
230 //! @endcond
231 
232 #endif
233 
234 /**********************
235  *      MACROS
236  **********************/
237 
238 #ifdef __cplusplus
239 } /*extern "C"*/
240 #endif
241 
242 #endif /*LV_MEM_H*/
243