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 "lv_string.h"
19 
20 #include "../misc/lv_types.h"
21 
22 /*********************
23  *      DEFINES
24  *********************/
25 
26 /**********************
27  *      TYPEDEFS
28  **********************/
29 
30 typedef void * lv_mem_pool_t;
31 
32 /**
33  * Heap information structure.
34  */
35 typedef struct {
36     size_t total_size;  /**< Total heap size */
37     size_t free_cnt;
38     size_t free_size;   /**< Size of available memory */
39     size_t free_biggest_size;
40     size_t used_cnt;
41     size_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 /**********************
47  * GLOBAL PROTOTYPES
48  **********************/
49 
50 /**
51  * Initialize to use malloc/free/realloc etc
52  */
53 void lv_mem_init(void);
54 
55 /**
56  * Drop all dynamically allocated memory and reset the memory pools' state
57  */
58 void lv_mem_deinit(void);
59 
60 lv_mem_pool_t lv_mem_add_pool(void * mem, size_t bytes);
61 
62 void lv_mem_remove_pool(lv_mem_pool_t pool);
63 
64 /**
65  * Allocate memory dynamically
66  * @param size requested size in bytes
67  * @return pointer to allocated uninitialized memory, or NULL on failure
68  */
69 void * lv_malloc(size_t size);
70 
71 /**
72  * Allocate a block of zeroed memory dynamically
73  * @param num requested number of element to be allocated.
74  * @param size requested size of each element in bytes.
75  * @return pointer to allocated zeroed memory, or NULL on failure
76  */
77 void * lv_calloc(size_t num, size_t size);
78 
79 /**
80  * Allocate zeroed memory dynamically
81  * @param size requested size in bytes
82  * @return pointer to allocated zeroed memory, or NULL on failure
83  */
84 void * lv_zalloc(size_t size);
85 
86 /**
87  * Allocate zeroed memory dynamically
88  * @param size requested size in bytes
89  * @return pointer to allocated zeroed memory, or NULL on failure
90  */
91 void * lv_malloc_zeroed(size_t size);
92 
93 /**
94  * Free an allocated data
95  * @param data pointer to an allocated memory
96  */
97 void lv_free(void * data);
98 
99 /**
100  * Reallocate a memory with a new size. The old content will be kept.
101  * @param data_p pointer to an allocated memory.
102  *               Its content will be copied to the new memory block and freed
103  * @param new_size the desired new size in byte
104  * @return pointer to the new memory, NULL on failure
105  */
106 void * lv_realloc(void * data_p, size_t new_size);
107 
108 /**
109  * Used internally to execute a plain `malloc` operation
110  * @param size      size in bytes to `malloc`
111  */
112 void * lv_malloc_core(size_t size);
113 
114 /**
115  * Used internally to execute a plain `free` operation
116  * @param p      memory address to free
117  */
118 void lv_free_core(void * p);
119 
120 /**
121  * Used internally to execute a plain realloc operation
122  * @param p         memory address to realloc
123  * @param new_size  size in bytes to realloc
124  */
125 void * lv_realloc_core(void * p, size_t new_size);
126 
127 /**
128  * Used internally by lv_mem_monitor() to gather LVGL heap state information.
129  * @param mon_p      pointer to lv_mem_monitor_t object to be populated.
130  */
131 void lv_mem_monitor_core(lv_mem_monitor_t * mon_p);
132 
133 lv_result_t lv_mem_test_core(void);
134 
135 /**
136  * @brief Tests the memory allocation system by allocating and freeing a block of memory.
137  * @return LV_RESULT_OK if the memory allocation system is working properly, or LV_RESULT_INVALID if there is an error.
138  */
139 lv_result_t lv_mem_test(void);
140 
141 /**
142  * Give information about the work memory of dynamic allocation
143  * @param mon_p pointer to a lv_mem_monitor_t variable,
144  *              the result of the analysis will be stored here
145  */
146 void lv_mem_monitor(lv_mem_monitor_t * mon_p);
147 
148 /**********************
149  *      MACROS
150  **********************/
151 
152 #ifdef __cplusplus
153 } /*extern "C"*/
154 #endif
155 
156 #endif /*LV_MEM_H*/
157