commit 50406e381949c0e490492af6cdb260779aca28b9 Author: laentropia Date: Mon May 25 15:15:52 2026 -0600 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ce85e8f --- /dev/null +++ b/.gitignore @@ -0,0 +1,60 @@ +# Directories +build/ +build-*/ +cmake-build-*/ +.cache/ +out/ +out/Debug/ +out/Release/ + +# xmake files +.xmake +.xmake/ +.xmake/* + +# Cmake files +CMakeCache.txt +cmake +CMakeFiles/ +cmake_install.cmake +CTestTestfile.cmake +Testing/ +compile_commands.json +../compile_commands.json +build/compile_commands.json + +# Make / Ninja +Makefile +*.ninja +*.ninja_deps +*.ninja_log +rules.ninja + +# Object files +*.o +*.obj +*.lo +*.la + +# Binaries +*.out +*.exe +*.dll +*.so +*.so.* +*.dylib +*.a + +# Debug / Sanitizer +*.dSYM/ +*.gcno +*.gcda +*.gcov + +# Editors +.vscode/ +.idea/ +*.swp +*.swo +*~ + diff --git a/include/allocator.h b/include/allocator.h new file mode 100644 index 0000000..4228033 --- /dev/null +++ b/include/allocator.h @@ -0,0 +1,20 @@ +#ifndef ALLOCATOR_H +#define ALLOCATOR_H + +#include + +typedef struct Allocator Allocator; + +Allocator allocator_default(void); + +struct Allocator { + void *ctx; + + void *(*alloc)(void *ctx, size_t size); + + void *(*realloc)(void *ctx, void *ptr, size_t old_size, size_t new_size); + + void (*free)(void *ctx, void *ptr, size_t size); +}; + +#endif diff --git a/src/allocator.c b/src/allocator.c new file mode 100644 index 0000000..2b23cd7 --- /dev/null +++ b/src/allocator.c @@ -0,0 +1,29 @@ +#include "allocator.h" +#include + +static void *stdlib_alloc(void *ctx, size_t size) { + (void)ctx; + return malloc(size); +} + +static void *stdlib_realloc(void *ctx, void *ptr, size_t old_size, + size_t new_size) { + (void)ctx; + (void)old_size; + return realloc(ptr, new_size); +} + +static void stdlib_free(void *ctx, void *ptr, size_t size) { + (void)ctx; + (void)size; + free(ptr); +} + +Allocator allocator_default(void) { + return (Allocator){ + .ctx = NULL, + .alloc = stdlib_alloc, + .realloc = stdlib_realloc, + .free = stdlib_free, + }; +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..e0b3275 --- /dev/null +++ b/src/main.c @@ -0,0 +1,5 @@ +#include "allocator.h" +#include +#include + +int main(void) { return EXIT_SUCCESS; } diff --git a/test/test_allocator.c b/test/test_allocator.c new file mode 100644 index 0000000..9d49529 --- /dev/null +++ b/test/test_allocator.c @@ -0,0 +1,8 @@ +#include "allocator.h" +#include +#include +#include +#include +#include + +int main(void) { return EXIT_SUCCESS; } diff --git a/xmake.lua b/xmake.lua new file mode 100644 index 0000000..6d1ea17 --- /dev/null +++ b/xmake.lua @@ -0,0 +1,81 @@ +set_project("allocator") +set_version("1.0.0") + +set_languages("c11") + +add_rules("mode.debug", "mode.release") + +-- Export compile_commands.json +add_rules("plugin.compile_commands.autoupdate") + +-- ========================================================= +-- Options +-- ========================================================= + +option("tests") +set_default(false) +set_showmenu(true) +option_end() + +option("example") +set_default(true) +set_showmenu(true) +option_end() + +option("asan") +set_default(false) +set_showmenu(true) +option_end() + +-- ========================================================= +-- Library +-- ========================================================= + +target("allocator") +set_kind("static") + +add_files("src/allocator.c") + +add_headerfiles("include/*.h") + +add_includedirs("include", { public = true }) + +if is_mode("debug") then + add_cflags("-Wall", "-Wextra", "-Wpedantic") +end + +-- ========================================================= +-- Example executable +-- ========================================================= + +if has_config("example") then + target("example") + set_kind("binary") + + add_files("src/main.c") + + add_deps("allocator") +end + +-- ========================================================= +-- Tests +-- ========================================================= + +if has_config("tests") then + add_requires("cmocka") + + target("test_mylib") + set_kind("binary") + + add_files("test/test_allocator.c") + + add_deps("allocator") + + add_packages("cmocka") + + if has_config("asan") then + add_cflags("-fsanitize=address", "-fno-omit-frame-pointer") + + add_ldflags("-fsanitize=address") + end +end