1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * stdlib function definitions for NOLIBC
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)
134 if (__builtin_expect(__builtin_mul_overflow(nmemb, size, &res), 0)) {
140 * No need to zero the heap, the MAP_ANONYMOUS in malloc()
146 static __attribute__((unused))
147 void *realloc(void *old_ptr, size_t new_size)
149 struct nolibc_heap *heap;
154 return malloc(new_size);
156 heap = container_of(old_ptr, struct nolibc_heap, user_p);
157 user_p_len = heap->len - sizeof(*heap);
159 * Don't realloc() if @user_p_len >= @new_size, this block of
160 * memory is still enough to handle the @new_size. Just return
163 if (user_p_len >= new_size)
166 ret = malloc(new_size);
167 if (__builtin_expect(!ret, 0))
170 memcpy(ret, heap->user_p, heap->len);
171 munmap(heap, heap->len);
175 /* Converts the unsigned long integer <in> to its hex representation into
176 * buffer <buffer>, which must be long enough to store the number and the
177 * trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The
178 * buffer is filled from the first byte, and the number of characters emitted
179 * (not counting the trailing zero) is returned. The function is constructed
180 * in a way to optimize the code size and avoid any divide that could add a
181 * dependency on large external functions.
183 static __attribute__((unused))
184 int utoh_r(unsigned long in, char *buffer)
186 signed char pos = (~0UL > 0xfffffffful) ? 60 : 28;
192 in -= (uint64_t)dig << pos;
194 if (dig || digits || pos < 0) {
196 dig += 'a' - '0' - 10;
197 buffer[digits++] = '0' + dig;
205 /* converts unsigned long <in> to an hex string using the static itoa_buffer
206 * and returns the pointer to that string.
208 static inline __attribute__((unused))
209 char *utoh(unsigned long in)
211 utoh_r(in, itoa_buffer);
215 /* Converts the unsigned long integer <in> to its string representation into
216 * buffer <buffer>, which must be long enough to store the number and the
217 * trailing zero (21 bytes for 18446744073709551615 in 64-bit, 11 for
218 * 4294967295 in 32-bit). The buffer is filled from the first byte, and the
219 * number of characters emitted (not counting the trailing zero) is returned.
220 * The function is constructed in a way to optimize the code size and avoid
221 * any divide that could add a dependency on large external functions.
223 static __attribute__((unused))
224 int utoa_r(unsigned long in, char *buffer)
228 int pos = (~0UL > 0xfffffffful) ? 19 : 9;
232 for (dig = 0, lim = 1; dig < pos; dig++)
235 if (digits || in >= lim || !pos) {
236 for (dig = 0; in >= lim; dig++)
238 buffer[digits++] = '0' + dig;
246 /* Converts the signed long integer <in> to its string representation into
247 * buffer <buffer>, which must be long enough to store the number and the
248 * trailing zero (21 bytes for -9223372036854775808 in 64-bit, 12 for
249 * -2147483648 in 32-bit). The buffer is filled from the first byte, and the
250 * number of characters emitted (not counting the trailing zero) is returned.
252 static __attribute__((unused))
253 int itoa_r(long in, char *buffer)
263 len += utoa_r(in, ptr);
267 /* for historical compatibility, same as above but returns the pointer to the
270 static inline __attribute__((unused))
271 char *ltoa_r(long in, char *buffer)
277 /* converts long integer <in> to a string using the static itoa_buffer and
278 * returns the pointer to that string.
280 static inline __attribute__((unused))
283 itoa_r(in, itoa_buffer);
287 /* converts long integer <in> to a string using the static itoa_buffer and
288 * returns the pointer to that string. Same as above, for compatibility.
290 static inline __attribute__((unused))
293 itoa_r(in, itoa_buffer);
297 /* converts unsigned long integer <in> to a string using the static itoa_buffer
298 * and returns the pointer to that string.
300 static inline __attribute__((unused))
301 char *utoa(unsigned long in)
303 utoa_r(in, itoa_buffer);
307 /* Converts the unsigned 64-bit integer <in> to its hex representation into
308 * buffer <buffer>, which must be long enough to store the number and the
309 * trailing zero (17 bytes for "ffffffffffffffff"). The buffer is filled from
310 * the first byte, and the number of characters emitted (not counting the
311 * trailing zero) is returned. The function is constructed in a way to optimize
312 * the code size and avoid any divide that could add a dependency on large
313 * external functions.
315 static __attribute__((unused))
316 int u64toh_r(uint64_t in, char *buffer)
318 signed char pos = 60;
323 if (sizeof(long) >= 8) {
324 dig = (in >> pos) & 0xF;
326 /* 32-bit platforms: avoid a 64-bit shift */
327 uint32_t d = (pos >= 32) ? (in >> 32) : in;
328 dig = (d >> (pos & 31)) & 0xF;
331 dig += 'a' - '0' - 10;
333 if (dig || digits || pos < 0)
334 buffer[digits++] = '0' + dig;
341 /* converts uint64_t <in> to an hex string using the static itoa_buffer and
342 * returns the pointer to that string.
344 static inline __attribute__((unused))
345 char *u64toh(uint64_t in)
347 u64toh_r(in, itoa_buffer);
351 /* Converts the unsigned 64-bit integer <in> to its string representation into
352 * buffer <buffer>, which must be long enough to store the number and the
353 * trailing zero (21 bytes for 18446744073709551615). The buffer is filled from
354 * the first byte, and the number of characters emitted (not counting the
355 * trailing zero) is returned. The function is constructed in a way to optimize
356 * the code size and avoid any divide that could add a dependency on large
357 * external functions.
359 static __attribute__((unused))
360 int u64toa_r(uint64_t in, char *buffer)
362 unsigned long long lim;
364 int pos = 19; /* start with the highest possible digit */
368 for (dig = 0, lim = 1; dig < pos; dig++)
371 if (digits || in >= lim || !pos) {
372 for (dig = 0; in >= lim; dig++)
374 buffer[digits++] = '0' + dig;
382 /* Converts the signed 64-bit integer <in> to its string representation into
383 * buffer <buffer>, which must be long enough to store the number and the
384 * trailing zero (21 bytes for -9223372036854775808). The buffer is filled from
385 * the first byte, and the number of characters emitted (not counting the
386 * trailing zero) is returned.
388 static __attribute__((unused))
389 int i64toa_r(int64_t in, char *buffer)
399 len += u64toa_r(in, ptr);
403 /* converts int64_t <in> to a string using the static itoa_buffer and returns
404 * the pointer to that string.
406 static inline __attribute__((unused))
407 char *i64toa(int64_t in)
409 i64toa_r(in, itoa_buffer);
413 /* converts uint64_t <in> to a string using the static itoa_buffer and returns
414 * the pointer to that string.
416 static inline __attribute__((unused))
417 char *u64toa(uint64_t in)
419 u64toa_r(in, itoa_buffer);
423 #endif /* _NOLIBC_STDLIB_H */