Initial commit

This commit is contained in:
2026-05-25 15:15:52 -06:00
commit 50406e3819
6 changed files with 203 additions and 0 deletions

60
.gitignore vendored Normal file
View File

@@ -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
*~

20
include/allocator.h Normal file
View File

@@ -0,0 +1,20 @@
#ifndef ALLOCATOR_H
#define ALLOCATOR_H
#include <stdlib.h>
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

29
src/allocator.c Normal file
View File

@@ -0,0 +1,29 @@
#include "allocator.h"
#include <stdlib.h>
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,
};
}

5
src/main.c Normal file
View File

@@ -0,0 +1,5 @@
#include "allocator.h"
#include <stdio.h>
#include <stdlib.h>
int main(void) { return EXIT_SUCCESS; }

8
test/test_allocator.c Normal file
View File

@@ -0,0 +1,8 @@
#include "allocator.h"
#include <cmocka.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
int main(void) { return EXIT_SUCCESS; }

81
xmake.lua Normal file
View File

@@ -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