1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * stdlib function definitions for NOLIBC
4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
7 #ifndef _NOLIBC_STDLIB_H
8 #define _NOLIBC_STDLIB_H
18 char user_p[] __attribute__((__aligned__));
21 /* Buffer used to store int-to-ASCII conversions. Will only be implemented if
22 * any of the related functions is implemented. The area is large enough to
23 * store "18446744073709551615" or "-9223372036854775808" and the final zero.
25 static __attribute__((unused)) char itoa_buffer[21];
28 * As much as possible, please keep functions alphabetically sorted.
31 /* must be exported, as it's used by libgcc for various divide functions */
32 __attribute__((weak,unused,noreturn,section(".text.nolibc_abort")))
35 sys_kill(sys_getpid(), SIGABRT);
39 static __attribute__((unused))
40 long atol(const char *s)
42 unsigned long ret = 0;
59 return neg ? -ret : ret;
62 static __attribute__((unused))
63 int atoi(const char *s)
68 static __attribute__((unused))
71 struct nolibc_heap *heap;
76 heap = container_of(ptr, struct nolibc_heap, user_p);
77 munmap(heap, heap->len);
80 /* getenv() tries to find the environment variable named <name> in the
81 * environment array pointed to by global variable "environ" which must be
82 * declared as a char **, and must be terminated by a NULL (it is recommended
83 * to set this variable to the "envp" argument of main()). If the requested
84 * environment variable exists its value is returned otherwise NULL is
85 * returned. getenv() is forcefully inlined so that the reference to "environ"
86 * will be dropped if unused, even at -O0.
88 static __attribute__((unused))
89 char *_getenv(const char *name, char **environ)
94 for (idx = 0; environ[idx]; idx++) {
95 for (i = 0; name[i] && name[i] == environ[idx][i];)
97 if (!name[i] && environ[idx][i] == '=')
98 return &environ[idx][i+1];
104 static inline __attribute__((unused,always_inline))
105 char *getenv(const char *name)
107 extern char **environ;
108 return _getenv(name, environ);
111 static __attribute__((unused))
112 void *malloc(size_t len)
114 struct nolibc_heap *heap;
116 /* Always allocate memory with size multiple of 4096. */
117 len = sizeof(*heap) + len;
118 len = (len + 4095UL) & -4096UL;
119 heap = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
121 if (__builtin_expect(heap == MAP_FAILED, 0))
128 static __attribute__((unused))
129 void *calloc(size_t size, size_t nmemb)
131 size_t x = size * nmemb;
133 if (__builtin_expect(size && ((x / size) != nmemb), 0)) {
139 * No need to zero the heap, the MAP_ANONYMOUS in malloc()
145 static __attribute__((unused))
146 void *realloc(void *old_ptr, size_t new_size)
148 struct nolibc_heap *heap;
153 return malloc(new_size);
155 heap = container_of(old_ptr, struct nolibc_heap, user_p);
156 user_p_len = heap->len - sizeof(*heap);
158 * Don't realloc() if @user_p_len >= @new_size, this block of
159 * memory is still enough to handle the @new_size. Just return
162 if (user_p_len >= new_size)
165 ret = malloc(new_size);
166 if (__builtin_expect(!ret, 0))
169 memcpy(ret, heap->user_p, heap->len);
170 munmap(heap, heap->len);
174 /* Converts the unsigned long integer <in> to its hex representation into
175 * buffer <buffer>, which must be long enough to store the number and the
176 * trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The
177 * buffer is filled from the first byte, and the number of characters emitted
178 * (not counting the trailing zero) is returned. The function is constructed
179 * in a way to optimize the code size and avoid any divide that could add a
180 * dependency on large external functions.
182 static __attribute__((unused))
183 int utoh_r(unsigned long in, char *buffer)
185 signed char pos = (~0UL > 0xfffffffful) ? 60 : 28;
191 in -= (uint64_t)dig << pos;
193 if (dig || digits || pos < 0) {
195 dig += 'a' - '0' - 10;
196 buffer[digits++] = '0' + dig;
204 /* converts unsigned long <in> to an hex string using the static itoa_buffer
205 * and returns the pointer to that string.
207 static inline __attribute__((unused))
208 char *utoh(unsigned long in)
210 utoh_r(in, itoa_buffer);
214 /* Converts the unsigned long integer <in> to its string representation into
215 * buffer <buffer>, which must be long enough to store the number and the
216 * trailing zero (21 bytes for 18446744073709551615 in 64-bit, 11 for
217 * 4294967295 in 32-bit). The buffer is filled from the first byte, and the
218 * number of characters emitted (not counting the trailing zero) is returned.
219 * The function is constructed in a way to optimize the code size and avoid
220 * any divide that could add a dependency on large external functions.
222 static __attribute__((unused))
223 int utoa_r(unsigned long in, char *buffer)
227 int pos = (~0UL > 0xfffffffful) ? 19 : 9;
231 for (dig = 0, lim = 1; dig < pos; dig++)
234 if (digits || in >= lim || !pos) {
235 for (dig = 0; in >= lim; dig++)
237 buffer[digits++] = '0' + dig;
245 /* Converts the signed long integer <in> to its string representation into
246 * buffer <buffer>, which must be long enough to store the number and the
247 * trailing zero (21 bytes for -9223372036854775808 in 64-bit, 12 for
248 * -2147483648 in 32-bit). The buffer is filled from the first byte, and the
249 * number of characters emitted (not counting the trailing zero) is returned.
251 static __attribute__((unused))
252 int itoa_r(long in, char *buffer)
262 len += utoa_r(in, ptr);
266 /* for historical compatibility, same as above but returns the pointer to the
269 static inline __attribute__((unused))
270 char *ltoa_r(long in, char *buffer)
276 /* converts long integer <in> to a string using the static itoa_buffer and
277 * returns the pointer to that string.
279 static inline __attribute__((unused))
282 itoa_r(in, itoa_buffer);
286 /* converts long integer <in> to a string using the static itoa_buffer and
287 * returns the pointer to that string. Same as above, for compatibility.
289 static inline __attribute__((unused))
292 itoa_r(in, itoa_buffer);
296 /* converts unsigned long integer <in> to a string using the static itoa_buffer
297 * and returns the pointer to that string.
299 static inline __attribute__((unused))
300 char *utoa(unsigned long in)
302 utoa_r(in, itoa_buffer);
306 /* Converts the unsigned 64-bit integer <in> to its hex representation into
307 * buffer <buffer>, which must be long enough to store the number and the
308 * trailing zero (17 bytes for "ffffffffffffffff"). The buffer is filled from
309 * the first byte, and the number of characters emitted (not counting the
310 * trailing zero) is returned. The function is constructed in a way to optimize
311 * the code size and avoid any divide that could add a dependency on large
312 * external functions.
314 static __attribute__((unused))
315 int u64toh_r(uint64_t in, char *buffer)
317 signed char pos = 60;
322 if (sizeof(long) >= 8) {
323 dig = (in >> pos) & 0xF;
325 /* 32-bit platforms: avoid a 64-bit shift */
326 uint32_t d = (pos >= 32) ? (in >> 32) : in;
327 dig = (d >> (pos & 31)) & 0xF;
330 dig += 'a' - '0' - 10;
332 if (dig || digits || pos < 0)
333 buffer[digits++] = '0' + dig;
340 /* converts uint64_t <in> to an hex string using the static itoa_buffer and
341 * returns the pointer to that string.
343 static inline __attribute__((unused))
344 char *u64toh(uint64_t in)
346 u64toh_r(in, itoa_buffer);
350 /* Converts the unsigned 64-bit integer <in> to its string representation into
351 * buffer <buffer>, which must be long enough to store the number and the
352 * trailing zero (21 bytes for 18446744073709551615). The buffer is filled from
353 * the first byte, and the number of characters emitted (not counting the
354 * trailing zero) is returned. The function is constructed in a way to optimize
355 * the code size and avoid any divide that could add a dependency on large
356 * external functions.
358 static __attribute__((unused))
359 int u64toa_r(uint64_t in, char *buffer)
361 unsigned long long lim;
363 int pos = 19; /* start with the highest possible digit */
367 for (dig = 0, lim = 1; dig < pos; dig++)
370 if (digits || in >= lim || !pos) {
371 for (dig = 0; in >= lim; dig++)
373 buffer[digits++] = '0' + dig;
381 /* Converts the signed 64-bit integer <in> to its string representation into
382 * buffer <buffer>, which must be long enough to store the number and the
383 * trailing zero (21 bytes for -9223372036854775808). The buffer is filled from
384 * the first byte, and the number of characters emitted (not counting the
385 * trailing zero) is returned.
387 static __attribute__((unused))
388 int i64toa_r(int64_t in, char *buffer)
398 len += u64toa_r(in, ptr);
402 /* converts int64_t <in> to a string using the static itoa_buffer and returns
403 * the pointer to that string.
405 static inline __attribute__((unused))
406 char *i64toa(int64_t in)
408 i64toa_r(in, itoa_buffer);
412 /* converts uint64_t <in> to a string using the static itoa_buffer and returns
413 * the pointer to that string.
415 static inline __attribute__((unused))
416 char *u64toa(uint64_t in)
418 u64toa_r(in, itoa_buffer);
422 /* make sure to include all global symbols */
425 #endif /* _NOLIBC_STDLIB_H */