Allocators' common interface.
Don't use malloc
, free
directly, use allocator instead, to use custom allocator like tcmalloc
, jemalloc
easily
This library depends on libft/memory
for initialize or move memory.
Caution: it's g_default_base_allocator
is NOT compatible with malloc
and free
.
Note: goal of this project is providing base allocator, not custom heaps and/or middleware.
To use it, you need to include its header.
#include <ft/base_allocator.h>
Include *_types.h instead if you don't want included size get too bigger and only need types not function prototypes (e.g. in header files for custom heap and/or middleware)
#include <ft/base_allocator_types.h>
You can use calloc
's functionality using ft_calloc
, and more functions such as ft_xalloc
, ft_rcalloc
, ft_memdup
, but with some more code.
t_base_allocator g_allocator = g_default_base_allocator;
// ...
T *new_array(T initial_value, size_t count)
{
T *result;
int i;
result = (T*)(ft_calloc(g_allocator, count, sizeof(T)));
i = -1;
if (result)
while (++i < count)
result[i] = initial_value;
return (result);
}
You can directly call allocator's function, but malloc
, realloc
, free
is only supported.
t_base_allocator g_allocator = g_default_base_allocator;
// ...
T *new_array(T initial_value, size_t count)
{
T *result;
int i;
result = (T*)(g_allocator->malloc(count * sizeof(T)));
i = -1;
if (result)
while (++i < count)
result[i] = initial_value;
return (result);
}
Any PR is welcome if below kept
- DON'T COMMIT FILES WITH 42 HEADER NOT REMOVED
- If possible, keep the Norm
Run init.sh
to initialize git pre-commit hook.
bash init.sh
Note: POSIX shell is not supported. use bash instead.
It will copy hooks/pre-commit.sh
into .git/hooks/pre-commit
, and set execute permission on it.
File history managed by VCS.
Just let VCS handle it, and stop polluting history :)
This pre-commit script will remove 42 header from Makefile
and *.{c,h,sh,mk}
files
Alternative: run shell command below before every commit, or never touch 42 header
find . -name '*.c' -o -name "*.h" -o -name "*.sh" -o -name "*.mk" -o -name "Makefile" \
| xargs -L1 sh hooks/scripts/42header-remover.sh
It will automatically download Github release assets for dependencies described in the ft_header_deps.properties
file.
After change dependencies, please run build/script/init-header-deps.sh
manually.