diff --git a/.gitignore b/.gitignore index 79daebc..e313e7e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ *.a *.so .vscode -malloc_test +/bld diff --git a/inc/ft_malloc.h b/inc/ft_malloc.h index 162ad46..2ee14f5 100644 --- a/inc/ft_malloc.h +++ b/inc/ft_malloc.h @@ -2,21 +2,6 @@ # define FT_MALLOC_H # include -# include - -# define MIN_ALLOC_UNITS 100 - -typedef struct s_header -{ - struct s_header *next; - size_t units; -} t_header; - -# define UINIT (sizeof(t_header)) - - -static t_header base; -static t_header *free_blocks = NULL; void free(void *ptr); void *malloc(size_t size); diff --git a/inc/meson.build b/inc/meson.build new file mode 100644 index 0000000..f127a63 --- /dev/null +++ b/inc/meson.build @@ -0,0 +1 @@ +install_headers('ft_malloc.h') \ No newline at end of file diff --git a/inc/tests.h b/inc/tests.h deleted file mode 100644 index 6474c2d..0000000 --- a/inc/tests.h +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* tests.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: gtertysh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/23 19:36:58 by gtertysh #+# #+# */ -/* Updated: 2018/10/23 19:40:12 by gtertysh ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef TESTS_H -# define TESTS_H - -int malloc_tests(void); - -#endif \ No newline at end of file diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..3057c41 --- /dev/null +++ b/meson.build @@ -0,0 +1,6 @@ +project('malloc', 'c') + +inc = include_directories('inc') +subdir('inc') +subdir('src') +subdir('t') \ No newline at end of file diff --git a/src/free.c b/src/free.c index be39420..394db36 100644 --- a/src/free.c +++ b/src/free.c @@ -2,28 +2,5 @@ void free(void *used) { - t_header *pivot; - t_header *to_be_freed; - - to_be_freed = (t_header *)used - UINIT; - pivot = free_blocks; - while(!(to_be_freed > pivot && to_be_freed < pivot->next)) - { - if (pivot >= pivot->next && (to_be_freed > pivot || to_be_freed < pivot->next)) - break ; - pivot = pivot->next; - } - if (to_be_freed + to_be_freed->units == pivot->next) - { - to_be_freed->units += pivot->units; - to_be_freed->next = pivot->next->next; - } - else if (pivot + pivot->units == to_be_freed) - { - pivot->units += to_be_freed->units; - pivot->next = to_be_freed->next; - } - else - pivot->next = to_be_freed; - free_blocks = pivot; + return ; } diff --git a/src/malloc.c b/src/malloc.c index b6a80d4..6128222 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -1,54 +1,7 @@ #include "ft_malloc.h" -#include - -t_header *morecore(size_t nuints) -{ - t_header *block; - if (nuints < MIN_ALLOC_UNITS) - nuints = MIN_ALLOC_UNITS; - block = mmap(NULL, nuints * UINIT, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); - if (block == MAP_FAILED) - return (NULL); - block->units = nuints; - free((void *)(block + UINIT)); - return free_blocks; -} void *malloc(size_t size) { - t_header *curr; - t_header *prev; - size_t nunits; - void *space; - - space = NULL; - nunits = (size + UINIT - 1) / (UINIT + 1); - if ((prev = free_blocks) == NULL) - { - base.next = free_blocks = prev = &base; - base.units = 0; - } - while(!space) - { - curr = prev->next; - if (curr->units >= nunits) - { - if (curr->units == nunits) - prev->next = curr->next; - else - { - curr->units -= nunits; - curr += curr->units; - curr->units = nunits; - } - } - free_blocks = prev; - space = (void *)(curr + UINIT); - if (curr == free_blocks) - if ((curr = morecore(nunits)) == NULL) - return (NULL); - prev = curr; - curr = curr->next; - } - return (space); + (void)size; + return (NULL); } \ No newline at end of file diff --git a/src/meson.build b/src/meson.build new file mode 100644 index 0000000..151a4ff --- /dev/null +++ b/src/meson.build @@ -0,0 +1,7 @@ +srcs = [ + 'malloc.c', + 'free.c', + 'realloc.c', +] + +ft_malloc = shared_library('ft_malloc', srcs, include_directories: inc) \ No newline at end of file diff --git a/src/realloc.c b/src/realloc.c new file mode 100644 index 0000000..4c78ac7 --- /dev/null +++ b/src/realloc.c @@ -0,0 +1,6 @@ +#include "ft_malloc.h" + +void *realloc(void *ptr, size_t size) +{ + return (NULL); +} \ No newline at end of file diff --git a/t/malloc_tests.c b/t/malloc_tests.c index 4b2c027..d3fc58b 100644 --- a/t/malloc_tests.c +++ b/t/malloc_tests.c @@ -1,5 +1,4 @@ #include "t.h" -#include "tests.h" int return_null_on_zero_size() { @@ -7,7 +6,7 @@ int return_null_on_zero_size() _END("return null on zero size"); } -int malloc_tests(void) +int main(void) { _SHOULD(return_null_on_zero_size); return 0; diff --git a/t/meson.build b/t/meson.build new file mode 100644 index 0000000..8099e0b --- /dev/null +++ b/t/meson.build @@ -0,0 +1,8 @@ +malloc_tests = executable( + 'malloc_tests', + 'malloc_tests.c', + include_directories: inc, + link_with: ft_malloc, +) + +test('malloc tests', malloc_tests) \ No newline at end of file diff --git a/t/tests.c b/t/tests.c deleted file mode 100644 index 8b73bac..0000000 --- a/t/tests.c +++ /dev/null @@ -1,31 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* tests.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: gtertysh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/10/23 21:00:24 by gtertysh #+# #+# */ -/* Updated: 2018/10/23 21:05:47 by gtertysh ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "t.h" -#include "tests.h" - -int all_tests() -{ - _VERIFY("malloc:", malloc_tests); - return 0; -} - -int main() -{ - int result; - - result = all_tests(); - if (result == 0) - printf("PASSED\n"); - - return result != 0; -} \ No newline at end of file