libclayer/src/stdlib.c

71 lines
1.6 KiB
C
Raw Normal View History

2022-12-25 08:58:00 +00:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2022-12-25 10:14:39 +00:00
#include <libclayer.h>
2022-12-27 09:05:30 +00:00
#include <libclayer/ctype.h>
#include <libclayer/stdlib.h>
#include <libclayer/string.h>
2022-12-25 08:58:00 +00:00
#include <stdbool.h>
2022-12-25 11:23:09 +00:00
void LIBCLAYER(exit)(const int status)
2022-12-25 08:58:00 +00:00
{
// Custom implementation
2022-12-25 10:14:39 +00:00
libclayer.exit(status);
2022-12-25 08:58:00 +00:00
}
2022-12-25 11:23:09 +00:00
void LIBCLAYER(abort)()
2022-12-25 08:58:00 +00:00
{
// Custom implementation
2022-12-25 10:14:39 +00:00
if (libclayer.abort) libclayer.abort();
2022-12-25 08:58:00 +00:00
// Default implementation
2022-12-25 11:23:09 +00:00
LIBCLAYER(exit)(EXIT_FAILURE);
2022-12-25 08:58:00 +00:00
}
2022-12-25 11:23:09 +00:00
void *LIBCLAYER(calloc)(const size_t nmemb, const size_t size)
2022-12-25 08:58:00 +00:00
{
// Custom implementation
2022-12-25 10:14:39 +00:00
if (libclayer.calloc) return libclayer.calloc(nmemb, size);
2022-12-25 08:58:00 +00:00
// Default implementation
const size_t total_size = nmemb * size;
if (!total_size) return NULL;
if (total_size / nmemb != size) return NULL;
2022-12-25 11:23:09 +00:00
void *const ptr = LIBCLAYER(malloc)(total_size);
if (ptr) LIBCLAYER(memset)(ptr, 0, total_size);
2022-12-25 08:58:00 +00:00
return ptr;
}
2022-12-25 11:23:09 +00:00
void LIBCLAYER(free)(void *const ptr)
2022-12-25 08:58:00 +00:00
{
// Custom implementation
2022-12-25 10:14:39 +00:00
libclayer.free(ptr);
2022-12-25 08:58:00 +00:00
}
2022-12-25 11:23:09 +00:00
void *LIBCLAYER(malloc)(const size_t size)
2022-12-25 08:58:00 +00:00
{
// Custom implementation
2022-12-25 10:14:39 +00:00
return libclayer.malloc(size);
2022-12-25 08:58:00 +00:00
}
2022-12-25 11:23:09 +00:00
void *LIBCLAYER(realloc)(void *const ptr, const size_t size)
2022-12-25 08:58:00 +00:00
{
// Custom implementation
2022-12-25 10:14:39 +00:00
return libclayer.realloc(ptr, size);
2022-12-25 08:58:00 +00:00
}
2022-12-25 11:23:09 +00:00
int LIBCLAYER(atoi)(const char *str)
2022-12-25 08:58:00 +00:00
{
2022-12-25 11:23:09 +00:00
while (LIBCLAYER(isspace)(*str)) ++str;
2022-12-25 08:58:00 +00:00
bool is_negative = false;
switch (*str) {
case '-': is_negative = true; // fall through
case '+': ++str;
}
int result = 0;
2022-12-25 11:23:09 +00:00
while (LIBCLAYER(isdigit)(*str)) result = 10 * result - (*str++ - '0');
2022-12-25 08:58:00 +00:00
return is_negative ? result : -result;
}