FastLED 3.9.15
Loading...
Searching...
No Matches
memory_resource.cpp.hpp
Go to the documentation of this file.
1// IWYU pragma: private
2
4#include "fl/stl/allocator.h" // fl::Malloc, fl::Free, PSRamAllocate, PSRamDeallocate
5#include "fl/stl/malloc.h" // fl::realloc
6#include "fl/stl/cstring.h" // fl::memset
7#include "fl/stl/noexcept.h"
8
9namespace fl {
10
11// Default implementation: reallocate not supported.
12void* memory_resource::do_reallocate(void* p, fl::size old_bytes, fl::size new_bytes) {
14 FASTLED_UNUSED(old_bytes);
15 FASTLED_UNUSED(new_bytes);
16 return nullptr;
17}
18
19// ======= Default Memory Resource (Malloc/Free/realloc) =======
20
21namespace {
22
24 protected:
25 void* do_allocate(fl::size bytes) override {
26 void* p = fl::Malloc(bytes);
27 if (p) {
28 fl::memset(p, 0, bytes);
29 }
30 return p;
31 }
32
33 void do_deallocate(void* p, fl::size bytes) override {
34 FASTLED_UNUSED(bytes);
35 fl::Free(p);
36 }
37
38 void* do_reallocate(void* p, fl::size old_bytes, fl::size new_bytes) override {
39 void* result = fl::realloc(p, new_bytes);
40 if (result && new_bytes > old_bytes) {
41 // Zero-initialize newly allocated region
42 fl::memset(static_cast<char*>(result) + old_bytes, 0, new_bytes - old_bytes);
43 }
44 return result;
45 }
46
47 bool do_is_equal(const memory_resource& other) const FL_NOEXCEPT override {
48 return this == &other;
49 }
50};
51
53 protected:
54 void* do_allocate(fl::size bytes) override {
55 return fl::PSRamAllocate(bytes, true); // zero-initialized
56 }
57
58 void do_deallocate(void* p, fl::size bytes) override {
59 FASTLED_UNUSED(bytes);
61 }
62
63 // PSRAM does not support realloc — returns nullptr to trigger alloc-copy-free path
64 // do_reallocate uses the base default (returns nullptr)
65
66 bool do_is_equal(const memory_resource& other) const FL_NOEXCEPT override {
67 return this == &other;
68 }
69};
70
71} // anonymous namespace
72
74 static DefaultMemoryResource instance;
75 return &instance;
76}
77
79 static PSRamMemoryResource instance;
80 return &instance;
81}
82
83} // namespace fl
void * do_reallocate(void *p, fl::size old_bytes, fl::size new_bytes) override
Default: returns nullptr (not supported). Override for realloc-capable resources.
bool do_is_equal(const memory_resource &other) const FL_NOEXCEPT override
bool do_is_equal(const memory_resource &other) const FL_NOEXCEPT override
virtual void * do_reallocate(void *p, fl::size old_bytes, fl::size new_bytes) FL_NOEXCEPT
Default: returns nullptr (not supported). Override for realloc-capable resources.
Polymorphic memory resource base class (PMR-style).
PMR-style polymorphic memory resource for type-erased allocation.
void Free(void *ptr)
void * memset(void *s, int c, size_t n) FL_NOEXCEPT
memory_resource * default_memory_resource() FL_NOEXCEPT
Get the default memory resource (wraps fl::Malloc / fl::Free / fl::realloc).
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
void PSRamDeallocate(void *ptr)
void * Malloc(fl::size size)
void * realloc(void *ptr, size_t new_size)
memory_resource * psram_memory_resource()
Get the PSRAM memory resource (wraps PSRamAllocate / PSRamDeallocate).
void * PSRamAllocate(fl::size size, bool zero)
Base definition for an LED controller.
Definition crgb.hpp:179
#define FASTLED_UNUSED(x)
#define FL_NOEXCEPT