You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if (pixels) d_free(pixels); //pixels = static_cast<uint32_t*>(d_realloc(pixels, sizeof(uint32_t) * length())); // using realloc can cause additional fragmentation instead of reducing it
459
+
if (pixels) d_free(pixels); // using realloc on large buffers can cause additional fragmentation instead of reducing it
// realloc with malloc fallback, original buffer is freed if realloc fails but not copied!
645
+
void *p_realloc_malloc(void *ptr, size_t size) {
646
+
void *newbuf = p_realloc(ptr, size); // try realloc first
647
+
if (newbuf) return newbuf; // realloc successful
648
+
p_free(ptr); // free old buffer if realloc failed
649
+
returnp_malloc(size); // fallback to malloc
650
+
}
651
+
643
652
void *p_calloc(size_t count, size_t size) {
644
653
int caps1 = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT;
645
654
int caps2 = MALLOC_CAP_DEFAULT | MALLOC_CAP_8BIT;
@@ -654,7 +663,7 @@ void *d_malloc(size_t size) {
654
663
int caps1 = MALLOC_CAP_DEFAULT | MALLOC_CAP_8BIT;
655
664
int caps2 = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT;
656
665
if (psramSafe) {
657
-
if (size > MIN_HEAP_SIZE) std::swap(caps1, caps2); // prefer PSRAM for large alloactions
666
+
if (heap_caps_get_largest_free_block(caps1) < 3*MIN_HEAP_SIZE && size > MIN_HEAP_SIZE) std::swap(caps1, caps2); // prefer PSRAM for large alloactions & when DRAM is low
658
667
returnheap_caps_malloc_prefer(size, 2, caps1, caps2); // otherwise prefer DRAM
if (size > MIN_HEAP_SIZE) std::swap(caps1, caps2); // prefer PSRAM for large alloactions
676
+
if (heap_caps_get_largest_free_block(caps1) < 3*MIN_HEAP_SIZE && size > MIN_HEAP_SIZE) std::swap(caps1, caps2); // prefer PSRAM for large alloactions & when DRAM is low
668
677
returnheap_caps_realloc_prefer(ptr, size, 2, caps1, caps2); // otherwise prefer DRAM
669
678
}
670
679
returnheap_caps_realloc(ptr, size, caps1);
671
680
}
672
681
682
+
// realloc with malloc fallback, original buffer is freed if realloc fails but not copied!
683
+
void *d_realloc_malloc(void *ptr, size_t size) {
684
+
void *newbuf = d_realloc(ptr, size); // try realloc first
0 commit comments