base for malloc

This commit is contained in:
Gregory 2019-05-04 20:23:32 +03:00
parent d806927b8c
commit 902bc6d7f0
89 changed files with 291 additions and 30 deletions

1
.gitignore vendored
View file

@ -3,4 +3,5 @@
*.so *.so
.vscode .vscode
/bld /bld
/debug_bld
/out /out

View file

@ -2,8 +2,18 @@
PWD=$(pwd) PWD=$(pwd)
OUT_DIR="$PWD/out" OUT_DIR="$PWD/out"
BLD_DIR="$PWD/bld" RELEASE_BLD_DIR="$PWD/bld"
DEBUG_BLD_DIR="$PWD/debug_bld"
meson $BLD_DIR --includedir=$OUT_DIR --libdir=$OUT_DIR --bindir=$OUT_DIR --prefix=$OUT_DIR LIB="libft_malloc.dylib"
ninja -C $BLD_DIR LINK="libft_malloc.so"
ninja -C $BLD_DIR install
meson $RELEASE_BLD_DIR --prefix=$OUT_DIR --libdir=$OUT_DIR --includedir=$OUT_DIR --buildtype=release
meson $DEBUG_BLD_DIR --buildtype=debug
ninja -C $DEBUG_BLD_DIR
ninja -C $RELEASE_BLD_DIR
ninja -C $RELEASE_BLD_DIR install
ln -sf "$OUT_DIR/$LIB" "$PWD/$LINK"

View file

@ -6,5 +6,6 @@
void free(void *ptr); void free(void *ptr);
void *malloc(size_t size); void *malloc(size_t size);
void *realloc(void *ptr, size_t size); void *realloc(void *ptr, size_t size);
void show_alloc_mem(void);
#endif #endif

37
inc/ft_malloc_internal.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef FT_MALLOC_INTERNAL_H
# define FT_MALLOC_INTERNAL_H
# include <stddef.h>
# include <stdint.h>
# define TINY 512
# define SMALL 1024
# define LARGE 1025
# define START -1
# define MAGIC 0xDEADBEEF
# define NALLOC 100
typedef struct s_chunk
{
uint8_t is_free;
size_t size;
struct s_chunk *next;
struct s_chunk *prev;
uint32_t magic;
} t_chunk;
# define CHUNK_SIZE(size) ((size) + sizeof(t_chunk))
typedef struct s_arena
{
int type;
size_t size;
struct s_arena *next;
t_chunk *heap;
} t_arena;
# define HEAP_SIZE(size) ((size) - sizeof(t_arena))
extern t_arena g_base;
#endif

View file

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

View file

@ -2,7 +2,7 @@ project(
'malloc', 'malloc',
'c', 'c',
default_options: [ default_options: [
'buildtype=release', 'buildtype=plain',
], ],
) )
@ -10,7 +10,6 @@ add_global_arguments(
'-Wall', '-Wall',
'-Wextra', '-Wextra',
'-Werror', '-Werror',
'-Wpointer-arith',
'-Wcast-align', '-Wcast-align',
'-Wwrite-strings', '-Wwrite-strings',
'-Wunreachable-code', '-Wunreachable-code',
@ -24,6 +23,25 @@ add_global_arguments(
) )
inc = include_directories('inc') inc = include_directories('inc')
subdir('inc')
subdir('src') install_headers('inc/ft_malloc.h')
sources = [
'src/malloc.c',
'src/free.c',
'src/realloc.c',
]
libft = subproject('libft')
libft_dep = libft.get_variable('libft_dep')
ft_malloc = shared_library(
'ft_malloc',
sources,
include_directories: inc,
dependencies: libft_dep,
install: true,
)
subdir('t') subdir('t')

View file

@ -1,7 +1,106 @@
#include <sys/mman.h>
#include "ft_malloc.h" #include "ft_malloc.h"
#include "ft_malloc_internal.h"
#include "libft.h"
void *malloc(size_t size) t_arena g_base = { .type = START, .size = 0, .next = &g_base, .heap = NULL};
static void add_arena(t_arena *arena, size_t size, int arena_type)
{ {
(void)size; arena->size = size;
return (NULL); arena->type = arena_type;
arena->heap = (t_chunk *)(arena + 1);
arena->heap->is_free = 1;
arena->heap->magic = MAGIC;
arena->heap->size = HEAP_SIZE(size);
arena->heap->next = NULL;
arena->heap->prev = NULL;
arena->next = g_base.next;
g_base.next = arena;
}
static size_t get_actual_size(size_t size, int type)
{
size_t minimum;
int page;
page = getpagesize();
if (type == LARGE)
minimum = size;
else
minimum = (type * NALLOC);
return ((minimum + sizeof(t_arena) + page - 1) / page * page);
}
static int get_arena_type(size_t size)
{
if (size <= TINY)
return (TINY);
if (size <= SMALL)
return (SMALL);
return (LARGE);
}
static t_arena *get_more_arena(size_t size, int type)
{
t_arena *arena;
int prot;
int flags;
size = get_actual_size(size, type);
prot = PROT_WRITE | PROT_READ;
flags = MAP_ANON | MAP_PRIVATE;
if ((arena = mmap(NULL, size, prot, flags, -1, 0)) == MAP_FAILED)
return (NULL);
add_arena(arena, size, type);
return (arena);
}
static t_chunk *chunk_heap(t_chunk *chunk, size_t size)
{
t_chunk *new_chunk;
if (chunk->size == size)
{
chunk->is_free = 0;
return (chunk);
}
chunk->size -= size;
new_chunk = (t_chunk *)((uint8_t *)chunk + chunk->size);
new_chunk->is_free = 0;
new_chunk->magic = MAGIC;
new_chunk->size = size;
new_chunk->next = chunk->next;
new_chunk->prev = chunk;
if (new_chunk->next)
new_chunk->next->prev = new_chunk;
return (chunk);
}
void *malloc(size_t size)
{
void *space;
t_arena *arena;
t_chunk *chunk;
int arena_type;
space = NULL;
size = CHUNK_SIZE(size);
arena = g_base.next;
arena_type = get_arena_type(size);
while(!space)
{
if(arena->type == arena_type && (chunk = arena->heap))
while(!space && chunk)
{
if (chunk->size >= size && chunk->is_free)
space = chunk_heap(chunk, size);
chunk = chunk->next;
}
arena = arena->next;
if (!space && arena == &g_base)
if ((arena = get_more_arena(size, arena_type)) == NULL)
return (NULL);
}
return (space + 1);
} }

