Minimal (de)allocs on customMappingTable (only grow, not shrink)

This commit is contained in:
Ewoud
2023-05-16 15:13:30 +02:00
parent b51972fe41
commit 21c7347296
4 changed files with 44 additions and 40 deletions

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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<customMappingSize; i++) {
if (!(i%Segment::maxWidth)) DEBUG_PRINTLN();
DEBUG_PRINTF("%4d,", customMappingTable[i]);
}
DEBUG_PRINTLN();
#endif
loadedLedmap = n;
f.close();
#ifdef WLED_DEBUG
DEBUG_PRINTF("Custom ledmap: %d\n", loadedLedmap);
for (uint16_t i=0; i<customMappingSize; i++) {
if (!(i%Segment::maxWidth)) DEBUG_PRINTLN();
DEBUG_PRINTF("%4d,", customMappingTable[i]);
}
DEBUG_PRINTLN();
#endif
} else { // memory allocation error
DEBUG_PRINTLN(F("Deserializemap: Ledmap alloc error."));
}
releaseJSONBufferLock();
return true;

View File

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