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 #ifdef LV_CONF_INCLUDE_SIMPLE
17 #include "lv_conf.h"
18 #else
19 #include "../../lv_conf.h"
20 #endif
21 
22 #include <stdint.h>
23 #include <stddef.h>
24 #include "lv_log.h"
25 
26 /*********************
27  *      DEFINES
28  *********************/
29 // Check windows
30 #ifdef __WIN64
31 #define LV_MEM_ENV64
32 #endif
33 
34 // Check GCC
35 #ifdef __GNUC__
36 #if defined(__x86_64__) || defined(__ppc64__)
37 #define LV_MEM_ENV64
38 #endif
39 #endif
40 
41 /**********************
42  *      TYPEDEFS
43  **********************/
44 
45 /**
46  * Heap information structure.
47  */
48 typedef struct
49 {
50     uint32_t total_size; /**< Total heap size */
51     uint32_t free_cnt;
52     uint32_t free_size; /**< Size of available memory */
53     uint32_t free_biggest_size;
54     uint32_t used_cnt;
55     uint8_t used_pct; /**< Percentage used */
56     uint8_t frag_pct; /**< Amount of fragmentation */
57 } lv_mem_monitor_t;
58 
59 /**********************
60  * GLOBAL PROTOTYPES
61  **********************/
62 
63 /**
64  * Initiaize the dyn_mem module (work memory and other variables)
65  */
66 void lv_mem_init(void);
67 
68 /**
69  * Allocate a memory dynamically
70  * @param size size of the memory to allocate in bytes
71  * @return pointer to the allocated memory
72  */
73 void * lv_mem_alloc(uint32_t size);
74 
75 /**
76  * Free an allocated data
77  * @param data pointer to an allocated memory
78  */
79 void lv_mem_free(const void * data);
80 
81 /**
82  * Reallocate a memory with a new size. The old content will be kept.
83  * @param data pointer to an allocated memory.
84  * Its content will be copied to the new memory block and freed
85  * @param new_size the desired new size in byte
86  * @return pointer to the new memory
87  */
88 void * lv_mem_realloc(void * data_p, uint32_t new_size);
89 
90 /**
91  * Join the adjacent free memory blocks
92  */
93 void lv_mem_defrag(void);
94 
95 /**
96  * Give information about the work memory of dynamic allocation
97  * @param mon_p pointer to a dm_mon_p variable,
98  *              the result of the analysis will be stored here
99  */
100 void lv_mem_monitor(lv_mem_monitor_t * mon_p);
101 
102 /**
103  * Give the size of an allocated memory
104  * @param data pointer to an allocated memory
105  * @return the size of data memory in bytes
106  */
107 uint32_t lv_mem_get_size(const void * data);
108 
109 /**********************
110  *      MACROS
111  **********************/
112 
113 /**
114  * Halt on NULL pointer
115  * p pointer to a memory
116  */
117 #if LV_USE_LOG == 0
118 #define lv_mem_assert(p)                                                                                               \
119     {                                                                                                                  \
120         if(p == NULL)                                                                                                  \
121             while(1)                                                                                                   \
122                 ;                                                                                                      \
123     }
124 #else
125 #define lv_mem_assert(p)                                                                                               \
126     {                                                                                                                  \
127         if(p == NULL) {                                                                                                \
128             LV_LOG_ERROR("Out of memory!");                                                                            \
129             while(1)                                                                                                   \
130                 ;                                                                                                      \
131         }                                                                                                              \
132     }
133 #endif
134 #ifdef __cplusplus
135 } /* extern "C" */
136 #endif
137 
138 #endif /*LV_MEM_H*/
139