From 21c7347296b80291e0af5b7240a611abc6b5ff90 Mon Sep 17 00:00:00 2001 From: Ewoud Date: Tue, 16 May 2023 15:13:30 +0200 Subject: [PATCH] Minimal (de)allocs on customMappingTable (only grow, not shrink) --- wled00/FX.h | 2 ++ wled00/FX_2Dfcn.cpp | 15 ++++++----- wled00/FX_fcn.cpp | 65 +++++++++++++++++++++++---------------------- wled00/wled.h | 2 +- 4 files changed, 44 insertions(+), 40 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index ab4a94c4..f4fabdff 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -701,6 +701,7 @@ class WS2812FX { // 96 bytes _modeCount(MODE_COUNT), _callback(nullptr), customMappingTable(nullptr), + customMappingTableSize(0), //WLEDMM customMappingSize(0), _lastShow(0), _segment_index(0), @@ -932,6 +933,7 @@ class WS2812FX { // 96 bytes show_callback _callback; uint16_t* customMappingTable; + uint16_t customMappingTableSize; //WLEDMM uint16_t customMappingSize; /*uint32_t*/ unsigned long _lastShow; // WLEDMM avoid losing precision diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index df0bbf75..55c3ce38 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -36,11 +36,6 @@ // so matrix should disable regular ledmap processing void WS2812FX::setUpMatrix() { #ifndef WLED_DISABLE_2D - // erase old ledmap, just in case. - if (customMappingTable != nullptr) delete[] customMappingTable; - customMappingTable = nullptr; - customMappingSize = 0; - // isMatrix is set in cfg.cpp or set.cpp if (isMatrix) { // calculate width dynamically because it will have gaps @@ -68,13 +63,19 @@ void WS2812FX::setUpMatrix() { return; } - customMappingTable = new uint16_t[Segment::maxWidth * Segment::maxHeight]; + //WLEDMM recreate customMappingTable if more space needed + if (Segment::maxWidth * Segment::maxHeight > customMappingTableSize) { + USER_PRINTF("setupmatrix customMappingTable alloc %d from %d\n", Segment::maxWidth * Segment::maxHeight, customMappingTableSize); + if (customMappingTable != nullptr) delete[] customMappingTable; + customMappingTable = new uint16_t[Segment::maxWidth * Segment::maxHeight]; + if (customMappingTable != nullptr) customMappingTableSize = Segment::maxWidth * Segment::maxHeight; + } if (customMappingTable != nullptr) { customMappingSize = Segment::maxWidth * Segment::maxHeight; // fill with empty in case we don't fill the entire matrix - for (size_t i = 0; i< customMappingSize; i++) { + for (size_t i = 0; i< customMappingTableSize; i++) { //WLEDMM use customMappingTableSize customMappingTable[i] = (uint16_t)-1; } diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 4cfc5544..f242b40c 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -2102,11 +2102,9 @@ bool WS2812FX::deserializeMap(uint8_t n) { if (!isFile) { // erase custom mapping if selecting nonexistent ledmap.json (n==0) - //WLEDM: doubt this is necessary as return false causes setupMatrix to deal with this - if (!isMatrix && !n && customMappingTable != nullptr) { + //WLEDM: doubt this is necessary as return false causes setupMatrix to deal with this !!!! + if (!isMatrix && !n) { customMappingSize = 0; - delete[] customMappingTable; - customMappingTable = nullptr; loadedLedmap = 0; //WLEDMM } return false; @@ -2126,14 +2124,6 @@ bool WS2812FX::deserializeMap(uint8_t n) { USER_PRINT(F("Reading LED map from ")); //WLEDMM use USER_PRINT USER_PRINTLN(fileName); - // erase old custom ledmap - if (customMappingTable != nullptr) { - customMappingSize = 0; - delete[] customMappingTable; - customMappingTable = nullptr; - loadedLedmap = 0; - } - //WLEDMM: read width and height (mandatory in file!!) f.find("\"width\":"); uint16_t maxWidth = f.readStringUntil('\n').toInt(); @@ -2152,29 +2142,40 @@ bool WS2812FX::deserializeMap(uint8_t n) { Segment::maxHeight = maxHeight; resetSegments(true); //WLEDMM not makeAutoSegments() as we only want to change bounds - customMappingSize = maxWidth * maxHeight; - customMappingTable = new uint16_t[customMappingSize]; + //WLEDMM recreate customMappingTable if more space needed + if (Segment::maxWidth * Segment::maxHeight > customMappingTableSize) { + USER_PRINTF("deserializemap customMappingTable alloc %d from %d\n", Segment::maxWidth * Segment::maxHeight, customMappingTableSize); + if (customMappingTable != nullptr) delete[] customMappingTable; + customMappingTable = new uint16_t[Segment::maxWidth * Segment::maxHeight]; + if (customMappingTable != nullptr) customMappingTableSize = Segment::maxWidth * Segment::maxHeight; + } - //WLEDMM: find the map values - f.find("\"map\":["); - uint16_t i=0; - do { //for each element in the array - int mapi = f.readStringUntil(',').toInt(); - // USER_PRINTF(", %d", mapi); - customMappingTable[i++] = (uint16_t) (mapi<0 ? 0xFFFFU : mapi); - } while (f.available()); + if (customMappingTable != nullptr) { + customMappingSize = maxWidth * maxHeight; - loadedLedmap = n; - f.close(); + //WLEDMM: find the map values + f.find("\"map\":["); + uint16_t i=0; + do { //for each element in the array + int mapi = f.readStringUntil(',').toInt(); + // USER_PRINTF(", %d", mapi); + customMappingTable[i++] = (uint16_t) (mapi<0 ? 0xFFFFU : mapi); + } while (f.available()); - #ifdef WLED_DEBUG - DEBUG_PRINTF("Custom ledmap: %d\n", loadedLedmap); - for (uint16_t i=0; i