hopefully a working allocator !

This commit is contained in:
Maix0 2024-05-10 21:40:19 +02:00
parent a149e9a860
commit b639b0c14e
8 changed files with 53 additions and 35 deletions

View file

@ -30,6 +30,6 @@ typedef struct s_node
t_node build_node(t_parse_node curr, t_const_str input);
t_str node_getstr(t_node *node);
void free_node(t_node t);
void free_node(t_node self);
#endif /* NODE_H */

View file

@ -32,7 +32,9 @@ t_hashmap_env *new_hashmap_with_buckets_env(
t_drop_env_fn drop, t_usize buckets)
{
t_hashmap_env *hmap;
t_usize i;
i = 0;
hmap = mem_alloc(sizeof(*hmap));
if (hmap == NULL)
return (NULL);
@ -44,6 +46,8 @@ t_hashmap_env *new_hashmap_with_buckets_env(
hmap->drop = drop;
if (hmap->buckets == NULL)
return ((void)me_free(hmap), NULL);
while (i < buckets)
hmap->buckets[i++] = NULL;
return (hmap);
}

View file

@ -41,6 +41,8 @@ void ft_free_utils(t_utils *s)
free(s->str_input);
if (s->path)
ft_free_strs(s->path);
if (s->env)
drop_hashmap_env(s->env);
ts_parser_delete(s->parser.parser);
}

View file

@ -117,6 +117,8 @@ void ft_take_args(t_utils *shcat)
shcat->str_input = readline((t_const_str)shcat->name_shell);
if (!shcat->str_input)
ft_exit(shcat, 0);
else if (ft_strcmp(shcat->str_input, "exit") == 0)
ft_exit(shcat, 0);
shcat->current_node = parse_str(&shcat->parser, shcat->str_input);
exec_shcat(shcat);
add_history(shcat->str_input);
@ -170,8 +172,8 @@ t_i32 main(t_i32 argc, t_str argv[], t_str envp[])
if (install_signal())
return (1);
utils = (t_utils){};
utils.env = create_env_map();
utils.parser = create_myparser();
utils.env = create_env_map();
utils.name_shell = "\001\x1B[93m\002"
"42sh"
"\001\x1B[32m\002"

View file

@ -81,15 +81,15 @@ t_str node_getstr(t_node *node)
return (node->single_str);
}
void free_node(t_node t)
void free_node(t_node self)
{
// t_usize idx;
//
// idx = 0;
// while (idx < t.childs_count)
// free_node(t.childs[idx++]);
// free(t.childs);
// if (t.single_str != NULL)
// free(t.single_str);
(void)(t);
t_usize idx;
if (self.single_str)
me_free(self.single_str);
idx = 0;
while (idx < self.childs_count)
free_node(self.childs[idx++]);
if (self.childs_count != 0)
me_free(self.childs);
}

View file

@ -17,8 +17,8 @@
#include "me/types.h"
#include <stdalign.h>
#define PAGE_SIZE_DEFAULT 4096 * 4
#define BLOCK_PADDING "\xFE\xDC\xAB\xC0\xFE\xEE"
#define PAGE_SIZE_DEFAULT 4096
#define BLOCK_PADDING "\xFE\xDC\xAB\xC0\xFE\xEE\x66"
typedef struct s_mblock
{

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */
/* Updated: 2024/05/09 17:57:54 by maiboyer ### ########.fr */
/* Updated: 2024/05/10 21:39:07 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,6 +31,7 @@ void *me_malloc(t_usize size)
if (block == NULL)
return (me_abort("Found no page for me_malloc"), NULL);
block->used = true;
mem_set_zero((t_u8 *)block + sizeof(*block), block->size);
return ((void *)(((t_usize)block) + sizeof(t_mblock)));
}
@ -51,10 +52,21 @@ void *me_realloc(void *ptr, t_usize new_size)
block = (void *)((t_u8 *)(ptr) - sizeof(t_mblock));
if (block->size <= new_size)
return (ptr);
if (block->next && block->next->page == block->page && !block->used &&
block->next->size + block->size + sizeof(t_mblock) >= new_size)
{
block->size = block->size + block->next->size + sizeof(t_mblock);
block->next = block->next->next;
return (ptr);
}
else
{
ret = me_malloc(new_size);
mem_copy(ret, ptr, block->size);
me_free(ptr);
return (ret);
}
}
void me_free(void *ptr)

View file

@ -28,21 +28,18 @@ t_mpage *alloc_page(t_usize size)
{
t_mpage *val;
size = usize_round_up_to(size, PAGE_SIZE_DEFAULT);
val = __libc_malloc(PAGE_SIZE_DEFAULT);
if (val == NULL || sizeof(t_mpage) >= PAGE_SIZE_DEFAULT)
size = usize_round_up_to(size + sizeof(t_mpage), PAGE_SIZE_DEFAULT);
val = __libc_malloc(size);
if (val == NULL || sizeof(t_mpage) + sizeof(t_mblock) >= PAGE_SIZE_DEFAULT)
return (NULL);
val->next = NULL;
val->page_size = PAGE_SIZE_DEFAULT;
val->page_size = size;
val->first = (t_mblock *)(((t_usize)val) + sizeof(t_mpage));
val->first->page = val;
val->first->next = NULL;
mem_copy(val->first->padding, BLOCK_PADDING, 7);
val->first->used = false;
val->first->size = PAGE_SIZE_DEFAULT - sizeof(t_mpage);
me_putstr_fd("Allocating a page of size ", 2);
me_putnbr_fd(size, 2);
me_putendl_fd("", 2);
val->first->size = size - sizeof(t_mblock) - sizeof(t_mpage);
return (val);
}
@ -50,8 +47,8 @@ t_mpage *get_head_arena(void)
{
static t_mpage *val = NULL;
if (val == NULL)
val = alloc_page(PAGE_SIZE_DEFAULT);
if (val == NULL && PAGE_SIZE_DEFAULT > sizeof(t_mpage))
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);
@ -63,15 +60,16 @@ t_mblock *split_block(t_mblock *self, t_usize min_size)
t_mblock *old_next;
min_size = usize_round_up_to(min_size, 16);
if (self->size > (min_size + sizeof(t_mpage) + 16))
if (self->size > (min_size + sizeof(t_mblock) + 16))
{
remaining = self->size - min_size - sizeof(t_mpage);
printf("Splitting %zu into %zu and %zu\n", self->size, min_size,
remaining);
remaining = self->size - min_size - sizeof(t_mblock);
printf("splitting %zu into %zu and %zu\n", self->size, min_size, remaining);
if (self->size == 80 && min_size == 16 && remaining == 32)
printf("HERE\n");
self->size = min_size;
old_next = self->next;
self->next =
(t_mblock *)(((t_usize)self) + self->size + sizeof(t_mpage));
(t_mblock *)(((t_usize)self) + self->size + sizeof(t_mblock));
self->next->page = self->page;
self->next->next = old_next;
self->next->used = false;
@ -97,7 +95,7 @@ t_mblock *get_block_for_size(t_usize size)
}
if (last == NULL)
return (NULL);
last->page->next = alloc_page(size);
last->page->next = alloc_page(size + sizeof(t_mblock));
if (last->page->next == NULL)
me_abort("Failed to alloc page!");
last->next = last->page->next->first;