Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
stdlib.h
Go to the documentation of this file.
1 #ifndef STDLIB_H_
2 #define STDLIB_H_
3 
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <wctype.h> //TODO: remove need for this? due to wchar_t
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif //__cplusplus
11 
12 #pragma mark - definitions & types -
13 
15 typedef struct
16 {
17  int quot;
18  int rem;
19 } div_t;
20 
22 typedef struct
23 {
24  long quot;
25  long rem;
26 } ldiv_t;
27 
29 typedef struct
30 {
31  long long quot;
32  long long rem;
33 } lldiv_t;
34 
36 typedef struct
37 {
38  intmax_t quot;
39  intmax_t rem;
40 } imaxdiv_t;
41 
43 #define EXIT_FAILURE 1
44 
46 #define EXIT_SUCCESS 0
47 
51 #define RAND_MAX (0x7fffffff)
52 
55 #define MB_CUR_MAX sizeof(wchar_t)
56 
57 #pragma mark - runtime -
58 
59 // TODO
60 
61 void abort(void) __attribute__((noreturn));
62 int atexit(void (*)(void));
63 void exit(int) __attribute__((noreturn));
64 
70 void _Exit(int) __attribute__((noreturn));
71 int at_quick_exit(void (*)(void));
72 void quick_exit(int) __attribute__((noreturn));
73 int cxa_atexit(void (*)(void*), void*, void*);
74 
75 #ifndef DISABLE_UNIMPLEMENTED_LIBC_APIS
76 // Unsupported in bare metal environments:
77 char* getenv(const char*);
78 #endif
79 
80 #ifndef DISABLE_UNIMPLEMENTED_LIBC_APIS
81 // Unsupported in bare metal environments:
82 int system(const char*);
83 #endif
84 
85 #pragma mark - Multibyte APIs -
86 
87 #ifndef DISABLE_UNIMPLEMENTED_LIBC_APIS
88 int mblen(const char*, size_t);
89 int mbtowc(wchar_t* __restrict, const char* __restrict, size_t);
90 int wctomb(char*, wchar_t);
91 size_t mbstowcs(wchar_t* __restrict, const char* __restrict, size_t);
92 size_t wcstombs(char* __restrict, const wchar_t* __restrict, size_t);
93 #endif
94 
95 #pragma mark - ascii-to-x -
96 
113 int atoi(const char* str);
114 
131 long atol(const char* str);
132 
149 long long atoll(const char* str);
150 
173 double atof(const char* str);
174 
175 #pragma mark - str-to-x -
176 
216 float strtof(const char* __restrict str, char** __restrict str_end);
217 
257 double strtod(const char* __restrict str, char** __restrict str_end);
258 
302 long strtol(const char* __restrict str, char** __restrict str_end, int base);
303 
345 unsigned long strtoul(const char* __restrict str, char** __restrict str_end, int base);
346 
390 long long strtoll(const char* __restrict str, char** __restrict str_end, int base);
391 
434 unsigned long long strtoull(const char* __restrict str, char** __restrict str_end, int base);
435 
436 // TODO:
437 long double strtold(const char* __restrict, char** __restrict);
438 
439 #pragma mark - math -
440 
450 int abs(int n);
451 
461 long labs(long n);
462 
472 long long llabs(long long n);
473 
483 intmax_t imaxabs(intmax_t j);
484 
502 div_t div(int x, int y);
503 
526 ldiv_t ldiv(long x, long y);
527 
550 lldiv_t lldiv(long long x, long long y);
551 
574 imaxdiv_t imaxdiv(intmax_t numer, intmax_t denom);
575 
576 #pragma mark - random number generation -
577 
578 int rand_r(unsigned int* ctx);
579 
595 int rand(void);
596 
608 void srand(unsigned seed);
609 
610 #pragma mark - sorting -
611 
636 int heapsort(void* vbase, size_t nmemb, size_t size, int (*compar)(const void*, const void*));
637 
669 int heapsort_r(void* vbase, size_t nmemb, size_t size, void* thunk,
670  int (*compar)(void*, const void*, const void*));
671 
693 void* bsearch(const void* key, const void* ptr, size_t count, size_t size,
694  int (*comp)(const void*, const void*));
695 
718 void qsort_r(void* a, size_t n, size_t es, void* thunk,
719  int (*cmp)(void*, const void*, const void*));
720 
739 void qsort(void* a, size_t n, size_t es, int (*compar)(const void*, const void*));
740 
741 #pragma mark - memory -
742 
756 void* malloc(size_t size);
757 
776 void free(void* ptr);
777 
789 void* calloc(size_t num, size_t size);
790 
814 void* realloc(void* ptr, size_t size);
815 
834 void* reallocf(void* ptr, size_t size);
835 
836 #ifdef _POSIX_MEMALIGN_VISIBLE
837 
852 int posix_memalign(void** memptr, size_t alignment, size_t size);
853 #endif
854 
855 #ifdef __cplusplus
856 }
857 #endif //__cplusplus
858 
859 #endif // STDLIB_H_
ldiv_t ldiv(long x, long y)
Computes both the quotient and the remainder of the division of the numerator x by the denominator y.
Definition: ldiv.c:35
int mblen(const char *, size_t)
long long strtoll(const char *__restrict str, char **__restrict str_end, int base)
Interprets an long long value in a byte string pointed to by str.
int abs(int n)
Computes the absolute value of an integer number.
Definition: abs.c:32
long long quot
Definition: stdlib.h:31
long atol(const char *str)
Interprets a Long value in a byte string pointed to by str.
Definition: atol.c:5
unsigned long long strtoull(const char *__restrict str, char **__restrict str_end, int base)
Interprets an unsigned long long value in a byte string pointed to by str.
Definition: strtoull.c:44
void free(void *ptr)
Deallocates allocated memory space.
__attribute__((noreturn, weak)) void __assert_fail(const char *expr
intmax_t imaxabs(intmax_t j)
Computes the absolute value of an intmax_t number.
Definition: imaxabs.c:29
char * getenv(const char *)
int cxa_atexit(void(*)(void *), void *, void *)
void _Exit(int) __attribute__((noreturn))
Division type for long integers.
Definition: stdlib.h:22
long rem
Definition: stdlib.h:25
void * calloc(size_t num, size_t size)
Allocates memory for an array of given number objects of size and initializes all bytes in the alloca...
Definition: calloc.c:11
long long atoll(const char *str)
Interprets a Long Long value in a byte string pointed to by str.
Definition: atoll.c:5
void abort(void) __attribute__((noreturn))
float strtof(const char *__restrict str, char **__restrict str_end)
Interprets a floating-point value in a byte string pointed to by str.
long labs(long n)
Computes the absolute value of an long number.
Definition: labs.c:32
unsigned long strtoul(const char *__restrict str, char **__restrict str_end, int base)
Interprets an unsigned long value in a byte string pointed to by str.
void qsort_r(void *a, size_t n, size_t es, void *thunk, int(*cmp)(void *, const void *, const void *))
Sorts the given array pointed to by ptr in ascending order.
void * bsearch(const void *key, const void *ptr, size_t count, size_t size, int(*comp)(const void *, const void *))
Finds an element equal to element pointed to by key in an array pointed to by ptr.
int system(const char *)
size_t wcstombs(char *__restrict, const wchar_t *__restrict, size_t)
lldiv_t lldiv(long long x, long long y)
Computes both the quotient and the remainder of the division of the numerator x by the denominator y.
Definition: lldiv.c:30
Division type for long long integers.
Definition: stdlib.h:29
int rand_r(unsigned int *ctx)
int wctomb(char *, wchar_t)
void * realloc(void *ptr, size_t size)
Reallocates the given area of memory.
Definition: realloc.c:4
void qsort(void *a, size_t n, size_t es, int(*compar)(const void *, const void *))
Sorts the given array pointed to by ptr in ascending order.
long strtol(const char *__restrict str, char **__restrict str_end, int base)
Interprets an long value in a byte string pointed to by str.
int quot
Definition: stdlib.h:17
double atof(const char *str)
Interprets a floating-point value in a byte string pointed to by str.
Definition: atof.c:57
int atexit(void(*)(void))
long double strtold(const char *__restrict, char **__restrict)
int mbtowc(wchar_t *__restrict, const char *__restrict, size_t)
size_t mbstowcs(wchar_t *__restrict, const char *__restrict, size_t)
int rand(void)
Returns a pseudo-random integer value between ​0​ and.
div_t div(int x, int y)
Computes both the quotient and the remainder of the division of the numerator x by the denominator y.
Definition: div.c:35
int rem
Definition: stdlib.h:18
void * malloc(size_t size)
Allocates size bytes of uninitialized storage.
void quick_exit(int) __attribute__((noreturn))
intmax_t rem
Definition: stdlib.h:39
void * reallocf(void *ptr, size_t size)
Reallocates the given area of memory.
Definition: realloc.c:26
void exit(int) __attribute__((noreturn))
long quot
Definition: stdlib.h:24
long long rem
Definition: stdlib.h:32
int cmp(Bigint *a, Bigint *b)
Definition: misc.c:570
int heapsort(void *vbase, size_t nmemb, size_t size, int(*compar)(const void *, const void *))
Sorts the given array pointed to by vbase in ascending order.
Division type for integers.
Definition: stdlib.h:15
int atoi(const char *str)
Interprets an integer value in a byte string pointed to by str.
Definition: atoi.c:5
intmax_t quot
Definition: stdlib.h:38
Division type for maximal integer storage.
Definition: stdlib.h:36
void srand(unsigned seed)
Seeds the pseudo-random number generator used by.
imaxdiv_t imaxdiv(intmax_t numer, intmax_t denom)
Computes both the quotient and the remainder of the division of the numerator x by the denominator y.
Definition: imaxdiv.c:30
long long llabs(long long n)
Computes the absolute value of an long long number.
Definition: llabs.c:29
int at_quick_exit(void(*)(void))
#define thunk
double strtod(const char *__restrict str, char **__restrict str_end)
Interprets a floating-point value in a byte string pointed to by str.
int heapsort_r(void *vbase, size_t nmemb, size_t size, void *thunk, int(*compar)(void *, const void *, const void *))
Sorts the given array pointed to by vbase in ascending order.