FastLED 3.10.8
Loading...
Searching...
No Matches

◆ aligned_alloc_via_malloc()

void * fl::detail::aligned_alloc_via_malloc ( fl::size_t alignment,
fl::size_t size )

Portable over-allocate-and-align used by aligned_alloc on bare-metal targets; free only with aligned_free_via_malloc (#4817).

Definition at line 27 of file cstdlib.cpp.hpp.

27 {
28 if (alignment < sizeof(void *)) {
29 alignment = sizeof(void *);
30 }
31 if ((alignment & (alignment - 1)) != 0) {
32 return nullptr; // alignment must be a power of two
33 }
34 const fl::size_t overhead = alignment - 1 + sizeof(void *);
35 if (size > static_cast<fl::size_t>(-1) - overhead) {
36 return nullptr; // size + overhead would wrap
37 }
38 void *raw = ::malloc(size + overhead);
39 if (raw == nullptr) {
40 return nullptr;
41 }
42 const fl::uptr start = fl::reinterpret_cast_<fl::uptr>(raw) + sizeof(void *);
43 const fl::uptr aligned = (start + alignment - 1) & ~(fl::uptr(alignment) - 1);
44 void **slot = fl::reinterpret_cast_<void **>(aligned);
45 slot[-1] = raw;
46 return slot;
47}
FASTLED_FORCE_INLINE void * malloc(size_t size) FL_NO_EXCEPT
Definition malloc.h:21
To reinterpret_cast_(const From &from) FL_NO_EXCEPT
Definition bit_cast.h:88
fl::size_t size_t
Definition s16x16x4.h:62

References fl::FL_NO_EXCEPT, fl::malloc(), and fl::reinterpret_cast_().

Referenced by fl::libc_impl::aligned_alloc().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: