Use hasp_mem for possible PSram memory allocations

This commit is contained in:
fvanroie 2021-11-21 08:23:43 +01:00
parent 5366ed3dda
commit 182a98644b
6 changed files with 51 additions and 37 deletions

View File

@ -1,26 +1,32 @@
/* MIT License - Copyright (c) 2019-2021 Francis Van Roie /* MIT License - Copyright (c) 2019-2021 Francis Van Roie
For full license information read the LICENSE file in the project folder */ For full license information read the LICENSE file in the project folder */
#ifndef HASP_PNG_H #ifndef HASP_MEM_H
#define HASP_PNG_H #define HASP_MEM_H
#include <stdlib.h> #include <stdlib.h>
#ifdef LODEPNG_NO_COMPILE_ALLOCATORS
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifdef LODEPNG_NO_COMPILE_ALLOCATORS
void* lodepng_calloc(size_t num,size_t size); void* lodepng_calloc(size_t num,size_t size);
void* lodepng_malloc(size_t size); void* lodepng_malloc(size_t size);
void* lodepng_realloc(void* ptr, size_t new_size); void* lodepng_realloc(void* ptr, size_t new_size);
void lodepng_free(void* ptr); void lodepng_free(void* ptr);
#endif // LODEPNG_NO_COMPILE_ALLOCATORS
bool hasp_use_psram();
void* hasp_calloc(size_t num,size_t size);
void* hasp_malloc(size_t size);
void* hasp_realloc(void* ptr, size_t new_size);
void hasp_free(void* ptr);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif // LODEPNG_NO_COMPILE_ALLOCATORS
#endif // HASP_PNG_H #endif // HASP_MEM_H

@ -1 +1 @@
Subproject commit aa40dd17b51500448a5601478a4278d21ac74386 Subproject commit 626fd6572742635926a1af501fc27b35db6585f8

View File

@ -412,14 +412,7 @@ static int32_t load_glyph(lv_fs_file_t* fp, lv_font_fmt_txt_dsc_t* font_dsc, uin
} }
uint8_t* glyph_bmp; uint8_t* glyph_bmp;
#ifdef ESP32 glyph_bmp = (uint8_t*)hasp_malloc(sizeof(uint8_t) * cur_bmp_size);
if(psramFound())
glyph_bmp = (uint8_t*)ps_malloc(sizeof(uint8_t) * cur_bmp_size);
else
glyph_bmp = (uint8_t*)malloc(sizeof(uint8_t) * cur_bmp_size);
#else
glyph_bmp = (uint8_t*)malloc(sizeof(uint8_t) * cur_bmp_size);
#endif
font_dsc->glyph_bitmap = glyph_bmp; font_dsc->glyph_bitmap = glyph_bmp;

View File

@ -9,12 +9,6 @@
#include "hasp_attribute_helper.h" #include "hasp_attribute_helper.h"
/*** Image Improvement ***/ /*** Image Improvement ***/
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266HTTPClient.h>
#define lodepng_malloc malloc
#define lodepng_free free
#endif
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
#include <HTTPClient.h> #include <HTTPClient.h>
#endif #endif
@ -22,7 +16,6 @@
#if HASP_USE_PNGDECODE > 0 #if HASP_USE_PNGDECODE > 0
#include "lv_png.h" #include "lv_png.h"
#include "lodepng.h" #include "lodepng.h"
#include "hasp_png.h"
#endif #endif
/*** Image Improvement ***/ /*** Image Improvement ***/
@ -1071,7 +1064,7 @@ static hasp_attribute_type_t special_attribute_src(lv_obj_t* obj, const char* pa
int len = total; int len = total;
int read = 0; int read = 0;
lv_img_dsc_t* img_dsc = (lv_img_dsc_t*)lv_mem_alloc(sizeof(lv_img_dsc_t)); lv_img_dsc_t* img_dsc = (lv_img_dsc_t*)lv_mem_alloc(sizeof(lv_img_dsc_t));
uint8_t* img_buf = (uint8_t*)(len > 0 ? lodepng_malloc(len) : NULL); uint8_t* img_buf = (uint8_t*)(len > 0 ? hasp_malloc(len) : NULL);
LOG_VERBOSE(TAG_ATTR, "HTTP OK: buffer created of %d bytes", len); LOG_VERBOSE(TAG_ATTR, "HTTP OK: buffer created of %d bytes", len);

View File

@ -2,23 +2,49 @@
/* MIT License - Copyright (c) 2019-2021 Francis Van Roie /* MIT License - Copyright (c) 2019-2021 Francis Van Roie
For full license information read the LICENSE file in the project folder */ For full license information read the LICENSE file in the project folder */
#ifdef LODEPNG_NO_COMPILE_ALLOCATORS
#include <stdlib.h> #include <stdlib.h>
#include "hasplib.h" #include "hasplib.h"
#include "hasp_png.h" #include "hasp_mem.h"
#ifdef ESP32
bool hasp_use_psram()
{
return psramFound() && ESP.getPsramSize() > 0;
}
#endif
void* hasp_malloc(size_t size)
{
#ifdef ESP32
return hasp_use_psram() ? ps_malloc(size) : malloc(size);
#else
return malloc(size);
#endif
}
/* NOTE: when realloc returns NULL, it leaves the original memory untouched */
void* hasp_realloc(void* ptr, size_t new_size)
{
#ifdef ESP32
return hasp_use_psram() ? ps_realloc(ptr, new_size) : realloc(ptr, new_size);
#else
return realloc(ptr, new_size);
#endif
}
void hasp_free(void* ptr)
{
free(ptr);
}
#ifdef LODEPNG_NO_COMPILE_ALLOCATORS
void* lodepng_malloc(size_t size) void* lodepng_malloc(size_t size)
{ {
#ifdef LODEPNG_MAX_ALLOC #ifdef LODEPNG_MAX_ALLOC
if(size > LODEPNG_MAX_ALLOC) return 0; if(size > LODEPNG_MAX_ALLOC) return 0;
#endif #endif
#ifdef ESP32 return hasp_malloc(size);
return psramFound() ? ps_malloc(size) : malloc(size);
#else
return malloc(size);
#endif
} }
/* NOTE: when realloc returns NULL, it leaves the original memory untouched */ /* NOTE: when realloc returns NULL, it leaves the original memory untouched */
@ -28,16 +54,11 @@ void* lodepng_realloc(void* ptr, size_t new_size)
if(new_size > LODEPNG_MAX_ALLOC) return 0; if(new_size > LODEPNG_MAX_ALLOC) return 0;
#endif #endif
#ifdef ESP32 return hasp_realloc(ptr, new_size);
return psramFound() ? ps_realloc(ptr, new_size) : realloc(ptr, new_size);
#else
return realloc(ptr, new_size);
#endif
} }
void lodepng_free(void* ptr) void lodepng_free(void* ptr)
{ {
free(ptr); hasp_free(ptr);
} }
#endif // LODEPNG_NO_COMPILE_ALLOCATORS #endif // LODEPNG_NO_COMPILE_ALLOCATORS

View File

@ -11,6 +11,7 @@
#include <stdint.h> #include <stdint.h>
#include "hasp_conf.h" #include "hasp_conf.h"
#include "hasp_mem.h"
#include "lv_conf.h" #include "lv_conf.h"
#include "lvgl.h" #include "lvgl.h"