@@ -128,10 +128,13 @@ void Segment::allocLeds() {
128128 if (ledsrgb) free (ledsrgb); // we need a bigger buffer, so free the old one first
129129 ledsrgb = (CRGB*)calloc (size, 1 );
130130 ledsrgbSize = ledsrgb?size:0 ;
131- if (ledsrgb == nullptr ) USER_PRINTLN (" allocLeds failed!!" );
131+ if (ledsrgb == nullptr ) {
132+ USER_PRINTLN (" allocLeds failed!!" );
133+ errorFlag = ERR_LOW_MEM; // WLEDMM raise errorflag
134+ }
132135 }
133136 else {
134- USER_PRINTF (" reuse Leds %u from %u\n " , size, ledsrgb?ledsrgbSize:0 );
137+ // USER_PRINTF("reuse Leds %u from %u\n", size, ledsrgb?ledsrgbSize:0);
135138 }
136139}
137140
@@ -212,26 +215,37 @@ bool Segment::allocateData(size_t len) {
212215 // DEBUG_PRINTF("allocateData(%u) start %d, stop %d, vlen %d\n", len, start, stop, virtualLength());
213216 deallocateData ();
214217 if (len == 0 ) return false ; // nothing to do
215- if (Segment::getUsedSegmentData () + len > MAX_SEGMENT_DATA) return false ; // not enough memory
218+ if (Segment::getUsedSegmentData () + len > MAX_SEGMENT_DATA) {
219+ // USER_PRINTF("Segment::allocateData: Segment data quota exceeded! used:%u request:%u max:%d\n", Segment::getUsedSegmentData(), len, MAX_SEGMENT_DATA);
220+ if (len > 0 ) errorFlag = ERR_LOW_SEG_MEM; // WLEDMM raise errorflag
221+ return false ; // not enough memory
222+ }
216223 // do not use SPI RAM on ESP32 since it is slow
217224 // #if defined(ARDUINO_ARCH_ESP32) && defined(BOARD_HAS_PSRAM) && defined(WLED_USE_PSRAM)
218225 // if (psramFound())
219226 // data = (byte*) ps_malloc(len);
220227 // else
221228 // #endif
222229 data = (byte*) malloc (len);
223- if (!data) { _dataLen = 0 ; return false ;} // allocation failed // WLEDMM reset dataLen
230+ if (!data) {
231+ _dataLen = 0 ; // WLEDMM reset dataLen
232+ errorFlag = ERR_LOW_MEM; // WLEDMM raise errorflag
233+ USER_PRINT (F (" Segment::allocateData: FAILED to allocate " ));
234+ USER_PRINT (len); USER_PRINTLN (F (" bytes." ));
235+ return false ;
236+ } // allocation failed
224237 Segment::addUsedSegmentData (len);
225238 _dataLen = len;
226239 memset (data, 0 , len);
240+ if (errorFlag == ERR_LOW_SEG_MEM) errorFlag = ERR_NONE; // WLEDMM reset errorflag on success
227241 return true ;
228242}
229243
230244void Segment::deallocateData () {
231245 if (!data) {_dataLen = 0 ; return ;} // WLEDMM reset dataLen
232246 free (data);
233247 data = nullptr ;
234- // DEBUG_PRINTLN(" deallocateData() called free()." );
248+ // USER_PRINTF("Segment:: deallocateData: free'd %d bytes.\n", _dataLen );
235249 Segment::addUsedSegmentData (-_dataLen);
236250 _dataLen = 0 ;
237251}
@@ -1656,6 +1670,7 @@ void WS2812FX::finalizeInit(void)
16561670 // #endif
16571671 if (arrSize > 0 ) Segment::_globalLeds = (CRGB*) malloc (arrSize); // WLEDMM avoid malloc(0)
16581672 if ((Segment::_globalLeds != nullptr ) && (arrSize > 0 )) memset (Segment::_globalLeds, 0 , arrSize); // WLEDMM avoid dereferencing nullptr
1673+ if ((Segment::_globalLeds == nullptr ) && (arrSize > 0 )) errorFlag = ERR_LOW_MEM; // WLEDMM raise errorflag
16591674 }
16601675
16611676 // segments are created in makeAutoSegments();
@@ -2403,7 +2418,10 @@ bool WS2812FX::deserializeMap(uint8_t n) {
24032418 if ((size > 0 ) && (customMappingTable == nullptr )) { // second try
24042419 DEBUG_PRINTLN (" deserializeMap: trying to get fresh memory block." );
24052420 customMappingTable = (uint16_t *) calloc (size, sizeof (uint16_t ));
2406- if (customMappingTable == nullptr ) DEBUG_PRINTLN (" deserializeMap: alloc failed!" );
2421+ if (customMappingTable == nullptr ) {
2422+ DEBUG_PRINTLN (" deserializeMap: alloc failed!" );
2423+ errorFlag = ERR_LOW_MEM; // WLEDMM raise errorflag
2424+ }
24072425 }
24082426 if (customMappingTable != nullptr ) customMappingTableSize = size;
24092427 }
0 commit comments