norminette pass
This commit is contained in:
parent
d59364c35e
commit
fc969750e4
19 changed files with 323 additions and 560 deletions
|
|
@ -11,9 +11,9 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ALLOC_H
|
||||
#define ALLOC_H
|
||||
# define ALLOC_H
|
||||
|
||||
#include "me/types.h"
|
||||
# include "me/types.h"
|
||||
|
||||
void *me_malloc(t_usize size);
|
||||
void *me_calloc(t_usize elem_count, t_usize elem_size);
|
||||
|
|
|
|||
|
|
@ -11,15 +11,15 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ALLOC_INTERNAL_H
|
||||
#define ALLOC_INTERNAL_H
|
||||
# define ALLOC_INTERNAL_H
|
||||
|
||||
#include "aq/alloc.h"
|
||||
#include "me/types.h"
|
||||
#include <stdalign.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
|
||||
# define PAGE_SIZE_DEFAULT 4096
|
||||
# define BLOCK_PADDING "\xFE\xDC\xAB\xC0\xFE\xEE\x66"
|
||||
# define POOL_ADDR (void *)0xDeadBeef
|
||||
|
||||
typedef struct s_mblock
|
||||
{
|
||||
|
|
|
|||
|
|
@ -11,21 +11,23 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ALLOCATOR_H
|
||||
#define ALLOCATOR_H
|
||||
# define ALLOCATOR_H
|
||||
|
||||
#include "me/types.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,
|
||||
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_free)(t_allocator *self,
|
||||
void *ptr);
|
||||
typedef void (*t_allocator_uninit)(t_allocator *self);
|
||||
|
||||
struct s_allocator
|
||||
|
|
|
|||
|
|
@ -6,48 +6,52 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/12 22:20:30 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/17 15:34:26 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 16:40:57 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef INTERNAL_VG_FUNCS_H
|
||||
#define INTERNAL_VG_FUNCS_H
|
||||
# define INTERNAL_VG_FUNCS_H
|
||||
|
||||
#include "me/types.h"
|
||||
#if !defined(NVALGRIND) || defined(VGHEADER)
|
||||
# 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
|
||||
# endif
|
||||
|
||||
#define ZEROED_POOL 1
|
||||
#define ZEROED_ALLOC 1
|
||||
# define ZEROED_POOL 1
|
||||
# define ZEROED_ALLOC 1
|
||||
|
||||
static inline t_usize redzone_size(void)
|
||||
{
|
||||
return (8);
|
||||
}
|
||||
|
||||
#ifdef VGFUNCS
|
||||
# ifdef VGFUNCS
|
||||
|
||||
static inline bool vg_running(void)
|
||||
{
|
||||
return (RUNNING_ON_VALGRIND != 0);
|
||||
}
|
||||
#else
|
||||
# else
|
||||
|
||||
static inline bool vg_running(void)
|
||||
{
|
||||
return (false);
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
|
||||
#define MEMPOOL_FLAG_MALLOCLIKE 1
|
||||
#define MEMPOOL_FLAG_AUTOFREE 2
|
||||
# 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_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);
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#ifndef LIBC_WRAPPER_H
|
||||
#define LIBC_WRAPPER_H
|
||||
# define LIBC_WRAPPER_H
|
||||
|
||||
#include "aq/allocator.h"
|
||||
#include "me/types.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);
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MELLOC_H
|
||||
#define MELLOC_H
|
||||
# define MELLOC_H
|
||||
|
||||
#include "aq/allocator.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);
|
||||
|
|
|
|||
|
|
@ -6,19 +6,19 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/15 15:27:46 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/16 14:58:27 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 16:55:59 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MELLOC_INTERAL_H
|
||||
#define MELLOC_INTERAL_H
|
||||
# define MELLOC_INTERAL_H
|
||||
|
||||
#include "aq/allocator.h"
|
||||
#include "me/types.h"
|
||||
# include "aq/allocator.h"
|
||||
# include "me/types.h"
|
||||
|
||||
#define PAGE_LIST_MAX 255
|
||||
#define PAGE_POW_2 12
|
||||
#define PAGE_ALIGN 3
|
||||
# define PAGE_LIST_MAX 255
|
||||
# define PAGE_POW_2 12
|
||||
# define PAGE_ALIGN 3
|
||||
|
||||
typedef struct s_chunk
|
||||
{
|
||||
|
|
@ -50,15 +50,36 @@ struct s_allocator_melloc
|
|||
t_page_list *list;
|
||||
};
|
||||
|
||||
t_error alloc_page_list(t_page_list **out);
|
||||
|
||||
void *m_malloc(struct s_allocator_melloc *self, t_usize size);
|
||||
void *m_alloc_array(struct s_allocator_melloc *self, t_usize size,
|
||||
t_usize count);
|
||||
void *m_realloc(struct s_allocator_melloc *self, void *ptr, t_usize min_size);
|
||||
void *m_realloc_array(struct s_allocator_melloc *self, void *ptr, t_usize size,
|
||||
t_usize count);
|
||||
void *m_alloc_array(struct s_allocator_melloc *self, \
|
||||
t_usize size, t_usize count);
|
||||
void *m_realloc(struct s_allocator_melloc *self, \
|
||||
void *ptr, t_usize min_size);
|
||||
void *m_realloc_array(struct s_allocator_melloc *self, \
|
||||
void *ptr, t_usize size, t_usize count);
|
||||
void m_free(struct s_allocator_melloc *self, void *ptr);
|
||||
void m_uninit(struct s_allocator_melloc *self);
|
||||
|
||||
t_chunk *find_chunk_of_size(struct s_allocator_melloc *alloc, t_usize size);
|
||||
t_chunk *get_first_block(t_page *page);
|
||||
t_chunk *get_next_block(t_chunk *chunk, bool find_zero);
|
||||
t_chunk *split_block(t_chunk *chunk, t_usize size);
|
||||
void merge_blocks(t_page *page);
|
||||
|
||||
t_error alloc_page_list(t_page_list **out);
|
||||
t_error alloc_new_page(struct s_allocator_melloc *alloc, t_usize page_size);
|
||||
|
||||
static inline t_usize round_to_pow2(t_usize val, t_usize pow)
|
||||
{
|
||||
pow = (1 << pow) - 1;
|
||||
return ((val + pow) & (~pow));
|
||||
}
|
||||
|
||||
static inline void *m_alloc_error(struct s_allocator_melloc *self, t_str msg)
|
||||
{
|
||||
self->uninit((void *)self);
|
||||
me_abort(msg);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
#endif /* MELLOC_INTERAL_H */
|
||||
|
|
|
|||
|
|
@ -4,9 +4,12 @@ lc_alloc/functions1
|
|||
lc_alloc/functions2
|
||||
me_alloc/functions1
|
||||
me_alloc/functions2
|
||||
me_alloc/internals
|
||||
me_alloc/pages
|
||||
me_alloc/realloc
|
||||
vg/dummy_block
|
||||
vg/dummy_mempool
|
||||
vg/dummy_mem_status
|
||||
vg/dummy_mempool
|
||||
vg/valgrind_block
|
||||
vg/valgrind_mempool
|
||||
vg/valgrind_mem_status
|
||||
vg/valgrind_mempool
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ void mem_free(void *ptr)
|
|||
t_mblock *cur;
|
||||
|
||||
if (ptr == NULL)
|
||||
return;
|
||||
return ;
|
||||
cur = (void *)(((t_usize)ptr) - sizeof(t_mblock));
|
||||
vg_mempool_free(POOL_ADDR, ptr);
|
||||
vg_mem_defined(cur, sizeof(*cur));
|
||||
|
|
@ -187,7 +187,7 @@ void mem_free(void *ptr)
|
|||
// t_mblock *cur;
|
||||
//
|
||||
// if (ptr == NULL)
|
||||
// return;
|
||||
// return ;
|
||||
// cur = get_block_from_ptr(ptr);
|
||||
// if (cur == NULL)
|
||||
// return (me_abort("Invalid free (not allocated with me_*alloc)!"));
|
||||
|
|
|
|||
|
|
@ -1,362 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/mem/mem.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);
|
||||
// }
|
||||
|
|
@ -38,7 +38,8 @@ void *lc_realloc(t_allocator *self, void *ptr, t_usize size)
|
|||
return (__libc_realloc(ptr, size));
|
||||
}
|
||||
|
||||
void *lc_realloc_array(t_allocator *self, void *ptr, t_usize size, t_usize elem)
|
||||
void *lc_realloc_array(t_allocator *self, void *ptr, t_usize size,
|
||||
t_usize elem)
|
||||
{
|
||||
(void)(self);
|
||||
return (__libc_reallocarray(ptr, size, elem));
|
||||
|
|
@ -49,4 +50,3 @@ void lc_free(t_allocator *self, void *ptr)
|
|||
(void)(self);
|
||||
return (__libc_free(ptr));
|
||||
}
|
||||
|
||||
|
|
|
|||
97
allocator/src/me_alloc/realloc.c
Normal file
97
allocator/src/me_alloc/realloc.c
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* realloc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/10 16:48:19 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 17:12:05 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "aq/allocator.h"
|
||||
#include "aq/internal_vg_funcs.h"
|
||||
#include "aq/melloc_interal.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/types.h"
|
||||
#include <errno.h>
|
||||
|
||||
static void *_realloc_inner_copy(struct s_allocator_melloc *self, void *ptr, \
|
||||
t_usize size, t_chunk *next)
|
||||
{
|
||||
t_chunk *chk;
|
||||
|
||||
chk = ptr - sizeof(*chk);
|
||||
vg_mem_no_access(next, sizeof(*next));
|
||||
next = m_realloc(self, NULL, size);
|
||||
vg_mem_defined(chk, sizeof(*chk));
|
||||
mem_move(next, ptr, chk->size);
|
||||
vg_mem_no_access(chk, sizeof(*chk));
|
||||
m_free(self, ptr);
|
||||
return (next);
|
||||
}
|
||||
|
||||
static void *_realloc_inner(struct s_allocator_melloc *self, void *ptr, \
|
||||
t_usize size)
|
||||
{
|
||||
t_usize old_size;
|
||||
t_chunk *chk;
|
||||
t_chunk *next;
|
||||
|
||||
chk = ptr - sizeof(*chk);
|
||||
vg_mem_defined(chk, sizeof(*chk));
|
||||
if (chk->size >= size)
|
||||
return (vg_mem_no_access(chk, sizeof(*chk)), ptr);
|
||||
next = get_next_block(chk, false);
|
||||
(vg_mem_defined(next, sizeof(*next)), vg_mem_defined(chk, sizeof(*chk)));
|
||||
if (next != NULL && !next->used && chk->size + next->size
|
||||
+ sizeof(*next) >= size)
|
||||
{
|
||||
old_size = chk->size;
|
||||
chk->size += next->size + sizeof(*next);
|
||||
return (vg_mem_undefined(next, next->size + sizeof(*next)), \
|
||||
vg_block_resize((void *)chk + sizeof(*chk), old_size, chk->size), \
|
||||
vg_mem_no_access(chk, sizeof(*chk)), ptr);
|
||||
}
|
||||
else
|
||||
return (_realloc_inner_copy(self, ptr, size, next));
|
||||
}
|
||||
|
||||
static void *_realloc_alloc(struct s_allocator_melloc *self, t_usize size)
|
||||
{
|
||||
t_chunk *chunk;
|
||||
|
||||
chunk = find_chunk_of_size(self, size);
|
||||
if (chunk == NULL)
|
||||
{
|
||||
if (alloc_new_page(self, size + sizeof(t_chunk) * 2))
|
||||
return (m_alloc_error(self, "Unable to alloc page"));
|
||||
chunk = find_chunk_of_size(self, size);
|
||||
if (chunk == NULL)
|
||||
return (m_alloc_error(self, "Unable to find block"));
|
||||
}
|
||||
vg_mem_defined((void *)chunk, sizeof(*chunk));
|
||||
chunk->used = true;
|
||||
vg_mem_defined((void *)chunk + sizeof(*chunk), chunk->size);
|
||||
mem_set_zero((void *)chunk + sizeof(*chunk), chunk->size);
|
||||
vg_block_malloc((void *)chunk + sizeof(*chunk), chunk->size);
|
||||
vg_mem_no_access((void *)chunk, sizeof(*chunk));
|
||||
return ((void *)chunk + sizeof(*chunk));
|
||||
}
|
||||
|
||||
void *m_realloc(struct s_allocator_melloc *self, void *ptr, t_usize size)
|
||||
{
|
||||
t_chunk *chunk;
|
||||
t_chunk *next;
|
||||
|
||||
if (size > INT32_MAX - sizeof(t_chunk) * 10)
|
||||
return (errno = ENOMEM, NULL);
|
||||
size = round_to_pow2(size, PAGE_ALIGN);
|
||||
if (ptr != NULL && size == 0)
|
||||
return (m_free(self, ptr), NULL);
|
||||
if (ptr == NULL)
|
||||
return (_realloc_alloc(self, size));
|
||||
else
|
||||
return (_realloc_inner(self, ptr, size));
|
||||
}
|
||||
|
|
@ -24,7 +24,6 @@ void vg_mempool_create_ext(void *pool, t_usize flags)
|
|||
actual_flags |= VALGRIND_MEMPOOL_METAPOOL;
|
||||
if (flags & MEMPOOL_FLAG_AUTOFREE)
|
||||
actual_flags |= VALGRIND_MEMPOOL_AUTO_FREE;
|
||||
|
||||
VALGRIND_CREATE_MEMPOOL_EXT(pool, 0, ZEROED_POOL, actual_flags);
|
||||
}
|
||||
|
||||
|
|
@ -36,7 +35,6 @@ void vg_mempool_resize(void *pool, void *ptr, t_usize size)
|
|||
void vg_mempool_create(void *pool)
|
||||
{
|
||||
VALGRIND_CREATE_MEMPOOL(pool, 0, ZEROED_POOL);
|
||||
|
||||
}
|
||||
|
||||
void vg_mempool_destroy(void *pool)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue