mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	[core] add reallocation support to RAMAllocator (#8390)
This commit is contained in:
		| @@ -719,6 +719,25 @@ template<class T> class RAMAllocator { | |||||||
|     return ptr; |     return ptr; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   T *reallocate(T *p, size_t n) { return this->reallocate(p, n, sizeof(T)); } | ||||||
|  |  | ||||||
|  |   T *reallocate(T *p, size_t n, size_t manual_size) { | ||||||
|  |     size_t size = n * sizeof(T); | ||||||
|  |     T *ptr = nullptr; | ||||||
|  | #ifdef USE_ESP32 | ||||||
|  |     if (this->flags_ & Flags::ALLOC_EXTERNAL) { | ||||||
|  |       ptr = static_cast<T *>(heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)); | ||||||
|  |     } | ||||||
|  |     if (ptr == nullptr && this->flags_ & Flags::ALLOC_INTERNAL) { | ||||||
|  |       ptr = static_cast<T *>(heap_caps_realloc(p, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)); | ||||||
|  |     } | ||||||
|  | #else | ||||||
|  |     // Ignore ALLOC_EXTERNAL/ALLOC_INTERNAL flags if external allocation is not supported | ||||||
|  |     ptr = static_cast<T *>(realloc(p, size));  // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc) | ||||||
|  | #endif | ||||||
|  |     return ptr; | ||||||
|  |   } | ||||||
|  |  | ||||||
|   void deallocate(T *p, size_t n) { |   void deallocate(T *p, size_t n) { | ||||||
|     free(p);  // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc) |     free(p);  // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc) | ||||||
|   } |   } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user