From a58aa54239d5f8c74030acb31a7e25a6fcd52db8 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 24 Nov 2020 15:40:40 +0000 Subject: [PATCH] skel --- .gitignore | 1 + lean/Makefile | 67 +++++++++++++++++++++++++++++++++++---- lean/include/map.h | 4 +-- lean/include/shuffle3.h | 6 ++++ lean/src/main.c | 35 +++++++++++++------- lean/src/map_callback.cpp | 4 +-- 6 files changed, 94 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 58b21ee..28eeec4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ obj build/ test/ +shuffle3-* diff --git a/lean/Makefile b/lean/Makefile index d09401b..2bd57f8 100644 --- a/lean/Makefile +++ b/lean/Makefile @@ -1,12 +1,65 @@ +SRC_C = $(wildcard src/*.c) +SRC_CXX = $(wildcard src/*.cpp) +INCLUDE = include +PROJECT=shuffle3 +COMMON_FLAGS = -Wall -pedantic $(addprefix -I,$(INCLUDE)) -fno-strict-aliasing +OPT_FLAGS?= -fgraphite -march=native +CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors + +CFLAGS += $(COMMON_FLAGS) --std=gnu11 +CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20 -fno-exceptions +LDFLAGS += + +RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) +RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS) +RELEASE_LDFLAGS?= -O3 -flto + +DEBUG_CFLAGS?= -O0 -g -DDEBUG +DEBUG_CXXFLAGS?= $(DEBUG_CFLAGS) +DEBUG_LDFLAGS?= + +# Objects + +OBJ_C = $(addprefix obj/c/,$(SRC_C:.c=.o)) +OBJ_CXX = $(addprefix obj/cxx/,$(SRC_CXX:.cpp=.o)) +OBJ = $(OBJ_C) $(OBJ_CXX) + +# Phonies + +.PHONY: release +release: | dirs $(PROJECT)-release + +.PHONY: debug +debug: | dirs $(PROJECT)-debug + +# Targets + +dirs: + @mkdir -p obj/c{,xx}/src + +obj/c/%.o: %.c + $(CC) -c $< $(CFLAGS) -o $@ $(LDFLAGS) + +obj/cxx/%.o: %.cpp + $(CXX) -c $< $(CXXFLAGS) -o $@ $(LDFLAGS) + + +$(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS) +$(PROJECT)-release: CXXFLAGS += $(RELEASE_CXXFLAGS) +$(PROJECT)-release: LDFLAGS += $(RELEASE_LDFLAGS) +$(PROJECT)-release: $(OBJ) + $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) + strip $@ + +$(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS) +$(PROJECT)-debug: CXXFLAGS += $(DEBUG_CXXFLAGS) +$(PROJECT)-debug: LDFLAGS += $(DEBUG_LDFLAGS) +$(PROJECT)-debug: $(OBJ) + $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) -test: - gcc -c src/*.c -Iinclude -Wall -pedantic --std=gnu11 - g++ -c src/*.cpp -Iinclude -Wall -pedantic --std=gnu++20 - g++ -o $@ *.o - ./$@ clean: - rm -f test - rm -f *.o + rm -rf obj + rm -f $(PROJECT)-{release,debug} diff --git a/lean/include/map.h b/lean/include/map.h index 87534a1..244be43 100644 --- a/lean/include/map.h +++ b/lean/include/map.h @@ -18,8 +18,8 @@ typedef struct mmap { int open_and_map(const char* file, mmap_t* restrict ptr); int unmap_and_close(mmap_t map); -typedef void* (*map_cb)(mmap_t map); -void* map_and_then(const char* file, map_cb callback); +typedef void* (*map_cb)(mmap_t map, void* user); +void* map_and_then(const char* file, map_cb callback, void* user); #ifdef __cplusplus } diff --git a/lean/include/shuffle3.h b/lean/include/shuffle3.h index 75f72e2..a6571d7 100644 --- a/lean/include/shuffle3.h +++ b/lean/include/shuffle3.h @@ -14,9 +14,15 @@ extern "C" { #define _FORCE_INLINE __attribute__((gnu_inline)) extern inline #endif +#ifdef DEBUG +#define dprintf(fmt, ...) printf("[dbg @" __FILE__ "->%s:%d] " fmt "\n", __func__, __LINE__ __VA_OPT__(,) __VA_ARGS__) +#else +#define dprintf(fmt, ...) +#endif #ifdef __cplusplus } + #endif #endif /* _SHUFFLE3_H */ diff --git a/lean/src/main.c b/lean/src/main.c index 330c412..b8f4452 100644 --- a/lean/src/main.c +++ b/lean/src/main.c @@ -11,6 +11,11 @@ _Static_assert(sizeof(float)==sizeof(uint32_t), "float is not 32 bits"); +struct prog_args { + int argc; + char ** argv; +}; + /* static void shuffle_file(const char* filename) { @@ -22,14 +27,29 @@ static void unshuffle_file(const char* filename) panic("unimplemented"); }*/ -static void* map_callback(mmap_t map) +static void* map_callback(mmap_t map, void* user) { - printf("fd %d mapped (sz %lu) (ptr %p)\n", map.fd, map.len, map.ptr); + struct prog_args args = *(struct prog_args*)user; + + dprintf("fd %d mapped (sz %lu) (ptr %p)\n", map.fd, map.len, map.ptr); return NULL; } + +int main(int argc, char** argv) +{ + struct prog_args args = {.argc = argc, .argv = argv}; + + if( argv[1] ) { + map_and_then(argv[1], &map_callback, &args); + } + + return 0; +} + +#ifdef _TEST static void do_test() { char* string = "Hello world.. how are you?????"; @@ -51,13 +71,4 @@ static void do_test() } printf("OK!\n\n"); } - -int main(int argc, char** argv) -{ - do_test(); - if( argv[1] ) { - map_and_then(argv[1], &map_callback); - } - - return 0; -} +#endif diff --git a/lean/src/map_callback.cpp b/lean/src/map_callback.cpp index 8ea87b8..3c3f7f5 100644 --- a/lean/src/map_callback.cpp +++ b/lean/src/map_callback.cpp @@ -4,8 +4,8 @@ #include -extern "C" void* map_and_then(const char* file, map_cb callback) +extern "C" void* map_and_then(const char* file, map_cb callback, void* user) { mm::mmap map(file); - return callback(map.as_raw()); + return callback(map.as_raw(), user); }