From 803ffda5bc04db2b7a261a1530abd31febcde9d4 Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Sun, 31 May 2015 13:24:35 +0200 Subject: [PATCH] Liba --- Makefile | 12 +++++++----- lib/malloc.h | 8 -------- liba/Makefile | 2 ++ liba/README.txt | 10 ++++++++++ {lib => liba/include}/assert.h | 10 +++++++--- liba/include/private/macros.h | 12 ++++++++++++ liba/include/private/types.h | 6 ++++++ liba/include/stdbool.h | 6 ++++++ liba/include/stdint.h | 11 +++++++++++ liba/include/stdlib.h | 17 +++++++++++++++++ liba/include/string.h | 18 ++++++++++++++++++ liba/include/unistd.h | 4 ++++ {lib => liba/src}/assert.c | 1 + {lib => liba/src}/malloc.c | 7 ++++--- liba/src/memcpy.c | 12 ++++++++++++ liba/src/memset.c | 8 ++++++++ platform/ili9341/ili9341.h | 2 +- platform/stm32f429/init_kbd.c | 1 - poincare/src/expression.cpp | 1 - src/hello.cpp | 2 +- 20 files changed, 127 insertions(+), 23 deletions(-) delete mode 100644 lib/malloc.h create mode 100644 liba/Makefile create mode 100644 liba/README.txt rename {lib => liba/include}/assert.h (67%) create mode 100644 liba/include/private/macros.h create mode 100644 liba/include/private/types.h create mode 100644 liba/include/stdbool.h create mode 100644 liba/include/stdint.h create mode 100644 liba/include/stdlib.h create mode 100644 liba/include/string.h create mode 100644 liba/include/unistd.h rename {lib => liba/src}/assert.c (92%) rename {lib => liba/src}/malloc.c (68%) create mode 100644 liba/src/memcpy.c create mode 100644 liba/src/memset.c diff --git a/Makefile b/Makefile index d0c9bbd4d..9bcb8e6ad 100644 --- a/Makefile +++ b/Makefile @@ -25,10 +25,11 @@ endif SFLAGS += -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 # Flags - Header search path -SFLAGS += -Ilib -I. -Iinclude -Iexternal/freertos/include -Iexternal -Iexternal/freertos/portable/GCC/ARM_CM4F -Iexternal/newlib/libc/include +SFLAGS += -Ilib -I. +#-Iexternal/freertos/include -Iexternal -Iexternal/freertos/portable/GCC/ARM_CM4F -Iexternal/newlib/libc/include # Flags - Building options -SFLAGS += -Wall -ffreestanding +SFLAGS += -Wall -ffreestanding -nostdinc # Flags - Optimizations ifeq ($(PRODUCTION),1) @@ -45,17 +46,18 @@ CXXFLAGS = -std=c++11 -fno-exceptions -fno-unwind-tables -fno-rtti -nostdlib products := boot.elf boot.hex boot.bin -objs += external/freertos/tasks.o external/freertos/list.o external/freertos/queue.o external/freertos/portable/GCC/ARM_CM4F/port.o external/freertos/portable/MemMang/heap_1.o -objs += $(addprefix external/newlib/libc/, string/memset.o string/memcpy.o string/strlen.o) +#objs += external/freertos/tasks.o external/freertos/list.o external/freertos/queue.o external/freertos/portable/GCC/ARM_CM4F/port.o external/freertos/portable/MemMang/heap_1.o +#objs += $(addprefix external/newlib/libc/, string/memset.o string/memcpy.o string/strlen.o) lib/private/mem5.o: CFLAGS += -w -objs += lib/assert.o lib/errno.o lib/private/mem5.o lib/cxx_new.o lib/malloc.o +objs += lib/errno.o lib/private/mem5.o lib/cxx_new.o objs += src/hello.o default: clean boot.elf +include liba/Makefile include platform/Makefile include kandinsky/Makefile include poincare/Makefile diff --git a/lib/malloc.h b/lib/malloc.h deleted file mode 100644 index 30032e63e..000000000 --- a/lib/malloc.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef LIB_MALLOC_H -#define LIB_MALLOC_H - -void free(void *ptr); -void * malloc(int size); -void * realloc(void *ptr, int size); - -#endif diff --git a/liba/Makefile b/liba/Makefile new file mode 100644 index 000000000..034536455 --- /dev/null +++ b/liba/Makefile @@ -0,0 +1,2 @@ +SFLAGS += -Iliba/include +objs += $(addprefix liba/src/, assert.o memcpy.o memset.o malloc.o) diff --git a/liba/README.txt b/liba/README.txt new file mode 100644 index 000000000..dd6b21be9 --- /dev/null +++ b/liba/README.txt @@ -0,0 +1,10 @@ +liba is an adhoc libc. + +We need a very small subset of the functionality provided by the standard C +library. We could use an available libc implementation, but those are usually +way to large and may have problematic licenses. + +It wouldn't even be fair to call liba a libc at all since it doesn't come close +to implementing a significant portion of the standard. However, we do need some +functionality usually provided by libc, so instead of reinventing the wheel we +just implement the same entities (types, functions, etc…). diff --git a/lib/assert.h b/liba/include/assert.h similarity index 67% rename from lib/assert.h rename to liba/include/assert.h index 39f6a0aea..ccbf4d8a3 100644 --- a/lib/assert.h +++ b/liba/include/assert.h @@ -1,5 +1,7 @@ -#ifndef ASSERT_H -#define ASSERT_H +#ifndef LIBA_ASSERT_H +#define LIBA_ASSERT_H + +#include "private/macros.h" #ifdef NDEBUG #define assert(e) ((void)0) @@ -7,8 +9,10 @@ #define assert(e) ((void) ((e) ? ((void)0) : __assert(#e, __FILE__, __LINE__))) #endif +LIBA_BEGIN_DECLS + void __assert(const char * expression, const char * file, int line); -void abort(void); +LIBA_END_DECLS #endif diff --git a/liba/include/private/macros.h b/liba/include/private/macros.h new file mode 100644 index 000000000..cd3c204a9 --- /dev/null +++ b/liba/include/private/macros.h @@ -0,0 +1,12 @@ +#ifndef LIBA_MACROS_H +#define LIBA_MACROS_H + +#ifdef __cplusplus +#define LIBA_BEGIN_DECLS extern "C" { +#define LIBA_END_DECLS } +#else +#define LIBA_BEGIN_DECLS +#define LIBA_END_DECLS +#endif + +#endif diff --git a/liba/include/private/types.h b/liba/include/private/types.h new file mode 100644 index 000000000..7e5c5188b --- /dev/null +++ b/liba/include/private/types.h @@ -0,0 +1,6 @@ +#ifndef LIBA_TYPES_H +#define LIBA_TYPES_H + +typedef int size_t; + +#endif diff --git a/liba/include/stdbool.h b/liba/include/stdbool.h new file mode 100644 index 000000000..96cba3370 --- /dev/null +++ b/liba/include/stdbool.h @@ -0,0 +1,6 @@ +#ifndef LIBA_STDBOOL_H +#define LIBA_STDBOOL_H + +typedef char bool; + +#endif diff --git a/liba/include/stdint.h b/liba/include/stdint.h new file mode 100644 index 000000000..6adf681f3 --- /dev/null +++ b/liba/include/stdint.h @@ -0,0 +1,11 @@ +#ifndef LIBA_STDINT_H +#define LIBA_STDINT_H + +typedef unsigned char uint8_t; +typedef unsigned int uint16_t; +typedef unsigned long uint32_t; +typedef unsigned long long uint64_t; + +typedef long long int64_t; + +#endif diff --git a/liba/include/stdlib.h b/liba/include/stdlib.h new file mode 100644 index 000000000..19c9dd6e1 --- /dev/null +++ b/liba/include/stdlib.h @@ -0,0 +1,17 @@ +#ifndef LIBA_MALLOC_H +#define LIBA_MALLOC_H + +#include "private/macros.h" +#include "private/types.h" + +LIBA_BEGIN_DECLS + +void free(void *ptr); +void * malloc(size_t size); +void * realloc(void *ptr, size_t size); + +void abort(void); + +LIBA_END_DECLS + +#endif diff --git a/liba/include/string.h b/liba/include/string.h new file mode 100644 index 000000000..929b69733 --- /dev/null +++ b/liba/include/string.h @@ -0,0 +1,18 @@ +#ifndef LIBA_STRING_H +#define LIBA_STRING_H + +#include "private/macros.h" +#include "private/types.h" + +#define NULL 0 + +LIBA_BEGIN_DECLS + +void * memcpy(void * dst, const void * src, size_t n); +void * memset(void * b, int c, size_t len); + +size_t strlen(const char * s); + +LIBA_END_DECLS + +#endif diff --git a/liba/include/unistd.h b/liba/include/unistd.h new file mode 100644 index 000000000..78c0c0bef --- /dev/null +++ b/liba/include/unistd.h @@ -0,0 +1,4 @@ +#ifndef LIBA_UNISTD_H +#define LIBA_UNISTD_H + +#endif diff --git a/lib/assert.c b/liba/src/assert.c similarity index 92% rename from lib/assert.c rename to liba/src/assert.c index 76c9ca927..94a7c4f41 100644 --- a/lib/assert.c +++ b/liba/src/assert.c @@ -1,4 +1,5 @@ #include +#include #ifdef NDEBUG #define assert(e) ((void)0) diff --git a/lib/malloc.c b/liba/src/malloc.c similarity index 68% rename from lib/malloc.c rename to liba/src/malloc.c index 2af29ebe2..9040b2a3d 100644 --- a/lib/malloc.c +++ b/liba/src/malloc.c @@ -1,4 +1,5 @@ -#include +#include +#include void free(void *ptr) { if (ptr != NULL) { @@ -6,7 +7,7 @@ void free(void *ptr) { } } -void * malloc(int size) { +void * malloc(size_t size) { void * p = NULL; if (size > 0) { p = memsys5MallocUnsafe(memsys5Roundup(size)); @@ -14,6 +15,6 @@ void * malloc(int size) { return p; } -void * realloc(void *ptr, int size) { +void * realloc(void *ptr, size_t size) { return memsys5Realloc(ptr, memsys5Roundup(size)); } diff --git a/liba/src/memcpy.c b/liba/src/memcpy.c new file mode 100644 index 000000000..90d05b266 --- /dev/null +++ b/liba/src/memcpy.c @@ -0,0 +1,12 @@ +#include + +void * memcpy(void * dst, const void * src, size_t n) { + char * destination = (char *)dst; + char * source = (char *)src; + + while (n--) { + *destination++ = *source++; + } + + return dst; +} diff --git a/liba/src/memset.c b/liba/src/memset.c new file mode 100644 index 000000000..6513b45bc --- /dev/null +++ b/liba/src/memset.c @@ -0,0 +1,8 @@ +#include + +void * memset(void * b, int c, size_t len) { + char * destination = (char *)b; + while (len--) { + *destination++ = (unsigned char)c; + } +} diff --git a/platform/ili9341/ili9341.h b/platform/ili9341/ili9341.h index 14fb4dc89..acf3c709d 100644 --- a/platform/ili9341/ili9341.h +++ b/platform/ili9341/ili9341.h @@ -1,7 +1,7 @@ #ifndef PLATFORM_ILI9341_H #define PLATFORM_ILI9341_H 1 -#include +#include #include /* This is the ILI9341 driver diff --git a/platform/stm32f429/init_kbd.c b/platform/stm32f429/init_kbd.c index f198bf2fd..3cc2d995e 100644 --- a/platform/stm32f429/init_kbd.c +++ b/platform/stm32f429/init_kbd.c @@ -8,7 +8,6 @@ #include #include -#include #include "registers.h" #include "init_kbd.h" #include diff --git a/poincare/src/expression.cpp b/poincare/src/expression.cpp index a164357ac..fd23049f2 100644 --- a/poincare/src/expression.cpp +++ b/poincare/src/expression.cpp @@ -1,5 +1,4 @@ #include -#include #include "expression_parser.hpp" #include "expression_lexer.hpp" diff --git a/src/hello.cpp b/src/hello.cpp index 268529d3d..e83c61b19 100644 --- a/src/hello.cpp +++ b/src/hello.cpp @@ -1,7 +1,7 @@ extern "C" { #include "hello.h" #include -#include +#include #include }