add meson

This commit is contained in:
Gregory 2019-05-03 00:18:45 +03:00
parent d1633c62bb
commit a8eb47d8fe
12 changed files with 33 additions and 140 deletions

2
.gitignore vendored
View file

@ -2,4 +2,4 @@
*.a
*.so
.vscode
malloc_test
/bld

View file

@ -2,21 +2,6 @@
# define FT_MALLOC_H
# include <stddef.h>
# include <stdint.h>
# 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);

1
inc/meson.build Normal file
View file

@ -0,0 +1 @@
install_headers('ft_malloc.h')

View file

@ -1,18 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

6
meson.build Normal file
View file

@ -0,0 +1,6 @@
project('malloc', 'c')
inc = include_directories('inc')
subdir('inc')
subdir('src')
subdir('t')

View file

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

View file

@ -1,54 +1,7 @@
#include "ft_malloc.h"
#include <sys/mman.h>
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);
}

7
src/meson.build Normal file
View file

@ -0,0 +1,7 @@
srcs = [
'malloc.c',
'free.c',
'realloc.c',
]
ft_malloc = shared_library('ft_malloc', srcs, include_directories: inc)

6
src/realloc.c Normal file
View file

@ -0,0 +1,6 @@
#include "ft_malloc.h"
void *realloc(void *ptr, size_t size)
{
return (NULL);
}

View file

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

8
t/meson.build Normal file
View file

@ -0,0 +1,8 @@
malloc_tests = executable(
'malloc_tests',
'malloc_tests.c',
include_directories: inc,
link_with: ft_malloc,
)
test('malloc tests', malloc_tests)

View file

@ -1,31 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tests.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}