MicrOS
stdlib.h
Go to the documentation of this file.
1 #ifndef STDLIB_H
2 #define STDLIB_H
3 
4 #include <stdint.h>
5 #include "string.h"
6 #include "time.h"
7 #include "micros.h"
8 
9 extern unsigned int seed;
10 
12 #define RAND_MAX INT32_MAX
13 
15 #define EXIT_SUCCESS 0
16 
18 #define EXIT_FAILURE -1
19 
21 
22 typedef struct div_t
23 {
25  int quot;
27  int rem;
28 } div_t;
29 
31 
32 typedef struct ldiv_t
33 {
35  long int quot;
37  long int rem;
38 } ldiv_t;
39 
41 
42 typedef struct lldiv_t
43 {
45  long long int quot;
47  long long int rem;
48 } lldiv_t;
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 /*
55  Returns the absolute value of parameter \p n.
56  \param n Integral value.
57  \return The absolute value of n.
58 */
59 int abs(int n);
60 
62 /*
63  Returns the absolute value of parameter \p n.
64  \param n Integral value.
65  \return The absolute value of n.
66 */
67 long int labs(long int n);
68 
70 /*
71  Returns the absolute value of parameter \p n.
72  \param n Integral value.
73  \return The absolute value of n.
74 */
75 long long int llabs(long long int n);
76 
78 /*
79  Returns the integral quotient and remainder of the division of \p numer by \p denom.
80  \param numer Number to divide.
81  \param denom Divider.
82  \return Struct with quotient and remainder.
83 */
84 div_t div(int numer, int denom);
85 
87 /*
88  Returns the integral quotient and remainder of the division of \p numer by \p denom.
89  \param numer Number to divide.
90  \param denom Divider.
91  \return Struct with quotient and remainder.
92 */
93 ldiv_t ldiv(long int numer, long int denom);
94 
96 /*
97  Returns the integral quotient and remainder of the division of \p numer by \p denom.
98  \param numer Number to divide.
99  \param denom Divider.
100  \return Struct with quotient and remainder.
101 */
102 lldiv_t lldiv(long long int numer, long long int denom);
103 
105 /*
106  Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int.
107  \param string C-string beginning with the representation of an integral number.
108  \return On success, the function returns the converted integral number as an int value.
109  */
110 int atoi(const char *string);
111 
113 /*
114  Parses the C-string str interpreting its content as an integral number, which is returned as a value of type long int.
115  \param string C-string beginning with the representation of an integral number.
116  \return On success, the function returns the converted integral number as an int value.
117  */
118 long int atol(const char *string);
119 
121 /*
122  Parses the C-string str interpreting its content as an integral number, which is returned as a value of type long long int.
123  \param string C-string beginning with the representation of an integral number.
124  \return On success, the function returns the converted integral number as an int value.
125  */
126 long long int atoll(const char *string);
127 
129 /*
130  Converts an integer value to a null-terminated string using the specified base and stores the result in the array given by str parameter.
131  \param input Value to be converted to a string.
132  \param buffer Array in memory where to store the resulting null-terminated string.
133  \param base Numerical base used to represent the value as a string, between 2 and 36, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary.
134  \return A pointer to the resulting null-terminated string, same as parameter str.
135  */
136 char *itoa(int input, char *buffer, int base);
137 
139 /*
140  Parses the C-string str interpreting its content as a floating point number (according to the current locale) and returns its value as a double.
141  If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number
142  \param str C-string beginning with the representation of a floating-point number.
143  \param endptr Reference to an already allocated object of type char*, whose value is set by the function to the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used.
144  \return On success, the function returns the converted floating point number as a value of type double. If no valid conversion could be performed, the function returns zero (0.0).
145  */
146 double strtod(const char* str, char** endptr);
147 
149 /*
150  Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.
151  \param size Size of the memory block, in bytes. size_t is an unsigned integral type.
152  \return On success, a pointer to the memory block allocated by the function. If the function failed to allocate the requested block of memory, a null pointer is returned.
153  */
154 void *malloc(size_t size);
155 
157 /*
158  Allocates a block of size bytes of memory with the specified align, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.
159  \param size Size of the memory block, in bytes. size_t is an unsigned integral type.
160  \param align Alignment of the block (eg. if align is set to 0x1000, then block will be allocated on addresses like 0x01110000, 0x01111000, 0x01112000,...).
161  \return On success, a pointer to the memory block allocated by the function. If the function failed to allocate the requested block of memory, a null pointer is returned.
162  */
163 void *malloc_align(size_t size, uint32_t align);
164 
166 /*
167  Allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits to zero.
168  \param num Number of elements to allocate.
169  \param size Size of each element.
170  \return On success, a pointer to the memory block allocated by the function. If the function failed to allocate the requested block of memory, a null pointer is returned.
171  */
172 void *calloc(size_t num, size_t size);
173 
175 /*
176  Changes the size of the memory block pointed to by ptr. The function may move the memory block to a new location (whose address is returned by the function).
177  \param ptr Pointer to a memory block previously allocated with malloc, calloc or realloc.
178  \param size New size for the memory block, in bytes. size_t is an unsigned integral type.
179  \return A pointer to the reallocated memory block, which may be either the same as ptr or a new location.
180  */
181 void *realloc(void *ptr, size_t size);
182 
184 /*
185  A block of memory previously allocated by a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.
186  \param ptr Pointer to a memory block previously allocated with malloc, calloc or realloc.
187  */
188 void free(void *ptr);
189 
191 /*
192  Returns a pseudo-random integral number in the range between 0 and RAND_MAX.
193  \return An integer value between 0 and RAND_MAX.
194  */
195 int rand();
196 
198 /*
199  The pseudo-random number generator is initialized using the argument passed as seed.
200  \param new_seed An integer value to be used as seed by the pseudo-random number generator algorithm.
201  */
202 void srand(unsigned int new_seed);
203 
205 /*
206  Sorts the \p num elements of the array pointed to by \p base, each element \p size bytes long, using the \p compar function to determine the order.
207  \param base Pointer to the first object of the array to be sorted.
208  \param num Number of elements in the array pointed to by \p base.
209  \param size Size in bytes of each element in the array.
210  \param compar Pointer to a function that compares two elements.
211 */
212 void qsort(void *base, size_t num, size_t size, int (*compar)(const void *, const void*));
213 
215 /*
216  Searches the given \p key in the array pointed to by \p base (which is formed by num elements, each of size bytes),
217  and returns a void* pointer to a matching element, if found.
218  \param key Pointer to the object that serves as key for the search.
219  \param base Pointer to the first object of the array where the search is performed.
220  \param num Number of elements in the array pointed to by \p base.
221  \param size Size in bytes of each element in the array.
222  \param compar Pointer to a function that compares two elements.
223  \return A pointer to an entry in the array that matches the search \p key. If there are more than one matching elements,
224  this may point to any of them. If key is not found, a null pointer is returned.
225 */
226 void* bsearch(const void* key, const void* base, size_t num, size_t size, int (*compar)(const void*,const void*));
227 
229 /*
230  Aborts the current process, producing an abnormal program termination.
231  */
232 void abort();
233 
235 /*
236  Terminates the process normally, performing the regular cleanup for terminating programs.
237  \param status Status code.
238  */
239 void exit(int status);
240 
242 /*
243  etrieves a C-string containing the value of the environment variable whose \p name is specified as argument.
244  \param name Name of the requested variable.
245  \return A C-string with the value of the requested environment variable, or a null pointer if such environment variable does not exist.
246  */
247 char* getenv(const char* name);
248 
249 // Helpers
250 
252 /*
253  \param first Pointer to the first object to be swapped.
254  \param second Pointer to the first object to be swapped.
255  \param size Size of element in bytes.
256 */
257 void __stdlib_swap(void *first, void *second, size_t size);
258 
260 /*
261  \param base Pointer to the first object of the array to be sorted.
262  \param num Number of elements in the array pointed to by \p base.
263  \param size Size in bytes of each element in the array.
264  \param compar Pointer to a function that compares two elements.
265  \return Index of key
266 */
267 int __stdlib_partition(void* base, size_t num, size_t size, int (*compar)(const void *, const void*));
268 
269 #ifdef __cplusplus
270 }
271 #endif
272 
273 #endif
char * itoa(int input, char *buffer, int base)
Convert integer to string (non-standard function)
Definition: itoa.c:3
void __stdlib_swap(void *first, void *second, size_t size)
Swap two elements. Used by qsort().
Definition: qsort.c:3
long int rem
Represents the remainder.
Definition: stdlib.h:37
int abs(int n)
Absolute value.
Definition: abs.c:4
void free(void *ptr)
Deallocate memory block.
Definition: free.c:3
ldiv_t ldiv(long int numer, long int denom)
Integral division.
Definition: ldiv.c:4
void * calloc(size_t num, size_t size)
Allocate and zero-initialize array.
Definition: calloc.c:3
long int quot
Represents the quotient.
Definition: stdlib.h:35
long int labs(long int n)
Absolute value.
Definition: labs.c:4
void exit(int status)
Terminate calling process.
Definition: exit.c:3
int __stdlib_partition(void *base, size_t num, size_t size, int(*compar)(const void *, const void *))
Sort and return position of key. Used by qsort().
Definition: qsort.c:18
div_t div(int numer, int denom)
Integral division.
Definition: div.c:4
void qsort(void *base, size_t num, size_t size, int(*compar)(const void *, const void *))
Sorts an array.
Definition: qsort.c:63
void * realloc(void *ptr, size_t size)
Reallocate memory block.
Definition: realloc.c:3
unsigned int seed
Definition: stdlib.c:3
Structure to represent the result value of an integral division.
Definition: stdlib.h:42
double strtod(const char *str, char **endptr)
Convert string to double.
Definition: strtod.c:44
void * malloc_align(size_t size, uint32_t align)
Allocate memory block with the specified align.
Definition: malloc_align.c:3
char * getenv(const char *name)
Get environment string.
Definition: getenv.c:3
int rand()
Generate random number.
Definition: rand.c:5
Structure to represent the result value of an integral division.
Definition: stdlib.h:32
long long int atoll(const char *string)
Convert string to integer.
Definition: atoll.c:3
long int atol(const char *string)
Convert string to integer.
Definition: atol.c:3
void * malloc(size_t size)
Allocate memory block.
Definition: malloc.c:3
long long int quot
Represents the quotient.
Definition: stdlib.h:45
void * bsearch(const void *key, const void *base, size_t num, size_t size, int(*compar)(const void *, const void *))
Binary search in array.
Definition: bsearch.c:3
lldiv_t lldiv(long long int numer, long long int denom)
Integral division.
Definition: lldiv.c:4
Structure to represent the result value of an integral division.
Definition: stdlib.h:22
int quot
Represents the quotient.
Definition: stdlib.h:25
char buffer[500]
Definition: physical_memory_manager.c:5
long long int llabs(long long int n)
Absolute value.
Definition: llabs.c:4
void abort()
Abort current process.
Definition: abort.c:3
int rem
Represents the remainder.
Definition: stdlib.h:27
int atoi(const char *string)
Convert string to integer.
Definition: atoi.c:3
size_t uint32_t
Unsigned integral type.
Definition: string.h:8
void srand(unsigned int new_seed)
Initialize random number generator.
Definition: srand.c:5
long long int rem
Represents the remainder.
Definition: stdlib.h:47