Put the custom allocator in its own lib, as to lessen the difficulty to switch between libc's allocator and a custom one (#7)

This commit is contained in:
Maix0 2024-05-14 18:56:53 +02:00 committed by GitHub
parent 713f0f0302
commit cb7f3c3fdf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
85 changed files with 1121 additions and 877 deletions

96
allocator/Makefile Normal file
View file

@ -0,0 +1,96 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/03 13:20:01 by maiboyer #+# #+# #
# Updated: 2024/05/14 18:44:53 by maiboyer ### ########.fr #
# #
# **************************************************************************** #
BUILD_DIR ?= build
SRC_DIR = src
INCLUDE_DIR = include output/include ../stdme/include
LIBS_DIR = .
GENERIC_DIR = output/src
GENERIC_INCLUDE = output/include
BASE_PATH ?= $(shell pwd)
NAME = libaq.a
LIB_NAME ?=
TARGET = $(BUILD_DIR)/$(NAME)
CC ?= clang
CFLAGS = -Wno-unused-command-line-argument -Wall -Werror -Wextra -g3 -L$(BUILD_DIR) -MMD -rdynamic -DBASE_PATH='"$(BASE_PATH)/"'
# CFLAGS += -fsanitize=address -fno-omit-frame-pointer -fsanitize-address-use-after-return=runtime -fno-common -fsanitize-address-use-after-scope
BONUS_FILES =
LIBS_NAME =
SUBJECT_URL =
SRC_FILES = $(shell cat src.list)
SRC = $(addsuffix .c,$(addprefix $(SRC_DIR)/,$(SRC_FILES)))
OBJ = $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(SRC_FILES)))
DEPS = $(addsuffix .d,$(addprefix $(BUILD_DIR)/,$(SRC_FILES)))
LIBS = $(addprefix $(LIBS_DIR)/,$(LIBS_NAME))
INCLUDES = $(addprefix -I,$(foreach P,$(INCLUDE_DIR) $(LIBS) $(addsuffix /include,$(LIBS)) vendor $(addsuffix /vendor,$(LIBS)),$(realpath $(P))))
COL_GRAY = \033[90m
COL_WHITE = \033[37m
COL_GREEN = \033[32m
COL_BOLD = \033[1m
COL_RESET = \033[0m
# TODO: REMOVE FOR RENDU !!!!!
CFLAGS += -DPRINT_BACKTRACE
.PHONY = all bonus clean re subject
all: $(NAME)
$(NAME): $(TARGET)
$(TARGET): $(OBJ)
@echo -e '$(COL_GRAY) Linking\t$(COL_GREEN)$(TARGET)$(COL_RESET)'
@#$(CC) $(INCLUDES) $(OBJ) $(CFLAGS) -o $(NAME)
@ar rcs $(BUILD_DIR)/$(NAME) $(OBJ)
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(dir $@)
@echo -e '$(COL_GRAY) Building\t$(COL_GREEN)$<$(COL_RESET)'
@$(CC) $(CFLAGS) $(WERROR) $(INCLUDES) -c $< -o $@
$(BUILD_DIR)/%.o: $(GENERIC_DIR)/%.c
@mkdir -p $(dir $@)
@echo -e '$(COL_GRAY) Building\t$(COL_GREEN)$<$(COL_RESET)'
@$(CC) $(CFLAGS) $(WERROR) $(INCLUDES) -c $< -o $@
clean:
@- $(foreach LIB,$(LIBS), \
make clean LIB_NAME=$(LIB)/ BUILD_DIR=$(realpath $(BUILD_DIR)) -C $(LIB) --no-print-directory || true;\
)
@- $(if $(LIB_NAME),,echo -e '$(COL_WHITE)Clearing Artefacts';rm -rf $(BUILD_DIR))
fclean: clean
@- $(foreach LIB,$(LIBS), \
make fclean LIB_NAME=$(LIB)/ BUILD_DIR=$(shell realpath $(BUILD_DIR)) -C $(LIB) --no-print-directory || true;\
)
@echo -e '$(COL_WHITE)Clearing Output $(COL_GRAY)$(LIB_NAME)$(NAME)'
@rm -f $(BUILD_DIR)$(NAME)
re: fclean all
subject: subject.txt
bat --plain ./subject.txt
subject.txt:
@curl $(SUBJECT_URL) | pdftotext -layout -nopgbrk -q - subject.txt
generate_filelist::
@/usr/bin/env zsh -c "tree -iFf --noreport output | rg '^output/src/(.*)\.c\$$' --replace '\$$1' | sort -u" > ./gen.list
@/usr/bin/env zsh -c "tree -iFf --noreport src | rg '^src/(.*)\.c\$$' --replace '\$$1' | sort -u" > ./src.list
-include $(DEPS)

0
allocator/gen.list Normal file
View file

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:42:02 by maiboyer #+# #+# */
/* Updated: 2024/05/14 17:44:38 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOC_H
#define ALLOC_H
#include "me/types.h"
void *me_malloc(t_usize size);
void *me_calloc(t_usize elem_count, t_usize elem_size);
void *me_realloc(void *ptr, t_usize size);
void *me_realloc_array(void *ptr, t_usize elem_size, t_usize elem_count);
void uninit_allocator(void);
#endif /* ALLOC_H */

View file

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_internal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */
/* Updated: 2024/05/14 16:12:54 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOC_INTERNAL_H
#define ALLOC_INTERNAL_H
#include "aq/alloc.h"
#include "me/types.h"
#include <stdalign.h>
#define PAGE_SIZE_DEFAULT 4096
#define BLOCK_PADDING "\xFE\xDC\xAB\xC0\xFE\xEE\x66"
#define POOL_ADDR (void *)0xDeadBeef
typedef struct s_mblock
{
struct s_mblock *next;
struct s_mpage *page;
t_usize size;
bool used;
t_u8 padding[7];
} t_mblock;
typedef struct s_mpage
{
t_usize page_size;
t_mblock *first;
struct s_mpage *next;
} t_mpage;
// Will never be null, as it will allocate a new arena if it needs to do so
t_mpage *get_head_arena(void);
// Will return ERROR if it couldn't malloc the page
t_error alloc_arena_page(t_usize min_size, t_mpage **out);
t_mblock *get_block_for_size(t_usize size);
void print_pages_info(void);
bool merge_block(t_mblock *self, t_usize min_size);
#endif /* ALLOC_INTERNAL_H */

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allocator.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 17:52:31 by maiboyer #+# #+# */
/* Updated: 2024/05/14 17:55:36 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOCATOR_H
#define ALLOCATOR_H
#include "me/types.h"
typedef struct s_allocator t_allocator;
typedef void *(*t_allocator_alloc)(t_allocator *self, t_usize size);
typedef void *(*t_allocator_alloc_array)(t_allocator *self, t_usize size,
t_usize count);
typedef void *(*t_allocator_realloc)(t_allocator *self, void *ptr,
t_usize requested_size);
typedef void *(*t_allocator_realloc_array)(t_allocator *self, void *ptr,
t_usize requested_size,
t_usize requested_count);
typedef void (*t_allocator_free)(t_allocator *self, void *ptr);
typedef void (*t_allocator_uninit)(t_allocator *self);
struct s_allocator
{
t_allocator_alloc alloc;
t_allocator_alloc_array alloc_array;
t_allocator_realloc realloc;
t_allocator_realloc_array realloc_array;
t_allocator_free free;
t_allocator_uninit uninit;
void *alloc_data;
};
#endif /* ALLOCATOR_H */

View file

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* internal_vg_funcs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 22:20:30 by maiboyer #+# #+# */
/* Updated: 2024/05/14 16:17:25 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef INTERNAL_VG_FUNCS_H
#define INTERNAL_VG_FUNCS_H
#include "aq/alloc_internal.h"
#include "me/types.h"
#if !defined(NVALGRIND) || defined(VGHEADER)
# ifdef NVALGRIND
# undef NVALGRIND
# endif
# define VGFUNCS
# include <valgrind/memcheck.h>
# include <valgrind/valgrind.h>
#endif
#define ZEROED_POOL 1
#define ZEROED_ALLOC 1
static inline t_usize redzone_size(void)
{
return (sizeof(t_mblock));
}
#ifdef VGFUNCS
static inline bool vg_running(void)
{
return (RUNNING_ON_VALGRIND != 0);
}
#else
static inline bool vg_running(void)
{
return (false);
}
#endif
#define MEMPOOL_FLAG_MALLOCLIKE 1
#define MEMPOOL_FLAG_AUTOFREE 2
void vg_block_malloc(void *ptr, t_usize size);
void vg_block_resize(void *ptr, t_usize oldsize, t_usize newsize);
void vg_block_free(void *ptr);
void vg_mem_no_access(void *ptr, t_usize size);
void vg_mem_undefined(void *ptr, t_usize size);
void vg_mem_defined(void *ptr, t_usize size);
void vg_mempool_create(void *pool);
void vg_mempool_create_ext(void *pool, t_usize flags);
void vg_mempool_destroy(void *pool);
void vg_mempool_alloc(void *pool, void *addr, t_usize size);
void vg_mempool_free(void *pool, void *addr);
void vg_mempool_resize(void *pool, void *psrc, t_usize size);
#endif /* INTERNAL_VG_FUNCS_H */

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libc_wrapper.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 17:52:11 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:48:06 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBC_WRAPPER_H
#define LIBC_WRAPPER_H
#include "aq/allocator.h"
#include "me/types.h"
void *lc_malloc(t_allocator *self, t_usize size);
void *lc_calloc(t_allocator *self, t_usize size, t_usize count);
void *lc_realloc(t_allocator *self, void *ptr, t_usize min_size);
void *lc_realloc_array(t_allocator *self, void *ptr, t_usize size,
t_usize count);
void lc_free(t_allocator *self, void *ptr);
void lc_uninit(t_allocator *self);
t_allocator lc_init(void);
#endif /* LIBC_WRAPPER_H */

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* melloc.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 17:54:03 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:08:16 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MELLOC_H
#define MELLOC_H
#include "aq/allocator.h"
void *m_malloc(t_allocator *self, t_usize size);
void *m_alloc_array(t_allocator *self, t_usize size, t_usize count);
void *m_realloc(t_allocator *self, void *ptr, t_usize min_size);
void *m_realloc_array(t_allocator *self, void *ptr, t_usize size,
t_usize count);
void m_free(t_allocator *self, void *ptr);
void m_uninit(t_allocator *self);
#endif /* MELLOC_H */

9
allocator/src.list Normal file
View file

@ -0,0 +1,9 @@
alloc
get_arena
lc_alloc/functions1
lc_alloc/functions2
me_alloc/functions1
me_alloc/functions2
vg/valgrind_block
vg/valgrind_mempool
vg/valgrind_mem_status

196
allocator/src/alloc.c Normal file
View file

@ -0,0 +1,196 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:26:01 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/allocator.h"
#include "aq/libc_wrapper.h"
typedef struct s_allocator_page
{
void *data;
t_usize size;
} t_allocator_page;
typedef struct s_page_list
{
t_usize allocated;
t_allocator_page a[10];
struct s_page_list *next;
} t_page_list;
/*
void *me_malloc(t_usize size)
{
t_mblock *block;
size = usize_round_up_to(size, 16);
block = get_block_for_size(size);
if (block == NULL)
return (me_abort("Found no page for me_malloc"), NULL);
vg_mem_defined(block, sizeof(*block));
vg_mempool_alloc(POOL_ADDR, (void *)(((t_usize)block) + sizeof(*block)),
block->size); block->used = true; mem_set_zero((t_u8 *)block + sizeof(*block),
block->size); vg_mem_no_access(block, sizeof(*block)); return ((void
*)(((t_usize)block) + sizeof(*block)));
}
void *me_calloc(t_usize elem_size, t_usize elem_count)
{
if (elem_size != 0 && elem_count > SIZE_MAX / elem_size)
me_abort("calloc overflow !");
return (me_malloc(elem_size * elem_count));
}
void *me_realloc(void *ptr, t_usize new_size)
{
t_mblock *block;
void *ret;
t_usize old_size;
if (ptr == NULL)
return (me_malloc(new_size));
block = (void *)((t_usize)(ptr) - sizeof(*block));
vg_mem_defined(block, sizeof(*block));
VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, block->size);
block->used = true;
if (block->size <= new_size)
return (vg_mem_no_access(block, sizeof(*block)), ptr);
old_size = block->size;
vg_mem_no_access(block, sizeof(*block));
if (false && merge_block(block, new_size))
{
vg_mem_defined(block, sizeof(*block));
vg_mempool_resize(POOL_ADDR, ptr, block->size);
VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, block->size);
mem_set_zero((t_u8 *)ptr + old_size, block->size - old_size);
vg_mem_no_access(block, sizeof(*block));
return (ptr);
}
else
{
ret = me_malloc(new_size);
mem_copy(ret, ptr, block->size);
mem_free(ptr);
return (ret);
}
}
void *me_realloc_array(void *ptr, t_usize elem_size, t_usize elem_count)
{
if (elem_size != 0 && elem_count > SIZE_MAX / elem_size)
me_abort("realloc_array overflow !");
return (me_realloc(ptr, elem_size * elem_count));
}
void mem_free(void *ptr)
{
t_mblock *cur;
if (ptr == NULL)
return;
cur = (void *)(((t_usize)ptr) - sizeof(t_mblock));
vg_mempool_free(POOL_ADDR, ptr);
vg_mem_defined(cur, sizeof(*cur));
cur->used = false;
merge_block(cur, ~(t_usize)0);
vg_mem_no_access(cur, sizeof(*cur));
}
*/
// void uninit_allocator(void)
// {
// t_ptr_table *table;
// t_ptr_table *table_next;
// t_usize i;
// t_usize unfree_count;
//
// unfree_count = 0;
// table = get_table();
//
// while (table)
// {
// i = 0;
// while (i < PTR_LENS)
// {
// if (table->table[i].ptr != NULL)
// {
// __libc_free(table->table[i].ptr);
// unfree_count++;
// }
// i++;
// }
// table_next = table->next;
// __libc_free(table);
// table = table_next;
// }
// if (unfree_count != 0)
// {
// me_putstr_fd("A total of ", 2);
// me_putnbr_fd(unfree_count, 2);
// me_putendl_fd(" blocks weren't freed !", 2);
// }
// }
// void *me_malloc(t_usize size)
// {
// t_mblock *block;
// void *ret;
//
// size = usize_round_up_to(size, 16);
// printf("Allocating %zu.\n", size);
// block = get_block_for_size(size);
// if (block
// == NULL)
// return (me_abort("Found no page for me_malloc"), NULL);
// block->used = true;
// ret = ((t_u8 *)block->page->data) + block->offset;
// mem_set_zero(ret, block->size);
// return (ret);
// }
//
// void *me_calloc(t_usize elem_size, t_usize elem_count)
// {
// if (elem_size != 0 && elem_count > SIZE_MAX / elem_size)
// return (NULL);
// return (me_malloc(elem_size * elem_count));
// }
//
// void *me_realloc(void *ptr, t_usize new_size)
// {
// t_mblock *block;
// void *ret;
//
// if (ptr == NULL)
// return (me_malloc(new_size));
// block = get_block_from_ptr(ptr);
// if (block == NULL || block->size <= new_size)
// return (ptr);
// if (!merge_next_block(block, new_size))
// return (ptr);
// else
// {
// ret = me_malloc(new_size);
// mem_copy(ret, ptr, block->size);
// mem_free(ptr);
// return (ret);
// }
// }
//
// void mem_free(void *ptr)
// {
// t_mblock *cur;
//
// if (ptr == NULL)
// return;
// cur = get_block_from_ptr(ptr);
// if (cur == NULL)
// return (me_abort("Invalid free (not allocated with me_*alloc)!"));
// cur->used = false;
// merge_next_block(cur, ~(t_usize)0);
// }

362
allocator/src/get_arena.c Normal file
View file

@ -0,0 +1,362 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_arena.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */
/* Updated: 2024/05/14 16:52:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/alloc.h"
#include "aq/alloc_internal.h"
#include "aq/internal_vg_funcs.h"
#include "me/fs/putendl_fd.h"
#include "me/fs/putnbr_fd.h"
#include "me/fs/putstr_fd.h"
#include "me/mem/mem_compare.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/num/usize.h"
#include <stdio.h>
#include <stdlib.h>
/*
void *__libc_malloc(size_t size);
void __libc_free(void *ptr);
t_mpage *alloc_page(t_usize size)
{
t_mpage *val;
size = usize_round_up_to(size + sizeof(t_mpage), PAGE_SIZE_DEFAULT);
val = __libc_malloc(size);
if (val == NULL)
return (NULL);
mem_set_zero(val, size);
val->next = NULL;
val->page_size = size - sizeof(*val);
val->first = (t_mblock *)(((t_usize)val) + sizeof(t_mpage));
val->first->page = val;
val->first->next = NULL;
val->first->used = false;
val->first->size = size - sizeof(t_mpage) - sizeof(t_mblock) * 2;
mem_copy(val->first->padding, BLOCK_PADDING, 7);
vg_mem_no_access(val, size);
return (val);
}
t_mpage *get_head_arena(void)
{
static t_mpage *val = NULL;
if (val == NULL &&
PAGE_SIZE_DEFAULT > sizeof(t_mpage) + sizeof(t_mblock) * 2 + 16)
{
vg_mempool_create(POOL_ADDR);
val = alloc_page(PAGE_SIZE_DEFAULT - sizeof(t_mpage));
}
if (val == NULL)
(me_putstr_fd("Failed to alloc first page", 2), exit(1));
return (val);
}
t_mblock *split_block(t_mblock *self, t_usize min_size)
{
t_usize remaining;
t_mblock *old_next;
vg_mem_defined(self, sizeof(*self));
min_size = usize_round_up_to(min_size, 16);
if (self->size > (min_size + sizeof(t_mblock) + 16))
{
remaining = self->size - min_size - sizeof(t_mblock);
printf("splitting %zu into %zu and %zu\n", self->size, min_size,
remaining);
self->size = min_size;
old_next = self->next;
self->next = (t_mblock *)(((t_usize)self) + sizeof(*self) + self->size);
vg_mem_defined(self->next, sizeof(*self->next));
self->next->page = self->page;
self->next->next = old_next;
self->next->used = false;
self->next->size = remaining;
mem_copy(self->next->padding, BLOCK_PADDING, 7);
vg_mem_no_access(self->next, sizeof(*self->next));
}
vg_mem_no_access(self, sizeof(*self));
return (self);
}
t_mblock *get_block_for_size(t_usize size)
{
t_mblock *last;
t_mblock *cur;
t_mpage *head;
last = NULL;
head = get_head_arena();
vg_mem_defined(head, sizeof(*head));
cur = head->first;
vg_mem_no_access(head, sizeof(*head));
while (cur)
{
vg_mem_defined(cur, sizeof(*cur));
if (!cur->used && cur->size >= size)
return (split_block(cur, size));
last = cur;
cur = cur->next;
vg_mem_no_access(last, sizeof(*last));
}
if (last == NULL)
return (NULL);
vg_mem_defined(last, sizeof(*last));
vg_mem_defined(last->page, sizeof(*last->page));
last->page->next = alloc_page(size + sizeof(t_mblock));
if (last->page->next == NULL)
me_abort("Failed to alloc page!");
vg_mem_defined(last->page->next, sizeof(*last->page->next));
last->next = last->page->next->first;
cur = last->page->next->first;
vg_mem_no_access(last->page->next, sizeof(*last->page->next));
vg_mem_no_access(last->page, sizeof(*last->page));
vg_mem_no_access(last, sizeof(*last));
return (split_block(cur, size));
}
void print_pages_info(void)
{
t_mpage *page;
t_mpage *old;
t_i32 page_nb;
page = get_head_arena();
page_nb = 0;
while (page != NULL)
{
vg_mem_defined(page, sizeof(*page));
me_putstr_fd("Page ", 2);
me_putnbr_fd(page_nb++, 2);
me_putstr_fd(" with size ", 2);
me_putnbr_fd((t_i32)page->page_size, 2);
me_putstr_fd("\n", 2);
old = page;
page = page->next;
vg_mem_no_access(old, sizeof(*old));
}
}
bool merge_block(t_mblock *self, t_usize min_size)
{
t_mblock *old_next;
vg_mem_defined(self, sizeof(*self));
vg_mem_defined(self->next, sizeof(*self->next));
if (!(self->next && !self->next->used && self->next->page == self->page &&
self->size + self->next->size + sizeof(t_mblock) >= min_size))
return (vg_mem_no_access(self->next, sizeof(*self->next)),
vg_mem_no_access(self, sizeof(*self)), false);
old_next = self->next;
self->size += sizeof(*self) + self->next->size;
self->next = self->next->next;
vg_mem_no_access(self, sizeof(*self));
vg_mem_no_access(old_next, sizeof(*old_next));
return (true);
}
void uninit_allocator(void)
{
t_mpage *page;
void *tmp;
t_mblock *block;
t_usize count_block;
page = get_head_arena();
count_block = 0;
vg_mem_defined(page, sizeof(*page));
block = page->first;
while (block)
{
vg_mem_defined(block, sizeof(*block));
if (block->used)
count_block += 1;
block = block->next;
}
while (page)
{
vg_mem_defined(page, sizeof(*page));
tmp = page->next;
__libc_free(page);
page = tmp;
}
if (count_block != 0)
(me_putnbr_fd(count_block, 2),
me_putendl_fd(" Blocks weren't freed when exiting !", 2));
}*/
//
// void free_ifn(void *ptr)
// {
// if (ptr != NULL)
// __libc_free(ptr);
// }
//
// t_mpage *alloc_page(t_usize size)
// {
// t_mpage *page;
// void *data;
// t_mblock *block;
//
// size = usize_round_up_to(size, PAGE_SIZE_DEFAULT);
// page = __libc_malloc(sizeof(t_mpage));
// block = __libc_malloc(sizeof(t_mblock));
// data = __libc_malloc(size);
// if (page == NULL || data == NULL || block == NULL ||
// PAGE_SIZE_DEFAULT <= (t_usize)0)
// return (free_ifn(page), free_ifn(data), free_ifn(block), NULL);
// page->data = data;
// page->next = NULL;
// page->page_size = size;
// page->first = block;
// block->offset = 0;
// block->page = page;
// block->next = NULL;
// block->used = false;
// block->size = size;
// mem_copy(block->padding, BLOCK_PADDING, 7);
// return (page);
// }
//
// t_mpage *get_head_arena(void)
// {
// static t_mpage *val = NULL;
//
// if (val == NULL)
// {
// val = alloc_page(PAGE_SIZE_DEFAULT);
// if (val == NULL)
// (me_putstr_fd("Failed to alloc first page", 2), exit(1));
// }
// return (val);
// }
//
// t_mblock *split_block(t_mblock *self, t_usize min_size)
// {
// t_usize remaining;
// t_mblock *new_next;
//
// min_size = usize_round_up_to(min_size, 16);
// if (self->size > min_size)
// {
// remaining = self->size - min_size - 1;
// new_next = __libc_malloc(sizeof(t_mblock));
// if (new_next == NULL)
// return (me_abort("Failed to alloc block"), NULL);
// printf("splitting %zu into %zu and %zu\n", self->size, min_size,
// remaining);
// self->size = min_size;
// new_next->page = self->page;
// new_next->next = self->next;
// new_next->offset = self->offset + self->size + 1;
// new_next->used = false;
// new_next->size = remaining;
// mem_copy(new_next->padding, BLOCK_PADDING, 7);
// self->next = new_next;
// }
// return (self);
// }
//
// t_mblock *get_block_for_size(t_usize size)
// {
// t_mblock *last;
// t_mblock *cur;
//
// last = NULL;
// cur = get_head_arena()->first;
// while (cur)
// {
// if (cur->page == NULL)
// me_abort("block doesn't have a page ?????");
// if (!cur->used && cur->size >= size)
// return (split_block(cur, size));
// last = cur;
// cur = cur->next;
// }
// if (last == NULL)
// return (NULL);
// last->page->next = alloc_page(size);
// if (last->page->next == NULL)
// me_abort("Failed to alloc page!");
// last->next = last->page->next->first;
// return (split_block(last->page->next->first, size));
// }
//
// t_mblock *get_block_from_ptr(void *ptr)
// {
// t_mpage *page;
// t_mblock *block;
//
// page = get_page_from_ptr(ptr);
// if (page == NULL)
// return (NULL);
// block = page->first;
// while (block && block->page == page)
// {
// if (((t_u8 *)page->data) + block->offset == (t_u8 *)ptr)
// return (block);
// block = block->next;
// }
// return (NULL);
// }
//
// t_mpage *get_page_from_ptr(void *ptr)
// {
// t_mpage *page;
//
// page = get_head_arena();
// while (page)
// {
// if ((t_u8 *)page->data <= (t_u8 *)ptr &&
// (t_u8 *)ptr < (((t_u8 *)page->data) + page->page_size))
// return (page);
// page = page->next;
// }
// return (NULL);
// }
//
// void print_pages_info(void)
// {
// t_mpage *page;
// t_i32 page_nb;
//
// page = get_head_arena();
// page_nb = 0;
// while (page != NULL)
// {
// me_putstr_fd("Page ", 2);
// me_putnbr_fd(page_nb++, 2);
// me_putstr_fd(" with size ", 2);
// me_putnbr_fd((t_i32)page->page_size, 2);
// me_putstr_fd("\n", 2);
// page = page->next;
// }
// }
//
// bool merge_next_block(t_mblock *cur, t_usize min_size)
// {
// t_mblock *next;
//
// if (cur->next != NULL && cur->page == cur->next->page && !cur->next->used &&
// cur->size + cur->next->size >= min_size)
// {
// cur->size += cur->next->size;
// next = cur->next;
// cur->next = cur->next->next;
// printf("merging two blocks %p and %p\n", cur, cur->next);
// __libc_free(next);
// return (NO_ERROR);
// }
// return (ERROR);
// }

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* functions1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 18:02:12 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:49:50 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/allocator.h"
#include "aq/libc_wrapper.h"
#include "me/types.h"
void *__libc_malloc(t_usize size);
void *__libc_calloc(t_usize size, t_usize elem);
void *__libc_realloc(void *ptr, t_usize size);
void *__libc_reallocarray(void *ptr, t_usize size, t_usize elem);
void __libc_free(void *ptr);
void *lc_malloc(t_allocator *self, t_usize size)
{
(void)(self);
return (__libc_malloc(size));
}
void *lc_calloc(t_allocator *self, t_usize size, t_usize elem)
{
(void)(self);
return (__libc_calloc(size, elem));
}
void *lc_realloc(t_allocator *self, void *ptr, t_usize size)
{
(void)(self);
return (__libc_realloc(ptr, size));
}
void *lc_realloc_array(t_allocator *self, void *ptr, t_usize size, t_usize elem)
{
(void)(self);
return (__libc_reallocarray(ptr, size, elem));
}
void lc_free(t_allocator *self, void *ptr)
{
(void)(self);
return (__libc_free(ptr));
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* functions2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 18:06:34 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:48:41 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/allocator.h"
#include "aq/libc_wrapper.h"
void lc_uninit(t_allocator *self)
{
(void)(self);
}
t_allocator lc_init(void)
{
return ((t_allocator){
.alloc = lc_malloc,
.alloc_array = lc_calloc,
.realloc = lc_realloc,
.realloc_array = lc_realloc_array,
.free = lc_free,
.uninit = lc_uninit,
.alloc_data = NULL,
});
}

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* functions1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 18:02:12 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:21:16 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/allocator.h"
#include "aq/libc_wrapper.h"
#include "me/types.h"
// void *__libc_malloc(t_usize size);
// void *__libc_calloc(t_usize size, t_usize elem);
// void *__libc_realloc(void *ptr, t_usize size);
// void *__libc_realloc_array(void *ptr, t_usize size, t_usize elem);
// void __libc_free(void *ptr);
//
// void *lc_malloc(t_allocator *self, t_usize size)
// {
// (void)(self);
// return (__libc_malloc(size));
// }
//
// void *lc_calloc(t_allocator *self, t_usize size, t_usize elem)
// {
// (void)(self);
// return (__libc_calloc(size, elem));
// }
//
// void *lc_realloc(t_allocator *self, void *ptr, t_usize size)
// {
// (void)(self);
// return (__libc_realloc(ptr, size));
// }
//
// void *lc_realloc_array(t_allocator *self, void *ptr, t_usize size, t_usize elem)
// {
// (void)(self);
// return (__libc_realloc_array(ptr, size, elem));
// }
//
// void lc_free(t_allocator *self, void *ptr)
// {
// (void)(self);
// return (__libc_free(ptr));
// }

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* functions2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 18:06:34 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:21:21 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/allocator.h"
#include "aq/libc_wrapper.h"
// void lc_uninit(t_allocator *self)
// {
// (void)(self);
// }

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* valgrind_block.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 22:51:55 by maiboyer #+# #+# */
/* Updated: 2024/05/12 22:55:45 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/internal_vg_funcs.h"
#include "valgrind/valgrind.h"
#ifdef VGFUNCS
void vg_block_malloc(void *ptr, t_usize size)
{
VALGRIND_MALLOCLIKE_BLOCK(ptr, size, redzone_size(), ZEROED_ALLOC);
}
void vg_block_resize(void *ptr, t_usize oldsize, t_usize newsize)
{
VALGRIND_RESIZEINPLACE_BLOCK(ptr, oldsize, newsize, redzone_size());
}
void vg_block_free(void *ptr)
{
VALGRIND_FREELIKE_BLOCK(ptr, redzone_size());
}
#endif

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* valgrind_mem_status.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 23:08:47 by maiboyer #+# #+# */
/* Updated: 2024/05/12 23:22:08 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/internal_vg_funcs.h"
#ifdef VGFUNCS
void vg_mem_no_access(void *ptr, t_usize size)
{
VALGRIND_MAKE_MEM_NOACCESS(ptr, size);
}
void vg_mem_undefined(void *ptr, t_usize size)
{
VALGRIND_MAKE_MEM_UNDEFINED(ptr, size);
}
void vg_mem_defined(void *ptr, t_usize size)
{
VALGRIND_MAKE_MEM_DEFINED(ptr, size);
}
#endif

View file

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* valgrind_mempool.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 22:33:30 by maiboyer #+# #+# */
/* Updated: 2024/05/14 16:17:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/internal_vg_funcs.h"
#include "valgrind/valgrind.h"
#ifdef VGFUNCS
void vg_mempool_create_ext(void *pool, t_usize flags)
{
t_usize actual_flags;
actual_flags = 0;
if (flags & MEMPOOL_FLAG_MALLOCLIKE)
actual_flags |= VALGRIND_MEMPOOL_METAPOOL;
if (flags & MEMPOOL_FLAG_AUTOFREE)
actual_flags |= VALGRIND_MEMPOOL_AUTO_FREE;
VALGRIND_CREATE_MEMPOOL_EXT(pool, redzone_size(), ZEROED_POOL,
actual_flags);
}
void vg_mempool_resize(void *pool, void *ptr, t_usize size)
{
VALGRIND_MEMPOOL_CHANGE(pool, ptr, ptr, size);
}
void vg_mempool_create(void *pool)
{
VALGRIND_CREATE_MEMPOOL(pool, redzone_size(), ZEROED_POOL);
}
void vg_mempool_destroy(void *pool)
{
VALGRIND_DESTROY_MEMPOOL(pool);
}
void vg_mempool_alloc(void *pool, void *addr, t_usize size)
{
VALGRIND_CREATE_BLOCK(addr, size, "mempool");
VALGRIND_MEMPOOL_ALLOC(pool, addr, size);
}
void vg_mempool_free(void *pool, void *addr)
{
VALGRIND_MEMPOOL_FREE(pool, addr);
}
#endif