brzeszczot

brzeszczot.git
git clone git://git.lenczewski.org/brzeszczot.git
Log | Files | Refs

commit 534459ac04b07611e39a7e88a07c541751327490
Author: MikoĊ‚aj Lenczewski <mblenczewski@gmail.com>
Date:   Sat, 11 Feb 2023 22:30:00 +0000

Initial commit

Diffstat:
A.editorconfig | 17+++++++++++++++++
A.gitignore | 10++++++++++
ALICENSE.txt | 0
AMakefile | 15+++++++++++++++
AREADME.txt | 2++
Abrzeszczot/include/brzeszczot.h | 8++++++++
Abrzeszczot/makefile.mk | 44++++++++++++++++++++++++++++++++++++++++++++
Abrzeszczot/src/brzeszczot.c | 11+++++++++++
Aconfig.mk | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Adocs/README.txt | 2++
Adocs/brzeszczot/README.txt | 2++
Adocs/libriot/README.txt | 2++
Ainclude/common.h | 126+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ainclude/test.h | 40++++++++++++++++++++++++++++++++++++++++
Ainclude/utils.h | 149+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alibriot/include/libriot.h | 6++++++
Alibriot/makefile.mk | 44++++++++++++++++++++++++++++++++++++++++++++
Alibriot/src/libriot.c | 1+
18 files changed, 530 insertions(+), 0 deletions(-)