View file

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

View file

@ -12,7 +12,7 @@
#include "libft.h" #include "libft.h"
static t_list *merge(t_list *a, t_list *b, int (*cmp)()) static t_list *merge(t_list *a, t_list *b, int (*cmp)(void *, void *))
{ {
t_list *result; t_list *result;
@ -63,7 +63,7 @@ static void split(t_list *source, t_list **front_ptr, t_list **back_ptr)
} }
} }
void ft_lst_merge_sort(t_list **head_ptr, int (*cmp)()) void ft_lst_merge_sort(t_list **head_ptr, int (*cmp)(void *, void *))
{ {
t_list *head; t_list *head;
t_list *a; t_list *a;

View file

@ -0,0 +1,95 @@
project('libft', 'c')
srcs = [
'ft_memset.c',
'ft_bzero.c',
'ft_memcpy.c',
'ft_memccpy.c',
'ft_memmove.c',
'ft_memchr.c',
'ft_memcmp.c',
'ft_strlen.c',
'ft_strdup.c',
'ft_strcpy.c',
'ft_strncpy.c',
'ft_strcat.c',
'ft_strncat.c',
'ft_strlcat.c',
'ft_strchr.c',
'ft_strrchr.c',
'ft_strstr.c',
'ft_strnstr.c',
'ft_strcmp.c',
'ft_strcmp_lex.c',
'ft_strncmp.c',
'ft_strtol.c',
'ft_atoi.c',
'ft_atof.c',
'ft_isalpha.c',
'ft_isspace.c',
'ft_isdigit.c',
'ft_isalnum.c',
'ft_isascii.c',
'ft_isprint.c',
'ft_toupper.c',
'ft_tolower.c',
'ft_memalloc.c',
'ft_memdel.c',
'ft_strnew.c',
'ft_strdel.c',
'ft_strclr.c',
'ft_striter.c',
'ft_striteri.c',
'ft_strmap.c',
'ft_strmap.c',
'ft_strmapi.c',
'ft_strequ.c',
'ft_strnequ.c',
'ft_strsub.c',
'ft_strjoin.c',
'ft_strtrim.c',
'ft_strsplit.c',
'ft_itoa.c',
'ft_putchar.c',
'ft_putstr.c',
'ft_putendl.c',
'ft_putnbr.c',
'ft_putchar_fd.c',
'ft_putstr_fd.c',
'ft_putendl_fd.c',
'ft_putnbr_fd.c',
'ft_lstnew.c',
'ft_lstdelone.c',
'ft_lstdel.c',
'ft_lstadd.c',
'ft_lstiter.c',
'ft_lstmap.c',
'ft_lstreduce.c',
'ft_lststrsplit.c',
'ft_lstfind.c',
'ft_lst_at.c',
'ft_lstadd_back.c',
'ft_lst_len.c',
'ft_lst_merge_sort.c',
'ft_lst_rev.c',
'ft_lst_search.c',
'ft_realloc.c',
'ft_read_file.c',
'get_next_line.c',
'ft_num_len.c',
'ft_str_table_len.c',
]
inc = include_directories('includes')
libft = static_library(
'ft',
srcs,
include_directories: inc,
# install : true,
)
libft_dep = declare_dependency(
include_directories : inc,
link_with : libft,
)

View file

@ -1,13 +1,26 @@
#include "t.h" #include "t.h"
#include "ft_malloc.h"
#include "ft_malloc_internal.h"
int return_null_on_zero_size(void)
int inital_base_next_point_to_itself(void)
{ {
_IS(1 == 1); _IS(g_base.next == &g_base);
_END("return null on zero size"); _END("inital_base_next_point_to_itself");
}
int returns_not_null_pointer(void)
{
void *ptr;
ptr = malloc(10);
_IS(ptr != NULL);
_END("returns_not_null_pointer");
} }
int main(void) int main(void)
{ {
_SHOULD(return_null_on_zero_size); _SHOULD(inital_base_next_point_to_itself);
_SHOULD(returns_not_null_pointer);
return 0; return 0;
} }