zephyr/lib/libc/minimal/include/stdlib.h
Christopher Friedt bd83df1552 libc: minimal: add qsort to the minimal libc
This change implements qsort() for the minimal libc via Heapsort.

Heapsort time complexity is O(n log(n)) in the best, average,
and worst cases. It is O(1) in space complexity (i.e. sorts
in-place) and is iterative rather than recursive. Heapsort is
not stable (i.e. does not preserve order of identical elements).

On cortex-m0, this implementation occupies ~240 bytes.

Fixes #28896

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2021-11-10 07:00:36 -05:00

79 lines
1.7 KiB
C

/* stdlib.h */
/*
* Copyright (c) 2011-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_STDLIB_H_
#define ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_STDLIB_H_
#include <stddef.h>
#include <limits.h>
#ifdef __cplusplus
extern "C" {
#endif
unsigned long strtoul(const char *nptr, char **endptr, int base);
long strtol(const char *nptr, char **endptr, int base);
int atoi(const char *s);
void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
void *reallocarray(void *ptr, size_t nmemb, size_t size);
void *bsearch(const void *key, const void *array,
size_t count, size_t size,
int (*cmp)(const void *key, const void *element));
void qsort_r(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *, void *), void *arg);
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
void _exit(int status);
static inline void exit(int status)
{
_exit(status);
}
void abort(void);
#ifdef CONFIG_MINIMAL_LIBC_RAND
#define RAND_MAX INT_MAX
int rand(void);
void srand(unsigned int seed);
#endif /* CONFIG_MINIMAL_LIBC_RAND */
static inline int abs(int __n)
{
return (__n < 0) ? -__n : __n;
}
static inline long labs(long __n)
{
return (__n < 0L) ? -__n : __n;
}
static inline long long llabs(long long __n)
{
return (__n < 0LL) ? -__n : __n;
}
static inline void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
typedef int (*compar3)(const void *, const void *, void *);
qsort_r(base, nmemb, size, (compar3)compar, NULL);
}
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_STDLIB_H_ */