Allocator is fully normed
This commit is contained in:
parent
fc969750e4
commit
882d5cb5bb
11 changed files with 351 additions and 325 deletions
|
|
@ -1,15 +1,18 @@
|
|||
alloc
|
||||
get_arena
|
||||
lc_alloc/functions1
|
||||
lc_alloc/functions2
|
||||
me_alloc/find_block
|
||||
me_alloc/functions1
|
||||
me_alloc/functions2
|
||||
me_alloc/internals
|
||||
me_alloc/merge_blocks
|
||||
me_alloc/pages
|
||||
me_alloc/realloc
|
||||
vg/dummy_block
|
||||
vg/dummy_mem_status
|
||||
vg/dummy_mempool
|
||||
vg/dummy_mempool_bis
|
||||
vg/valgrind_block
|
||||
vg/valgrind_mem_status
|
||||
vg/valgrind_mempool
|
||||
vg/valgrind_mempool_bis
|
||||
|
|
|
|||
56
allocator/src/me_alloc/find_block.c
Normal file
56
allocator/src/me_alloc/find_block.c
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* find_block.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/10 17:40:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 17:41:07 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "aq/internal_vg_funcs.h"
|
||||
#include "aq/melloc_interal.h"
|
||||
#include "me/types.h"
|
||||
|
||||
static bool _find_chunk_of_size_inner(t_usize size, t_page_list *list, \
|
||||
t_chunk **chunk)
|
||||
{
|
||||
vg_mem_defined(*chunk, sizeof(**chunk));
|
||||
if (!(*chunk)->used && (*chunk)->size >= size)
|
||||
{
|
||||
vg_mem_no_access(*chunk, sizeof(**chunk));
|
||||
vg_mem_no_access(list, sizeof(*list));
|
||||
return (*chunk = split_block(*chunk, size), true);
|
||||
}
|
||||
vg_mem_no_access(*chunk, sizeof(**chunk));
|
||||
*chunk = get_next_block(*chunk, false);
|
||||
return (false);
|
||||
}
|
||||
|
||||
t_chunk *find_chunk_of_size(struct s_allocator_melloc *alloc, t_usize size)
|
||||
{
|
||||
t_page_list *list;
|
||||
t_page_list *list_next;
|
||||
t_usize idx;
|
||||
t_chunk *chunk;
|
||||
|
||||
list = alloc->list;
|
||||
while (list != NULL)
|
||||
{
|
||||
vg_mem_defined(list, sizeof(*list));
|
||||
idx = 0;
|
||||
while (idx < list->len)
|
||||
{
|
||||
chunk = get_first_block(&list->pages[idx++]);
|
||||
while (chunk)
|
||||
if (_find_chunk_of_size_inner(size, list, &chunk))
|
||||
return (chunk);
|
||||
}
|
||||
list_next = list->next;
|
||||
vg_mem_no_access(list, sizeof(*list));
|
||||
list = list_next;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
|
@ -6,321 +6,41 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/14 18:02:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/06/28 19:44:43 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 17:21:55 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/types.h"
|
||||
|
||||
#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 <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.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);
|
||||
|
||||
t_chunk *get_first_block(t_page *page)
|
||||
{
|
||||
t_chunk *chunk;
|
||||
if (page == NULL)
|
||||
return (NULL);
|
||||
vg_mem_defined(page, sizeof(*page));
|
||||
chunk = page->data;
|
||||
vg_mem_no_access(page, sizeof(*page));
|
||||
return (chunk);
|
||||
}
|
||||
|
||||
t_chunk *get_next_block(t_chunk *chunk, bool find_zero)
|
||||
{
|
||||
t_chunk *next;
|
||||
|
||||
if (chunk == NULL)
|
||||
return (NULL);
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
next = (void *)(chunk) + sizeof(*chunk) + chunk->size;
|
||||
vg_mem_no_access(chunk, sizeof(*chunk));
|
||||
vg_mem_defined(next, sizeof(*next));
|
||||
if (!find_zero && next->size == 0)
|
||||
{
|
||||
vg_mem_no_access(next, sizeof(*next));
|
||||
return (NULL);
|
||||
}
|
||||
vg_mem_no_access(next, sizeof(*next));
|
||||
return (next);
|
||||
}
|
||||
|
||||
t_usize round_to_pow2(t_usize val, t_usize pow)
|
||||
{
|
||||
pow = (1 << pow) - 1;
|
||||
|
||||
return ((val + pow) & (~pow));
|
||||
}
|
||||
|
||||
t_error alloc_page_list(t_page_list **out)
|
||||
{
|
||||
t_page_list *val;
|
||||
|
||||
if (out == NULL)
|
||||
return (ERROR);
|
||||
val = __libc_calloc(sizeof(*val), 1);
|
||||
if (val == NULL)
|
||||
return (ERROR);
|
||||
*out = val;
|
||||
vg_mem_no_access(val, sizeof(*val));
|
||||
vg_mempool_create_ext(val, MEMPOOL_FLAG_MALLOCLIKE);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
void merge_blocks(t_page *page)
|
||||
{
|
||||
t_chunk *cur;
|
||||
t_chunk *next;
|
||||
bool did_merge;
|
||||
|
||||
did_merge = true;
|
||||
while (did_merge)
|
||||
{
|
||||
cur = get_first_block(page);
|
||||
next = get_next_block(cur, false);
|
||||
did_merge = false;
|
||||
while (cur != NULL && next != NULL)
|
||||
{
|
||||
(vg_mem_defined(cur, sizeof(*cur)), vg_mem_defined(next, sizeof(*next)));
|
||||
if (!cur->used && !next->used)
|
||||
{
|
||||
did_merge = true;
|
||||
cur->size += sizeof(*cur);
|
||||
cur->size += next->size;
|
||||
(vg_mem_no_access(cur, sizeof(*cur)), vg_mem_no_access(next, sizeof(*next)));
|
||||
next = get_next_block(cur, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
(vg_mem_no_access(cur, sizeof(*cur)), vg_mem_no_access(next, sizeof(*next)));
|
||||
cur = next;
|
||||
next = get_next_block(cur, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t_chunk *split_block(t_chunk *chunk, t_usize size)
|
||||
{
|
||||
t_usize remaining;
|
||||
t_chunk *next;
|
||||
t_chunk *ac_next;
|
||||
|
||||
if (chunk == NULL)
|
||||
return (NULL);
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
if (chunk->size > size + sizeof(*chunk) + (2 << PAGE_ALIGN))
|
||||
{
|
||||
remaining = chunk->size - size - sizeof(*chunk);
|
||||
next = get_next_block(chunk, true);
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
// eprintf("before size = %zu\n", (t_usize)chunk->size);
|
||||
chunk->size = size;
|
||||
// eprintf("after size = %zu\n", (t_usize)chunk->size);
|
||||
ac_next = get_next_block(chunk, true);
|
||||
// eprintf("chunk = %p\n", chunk);
|
||||
// eprintf("next = %p\n", next);
|
||||
// eprintf("ac_next = %p\n", ac_next);
|
||||
// eprintf("get_next_block(ac_next, true) = %p\n",
|
||||
// get_next_block(ac_next, true));
|
||||
vg_mem_defined(ac_next, sizeof(*ac_next));
|
||||
ac_next->used = false;
|
||||
ac_next->size = remaining;
|
||||
assert(next == get_next_block(ac_next, true));
|
||||
vg_mem_no_access(ac_next, sizeof(*ac_next));
|
||||
}
|
||||
vg_mem_no_access(chunk, sizeof(*chunk));
|
||||
return (chunk);
|
||||
}
|
||||
|
||||
t_chunk *find_chunk_of_size(struct s_allocator_melloc *alloc, t_usize size)
|
||||
{
|
||||
t_page_list *list;
|
||||
t_page_list *list_next;
|
||||
t_usize idx;
|
||||
t_chunk *chunk;
|
||||
|
||||
list = alloc->list;
|
||||
while (list != NULL)
|
||||
{
|
||||
vg_mem_defined(list, sizeof(*list));
|
||||
idx = 0;
|
||||
while (idx < list->len)
|
||||
{
|
||||
// printf("[%zu]list\n", idx);
|
||||
chunk = get_first_block(&list->pages[idx++]);
|
||||
while (chunk)
|
||||
{
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
if (!chunk->used && chunk->size >= size)
|
||||
{
|
||||
vg_mem_no_access(chunk, sizeof(*chunk));
|
||||
vg_mem_no_access(list, sizeof(*list));
|
||||
return (split_block(chunk, size));
|
||||
}
|
||||
vg_mem_no_access(chunk, sizeof(*chunk));
|
||||
chunk = get_next_block(chunk, false);
|
||||
}
|
||||
}
|
||||
list_next = list->next;
|
||||
vg_mem_no_access(list, sizeof(*list));
|
||||
list = list_next;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
t_error alloc_new_page(struct s_allocator_melloc *alloc, t_usize page_size)
|
||||
{
|
||||
t_page_list *list;
|
||||
t_page_list *list_bis;
|
||||
t_chunk *chunk;
|
||||
|
||||
page_size = round_to_pow2(page_size, PAGE_POW_2);
|
||||
if (alloc->list == NULL && alloc_page_list(&alloc->list))
|
||||
return (ERROR);
|
||||
list = alloc->list;
|
||||
while (list)
|
||||
{
|
||||
vg_mem_defined(list, sizeof(*list));
|
||||
if (list->len != PAGE_LIST_MAX)
|
||||
break;
|
||||
if (list->next == NULL && alloc_page_list(&list->next))
|
||||
return (vg_mem_no_access(list, sizeof(*list)), ERROR);
|
||||
list_bis = list;
|
||||
list = list->next;
|
||||
vg_mem_no_access(list_bis, sizeof(*list_bis));
|
||||
}
|
||||
if (list == NULL)
|
||||
return (ERROR);
|
||||
vg_mem_defined(list, sizeof(*list));
|
||||
list->pages[list->len].data = __libc_calloc(page_size, 1);
|
||||
if (list->pages[list->len].data == NULL)
|
||||
return (ERROR);
|
||||
list->pages[list->len].size = page_size;
|
||||
|
||||
// eprintf("thingy!\n");
|
||||
// eprintf("(%p)pool_exist = %s\n", list,
|
||||
// VALGRIND_MEMPOOL_EXISTS(list) ? "true" : "false");
|
||||
vg_mempool_alloc(list, list->pages[list->len].data, page_size);
|
||||
chunk = get_first_block(&list->pages[list->len]);
|
||||
chunk->used = false;
|
||||
chunk->size = page_size - sizeof(*chunk) * 2;
|
||||
vg_mem_no_access(chunk, sizeof(*chunk));
|
||||
list->len++;
|
||||
vg_mem_no_access(list, sizeof(*list));
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
void *m_alloc_error(struct s_allocator_melloc *self, t_str msg)
|
||||
{
|
||||
self->uninit((void *)self);
|
||||
me_abort(msg);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void *m_malloc(struct s_allocator_melloc *self, t_usize size)
|
||||
{
|
||||
return (m_realloc(self, NULL, size));
|
||||
}
|
||||
|
||||
void *m_alloc_array(struct s_allocator_melloc *self, t_usize size, t_usize count)
|
||||
void *m_alloc_array(struct s_allocator_melloc *self, t_usize size,
|
||||
t_usize count)
|
||||
{
|
||||
if (size != 0 && count > SIZE_MAX / size)
|
||||
return (m_alloc_error(self, "Alloc array overflow"));
|
||||
return (m_realloc(self, NULL, size * count));
|
||||
}
|
||||
|
||||
#include "stdlib.h"
|
||||
|
||||
void *m_realloc(struct s_allocator_melloc *self, void *ptr, t_usize size)
|
||||
{
|
||||
t_chunk *chunk;
|
||||
t_chunk *next;
|
||||
t_usize old_size;
|
||||
|
||||
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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
else
|
||||
{
|
||||
chunk = ptr - sizeof(*chunk);
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
if (chunk->size >= size)
|
||||
return (vg_mem_no_access(chunk, sizeof(*chunk)), ptr);
|
||||
next = get_next_block(chunk, false);
|
||||
vg_mem_defined(next, sizeof(*next));
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
if (next != NULL && !next->used && chunk->size + next->size + sizeof(*next) >= size)
|
||||
{
|
||||
old_size = chunk->size;
|
||||
chunk->size += next->size + sizeof(*next);
|
||||
vg_mem_undefined(next, next->size + sizeof(*next));
|
||||
vg_block_resize((void *)chunk + sizeof(*chunk), old_size, chunk->size);
|
||||
vg_mem_no_access(chunk, sizeof(*chunk));
|
||||
return (ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
vg_mem_no_access(next, sizeof(*next));
|
||||
next = m_realloc(self, NULL, size);
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
mem_move(next, ptr, chunk->size);
|
||||
vg_mem_no_access(chunk, sizeof(*chunk));
|
||||
m_free(self, ptr);
|
||||
return (next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void *m_realloc_array(struct s_allocator_melloc *self, void *ptr, t_usize size, t_usize count)
|
||||
{
|
||||
if (size != 0 && count > SIZE_MAX / size)
|
||||
return (m_alloc_error(self, "Realloc array overflow"));
|
||||
return (m_realloc(self, ptr, size * count));
|
||||
}
|
||||
|
||||
void m_free(struct s_allocator_melloc *self, void *ptr)
|
||||
{
|
||||
t_chunk *chunk;
|
||||
|
||||
(void)(self);
|
||||
if (ptr == NULL)
|
||||
return;
|
||||
return ;
|
||||
vg_block_free(ptr);
|
||||
chunk = ptr - sizeof(*chunk);
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
|
|
@ -351,9 +71,8 @@ void m_uninit(struct s_allocator_melloc *self)
|
|||
idx++;
|
||||
}
|
||||
list_next = list->next;
|
||||
__libc_free(list);
|
||||
vg_mempool_destroy(list);
|
||||
vg_mem_no_access(list, sizeof(*list));
|
||||
(__libc_free(list), vg_mempool_destroy(list), \
|
||||
vg_mem_no_access(list, sizeof(*list)));
|
||||
list = list_next;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
71
allocator/src/me_alloc/internals.c
Normal file
71
allocator/src/me_alloc/internals.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* internals.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/10 16:49:31 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 17:39:53 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "aq/internal_vg_funcs.h"
|
||||
#include "aq/melloc_interal.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_chunk *get_first_block(t_page *page)
|
||||
{
|
||||
t_chunk *chunk;
|
||||
|
||||
if (page == NULL)
|
||||
return (NULL);
|
||||
vg_mem_defined(page, sizeof(*page));
|
||||
chunk = page->data;
|
||||
vg_mem_no_access(page, sizeof(*page));
|
||||
return (chunk);
|
||||
}
|
||||
|
||||
t_chunk *get_next_block(t_chunk *chunk, bool find_zero)
|
||||
{
|
||||
t_chunk *next;
|
||||
|
||||
if (chunk == NULL)
|
||||
return (NULL);
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
next = (void *)(chunk) + sizeof(*chunk) + chunk->size;
|
||||
vg_mem_no_access(chunk, sizeof(*chunk));
|
||||
vg_mem_defined(next, sizeof(*next));
|
||||
if (!find_zero && next->size == 0)
|
||||
{
|
||||
vg_mem_no_access(next, sizeof(*next));
|
||||
return (NULL);
|
||||
}
|
||||
vg_mem_no_access(next, sizeof(*next));
|
||||
return (next);
|
||||
}
|
||||
|
||||
t_chunk *split_block(t_chunk *chunk, t_usize size)
|
||||
{
|
||||
t_usize remaining;
|
||||
t_chunk *next;
|
||||
t_chunk *ac_next;
|
||||
|
||||
if (chunk == NULL)
|
||||
return (NULL);
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
if (chunk->size > size + sizeof(*chunk) + (2 << PAGE_ALIGN))
|
||||
{
|
||||
remaining = chunk->size - size - sizeof(*chunk);
|
||||
next = get_next_block(chunk, true);
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
chunk->size = size;
|
||||
ac_next = get_next_block(chunk, true);
|
||||
vg_mem_defined(ac_next, sizeof(*ac_next));
|
||||
ac_next->used = false;
|
||||
ac_next->size = remaining;
|
||||
vg_mem_no_access(ac_next, sizeof(*ac_next));
|
||||
}
|
||||
vg_mem_no_access(chunk, sizeof(*chunk));
|
||||
return (chunk);
|
||||
}
|
||||
55
allocator/src/me_alloc/merge_blocks.c
Normal file
55
allocator/src/me_alloc/merge_blocks.c
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* merge_blocks.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/10 17:39:14 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 17:41:30 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/types.h"
|
||||
#include "aq/internal_vg_funcs.h"
|
||||
#include "aq/melloc_interal.h"
|
||||
|
||||
static void _merge_blocks(t_chunk **cur, t_chunk **next, \
|
||||
bool *did_merge)
|
||||
{
|
||||
(vg_mem_defined(*cur, sizeof(**cur)), vg_mem_defined(next,
|
||||
sizeof(**next)));
|
||||
if (!(*cur)->used && !(*next)->used)
|
||||
{
|
||||
*did_merge = true;
|
||||
(*cur)->size += sizeof(**cur);
|
||||
(*cur)->size += (*next)->size;
|
||||
(vg_mem_no_access(*cur, sizeof(**cur)), vg_mem_no_access(*next, \
|
||||
sizeof(**next)));
|
||||
*next = get_next_block(*cur, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
(vg_mem_no_access(*cur, sizeof(**cur)), vg_mem_no_access(*next, \
|
||||
sizeof(**next)));
|
||||
*cur = *next;
|
||||
*next = get_next_block(*cur, false);
|
||||
}
|
||||
}
|
||||
|
||||
void merge_blocks(t_page *page)
|
||||
{
|
||||
t_chunk *cur;
|
||||
t_chunk *next;
|
||||
bool did_merge;
|
||||
|
||||
did_merge = true;
|
||||
while (did_merge)
|
||||
{
|
||||
cur = get_first_block(page);
|
||||
next = get_next_block(cur, false);
|
||||
did_merge = false;
|
||||
while (cur != NULL && next != NULL)
|
||||
_merge_blocks(&cur, &next, &did_merge);
|
||||
}
|
||||
}
|
||||
82
allocator/src/me_alloc/pages.c
Normal file
82
allocator/src/me_alloc/pages.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pages.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/10 16:51:10 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 17:25:19 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>
|
||||
|
||||
void *__libc_malloc(t_usize size);
|
||||
void __libc_free(void *ptr);
|
||||
|
||||
t_error alloc_page_list(t_page_list **out)
|
||||
{
|
||||
t_page_list *val;
|
||||
|
||||
if (out == NULL)
|
||||
return (ERROR);
|
||||
val = __libc_malloc(sizeof(*val));
|
||||
if (val == NULL)
|
||||
return (ERROR);
|
||||
mem_set_zero(val, sizeof(*val));
|
||||
*out = val;
|
||||
vg_mem_no_access(val, sizeof(*val));
|
||||
vg_mempool_create_ext(val, MEMPOOL_FLAG_MALLOCLIKE);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _alloc_new_page_inner(t_usize page_size, t_page_list *list)
|
||||
{
|
||||
t_chunk *chunk;
|
||||
|
||||
if (list == NULL)
|
||||
return (ERROR);
|
||||
vg_mem_defined(list, sizeof(*list));
|
||||
list->pages[list->len].data = __libc_malloc(page_size);
|
||||
mem_set_zero(list->pages[list->len].data, page_size);
|
||||
if (list->pages[list->len].data == NULL)
|
||||
return (ERROR);
|
||||
list->pages[list->len].size = page_size;
|
||||
vg_mempool_alloc(list, list->pages[list->len].data, page_size);
|
||||
chunk = get_first_block(&list->pages[list->len]);
|
||||
chunk->used = false;
|
||||
chunk->size = page_size - sizeof(*chunk) * 2;
|
||||
vg_mem_no_access(chunk, sizeof(*chunk));
|
||||
list->len++;
|
||||
vg_mem_no_access(list, sizeof(*list));
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error alloc_new_page(struct s_allocator_melloc *alloc, t_usize page_size)
|
||||
{
|
||||
t_page_list *list;
|
||||
t_page_list *list_bis;
|
||||
|
||||
page_size = round_to_pow2(page_size, PAGE_POW_2);
|
||||
if (alloc->list == NULL && alloc_page_list(&alloc->list))
|
||||
return (ERROR);
|
||||
list = alloc->list;
|
||||
while (list)
|
||||
{
|
||||
vg_mem_defined(list, sizeof(*list));
|
||||
if (list->len != PAGE_LIST_MAX)
|
||||
break ;
|
||||
if (list->next == NULL && alloc_page_list(&list->next))
|
||||
return (vg_mem_no_access(list, sizeof(*list)), ERROR);
|
||||
list_bis = list;
|
||||
list = list->next;
|
||||
vg_mem_no_access(list_bis, sizeof(*list_bis));
|
||||
}
|
||||
return (_alloc_new_page_inner(page_size, list));
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* 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 */
|
||||
/* Updated: 2024/07/10 17:25:37 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -82,9 +82,6 @@ static void *_realloc_alloc(struct s_allocator_melloc *self, t_usize size)
|
|||
|
||||
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);
|
||||
|
|
@ -95,3 +92,11 @@ void *m_realloc(struct s_allocator_melloc *self, void *ptr, t_usize size)
|
|||
else
|
||||
return (_realloc_inner(self, ptr, size));
|
||||
}
|
||||
|
||||
void *m_realloc_array(struct s_allocator_melloc *self, void *ptr,
|
||||
t_usize size, t_usize count)
|
||||
{
|
||||
if (size != 0 && count > SIZE_MAX / size)
|
||||
return (m_alloc_error(self, "Realloc array overflow"));
|
||||
return (m_realloc(self, ptr, size * count));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/12 22:33:30 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/17 15:29:59 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 17:24:32 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,12 +14,6 @@
|
|||
|
||||
#ifndef VGFUNCS
|
||||
|
||||
void vg_mempool_create_ext(void *pool, t_usize flags)
|
||||
{
|
||||
(void)(pool);
|
||||
(void)(flags);
|
||||
}
|
||||
|
||||
void vg_mempool_resize(void *pool, void *ptr, t_usize size)
|
||||
{
|
||||
(void)(pool);
|
||||
|
|
|
|||
23
allocator/src/vg/dummy_mempool_bis.c
Normal file
23
allocator/src/vg/dummy_mempool_bis.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* dummy_mempool_bis.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/12 22:33:30 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 17:24:39 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "aq/internal_vg_funcs.h"
|
||||
|
||||
#ifndef VGFUNCS
|
||||
|
||||
void vg_mempool_create_ext(void *pool, t_usize flags)
|
||||
{
|
||||
(void)(pool);
|
||||
(void)(flags);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/12 22:33:30 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/17 15:30:09 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 17:24:19 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,18 +15,6 @@
|
|||
|
||||
#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, 0, ZEROED_POOL, actual_flags);
|
||||
}
|
||||
|
||||
void vg_mempool_resize(void *pool, void *ptr, t_usize size)
|
||||
{
|
||||
VALGRIND_MEMPOOL_CHANGE(pool, ptr, ptr, size);
|
||||
|
|
|
|||
30
allocator/src/vg/valgrind_mempool_bis.c
Normal file
30
allocator/src/vg/valgrind_mempool_bis.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* valgrind_mempool_bis.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/12 22:33:30 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 17:24:11 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, 0, ZEROED_POOL, actual_flags);
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue