Made a memory allocator (crude)
This commit is contained in:
parent
b5c7344851
commit
941bac31b6
53 changed files with 469 additions and 146 deletions
89
stdme/src/alloc/alloc.c
Normal file
89
stdme/src/alloc/alloc.c
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* alloc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/07 14:54:56 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/alloc/alloc.h"
|
||||
#include "me/alloc/alloc_internal.h"
|
||||
#include "me/fs/putstr_fd.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
#include "me/num/usize.h"
|
||||
#include <stdalign.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// the `+ 16` is twice the size of size_t, only to stay aligned on a 16 byte
|
||||
// alignement
|
||||
void *me_malloc(t_usize size)
|
||||
{
|
||||
t_arena_page *arena;
|
||||
void *ret;
|
||||
|
||||
size = usize_round_up_to(size, 16);
|
||||
if (size + 16 > ARENA_SIZE)
|
||||
me_abort();
|
||||
arena = get_head_arena();
|
||||
while (arena->next != NULL && arena->current_index + 16 + size > ARENA_SIZE)
|
||||
arena = arena->next;
|
||||
if (arena->current_index + 16 + size > ARENA_SIZE)
|
||||
{
|
||||
if (alloc_arena(&arena->next))
|
||||
me_abort();
|
||||
arena = arena->next;
|
||||
}
|
||||
*(t_usize *)&arena->bytes[arena->current_index] = size;
|
||||
ret = (void *)&arena->bytes[arena->current_index + 16];
|
||||
arena->current_index += 16 + 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_arena_page *arena;
|
||||
t_usize size;
|
||||
void *ret;
|
||||
|
||||
arena = get_head_arena();
|
||||
while (arena != NULL && !((void *)&arena->bytes <= ptr &&
|
||||
ptr <= (void *)(&arena->bytes) + ARENA_SIZE))
|
||||
arena = arena->next;
|
||||
if (arena == NULL)
|
||||
return (NULL);
|
||||
size = *(t_usize *)((t_u8 *)(ptr)-16);
|
||||
if (size <= new_size)
|
||||
return (ptr);
|
||||
ret = me_malloc(new_size);
|
||||
mem_copy(ret, ptr, size);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
void me_free(void *ptr)
|
||||
{
|
||||
t_arena_page *arena;
|
||||
|
||||
arena = get_head_arena();
|
||||
while (arena != NULL && !((void *)&arena->bytes <= ptr &&
|
||||
ptr <= (void *)(&arena->bytes) + ARENA_SIZE))
|
||||
arena = arena->next;
|
||||
if (arena == NULL)
|
||||
{
|
||||
me_putstr_fd("Tried to free with me_free !\n", 2);
|
||||
free(ptr);
|
||||
}
|
||||
}
|
||||
45
stdme/src/alloc/get_arena.c
Normal file
45
stdme/src/alloc/get_arena.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_arena.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/07 14:20:20 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/alloc/alloc.h"
|
||||
#include "me/alloc/alloc_internal.h"
|
||||
#include "me/fs/putstr_fd.h"
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void *__libc_malloc(size_t size);
|
||||
|
||||
t_arena_page *get_head_arena(void)
|
||||
{
|
||||
static t_arena_page *val = NULL;
|
||||
|
||||
if (val == NULL)
|
||||
{
|
||||
if (alloc_arena(&val))
|
||||
{
|
||||
me_putstr_fd("Error: malloc failed\n", 2);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
return (val);
|
||||
}
|
||||
|
||||
t_error alloc_arena(t_arena_page **out)
|
||||
{
|
||||
if (out == NULL)
|
||||
return (ERROR);
|
||||
*out = __libc_malloc(sizeof(**out));
|
||||
if (*out == NULL)
|
||||
return (ERROR);
|
||||
mem_set_zero(*out, sizeof(**out));
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
|
@ -6,15 +6,15 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/16 17:52:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/04/30 14:14:03 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/05/07 15:04:07 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/buffered_str/buf_str.h"
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/mem/mem_realloc.h"
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
#include "me/string/str_l_cat.h"
|
||||
#include "me/string/str_l_copy.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
|
@ -29,11 +29,9 @@ t_error str_reserve(t_buffer_str *buf, t_usize size)
|
|||
while (size > buf->capacity)
|
||||
{
|
||||
new_capacity = (buf->capacity * 3) / 2 + 1;
|
||||
temp_buffer = mem_alloc(new_capacity);
|
||||
temp_buffer = mem_realloc(buf->buf, new_capacity);
|
||||
if (temp_buffer == NULL)
|
||||
return (true);
|
||||
str_l_copy(temp_buffer, buf->buf, new_capacity);
|
||||
free(buf->buf);
|
||||
buf->buf = temp_buffer;
|
||||
buf->capacity = new_capacity;
|
||||
}
|
||||
|
|
@ -52,11 +50,9 @@ bool push_str_buffer(t_buffer_str *buf, t_const_str to_push)
|
|||
while (buf->len + to_push_len + 2 > buf->capacity)
|
||||
{
|
||||
new_capacity = (buf->capacity * 3) / 2 + 1;
|
||||
temp_buffer = mem_alloc(new_capacity);
|
||||
temp_buffer = mem_realloc(buf->buf, new_capacity);
|
||||
if (temp_buffer == NULL)
|
||||
return (true);
|
||||
str_l_copy(temp_buffer, buf->buf, new_capacity);
|
||||
free(buf->buf);
|
||||
buf->buf = temp_buffer;
|
||||
buf->capacity = new_capacity;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ static bool handle_leftovers(t_file fd, char *temp_buffer, t_buffer_str *buf)
|
|||
if (copy_next_line_into_buffer(fd, buf, temp_buffer,
|
||||
str_len(static_buffer->buf)))
|
||||
{
|
||||
free(temp_buffer);
|
||||
me_free(temp_buffer);
|
||||
return (true);
|
||||
}
|
||||
}
|
||||
|
|
@ -118,10 +118,10 @@ t_buffer_str get_next_line(t_file fd, bool *error)
|
|||
return (buf);
|
||||
while (!read_and_copy(fd, &buf, temp_buffer, &flags) && !flags.empty_read)
|
||||
;
|
||||
free(temp_buffer);
|
||||
me_free(temp_buffer);
|
||||
if (flags.error || flags.empty_read)
|
||||
{
|
||||
free(buf.buf);
|
||||
me_free(buf.buf);
|
||||
return (*error = true, (t_buffer_str){0});
|
||||
}
|
||||
return (buf);
|
||||
|
|
|
|||
|
|
@ -101,6 +101,6 @@ t_u64 sip13_finish(t_sip13 *self)
|
|||
state.v2 ^= 0xff;
|
||||
compress(&state);
|
||||
compress(&state);
|
||||
free(self);
|
||||
me_free(self);
|
||||
return (state.v0 ^ state.v1 ^ state.v2 ^ state.v3);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ t_i32 qoi_write(t_const_str filename, const void *data,
|
|||
if (me_write(f, encoded, size))
|
||||
return (me_close(f, NULL), 0);
|
||||
me_close(f, NULL);
|
||||
free(encoded);
|
||||
me_free(encoded);
|
||||
return (size);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ void list_free_all(t_list **lst, void (*del)(void *))
|
|||
del((*lst)->content);
|
||||
tmp = *lst;
|
||||
*lst = (*lst)->next;
|
||||
free(tmp);
|
||||
me_free(tmp);
|
||||
}
|
||||
*lst = NULL;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,5 +18,5 @@ void list_free_one(t_list *lst, void (*del)(void *))
|
|||
if (lst == NULL || del == NULL)
|
||||
return ;
|
||||
del(lst->content);
|
||||
free(lst);
|
||||
me_free(lst);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,21 +6,14 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/06 14:47:49 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 18:14:11 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/05/07 12:45:31 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include <stdlib.h>
|
||||
#include "me/alloc/alloc.h"
|
||||
|
||||
void *mem_alloc(t_usize size)
|
||||
{
|
||||
void *out;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
out = malloc(size);
|
||||
while (out && i < size)
|
||||
((t_u8 *)out)[i++] = 0;
|
||||
return (out);
|
||||
return (me_malloc(size));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,20 +6,15 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 15:53:21 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 18:14:47 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/05/07 12:46:04 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/alloc/alloc.h"
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void *mem_alloc_array(t_usize item_count, t_usize item_size)
|
||||
{
|
||||
t_usize multiplied;
|
||||
|
||||
multiplied = item_count * item_size;
|
||||
if (multiplied == 0 || multiplied / item_count != item_size)
|
||||
return (NULL);
|
||||
return (mem_alloc(multiplied));
|
||||
return (me_calloc(item_count, item_size));
|
||||
}
|
||||
|
|
|
|||
19
stdme/src/mem/mem_realloc.c
Normal file
19
stdme/src/mem/mem_realloc.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mem_realloc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 12:46:18 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/07 12:47:12 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_realloc.h"
|
||||
#include "me/alloc/alloc.h"
|
||||
|
||||
void *mem_realloc(void *ptr, t_usize size)
|
||||
{
|
||||
return (me_realloc(ptr, size));
|
||||
}
|
||||
25
stdme/src/num/usize/round_up.c
Normal file
25
stdme/src/num/usize/round_up.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* round_up.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 11:04:51 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/07 11:06:22 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/num/usize.h"
|
||||
|
||||
t_usize usize_round_up_to(t_usize self, t_usize mul)
|
||||
{
|
||||
t_usize mod;
|
||||
|
||||
if (mul == 0)
|
||||
return (self);
|
||||
mod = self % mul;
|
||||
if (mod == 0)
|
||||
return (self);
|
||||
return (self + (mul - mod));
|
||||
}
|
||||
20
stdme/src/os/abort.c
Normal file
20
stdme/src/os/abort.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* abort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/07 13:09:59 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/alloc/alloc_internal.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void me_abort(void)
|
||||
{
|
||||
me_exit(1);
|
||||
}
|
||||
30
stdme/src/os/exit.c
Normal file
30
stdme/src/os/exit.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* exit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/07 15:01:38 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/types.h"
|
||||
#include "me/alloc/alloc_internal.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void me_exit(t_i32 exit_code)
|
||||
{
|
||||
t_arena_page *arena;
|
||||
t_arena_page *tmp;
|
||||
|
||||
arena = get_head_arena();
|
||||
while (arena)
|
||||
{
|
||||
tmp = arena->next;
|
||||
free(arena);
|
||||
arena = tmp;
|
||||
}
|
||||
exit(exit_code);
|
||||
}
|
||||
|
|
@ -73,8 +73,8 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
|
|||
}
|
||||
sp_index = 0;
|
||||
while (splitted_path[sp_index])
|
||||
free(splitted_path[sp_index++]);
|
||||
free(splitted_path);
|
||||
me_free(splitted_path[sp_index++]);
|
||||
me_free(splitted_path);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
|
|
@ -97,7 +97,7 @@ t_error find_binary(t_spawn_info *info, t_process *process)
|
|||
}
|
||||
if (access(s.buf, X_OK | R_OK) == 0)
|
||||
{
|
||||
free(info->binary_path);
|
||||
me_free(info->binary_path);
|
||||
info->binary_path = s.buf;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
|
@ -117,7 +117,7 @@ static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
|
|||
close(info.stderr.vals.fd.value);
|
||||
vec_str_free(info.arguments);
|
||||
vec_str_free(info.environement);
|
||||
free(info.binary_path);
|
||||
me_free(info.binary_path);
|
||||
}
|
||||
|
||||
t_error spawn_process(t_spawn_info info, t_process *process)
|
||||
|
|
|
|||
|
|
@ -66,5 +66,5 @@ void printf_s(t_printf_arg data, t_printf_func f)
|
|||
.fill_zero = 0, .fill = 0, .len = len, .pretty = "", .pretty_len = 0, \
|
||||
.str = start_num, .allow_zero_fill = false, .sign = NULL, \
|
||||
.sign_len = 0, }, data, f);
|
||||
free(start_num);
|
||||
me_free(start_num);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,5 +85,5 @@ void printf_d(t_printf_arg data, t_printf_func f)
|
|||
.fill_zero = 0, .fill = 0, .sign = sign, .pretty = NULL, .len = \
|
||||
str_len(start_num), .pretty_len = 0, .str = start_num, .allow_zero_fill \
|
||||
= true, .sign_len = str_len(sign), }, data, f);
|
||||
free(start_num);
|
||||
me_free(start_num);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,5 +72,5 @@ void printf_u(t_printf_arg data, t_printf_func f)
|
|||
.fill_zero = 0, .fill = 0, .len = str_len(start_num), \
|
||||
.pretty = NULL, .pretty_len = 0, .str = start_num, \
|
||||
.allow_zero_fill = true, .sign = NULL, .sign_len = 0, }, data, f);
|
||||
free(start_num);
|
||||
me_free(start_num);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ t_printf_arg print_substr(t_usize *c_idx, t_usize *nxt, t_const_str fmt,
|
|||
|
||||
truc = str_substring(fmt, *c_idx, *nxt - *c_idx);
|
||||
extra.f(truc, *nxt - *c_idx, extra.p_args);
|
||||
free(truc);
|
||||
me_free(truc);
|
||||
*c_idx = *nxt + 1;
|
||||
return ((t_printf_arg){
|
||||
.p_args = extra.p_args,
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs,
|
|||
data->flags &= (~ZERO_ALIGN);
|
||||
data->flags |= ALIGN;
|
||||
if (strs.free_out)
|
||||
*strs.out = (free(*strs.out), (t_str)mem_alloc_array(1, 1));
|
||||
*strs.out = (me_free(*strs.out), (t_str)mem_alloc_array(1, 1));
|
||||
else
|
||||
*strs.out = "";
|
||||
*strs.pretty = "";
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ t_usize me_printf(t_const_str fmt, ...)
|
|||
va_end(args);
|
||||
len = str_len(str);
|
||||
write(1, str, len);
|
||||
free(str);
|
||||
me_free(str);
|
||||
return (len);
|
||||
}
|
||||
|
||||
|
|
@ -114,7 +114,7 @@ t_usize me_eprintf(t_const_str fmt, ...)
|
|||
va_end(args);
|
||||
len = str_len(str);
|
||||
write(2, str, len);
|
||||
free(str);
|
||||
me_free(str);
|
||||
return (len);
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -101,8 +101,8 @@ int main(void) {
|
|||
printf("\n");
|
||||
printf("\n");
|
||||
}
|
||||
free(dest_libc);
|
||||
free(dest_ft);
|
||||
me_free(dest_libc);
|
||||
me_free(dest_ft);
|
||||
}
|
||||
}
|
||||
R*/
|
||||
|
|
|
|||
|
|
@ -97,8 +97,8 @@ int main(void) {
|
|||
printf("\n");
|
||||
printf("\n");
|
||||
}
|
||||
free(dest_libc);
|
||||
free(dest_ft);
|
||||
me_free(dest_libc);
|
||||
me_free(dest_ft);
|
||||
}
|
||||
}
|
||||
R*/
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ static t_usize local_count_words(t_const_str str, char chr)
|
|||
static t_str *local_split_freeall(t_str **to_free)
|
||||
{
|
||||
while (*to_free)
|
||||
free(*(to_free++));
|
||||
me_free(*(to_free++));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue