Implement libc callbacks (#112)

This commit is contained in:
Alex Kotov 2022-11-27 05:19:14 +04:00 committed by GitHub
parent 666b2cdc7e
commit 2a52c22f36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 4 deletions

View File

@ -11,6 +11,7 @@ endif
libc_la_SOURCES = \ libc_la_SOURCES = \
src/ctype.c \ src/ctype.c \
src/errno.c \ src/errno.c \
src/kernaux.c \
src/stdlib.c \ src/stdlib.c \
src/string.c src/string.c

View File

@ -1,4 +1,5 @@
nobase_include_HEADERS = \ nobase_include_HEADERS = \
kernaux/libc.h \
ctype.h \ ctype.h \
errno.h \ errno.h \
inttypes.h \ inttypes.h \

View 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

View File

@ -12,10 +12,14 @@ extern "C" {
int atoi(const char *str); int atoi(const char *str);
__attribute__((noreturn))
void abort(); void abort();
__attribute__((noreturn))
void exit(int status); void exit(int status);
void *calloc(size_t nmemb, size_t size);
void free(void *ptr); void free(void *ptr);
void *malloc(size_t size);
void *realloc(void *ptr, size_t size); void *realloc(void *ptr, size_t size);
#ifdef __cplusplus #ifdef __cplusplus

17
libc/src/kernaux.c Normal file
View 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,
};

View File

@ -2,22 +2,60 @@
#include "config.h" #include "config.h"
#endif #endif
#include <kernaux/libc.h>
#include <ctype.h> #include <ctype.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
// TODO: stub void exit(const int status)
void exit(const int status __attribute__((unused)))
{ {
for (;;); // Custom implementation
kernaux_libc.exit(status);
} }
// TODO: stub
void abort() void abort()
{ {
// Custom implementation
if (kernaux_libc.abort) kernaux_libc.abort();
// Default implementation
exit(EXIT_FAILURE); 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) int atoi(const char *str)
{ {
while (isspace(*str)) ++str; while (isspace(*str)) ++str;