(experimental) use malloc/realloc for customMappingTable

if this works better, we should cherry-pick it into mdev.
This commit is contained in:
Frank
2023-05-21 19:38:04 +02:00
parent 894fdce4b5
commit fc193408d8
3 changed files with 28 additions and 8 deletions

View File

@@ -66,8 +66,17 @@ void WS2812FX::setUpMatrix() {
//WLEDMM recreate customMappingTable if more space needed //WLEDMM recreate customMappingTable if more space needed
if (Segment::maxWidth * Segment::maxHeight > customMappingTableSize) { if (Segment::maxWidth * Segment::maxHeight > customMappingTableSize) {
USER_PRINTF("setupmatrix customMappingTable alloc %d from %d\n", Segment::maxWidth * Segment::maxHeight, customMappingTableSize); USER_PRINTF("setupmatrix customMappingTable alloc %d from %d\n", Segment::maxWidth * Segment::maxHeight, customMappingTableSize);
if (customMappingTable != nullptr) delete[] customMappingTable; //if (customMappingTable != nullptr) delete[] customMappingTable;
customMappingTable = new uint16_t[Segment::maxWidth * Segment::maxHeight]; //customMappingTable = new uint16_t[Segment::maxWidth * Segment::maxHeight];
// don't use new / delete
if (customMappingTable != nullptr) { // resize
customMappingTable = (uint16_t*) reallocf(customMappingTable, sizeof(uint16_t) * Segment::maxWidth * Segment::maxHeight); // reallocf will free memory if it cannot resize
}
if (customMappingTable == nullptr) { // second try
DEBUG_PRINTLN("setUpMatrix: trying to get fresh memory block.");
customMappingTable = (uint16_t*) calloc(Segment::maxWidth * Segment::maxHeight, sizeof(uint16_t));
if (customMappingTable == nullptr) DEBUG_PRINTLN("setUpMatrix: alloc failed");
}
if (customMappingTable != nullptr) customMappingTableSize = Segment::maxWidth * Segment::maxHeight; if (customMappingTable != nullptr) customMappingTableSize = Segment::maxWidth * Segment::maxHeight;
} }
@@ -142,6 +151,7 @@ void WS2812FX::setUpMatrix() {
DEBUG_PRINTLN(); DEBUG_PRINTLN();
#endif #endif
} else { // memory allocation error } else { // memory allocation error
customMappingTableSize = 0;
DEBUG_PRINTLN(F("Ledmap alloc error.")); DEBUG_PRINTLN(F("Ledmap alloc error."));
isMatrix = false; isMatrix = false;
panels = 0; panels = 0;

View File

@@ -1478,7 +1478,7 @@ void WS2812FX::finalizeInit(void)
free(Segment::_globalLeds); free(Segment::_globalLeds);
Segment::_globalLeds = nullptr; Segment::_globalLeds = nullptr;
} }
if (useLedsArray) { if (useLedsArray && getLengthTotal()>0) { // WLEDMM avoid malloc(0)
size_t arrSize = sizeof(CRGB) * getLengthTotal(); size_t arrSize = sizeof(CRGB) * getLengthTotal();
#if defined(ARDUINO_ARCH_ESP32) && defined(BOARD_HAS_PSRAM) && defined(WLED_USE_PSRAM) #if defined(ARDUINO_ARCH_ESP32) && defined(BOARD_HAS_PSRAM) && defined(WLED_USE_PSRAM)
if (psramFound()) if (psramFound())
@@ -1486,7 +1486,7 @@ void WS2812FX::finalizeInit(void)
else else
#endif #endif
Segment::_globalLeds = (CRGB*) malloc(arrSize); Segment::_globalLeds = (CRGB*) malloc(arrSize);
memset(Segment::_globalLeds, 0, arrSize); if (Segment::_globalLeds != nullptr) memset(Segment::_globalLeds, 0, arrSize); // WLEDMM avoid dereferencing null pointer
} }
//segments are created in makeAutoSegments(); //segments are created in makeAutoSegments();
@@ -2155,9 +2155,18 @@ bool WS2812FX::deserializeMap(uint8_t n) {
//WLEDMM recreate customMappingTable if more space needed //WLEDMM recreate customMappingTable if more space needed
if (Segment::maxWidth * Segment::maxHeight > customMappingTableSize) { if (Segment::maxWidth * Segment::maxHeight > customMappingTableSize) {
USER_PRINTF("deserializemap customMappingTable alloc %d from %d\n", Segment::maxWidth * Segment::maxHeight, customMappingTableSize); USER_PRINTF("deserializemap customMappingTable alloc %d from %d\n", Segment::maxWidth * Segment::maxHeight, customMappingTableSize);
if (customMappingTable != nullptr) delete[] customMappingTable; //if (customMappingTable != nullptr) delete[] customMappingTable;
customMappingTable = new uint16_t[Segment::maxWidth * Segment::maxHeight]; //customMappingTable = new uint16_t[Segment::maxWidth * Segment::maxHeight];
if (customMappingTable != nullptr) customMappingTableSize = Segment::maxWidth * Segment::maxHeight; // don't use new / delete
if (customMappingTable != nullptr) {
customMappingTable = (uint16_t*) reallocf(customMappingTable, sizeof(uint16_t) * Segment::maxWidth * Segment::maxHeight); // reallocf will free memory if it cannot resize
}
if (customMappingTable == nullptr) { // second try
DEBUG_PRINTLN("deserializeMap: trying to get fresh memory block.");
customMappingTable = (uint16_t*) calloc(Segment::maxWidth * Segment::maxHeight, sizeof(uint16_t));
if (customMappingTable == nullptr) DEBUG_PRINTLN("deserializeMap: alloc failed!");
}
if (customMappingTable != nullptr) customMappingTableSize = Segment::maxWidth * Segment::maxHeight;
} }
if (customMappingTable != nullptr) { if (customMappingTable != nullptr) {
@@ -2184,6 +2193,7 @@ bool WS2812FX::deserializeMap(uint8_t n) {
DEBUG_PRINTLN(); DEBUG_PRINTLN();
#endif #endif
} else { // memory allocation error } else { // memory allocation error
customMappingTableSize = 0;
DEBUG_PRINTLN(F("Deserializemap: Ledmap alloc error.")); DEBUG_PRINTLN(F("Deserializemap: Ledmap alloc error."));
} }

View File

@@ -8,7 +8,7 @@
*/ */
// version code in format yymmddb (b = daily build) // version code in format yymmddb (b = daily build)
#define VERSION 2305170 #define VERSION 2305210
//uncomment this if you have a "my_config.h" file you'd like to use //uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG //#define WLED_USE_MY_CONFIG