FastLED 3.10.8
Loading...
Searching...
No Matches
cstdlib.h
Go to the documentation of this file.
1#pragma once
2
4// FastLED C Standard Library Compatibility Header
5//
6// This header provides a compatibility layer for third-party code that
7// expects standard C library functions like malloc, free, exit, etc.
9
10#include "fl/stl/stdint.h"
11#include "fl/stl/cstddef.h"
13#include "fl/stl/noexcept.h"
14
15namespace fl {
16// C-style comparison function type for qsort
17typedef int (*qsort_compare_fn)(const void*, const void*);
18
19// Implementations. The public fl::name functions below are templates with an
20// unused defaulted parameter, so wherever the C library version is also
21// visible (ADL on an fl type, or a using-directive for fl) the non-template
22// ::name wins the tie instead of the call being ambiguous (FastLED#4827).
23namespace libc_impl {
24void *aligned_alloc(fl::size_t alignment, fl::size_t size) FL_NO_EXCEPT;
25long strtol(const char* str, char** endptr, int base) FL_NO_EXCEPT;
26unsigned long strtoul(const char* str, char** endptr, int base) FL_NO_EXCEPT;
27int atoi(const char* str) FL_NO_EXCEPT;
28long atol(const char* str) FL_NO_EXCEPT;
29double strtod(const char* str, char** endptr) FL_NO_EXCEPT;
30void qsort(void* base, size_t nmemb, size_t size, qsort_compare_fn compar) FL_NO_EXCEPT;
31u32 rand() FL_NO_EXCEPT;
32const char* getenv(const char* name) FL_NO_EXCEPT;
33} // namespace libc_impl
34
35
36// ============================================================================
37// fl::aligned_alloc / fl::aligned_free — Portable over-aligned allocation
38// ============================================================================
39// Mirrors std::aligned_alloc from <cstdlib> (C++17 / C11).
40// Falls back to plain malloc on platforms where over-alignment is
41// unsupported or unnecessary (AVR, ESP8266).
42// Defined in cstdlib.cpp.hpp.
43
44template <typename FlLibcTag = void>
46
47namespace detail {
52} // namespace detail
53
54// Portable free for memory obtained from fl::aligned_alloc.
55// On POSIX, std::free suffices; on Windows, _aligned_free is required.
56void aligned_free(void *ptr) FL_NO_EXCEPT;
57
58// Convert string to long integer
59// Similar to standard strtol but without locale support
60template <typename FlLibcTag = void>
61FASTLED_FORCE_INLINE long strtol(const char* str, char** endptr, int base) FL_NO_EXCEPT { return libc_impl::strtol(str, endptr, base); }
62
63// Convert string to unsigned long integer
64template <typename FlLibcTag = void>
65FASTLED_FORCE_INLINE unsigned long strtoul(const char* str, char** endptr, int base) FL_NO_EXCEPT { return libc_impl::strtoul(str, endptr, base); }
66
67// Convert string to integer
68template <typename FlLibcTag = void>
69FASTLED_FORCE_INLINE int atoi(const char* str) FL_NO_EXCEPT { return libc_impl::atoi(str); }
70
71// Convert string to long
72template <typename FlLibcTag = void>
73FASTLED_FORCE_INLINE long atol(const char* str) FL_NO_EXCEPT { return libc_impl::atol(str); }
74
75// Convert string to double
76template <typename FlLibcTag = void>
77FASTLED_FORCE_INLINE double strtod(const char* str, char** endptr) FL_NO_EXCEPT { return libc_impl::strtod(str, endptr); }
78
79
80// qsort - Quick sort function compatible with C stdlib qsort
81// Sorts an array of elements using the provided comparison function
82template <typename FlLibcTag = void>
83FASTLED_FORCE_INLINE void qsort(void* base, size_t nmemb, size_t size, qsort_compare_fn compar) FL_NO_EXCEPT { return libc_impl::qsort(base, nmemb, size, compar); }
84
85// Pseudo-random number generator (mirrors ::rand()).
86// Returns u32 — values are always non-negative [0, RAND_MAX].
87// Fixed-width avoids AVR's 16-bit int truncation.
88template <typename FlLibcTag = void>
90
91// Get the value of an environment variable
92// Only functional on FASTLED_TESTING (stub platform), returns nullptr otherwise
93// This avoids std:: namespace dependencies on embedded platforms
94template <typename FlLibcTag = void>
95FASTLED_FORCE_INLINE const char* getenv(const char* name) FL_NO_EXCEPT { return libc_impl::getenv(name); }
96
97} // namespace fl
#define FASTLED_FORCE_INLINE
void aligned_free_via_malloc(void *ptr) FL_NO_EXCEPT
void * aligned_alloc_via_malloc(fl::size_t alignment, fl::size_t size) FL_NO_EXCEPT
Portable over-allocate-and-align used by aligned_alloc on bare-metal targets; free only with aligned_...
__SIZE_TYPE__ size_t
Definition s16x16x4.h:16
unsigned long strtoul(const char *str, char **endptr, int base) FL_NO_EXCEPT
long strtol(const char *str, char **endptr, int base) FL_NO_EXCEPT
const char * getenv(const char *name) FL_NO_EXCEPT
void qsort(void *base, size_t nmemb, size_t size, qsort_compare_fn compar) FL_NO_EXCEPT
int atoi(const char *str) FL_NO_EXCEPT
void * aligned_alloc(fl::size_t alignment, fl::size_t size) FL_NO_EXCEPT
double strtod(const char *str, char **endptr) FL_NO_EXCEPT
u32 rand() FL_NO_EXCEPT
long atol(const char *str) FL_NO_EXCEPT
FASTLED_FORCE_INLINE long atol(const char *str) FL_NO_EXCEPT
Definition cstdlib.h:73
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
FASTLED_FORCE_INLINE u32 rand() FL_NO_EXCEPT
Definition cstdlib.h:89
FASTLED_FORCE_INLINE int atoi(const char *str) FL_NO_EXCEPT
Definition cstdlib.h:69
int(* qsort_compare_fn)(const void *, const void *)
Definition cstdlib.h:17
fl::size_t size_t
Definition s16x16x4.h:62
FASTLED_FORCE_INLINE unsigned long strtoul(const char *str, char **endptr, int base) FL_NO_EXCEPT
Definition cstdlib.h:65
FASTLED_FORCE_INLINE void qsort(void *base, size_t nmemb, size_t size, qsort_compare_fn compar) FL_NO_EXCEPT
Definition cstdlib.h:83
void aligned_free(void *ptr) FL_NO_EXCEPT
FASTLED_FORCE_INLINE const char * getenv(const char *name) FL_NO_EXCEPT
Definition cstdlib.h:95
FASTLED_FORCE_INLINE double strtod(const char *str, char **endptr) FL_NO_EXCEPT
Definition cstdlib.h:77
FASTLED_FORCE_INLINE void * aligned_alloc(fl::size_t alignment, fl::size_t size) FL_NO_EXCEPT
Definition cstdlib.h:45
FASTLED_FORCE_INLINE long strtol(const char *str, char **endptr, int base) FL_NO_EXCEPT
Definition cstdlib.h:61
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NO_EXCEPT