Avril 4 years ago
parent 57b01b78f3
commit a58aa54239
Signed by: flanchan
GPG Key ID: 284488987C31F630

1
.gitignore vendored

@ -2,3 +2,4 @@
obj obj
build/ build/
test/ test/
shuffle3-*

@ -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: clean:
rm -f test rm -rf obj
rm -f *.o rm -f $(PROJECT)-{release,debug}

@ -18,8 +18,8 @@ typedef struct mmap {
int open_and_map(const char* file, mmap_t* restrict ptr); int open_and_map(const char* file, mmap_t* restrict ptr);
int unmap_and_close(mmap_t map); int unmap_and_close(mmap_t map);
typedef void* (*map_cb)(mmap_t map); typedef void* (*map_cb)(mmap_t map, void* user);
void* map_and_then(const char* file, map_cb callback); void* map_and_then(const char* file, map_cb callback, void* user);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -14,9 +14,15 @@ extern "C" {
#define _FORCE_INLINE __attribute__((gnu_inline)) extern inline #define _FORCE_INLINE __attribute__((gnu_inline)) extern inline
#endif #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 #ifdef __cplusplus
} }
#endif #endif
#endif /* _SHUFFLE3_H */ #endif /* _SHUFFLE3_H */

@ -11,6 +11,11 @@
_Static_assert(sizeof(float)==sizeof(uint32_t), "float is not 32 bits"); _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) static void shuffle_file(const char* filename)
{ {
@ -22,14 +27,29 @@ static void unshuffle_file(const char* filename)
panic("unimplemented"); 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; 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() static void do_test()
{ {
char* string = "Hello world.. how are you?????"; char* string = "Hello world.. how are you?????";
@ -51,13 +71,4 @@ static void do_test()
} }
printf("OK!\n\n"); printf("OK!\n\n");
} }
#endif
int main(int argc, char** argv)
{
do_test();
if( argv[1] ) {
map_and_then(argv[1], &map_callback);
}
return 0;
}

@ -4,8 +4,8 @@
#include <map.h> #include <map.h>
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); mm::mmap map(file);
return callback(map.as_raw()); return callback(map.as_raw(), user);
} }

Loading…
Cancel
Save