From 95ead7e764df405452fcbb04380f836e947e8f55 Mon Sep 17 00:00:00 2001 From: Mosder Date: Thu, 30 Jul 2026 17:26:31 +0200 Subject: feat: initialized the repo and added some blank files --- .gitignore | 1 + Makefile | 35 +++++++++++ README.md | 4 ++ include/moslib.h | 7 +++ include/moslib/ds/array.h | 146 +++++++++++++++++++++++++++++++++++++++++++++ include/moslib/mem/alloc.h | 2 + include/moslib/tests.h | 2 + src/ds/array.c | 0 src/mem/alloc.c | 0 src/tests.c | 0 tests/main.c | 3 + 11 files changed, 200 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 include/moslib.h create mode 100644 include/moslib/ds/array.h create mode 100644 include/moslib/mem/alloc.h create mode 100644 include/moslib/tests.h create mode 100644 src/ds/array.c create mode 100644 src/mem/alloc.c create mode 100644 src/tests.c create mode 100644 tests/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9bf3746 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +compile_commands.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bfdd2f0 --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +CC=gcc +CFLAGS=-std=c99 -Wall -Wextra -I./include -O2 -s +CFLAGS_TEST=-std=c99 -Wall -Wextra -Og -g -fsanitize=address +AR=ar rcs + +both: shared static clean + +shared: compile headers + $(CC) -shared *.o -o libmoslib.so + sudo mv libmoslib.so /usr/local/lib + +static: compile headers + $(AR) libmoslib.a *.o + sudo mv libmoslib.a /usr/local/lib + +headers: + sudo cp -r include/* /usr/local/include + +compile: memory structures + $(CC) $(CFLAGS) -c src/tests.c + +memory: + $(CC) $(CFLAGS) -c src/mem/alloc.c + +structures: + $(CC) $(CFLAGS) -c src/ds/array.c + +test: + $(CC) $(CFLAGS_TEST) tests/main.c -o run_tests + ./run_tests + rm run_tests + +.PHONY: clean +clean: + rm *.o diff --git a/README.md b/README.md new file mode 100644 index 0000000..29e056b --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Personal library for a bunch of useful stuff + +Just a bunch of useful code for me put into a single lib, so I don't have to implement it every single time. +Defnitely not the best implementations ever made, but they're mine, so idc. diff --git a/include/moslib.h b/include/moslib.h new file mode 100644 index 0000000..a781a34 --- /dev/null +++ b/include/moslib.h @@ -0,0 +1,7 @@ +// moslib.h +// Main header file of the library +// Contains all other header files + +#include "moslib/ds/array.h" +#include "moslib/mem/alloc.h" +#include "moslib/tests.h" diff --git a/include/moslib/ds/array.h b/include/moslib/ds/array.h new file mode 100644 index 0000000..a9fb00b --- /dev/null +++ b/include/moslib/ds/array.h @@ -0,0 +1,146 @@ +// moslib/ds/array.h +// Implementation of dynamic array + +// ======================================== QUICK USAGE GUIDE ========================================= +// ---------------------------------------------------------------------------------------------------- +// +// Define the dynamic array of type T as: +// +// T *array = NULL; +// +// Then on the first usage of the array it's gonna be initialized. +// You can use arr_set_cap function right after definition to initalize to the specified capacity. +// +// ---------------------------------------------------------------------------------------------------- +// +// You can access the items simply by array indexing: +// +// T item = array[i]; +// +// You can also get a pointer to an item with these: +// +// T *p_item = arr_get(array, i); +// T *p_item = arr_first(array); +// T *p_item = arr_last(array); +// +// It will provide bounds checking - arr_get returning NULL when index is out of bounds +// - both arr_first and arr_last returning NULL when array is empty +// +// ---------------------------------------------------------------------------------------------------- +// +// To append/push to the array use: +// +// size_t index = arr_append(&array, item); +// +// or: +// +// size_t index = arr_push(&array, item); +// +// It returns the index of the newly appended/pushed item. +// +// To insert into specific index of the array and push items after it to the right use: +// +// size_t index = arr_insert(&array, i, item); +// +// It returns the index of the newly inserted item +// If index is greater or equal to array length then it functions identically to arr_append/arr_push. +// +// To put into specific index (and update if value exists) use: +// +// size_t zerod = arr_put(&array, i, item); +// +// If index is greater or equal to array length it will extend the length to the put item and it will +// zero all items between previously last item to the new item. +// It returns the count of items that were zerod that way. +// +// If you want to add more items than 1 at once, you can use these: +// +// size_t index = arr_append_n(&array, items, n_items); +// size_t index = arr_push_n(&array, items, n_items); +// size_t index = arr_insert_n(&array, i, items, n_items); +// size_t zerod = arr_put_n(&array, i, items, n_items); +// +// They work the same way as their counterparts without n, but accept an array of items instead. +// +// ---------------------------------------------------------------------------------------------------- +// +// To concatenate two dynamic arrays of the same type use: +// +// size_t index = arr_concat(&array1, array2); +// +// First array will be the destination. +// Returns index of the first item of array2 in array1. +// +// ---------------------------------------------------------------------------------------------------- +// +// To delete item from an array at specific index and retrieve it you can use: +// +// T item = arr_pop_i(array, i); +// T item = arr_pop(array); // same as arr_pop_i(array, arr_len(array)-1) +// +// No bounds checking, so it may return garbage or segfault. +// +// You can just delete items on given index with: +// +// size_t deleted = arr_del(array, i); +// +// It will delete and item at given index and move items after to the left. +// Returns count of deleted items - it will be 0 if index is out of bounds. +// It also has an n variant: +// +// size_t deleted = arr_del_n(array, i, n); +// +// It works the same way but deleted n consecutive items instead of 1. +// +// You can also remove from the front or the back: +// +// size_t deleted = arr_del_front(array, n); +// size_t deleted = arr_del_back(array, n); +// +// They work like arr_del_n without needing to specify the indices. +// +// ---------------------------------------------------------------------------------------------------- +// +// You can use these to get a pointer to the previous or next item in array: +// +// T *p_prev_item = arr_prev(array, p_item); +// T *p_next_item = arr_next(array, p_item); +// +// When p_item is NULL it will return pointer to the last/first item. +// When there's no prev/next item - it will return NULL. +// +// ---------------------------------------------------------------------------------------------------- +// +// To get array's current length use: +// +// size_t length = arr_len(array); +// +// You can also set length: +// +// size_t deleted = arr_set_len(&array, new_len); +// +// If it's bigger than current length - it will zero new items. +// If it's smaller than current length - it will delete items. +// Returns amount of items deleted in this way. +// +// ---------------------------------------------------------------------------------------------------- +// +// To get array's current capacity use: +// +// size_t cap = arr_cap(array); +// +// You can also set capacity: +// +// size_t new_cap = arr_cap(&array, target_cap); +// +// When target_cap is lower than array length it will set capacity to array length. +// Returns the new capacity. +// +// ---------------------------------------------------------------------------------------------------- +// +// To free the array from memory use: +// +// arr_free(array); +// +// ---------------------------------------------------------------------------------------------------- +// ===================================== END OF QUICK USAGE GUIDE ===================================== diff --git a/include/moslib/mem/alloc.h b/include/moslib/mem/alloc.h new file mode 100644 index 0000000..c542ea8 --- /dev/null +++ b/include/moslib/mem/alloc.h @@ -0,0 +1,2 @@ +// moslib/mem/alloc.h +// Helper functions for general memory management diff --git a/include/moslib/tests.h b/include/moslib/tests.h new file mode 100644 index 0000000..40fb533 --- /dev/null +++ b/include/moslib/tests.h @@ -0,0 +1,2 @@ +// moslib/tests.h +// Helper functions for writing and running tests diff --git a/src/ds/array.c b/src/ds/array.c new file mode 100644 index 0000000..e69de29 diff --git a/src/mem/alloc.c b/src/mem/alloc.c new file mode 100644 index 0000000..e69de29 diff --git a/src/tests.c b/src/tests.c new file mode 100644 index 0000000..e69de29 diff --git a/tests/main.c b/tests/main.c new file mode 100644 index 0000000..9b6bdc2 --- /dev/null +++ b/tests/main.c @@ -0,0 +1,3 @@ +int main(void) { + return 0; +} -- cgit v1.2.3