diff --git a/CMakeLists.txt b/CMakeLists.txt index 18c3e95..83c13e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -269,13 +269,15 @@ endif() if(gint IN_LIST TARGET_FOLDERS) list(APPEND SOURCES - # stdlib - src/stdlib/target/gint/free.c - src/stdlib/target/gint/malloc.c - src/stdlib/target/gint/realloc.c - # time - src/time/target/gint/clock.c - src/time/target/gint/time.c) + # stdlib HAL + src/stdlib/fxlibc_hal_malloc.c + src/stdlib/free.c + src/stdlib/malloc.c + src/stdlib/realloc.c + # time HAL + src/time/fxlibc_hal_time.c + src/time/clock.c + src/time/time.c) endif() diff --git a/include/fxlibc/hal.h b/include/fxlibc/hal.h new file mode 100644 index 0000000..ffdbbee --- /dev/null +++ b/include/fxlibc/hal.h @@ -0,0 +1,25 @@ +#ifndef __FXLIBC_HAL_H__ +# define __FXLIBC_HAL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +extern void *fxlibc_hal_malloc(size_t __size); + +extern void fxlibc_hal_free(void *__ptr); + +extern void *fxlibc_hal_realloc(void *__ptr, size_t __size); + +extern void fxlibc_hal_rawtime(struct tm *__tm); + +extern clock_t fxlibc_hal_clock(void); + +#ifdef __cplusplus +} +#endif + +#endif /*__FXLIBC_HAL_H__*/ diff --git a/src/stdlib/free.c b/src/stdlib/free.c new file mode 100644 index 0000000..6ee451c --- /dev/null +++ b/src/stdlib/free.c @@ -0,0 +1,7 @@ +#include +#include + +void free(void *ptr) +{ + return fxlibc_hal_free(ptr); +} diff --git a/src/stdlib/fxlibc_hal_malloc.c b/src/stdlib/fxlibc_hal_malloc.c new file mode 100644 index 0000000..6954717 --- /dev/null +++ b/src/stdlib/fxlibc_hal_malloc.c @@ -0,0 +1,30 @@ +#include +#include +#include + +#if FXLIBC_HAL_DEFAULT_MALLOC + +__attribute__((weak)) +void *fxlibc_hal_malloc(size_t size) +{ + (void)size; + errno = ENOMEM; + return NULL; +} + +__attribute__((weak)) +void fxlibc_hal_free(void *ptr) +{ + (void)ptr; +} + +__attribute__((weak)) +void *fxlibc_hal_realloc(void *ptr, size_t size) +{ + (void)ptr; + (void)size; + errno = ENOMEM; + return NULL; +} + +#endif /* FXLIBC_HAL_DEFAULT_MALLOC */ diff --git a/src/stdlib/malloc.c b/src/stdlib/malloc.c new file mode 100644 index 0000000..506fcbb --- /dev/null +++ b/src/stdlib/malloc.c @@ -0,0 +1,11 @@ +#include +#include +#include + +void *malloc(size_t size) +{ + void *ptr = fxlibc_hal_malloc(size); + if(ptr == NULL) + errno = ENOMEM; + return ptr; +} diff --git a/src/stdlib/realloc.c b/src/stdlib/realloc.c new file mode 100644 index 0000000..ac133db --- /dev/null +++ b/src/stdlib/realloc.c @@ -0,0 +1,11 @@ +#include +#include +#include + +void *realloc(void *ptr, size_t size) +{ + ptr = fxlibc_hal_realloc(ptr, size); + if(ptr == NULL) + errno = ENOMEM; + return ptr; +} diff --git a/src/stdlib/target/gint/free.c b/src/stdlib/target/gint/free.c deleted file mode 100644 index 5fad8d1..0000000 --- a/src/stdlib/target/gint/free.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -extern void kfree(void *ptr); - -void free(void *ptr) -{ - return kfree(ptr); -} diff --git a/src/stdlib/target/gint/malloc.c b/src/stdlib/target/gint/malloc.c deleted file mode 100644 index 1a9b8be..0000000 --- a/src/stdlib/target/gint/malloc.c +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include - -extern void *kmalloc(size_t size, char const *arena_name); - -void *malloc(size_t size) -{ - void *ptr = kmalloc(size, NULL); - if(ptr == NULL) - errno = ENOMEM; - return ptr; -} diff --git a/src/stdlib/target/gint/realloc.c b/src/stdlib/target/gint/realloc.c deleted file mode 100644 index b37c445..0000000 --- a/src/stdlib/target/gint/realloc.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -extern void *krealloc(void *ptr, size_t size); - -void *realloc(void *ptr, size_t size) -{ - return krealloc(ptr, size); -} diff --git a/src/time/clock.c b/src/time/clock.c new file mode 100644 index 0000000..dce46a7 --- /dev/null +++ b/src/time/clock.c @@ -0,0 +1,15 @@ +#include +#include + +static clock_t clock_init; + +__attribute__((constructor)) +static void clock_initialize(void) +{ + clock_init = fxlibc_hal_clock(); +} + +clock_t clock(void) +{ + return fxlibc_hal_clock() - clock_init; +} diff --git a/src/time/fxlibc_hal_time.c b/src/time/fxlibc_hal_time.c new file mode 100644 index 0000000..2c375d5 --- /dev/null +++ b/src/time/fxlibc_hal_time.c @@ -0,0 +1,18 @@ +#include +#include + +#if FXLIBC_HAL_DEFAULT_TIME + +__attribute__((weak)) +void fxlibc_hal_rawtime(struct tm *tm) +{ + memset(tm, 0, sizeof *tm); +} + +__attribute__((weak)) +static clock_t fxlibc_hal_clock(void) +{ + return 0; +} + +#endif /* FXLIBC_HAL_DEFAULT_TIME */ diff --git a/src/time/target/gint/clock.c b/src/time/target/gint/clock.c deleted file mode 100644 index a284ef2..0000000 --- a/src/time/target/gint/clock.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -uint32_t rtc_ticks(void); - -static clock_t clock_init; - -static clock_t clock_abs(void) -{ - return (CLOCKS_PER_SEC * (uint64_t)rtc_ticks()) / 128; -} - -__attribute__((constructor)) -static void clock_initialize(void) -{ - clock_init = clock_abs(); -} - -clock_t clock(void) -{ - return clock_abs() - clock_init; -} diff --git a/src/time/target/gint/time.c b/src/time/target/gint/time.c deleted file mode 100644 index 69594d2..0000000 --- a/src/time/target/gint/time.c +++ /dev/null @@ -1,37 +0,0 @@ -#include - -typedef struct -{ - uint16_t year; - uint8_t week_day; - uint8_t month; - uint8_t month_day; - uint8_t hours; - uint8_t minutes; - uint8_t seconds; - uint8_t ticks; - -} rtc_time_t; - -void rtc_get_time(rtc_time_t *time); - -time_t time(time_t *timeptr) -{ - rtc_time_t rtc; - struct tm tm; - time_t calendar; - - rtc_get_time(&rtc); - tm.tm_sec = rtc.seconds; - tm.tm_min = rtc.minutes; - tm.tm_hour = rtc.hours; - tm.tm_mday = rtc.month_day; - tm.tm_mon = rtc.month; - tm.tm_year = rtc.year - 1900; - tm.tm_isdst = 0; - - calendar = mktime(&tm); - if(timeptr != NULL) - *timeptr = calendar; - return calendar; -} diff --git a/src/time/time.c b/src/time/time.c new file mode 100644 index 0000000..774c3db --- /dev/null +++ b/src/time/time.c @@ -0,0 +1,15 @@ +#include +#include + +time_t time(time_t *timeptr) +{ + struct tm tm; + time_t calendar; + + fxlibc_hal_rawtime(&tm); + + calendar = mktime(&tm); + if(timeptr != NULL) + *timeptr = calendar; + return calendar; +}