matter.c

Cross-platform minimalist libc
git clone git://git.finwo.net/lib/matter.c
Log | Files | Refs | README | LICENSE

commit f473b997c9b9882db206e19de1139318be91a118
parent a0b314586ed6ef329e081d9edbe0b83f1f591737
Author: Yersa Nordman <finwo@pm.me>
Date:   Sat, 27 May 2023 00:52:10 +0200

Merge pull request #3 from finwo/static-rebuild

Static rebuild
Diffstat:
M.gitignore | 2++
MLICENSE | 2+-
DMakefile | 30------------------------------
AREADME.md | 6++++++
Darch/wasm32/config.mk | 4----
Darch/wasm32/include/bits/alltypes.h | 15---------------
Darch/wasm32/include/matter/page_size.h | 6------
Darch/wasm32/src/unistd/brk.c | 31-------------------------------
Mconfig.mk | 3++-
Ainclude/bits/alltypes.h | 23+++++++++++++++++++++++
Minclude/matter/page_size.h | 5+++++
Minclude/stdlib.h | 1+
Mpackage.ini | 14+++++++++++---
Msrc/malloc/malloc.c | 5-----
Msrc/string/memcmp.c | 5-----
Msrc/string/memcpy.c | 5-----
Msrc/string/memset.c | 5-----
Msrc/string/strlen.c | 9++-------
Asrc/unistd/brk.c | 31+++++++++++++++++++++++++++++++
19 files changed, 84 insertions(+), 118 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,5 @@ +/bak/ +/lib/ *.o *.a *.ll diff --git a/LICENSE b/LICENSE @@ -1,7 +1,7 @@ The MIT License (MIT) ===================== -Copyright © 2020 finwo +Copyright © 2023 finwo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation diff --git a/Makefile b/Makefile @@ -1,30 +0,0 @@ -TARGET ?= wasm32 - -include arch/$(TARGET)/config.mk - -SRC=$(shell find -L arch/$(TARGET)/src -type f -name \*.c) -SRC+=$(shell find -L src -type f -name \*.c) -OBJ=$(SRC:.c=.o) - -INCLUDES?= -INCLUDES+=-Iarch/$(TARGET)/include -INCLUDES+=-Iinclude - -CFLAGS?= -CFLAGS+=-nostdinc - -libmatter.a: $(OBJ) - rm -f $@ - $(AR) -cvq $@ $(OBJ) - -libmatter.so: $(OBJ) - $(CC) -fPIC --shared $(CFLAGS) $(INCLUDES) -o $@ $(SRC) - -%.o: %.c - $(LLE) -Ofast -S $(CFLAGS) $(INCLUDES) -c $< -o $(@:.o=.ll) - $(LLC) -filetype=obj -O3 $(@:.o=.ll) - -.PHONY: clean -clean: - rm -f $(shell find -L . -type f -name *.o) - rm -f $(shell find -L . -type f -name *.ll) diff --git a/README.md b/README.md @@ -0,0 +1,6 @@ +finwo/matter +============ + +Just a small embeddable standard C library for your wasm projects, because that stuff matters + + diff --git a/arch/wasm32/config.mk b/arch/wasm32/config.mk @@ -1,4 +0,0 @@ -CC=clang --target=wasm32-unknown-unknown-wasm -LLE=$(CC) -emit-llvm -LLC=llc -march=wasm32 -AR=llvm-ar diff --git a/arch/wasm32/include/bits/alltypes.h b/arch/wasm32/include/bits/alltypes.h @@ -1,15 +0,0 @@ -#ifndef _BITS_ALLTYPES_H_ -#define _BITS_ALLTYPES_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#define _Addr int - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // _BITS_ALLTYPES_H_ - diff --git a/arch/wasm32/include/matter/page_size.h b/arch/wasm32/include/matter/page_size.h @@ -1,6 +0,0 @@ -#ifndef _MATTER_PAGE_SIZE_H_ -#define _MATTER_PAGE_SIZE_H_ - -#define PAGE_SIZE (64*1024) - -#endif diff --git a/arch/wasm32/src/unistd/brk.c b/arch/wasm32/src/unistd/brk.c @@ -1,31 +0,0 @@ -#ifndef _UNISTD_BRK_C_ -#define _UNISTD_BRK_C_ - -#include <stddef.h> -#include <unistd.h> - -#include <matter/page_size.h> - -extern unsigned char __heap_base; -void *break_pointer = &__heap_base; - -int brk(void *addr) { - break_pointer = addr; - return 0; -} - -void * sbrk(ssize_t increment) { - size_t current_pages = __builtin_wasm_memory_size(0); - void *heap_limit = (void *)(current_pages * (size_t)PAGE_SIZE); - - void *ret = break_pointer; - break_pointer += increment; - - if (break_pointer > heap_limit) { - __builtin_wasm_memory_grow(0, ((break_pointer - heap_limit) / PAGE_SIZE) + 1); - } - - return ret; -} - -#endif // _UNISTD_BRK_C_ diff --git a/config.mk b/config.mk @@ -1 +1,2 @@ -SRC+=__DIRNAME/libmatter.a +override CFLAGS+=-nostdinc -fno-builtin +SRC+=$(wildcard __DIRNAME/src/*/*.c) diff --git a/include/bits/alltypes.h b/include/bits/alltypes.h @@ -0,0 +1,23 @@ +#ifndef _BITS_ALLTYPES_H_ +#define _BITS_ALLTYPES_H_ + +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#if __WORDSIZE == 32 +#define _Addr int +#endif + +#if __WORDSIZE == 64 +#define _Addr long +#endif + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _BITS_ALLTYPES_H_ + diff --git a/include/matter/page_size.h b/include/matter/page_size.h @@ -3,4 +3,9 @@ #define PAGE_SIZE (4*1024) +#ifdef __wasm__ +#undef PAGE_SIZE +#define PAGE_SIZE (64*1024) +#endif + #endif diff --git a/include/stdlib.h b/include/stdlib.h @@ -2,6 +2,7 @@ #define _STDLIB_H_ #include <stdint.h> +#include <malloc.h> void srand(unsigned s); int rand(void); diff --git a/package.ini b/package.ini @@ -3,6 +3,14 @@ name=finwo/matter [export] config.mk=config.mk - -[build] -0000=make libmatter.a +include/bits/alltypes.h=include/bits/alltypes.h +include/matter/page_size.h=include/matter/page_size.h +include/float.h=include/float.h +include/malloc.h=include/malloc.h +include/stdarg.h=include/stdarg.h +include/stdbool.h=include/stdbool.h +include/stddef.h=include/stddef.h +include/stdint.h=include/stdint.h +include/stdlib.h=include/stdlib.h +include/string.h=include/string.h +include/unistd.h=include/unistd.h diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c @@ -1,6 +1,3 @@ -#ifndef _MALLOC_MALLOC_C_ -#define _MALLOC_MALLOC_C_ - #include <malloc.h> #include <string.h> #include <unistd.h> @@ -121,5 +118,3 @@ void *calloc(size_t nelem, size_t elsize) { memset(ptr, 0, size); return ptr; } - -#endif // _MALLOC_MALLOC_C_ diff --git a/src/string/memcmp.c b/src/string/memcmp.c @@ -1,6 +1,3 @@ -#ifndef _STRING_MEMCMP_C_ -#define _STRING_MEMCMP_C_ - #include <string.h> int memcmp (const void *str1, const void *str2, int count) { @@ -15,5 +12,3 @@ int memcmp (const void *str1, const void *str2, int count) { return 0; } - -#endif // _STRING_MEMCMP_C_ diff --git a/src/string/memcpy.c b/src/string/memcpy.c @@ -1,6 +1,3 @@ -#ifndef _STRING_MEMCPY_C_ -#define _STRING_MEMCPY_C_ - #include <string.h> void * memcpy (void *dest, const void *src, size_t len) { @@ -11,5 +8,3 @@ void * memcpy (void *dest, const void *src, size_t len) { } return dest; } - -#endif // _STRING_MEMCPY_C_ diff --git a/src/string/memset.c b/src/string/memset.c @@ -1,6 +1,3 @@ -#ifndef _STRING_MEMSET_C_ -#define _STRING_MEMSET_C_ - #include <string.h> void * memset (void *dest, int val, size_t len) { @@ -9,5 +6,3 @@ void * memset (void *dest, int val, size_t len) { *ptr++ = val; return dest; } - -#endif // _STRING_MEMSET_C_ diff --git a/src/string/strlen.c b/src/string/strlen.c @@ -1,13 +1,10 @@ -#ifndef _STRING_H_ -#define _STRING_H_ +#include <string.h> #ifdef __cplusplus extern "C" { #endif -static unsigned int -mini_strlen(const char *s) -{ +static unsigned int strlen(const char *s) { unsigned int len = 0; while (s[len] != '\0') len++; return len; @@ -16,5 +13,3 @@ mini_strlen(const char *s) #ifdef __cplusplus } // extern "C" #endif - -#endif // _STRING_H_ diff --git a/src/unistd/brk.c b/src/unistd/brk.c @@ -0,0 +1,31 @@ +#include <stddef.h> +#include <unistd.h> + +#include <matter/page_size.h> + + +extern unsigned char __heap_base; +void *break_pointer = &__heap_base; + +int brk(void *addr) { + break_pointer = addr; + return 0; +} + +#ifdef __wasm__ + +void * sbrk(ssize_t increment) { + size_t current_pages = __builtin_wasm_memory_size(0); + void *heap_limit = (void *)(current_pages * PAGE_SIZE); + + void *ret = break_pointer; + break_pointer += increment; + + if (break_pointer > heap_limit) { + __builtin_wasm_memory_grow(0, ((break_pointer - heap_limit) / PAGE_SIZE) + 1); + } + + return ret; +} + +#endif // __wasm__