mirror of
https://github.com/tailix/libkernaux.git
synced 2025-02-17 15:45:32 -05:00
Implement libc callbacks (#112)
This commit is contained in:
parent
666b2cdc7e
commit
2a52c22f36
6 changed files with 91 additions and 4 deletions
|
@ -11,6 +11,7 @@ endif
|
|||
libc_la_SOURCES = \
|
||||
src/ctype.c \
|
||||
src/errno.c \
|
||||
src/kernaux.c \
|
||||
src/stdlib.c \
|
||||
src/string.c
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
nobase_include_HEADERS = \
|
||||
kernaux/libc.h \
|
||||
ctype.h \
|
||||
errno.h \
|
||||
inttypes.h \
|
||||
|
|
26
libc/include/kernaux/libc.h
Normal file
26
libc/include/kernaux/libc.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef KERNAUX_INCLUDED_LIBC
|
||||
#define KERNAUX_INCLUDED_LIBC
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct KernAux_Libc {
|
||||
void (*abort)() __attribute__((noreturn));
|
||||
void (*exit)(int status) __attribute__((noreturn));
|
||||
|
||||
void *(*calloc)(size_t nmemb, size_t size);
|
||||
void (*free)(void *ptr);
|
||||
void *(*malloc)(size_t size);
|
||||
void *(*realloc)(void *ptr, size_t size);
|
||||
};
|
||||
|
||||
extern struct KernAux_Libc kernaux_libc;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -12,10 +12,14 @@ extern "C" {
|
|||
|
||||
int atoi(const char *str);
|
||||
|
||||
__attribute__((noreturn))
|
||||
void abort();
|
||||
__attribute__((noreturn))
|
||||
void exit(int status);
|
||||
|
||||
void *calloc(size_t nmemb, size_t size);
|
||||
void free(void *ptr);
|
||||
void *malloc(size_t size);
|
||||
void *realloc(void *ptr, size_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
17
libc/src/kernaux.c
Normal file
17
libc/src/kernaux.c
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <kernaux/libc.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct KernAux_Libc kernaux_libc = {
|
||||
.abort = NULL,
|
||||
.exit = NULL,
|
||||
|
||||
.calloc = NULL,
|
||||
.free = NULL,
|
||||
.malloc = NULL,
|
||||
.realloc = NULL,
|
||||
};
|
|
@ -2,22 +2,60 @@
|
|||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <kernaux/libc.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// TODO: stub
|
||||
void exit(const int status __attribute__((unused)))
|
||||
void exit(const int status)
|
||||
{
|
||||
for (;;);
|
||||
// Custom implementation
|
||||
kernaux_libc.exit(status);
|
||||
}
|
||||
|
||||
// TODO: stub
|
||||
void abort()
|
||||
{
|
||||
// Custom implementation
|
||||
if (kernaux_libc.abort) kernaux_libc.abort();
|
||||
|
||||
// Default implementation
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void *calloc(const size_t nmemb, const size_t size)
|
||||
{
|
||||
// Custom implementation
|
||||
if (kernaux_libc.calloc) return kernaux_libc.calloc(nmemb, size);
|
||||
|
||||
// Default implementation
|
||||
const size_t total_size = nmemb * size;
|
||||
if (!total_size) return NULL;
|
||||
if (total_size / nmemb != size) return NULL;
|
||||
void *const ptr = malloc(total_size);
|
||||
if (ptr) memset(ptr, 0, total_size);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void free(void *const ptr)
|
||||
{
|
||||
// Custom implementation
|
||||
kernaux_libc.free(ptr);
|
||||
}
|
||||
|
||||
void *malloc(const size_t size)
|
||||
{
|
||||
// Custom implementation
|
||||
return kernaux_libc.malloc(size);
|
||||
}
|
||||
|
||||
void *realloc(void *const ptr, const size_t size)
|
||||
{
|
||||
// Custom implementation
|
||||
return kernaux_libc.realloc(ptr, size);
|
||||
}
|
||||
|
||||
int atoi(const char *str)
|
||||
{
|
||||
while (isspace(*str)) ++str;
|
||||
|
|
Loading…
Add table
Reference in a new issue