mirror of
https://github.com/lvgl/lvgl.git
synced 2026-05-21 05:51:45 +08:00
feat(stdlib): add and use lv_malloc_zeroed (#4728)
This commit is contained in:
@@ -186,7 +186,7 @@ void lv_free_core(void * p)
|
||||
void lv_mem_monitor_core(lv_mem_monitor_t * mon_p)
|
||||
{
|
||||
/*Init the data*/
|
||||
lv_memset(mon_p, 0, sizeof(lv_mem_monitor_t));
|
||||
lv_memzero(mon_p, sizeof(lv_mem_monitor_t));
|
||||
LV_TRACE_MEM("begin");
|
||||
|
||||
lv_pool_t * pool_p;
|
||||
|
||||
+28
-3
@@ -83,8 +83,8 @@ void * lv_malloc(size_t size)
|
||||
LV_LOG_INFO("used: %6d (%3d %%), frag: %3d %%, biggest free: %6d",
|
||||
(int)(mon.total_size - mon.free_size), mon.used_pct, mon.frag_pct,
|
||||
(int)mon.free_biggest_size);
|
||||
return NULL;
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if LV_MEM_ADD_JUNK
|
||||
@@ -92,7 +92,33 @@ void * lv_malloc(size_t size)
|
||||
#endif
|
||||
|
||||
LV_TRACE_MEM("allocated at %p", alloc);
|
||||
return alloc;
|
||||
}
|
||||
|
||||
void * lv_malloc_zeroed(size_t size)
|
||||
{
|
||||
LV_TRACE_MEM("allocating %lu bytes", (unsigned long)size);
|
||||
if(size == 0) {
|
||||
LV_TRACE_MEM("using zero_mem");
|
||||
return &zero_mem;
|
||||
}
|
||||
|
||||
void * alloc = lv_malloc_core(size);
|
||||
if(alloc == NULL) {
|
||||
LV_LOG_INFO("couldn't allocate memory (%lu bytes)", (unsigned long)size);
|
||||
#if LV_LOG_LEVEL <= LV_LOG_LEVEL_INFO
|
||||
lv_mem_monitor_t mon;
|
||||
lv_mem_monitor(&mon);
|
||||
LV_LOG_INFO("used: %6d (%3d %%), frag: %3d %%, biggest free: %6d",
|
||||
(int)(mon.total_size - mon.free_size), mon.used_pct, mon.frag_pct,
|
||||
(int)mon.free_biggest_size);
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lv_memzero(alloc, size);
|
||||
|
||||
LV_TRACE_MEM("allocated at %p", alloc);
|
||||
return alloc;
|
||||
}
|
||||
|
||||
@@ -107,7 +133,6 @@ void lv_free(void * data)
|
||||
if(data == NULL) return;
|
||||
|
||||
lv_free_core(data);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,7 +176,7 @@ lv_result_t lv_mem_test(void)
|
||||
|
||||
void lv_mem_monitor(lv_mem_monitor_t * mon_p)
|
||||
{
|
||||
lv_memset(mon_p, 0, sizeof(lv_mem_monitor_t));
|
||||
lv_memzero(mon_p, sizeof(lv_mem_monitor_t));
|
||||
lv_mem_monitor_core(mon_p);
|
||||
}
|
||||
|
||||
|
||||
+10
-5
@@ -64,12 +64,19 @@ lv_mem_pool_t lv_mem_add_pool(void * mem, size_t bytes);
|
||||
void lv_mem_remove_pool(lv_mem_pool_t pool);
|
||||
|
||||
/**
|
||||
* Allocate a memory dynamically
|
||||
* @param size size of the memory to allocate in bytes
|
||||
* @return pointer to the allocated memory
|
||||
* Allocate memory dynamically
|
||||
* @param size requested size in bytes
|
||||
* @return pointer to allocated uninitialized memory, or NULL on failure
|
||||
*/
|
||||
void * lv_malloc(size_t size);
|
||||
|
||||
/**
|
||||
* Allocate zeroed memory dynamically
|
||||
* @param size requested size in bytes
|
||||
* @return pointer to allocated zeroed memory, or NULL on failure
|
||||
*/
|
||||
void * lv_malloc_zeroed(size_t size);
|
||||
|
||||
/**
|
||||
* Free an allocated data
|
||||
* @param data pointer to an allocated memory
|
||||
@@ -92,8 +99,6 @@ void * lv_realloc(void * data_p, size_t new_size);
|
||||
*/
|
||||
void * lv_malloc_core(size_t size);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Used internally to execute a plain `free` operation
|
||||
* @param p memory address to free
|
||||
|
||||
Reference in New Issue
Block a user