diff --git a/.editorconfig b/.editorconfig @@ -0,0 +1,17 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +charset = utf-8 + +guidelines = 80, 120, 160 + +[*.{c,cpp,h,hpp,tpp}] +indent_style = tab +indent_size = 8 + +[*.{conf,json,md,txt}] +indent_style = space +indent_size = 2 diff --git a/.gitignore b/.gitignore @@ -0,0 +1,10 @@ +bin/ +lib/ +obj/ + +*.bin +*.log + +**/.*.swp + +.vs/ diff --git a/LICENSE.txt b/LICENSE.txt diff --git a/Makefile b/Makefile @@ -0,0 +1,15 @@ +.PHONY: all build clean test + +all: build test + +include config.mk + +clean: + rm -fr $(BIN) $(LIB) $(OBJ) + +build: libriot-build brzeszczot-build + +test: libriot-test brzeszczot-test + +include libriot/makefile.mk +include brzeszczot/makefile.mk diff --git a/README.txt b/README.txt @@ -0,0 +1,2 @@ +brzeszczot +============================================================================== diff --git a/brzeszczot/include/brzeszczot.h b/brzeszczot/include/brzeszczot.h @@ -0,0 +1,8 @@ +#ifndef BRZESZCZOT_H +#define BRZESZCZOT_H + +#include "common.h" + +#include "libriot.h" + +#endif /* BRZESZCZOT_H */ diff --git a/brzeszczot/makefile.mk b/brzeszczot/makefile.mk @@ -0,0 +1,44 @@ +.PHONY: brzeszczot brzeszczot-build brzeszczot-test + +BRZESZCZOT_MAJOR := 0 +BRZESZCZOT_MINOR := 1 +BRZESZCZOT_PATCH := 0 +BRZESZCZOT_VERSION := $(BRZESZCZOT_MAJOR).$(BRZESZCZOT_MINOR).$(BRZESZCZOT_PATCH) + +BRZESZCZOT_CFLAGS := \ + $(CFLAGS) \ + $(CPPFLAGS) \ + -DBRZESZCZOT_VERSION_MAJOR="\"$(BRZESZCZOT_MAJOR)"\" \ + -DBRZESZCZOT_VERSION_MINOR="\"$(BRZESZCZOT_MINOR)"\" \ + -DBRZESZCZOT_VERSION_PATCH="\"$(BRZESZCZOT_PATCH)"\" \ + -DBRZESZCZOT_VERSION="\"$(BRZESZCZOT_VERSION)"\" \ + -Ibrzeszczot/include -Ilibriot/include + +BRZESZCZOT_FLAGS := \ + $(BRZESZCZOT_CFLAGS) \ + $(LDFLAGS) -lriot + +BRZESZCZOT_SOURCES := brzeszczot/src/brzeszczot.c + +BRZESZCZOT_OBJECTS := $(BRZESZCZOT_SOURCES:%.c=$(OBJ)/%.c.o) +BRZESZCZOT_OBJDEPS := $(BRZESZCZOT_OBJECTS:%.o=%.d) + +-include $(BRZESZCZOT_OBJDEPS) + +$(BRZESZCZOT_OBJECTS): $(OBJ)/%.c.o: %.c | $(OBJ) + @mkdir -p $(dir $@) + $(CC) -MMD -o $@ -c $< $(BRZESZCZOT_CFLAGS) + +$(BIN)/brzeszczot: brzeszczot-deps $(BRZESZCZOT_OBJECTS) | $(BIN) + @mkdir -p $(dir $@) + $(CC) -static -o $@ $(wordlist 2,$(words $^),$^) $(BRZESZCZOT_FLAGS) + +brzeszczot-deps: $(LIB)/libriot.a + +brzeszczot-build: $(BIN)/brzeszczot + +brzeszczot-test-deps: + +brzeszczot-test: + +brzeszczot: brzeszczot-build brzeszczot-test diff --git a/brzeszczot/src/brzeszczot.c b/brzeszczot/src/brzeszczot.c @@ -0,0 +1,11 @@ +#include "brzeszczot.h" + +s32 +main(s32 argc, char **argv) { + (void) argc; + (void) argv; + + dbglog("Version: " BRZESZCZOT_VERSION "\n"); + + return 0; +} diff --git a/config.mk b/config.mk @@ -0,0 +1,51 @@ +## toolchain +CC ?= cc +CXX ?= c++ +AR ?= ar +RANLIB ?= ranlib + +STRIP ?= strip + +TAR ?= tar +ZIP ?= gzip + +## project build directories +BIN := bin +OBJ := obj +LIB := lib +TST := $(BIN)/tests + +$(BIN): + @mkdir -p $(BIN) + +$(OBJ): + @mkdir -p $(OBJ) + +$(LIB): + @mkdir -p $(LIB) + +$(TST): $(BIN) + @mkdir -p $(TST) + +## project-wide includes +INC := include + +## append project-wide toolchain flags +CFLAGS := $(CFLAGS) -std=c11 -Wall -Wextra -Wpedantic -Werror +CPPFLAGS := $(CPPFLAGS) -I$(INC) -D_XOPEN_SOURCE=700 +LDFLAGS := $(LDFLAGS) -L$(LIB) + +REGIME ?= DEBUG + +DEBUG_CFLAGS := $(DEBUG_CFLAGS) -Og -ggdb +DEBUG_CPPFLAGS := $(DEBUG_CPPFLAGS) -UNDEBUG +DEBUG_LDFLAGS := $(DEBUG_LDFLAGS) + +RELEASE_CFLAGS := $(RELEASE_CFLAGS) -O3 -flto +RELEASE_CPPFLAGS:= $(RELEASE_CPPFLAGS) -DNDEBUG +RELEASE_LDFLAGS := $(RELEASE_LDFLAGS) -flto + +## append regime-specific toolchain flags +CFLAGS := $(CFLAGS) $($(REGIME)_CFLAGS) +CPPFLAGS := $(CPPFLAGS) $($(REGIME)_CPPFLAGS) +LDFLAGS := $(LDFLAGS) $($(REGIME)_LDFLAGS) diff --git a/docs/README.txt b/docs/README.txt @@ -0,0 +1,2 @@ +brzeszczot +============================================================================== diff --git a/docs/brzeszczot/README.txt b/docs/brzeszczot/README.txt @@ -0,0 +1,2 @@ +brzeszczot +============================================================================== diff --git a/docs/libriot/README.txt b/docs/libriot/README.txt @@ -0,0 +1,2 @@ +libriot +============================================================================== diff --git a/include/common.h b/include/common.h @@ -0,0 +1,126 @@ +#ifndef COMMON_H +#define COMMON_H + +#ifdef __CPLUSPLUS +extern "C" { +#endif /* __CPLUSPLUS */ + +#ifdef __CPLUSPLUS + #include <cassert> + #include <cerrno> + #include <cfloat> + #include <climits> + #include <csetjmp> + #include <cstdalign> + #include <cstdatomic> + #include <cstdarg> + #include <cstdbool> + #include <cstddef> + #include <cstdint> + #include <cstdio> + #include <cstdlib> + #include <cstdnoreturn> + #include <cstring> +#else + #include <assert.h> + #include <errno.h> + #include <float.h> + #include <limits.h> + #include <setjmp.h> + #include <stdalign.h> + #include <stdatomic.h> + #include <stdarg.h> + #include <stdbool.h> + #include <stddef.h> + #include <stdint.h> + #include <stdio.h> + #include <stdlib.h> + #include <stdnoreturn.h> + #include <string.h> +#endif + +typedef int32_t b32; + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + +typedef float f32; +typedef double f64; + +#define KiB 1024ULL +#define MiB 1024 * KiB +#define GiB 1024 * MiB + +#define ARRLEN(arr) (sizeof(arr) / sizeof((arr)[0])) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define BITS_SET(value, mask) (((value) & mask) == (mask)) + +#define RELPTR_MASK(relptr_ty) ((relptr_ty)1 << ((sizeof(relptr_ty) * 8) - 1)) +#define RELPTR_NULL (0) + +#define RELPTR_ENC(relptr_ty, ptroff) \ + ((relptr_ty)((ptroff) ^ RELPTR_MASK(relptr_ty))) + +#define RELPTR_DEC(relptr_ty, relptr) \ + ((relptr_ty)((relptr) ^ RELPTR_MASK(relptr_ty))) + +#define RELPTR_ABS2REL(relptr_ty, base, absptr) \ + ((absptr) \ + ? RELPTR_ENC(relptr_ty, (u8*)absptr - (u8*)base) \ + : RELPTR_NULL) + +#define RELPTR_REL2ABS(absptr_ty, relptr_ty, base, relptr) \ + ((relptr) \ + ? ((absptr_ty)((u8*)base + RELPTR_DEC(relptr_ty, relptr))) \ + : NULL) + +#define errloc() fprintf(stderr, "%s:%d:%s: ", __FILE__, __LINE__, __func__); + +#define errlog(...) \ +do { \ + errloc(); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, "\n"); \ + fflush(stderr); \ +} while (0); + +#define unreachable(...) \ +do { \ + errloc(); \ + fprintf(stderr, "UNREACHABLE: "); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, "\n"); \ + fflush(stderr); \ + abort(); \ +} while (0); + +#define unimplemented(...) \ +do { \ + errloc(); \ + fprintf(stderr, "UNIMPLEMENTED: "); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, "\n"); \ + fflush(stderr); \ + abort(); \ +} while (0); + +#ifndef NDEBUG + #define dbglog(...) errlog(__VA_ARGS__) +#else + #define dbglog(...) +#endif + +#ifdef __CPLUSPLUS +}; +#endif /* __CPLUSPLUS */ + +#endif /* COMMON_H */ diff --git a/include/test.h b/include/test.h @@ -0,0 +1,40 @@ +#ifndef TEST_H +#define TEST_H + +#include "common.h" + +#ifdef __CPLUSPLUS +extern "C" { +#endif /* __CPLUSPLUS */ + +#define TEST_PASS() return 1; +#define TEST_FAIL() return 0; + +#define TESTS_BEGIN() s32 __test_suite_result = 0; +#define TESTS_END() return __test_suite_result; + +#define TEST_RUN(name) \ +do { \ + errlog("%s:%s:", __FILE__, #name); \ + if (name()) { \ + errlog("%s:%s: OK", __FILE__, #name); \ + } else { \ + errlog("%s:%s: FAILED", __FILE__, #name); \ + __test_suite_result = 1; \ + } \ +} while (0); + +#define _TEST_ASSERT_IMPL(cond, msg) \ +errlog("[%s:%d] %s: %s\n", __func__, __LINE__, #cond, msg) + +#define TEST_ASSERT(cond, msg) \ +if (!(cond)) { _TEST_ASSERT_IMPL(cond, msg); TEST_FAIL() } + +#define TEST_EXPECT(cond, msg) \ +if (!(cond)) { _TEST_ASSERT_IMPL(cond, msg); } + +#ifdef __CPLUSPLUS +}; +#endif /* __CPLUSPLUS */ + +#endif /* TEST_H */ diff --git a/include/utils.h b/include/utils.h @@ -0,0 +1,149 @@ +#ifndef UTILS_H +#define UTILS_H + +#include "common.h" + +#ifdef __CPLUSPLUS +extern "C" { +#endif /* __CPLUSPLUS */ + +struct str_view { + char *ptr; + u64 len; +}; + +static inline bool +str_view_equal(struct str_view a, struct str_view b) { + return a.len == b.len && strncmp(a.ptr, b.ptr, a.len) == 0; +} + +#define CONST_CSTR_SV(ccstr) \ + ((struct str_view){ .ptr = ccstr, .len = sizeof(ccstr) - 1, }) + +#define CSTR_SV(cstr) \ + ((struct str_view){ .ptr = cstr, .len = strlen(cstr), }) + +struct mem_stream { + u8 *ptr; + u64 cur, len; +}; + +static inline bool +mem_stream_eof(struct mem_stream *self) { + assert(self); + + return self->len == self->cur; +} + +static inline u8 * +mem_stream_headptr(struct mem_stream *self) { + assert(self); + + return self->ptr + self->cur; +} + +static inline bool +mem_stream_skip(struct mem_stream *self, u64 len) { + assert(self); + + if (self->len - self->cur < len) + return false; + + self->cur += len; + + return true; +} + +static inline bool +mem_stream_peek(struct mem_stream *self, u64 off, void *buf, u64 len) { + assert(self); + assert(buf); + + if (self->len - off - self->cur < len) + return false; + + memcpy(buf, self->ptr + self->cur + off, len); + + return true; +} + +static inline bool +mem_stream_consume(struct mem_stream *self, void *buf, u64 len) { + assert(self); + assert(buf); + + return mem_stream_peek(self, 0, buf, len) && mem_stream_skip(self, len); +} + +struct mem_pool { + u8 *ptr; + u64 cap, len; +}; + +static inline bool +mem_pool_resize(struct mem_pool *self, u64 capacity) { + assert(self); + + u8 *ptr = realloc(self->ptr, capacity); + if (!ptr) return false; + + self->ptr = ptr; + self->cap = capacity; + + return true; +} + +static inline bool +mem_pool_init(struct mem_pool *self, u64 capacity) { + assert(self); + + self->ptr = NULL; + self->cap = self->len = 0; + + return mem_pool_resize(self, capacity); +} + +static inline void +mem_pool_free(struct mem_pool *self) { + assert(self); + + free(self->ptr); +} + +static inline void +mem_pool_reset(struct mem_pool *self) { + assert(self); + + self->len = 0; +} + +static inline bool +mem_pool_prealloc(struct mem_pool *self, u64 size) { + assert(self); + + return self->len + size <= self->cap || mem_pool_resize(self, self->len + size); +} + +static inline void * +mem_pool_alloc(struct mem_pool *self, u64 alignment, u64 size) { + assert(self); + assert(alignment); + assert(alignment == 1 || alignment % 2 == 0); + + u64 alignment_off = alignment - 1; + u64 aligned_len = (self->len + alignment_off) & ~alignment_off; + + if (!mem_pool_prealloc(self, (aligned_len - self->len) + size)) + return NULL; + + void *ptr = self->ptr + aligned_len; + self->len = aligned_len + size; + + return ptr; +} + +#ifdef __CPLUSPLUS +}; +#endif /* __CPLUSPLUS */ + +#endif /* UTILS_H */ diff --git a/libriot/include/libriot.h b/libriot/include/libriot.h @@ -0,0 +1,6 @@ +#ifndef LIBRIOT_H +#define LIBRIOT_H + +#include "common.h" + +#endif /* LIBRIOT_H */ diff --git a/libriot/makefile.mk b/libriot/makefile.mk @@ -0,0 +1,44 @@ +.PHONY: libriot libriot-build libriot-test + +LIBRIOT_MAJOR := 0 +LIBRIOT_MINOR := 1 +LIBRIOT_PATCH := 0 +LIBRIOT_VERSION := $(LIBRIOT_MAJOR).$(LIBRIOT_MINOR).$(LIBRIOT_PATCH) + +LIBRIOT_CFLAGS := \ + $(CFLAGS) \ + $(CPPFLAGS) \ + -DLIBRIOT_VERSION_MAJOR="\"$(LIBRIOT_MAJOR)"\" \ + -DLIBRIOT_VERSION_MINOR="\"$(LIBRIOT_MINOR)"\" \ + -DLIBRIOT_VERSION_PATCH="\"$(LIBRIOT_PATCH)"\" \ + -DLIBRIOT_VERSION="\"$(LIBRIOT_VERSION)"\" \ + -Ilibriot/include + +LIBRIOT_FLAGS := \ + $(LIBRIOT_CFLAGS) \ + $(LDFLAGS) + +LIBRIOT_SOURCES := libriot/src/libriot.c + +LIBRIOT_OBJECTS := $(LIBRIOT_SOURCES:%.c=$(OBJ)/%.c.o) +LIBRIOT_OBJDEPS := $(LIBRIOT_OBJECTS:%.o=%.d) + +-include $(LIBRIOT_OBJDEPS) + +$(LIBRIOT_OBJECTS): $(OBJ)/%.c.o: %.c | $(OBJ) + @mkdir -p $(shell dirname $@) + $(CC) -MMD -o $@ -c $< $(LIBRIOT_CFLAGS) + +$(LIB)/libriot.a: libriot-deps $(LIBRIOT_OBJECTS) | $(LIB) + @mkdir -p $(shell dirname $@) + $(AR) -rcs $@ $(wordlist 2,$(words $^),$^) + +libriot-deps: + +libriot-build: $(LIB)/libriot.a + +libriot-test-deps: + +libriot-test: + +libriot: libriot-build libriot-test diff --git a/libriot/src/libriot.c b/libriot/src/libriot.c @@ -0,0 +1 @@ +#include "libriot.h"