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

View file

@ -6,13 +6,13 @@
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/03 13:20:01 by maiboyer #+# #+# #
# Updated: 2024/05/09 21:27:31 by maiboyer ### ########.fr #
# Updated: 2024/05/14 18:41:46 by maiboyer ### ########.fr #
# #
# **************************************************************************** #
BUILD_DIR ?= build
SRC_DIR = src
INCLUDE_DIR = include output/include
INCLUDE_DIR = include output/include ../allocator/include
LIBS_DIR = .
GENERIC_DIR = output/src
GENERIC_INCLUDE = output/include

View file

@ -43,7 +43,7 @@ t_hashmap_C__PREFIX__ *new_hashmap_with_buckets_C__PREFIX__(
hmap->cfunc = cfunc;
hmap->drop = drop;
if (hmap->buckets == NULL)
return ((void)me_free(hmap), NULL);
return ((void)mem_free(hmap), NULL);
return (hmap);
}
@ -57,13 +57,13 @@ void drop_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap)
if (hmap->buckets[index])
{
hmap->drop(hmap->buckets[index]->kv);
me_free(hmap->buckets[index]);
mem_free(hmap->buckets[index]);
}
index++;
}
hasher_finish(&hmap->hasher);
me_free(hmap->buckets);
me_free(hmap);
mem_free(hmap->buckets);
mem_free(hmap);
}
t_entry_C__PREFIX__ *hashmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,

View file

@ -51,6 +51,6 @@ void remove_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key)
else
prev->next = entry->next;
hmap->drop(entry->kv);
me_free(entry);
mem_free(entry);
hmap->buckets[hashed_key % hmap->num_buckets] = NULL;
}

View file

@ -14,7 +14,7 @@
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/alloc/alloc.h"
#include "me/mem/mem.h"
#include "me/vec/vec_C__PREFIX__.h"
#include <stdlib.h>
@ -43,7 +43,7 @@ t_error vec_C__PREFIX___push(t_vec_C__PREFIX__ *vec, C__TYPENAME__ element)
new_capacity = (vec->capacity * 3) / 2 + 1;
while (vec->len + 1 > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(C__TYPENAME__));
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(C__TYPENAME__));
vec->capacity = new_capacity;
}
vec->buffer[vec->len] = element;
@ -63,7 +63,7 @@ t_error vec_C__PREFIX___reserve(t_vec_C__PREFIX__ *vec, t_usize wanted_capacity)
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(C__TYPENAME__));
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(C__TYPENAME__));
vec->capacity = new_capacity;
}
return (NO_ERROR);
@ -100,5 +100,5 @@ void vec_C__PREFIX___free(t_vec_C__PREFIX__ vec)
vec.len--;
}
}
me_free(vec.buffer);
mem_free(vec.buffer);
}

View file

@ -1,34 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_dumb_internal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 15:03:59 by maiboyer #+# #+# */
/* Updated: 2024/05/12 16:42:39 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOC_DUMB_INTERNAL_H
#define ALLOC_DUMB_INTERNAL_H
#include "me/types.h"
#define PTR_LENS 255
typedef struct s_ptr
{
void *ptr;
t_usize size;
} t_ptr;
typedef struct s_ptr_table
{
t_ptr table[PTR_LENS];
struct s_ptr_table *next;
} t_ptr_table;
t_ptr_table *get_table(void);
#endif /* ALLOC_DUMB_INTERNAL_H */

View file

@ -1,52 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_internal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */
/* Updated: 2024/05/12 14:02:32 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOC_INTERNAL_H
#define ALLOC_INTERNAL_H
#include "me/alloc/alloc.h"
#include "me/types.h"
#include <stdalign.h>
#define PAGE_SIZE_DEFAULT 4096
#define BLOCK_PADDING "\xFE\xDC\xAB\xC0\xFE\xEE\x66"
typedef struct s_mblock
{
struct s_mblock *next;
struct s_mpage *page;
t_usize size;
t_usize offset;
bool used;
t_u8 padding[7];
} t_mblock;
typedef struct s_mpage
{
t_usize page_size;
void *data;
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);
t_mpage *get_page_from_ptr(void *ptr);
t_mblock *get_block_from_ptr(void *ptr);
t_error merge_next_block(t_mblock *self, t_usize min_size);
#endif /* ALLOC_INTERNAL_H */

View file

@ -56,7 +56,7 @@ typedef struct s_blx
void *win;
t_blx_input inputs;
t_run_function func;
t_free_function me_free;
t_free_function mem_free;
t_blx_app app;
t_blx_data _data;
} t_blx;

View file

@ -29,9 +29,9 @@ t_error str_reserve(t_buffer_str *buf, t_usize size);
static inline void str_free(t_buffer_str buf)
{
void me_free(void *);
void mem_free(void *);
me_free(buf.buf);
mem_free(buf.buf);
}
static inline char str_pop(t_buffer_str *buf)

View file

@ -64,7 +64,7 @@ See the function declaration below for the signature and more information.
If you don't want/need the qoi_read and qoi_write functions, you can define
QOI_NO_STDIO before including this library.
This library uses malloc() and me_free(). To supply your own malloc implementation
This library uses malloc() and mem_free(). To supply your own malloc implementation
you can define QOI_MALLOC and QOI_FREE before including this library.
This library uses memset() to zero-initialize the index. To supply your own
@ -273,7 +273,7 @@ The function either returns NULL on failure (invalid data, or malloc or fopen
failed) or a pot_i32er to the decoded pixels. On success, the t_qoi_desc struct
will be filled with the description from the file header.
The returned pixel data should be me_free()d after use. */
The returned pixel data should be mem_free()d after use. */
void *qoi_read(t_const_str filename, t_qoi_desc *desc,
t_i32 channels);
@ -286,7 +286,7 @@ The function either returns NULL on failure (invalid parameters or malloc
failed) or a pot_i32er to the encoded data on success. On success the out_len
is set to the size in bytes of the encoded data.
The returned qoi data should be me_free()d after use. */
The returned qoi data should be mem_free()d after use. */
void *qoi_encode(const void *data, const t_qoi_desc *desc,
t_i32 *out_len);
@ -297,7 +297,7 @@ The function either returns NULL on failure (invalid parameters or malloc
failed) or a pot_i32er to the decoded pixels. On success, the t_qoi_desc struct
is filled with the description from the file header.
The returned pixel data should be me_free()d after use. */
The returned pixel data should be mem_free()d after use. */
void *qoi_decode(const void *data, t_i32 size, t_qoi_desc *desc,
t_i32 channels);

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_add_back.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:38:45 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:27:56 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_ADD_BACK_H
# define LIST_ADD_BACK_H
# include "me/types.h"
void list_add_back(t_list **list, t_list *new);
#endif

View file

@ -1,19 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_add_front.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:15:23 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:28:15 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_ADD_FRONT_H
# define LIST_ADD_FRONT_H
# include "me/types.h"
void list_add_front(t_list **lst, t_list *new);
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_alloc_node.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 19:57:28 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:28:30 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_ALLOC_NODE_H
# define LIST_ALLOC_NODE_H
# include "me/types.h"
t_list *list_alloc_node(void *content);
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_free_one.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:30:20 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:29:15 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_FREE_ONE_H
# define LIST_FREE_ONE_H
# include "me/types.h"
void list_free_one(t_list *lst, void (*del)(void *));
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_get_last.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:37:08 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:29:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_GET_LAST_H
# define LIST_GET_LAST_H
# include "me/types.h"
t_list *list_get_last(t_list *list);
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_iter.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:39:05 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:29:51 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_ITER_H
# define LIST_ITER_H
# include "me/types.h"
void list_iter(t_list *list, void (*f)(void *));
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_map.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:40:24 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:30:08 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_MAP_H
# define LIST_MAP_H
# include "me/types.h"
t_list *list_map(t_list *lst, void *(*f)(void *), void (*del)(void *));
#endif

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_size.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:23:19 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:30:23 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_SIZE_H
# define LIST_SIZE_H
# include "me/types.h"
t_usize list_size(t_list *lst);
#endif

View file

@ -1,20 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_free_all.h :+: :+: :+: */
/* _allocator.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:35:20 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:28:47 by maiboyer ### ########.fr */
/* Created: 2024/05/14 18:27:07 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:27:40 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_FREE_ALL_H
# define LIST_FREE_ALL_H
#ifndef _ALLOCATOR_H
#define _ALLOCATOR_H
# include "me/types.h"
#include "aq/allocator.h"
void list_free_all(t_list **lst, void (*del)(void *));
t_allocator *global_allocator(void);
void uninit_global_allocator(void);
#endif
#endif /* _ALLOCATOR_H */

View file

@ -1,23 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc.h :+: :+: :+: */
/* mem.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:42:02 by maiboyer #+# #+# */
/* Updated: 2024/05/12 17:10:51 by maiboyer ### ########.fr */
/* Created: 2024/05/14 18:32:57 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:39:13 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOC_H
#define ALLOC_H
#ifndef MEM_H
#define MEM_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 *mem_alloc(t_usize size);
void *mem_alloc_array(t_usize size, t_usize count);
void *mem_realloc(void *ptr, t_usize size);
void *mem_realloc_array(void *ptr, t_usize size, t_usize count);
void mem_free(void *ptr);
#endif /* ALLOC_H */
#endif /* MEM_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 14:31:12 by maiboyer #+# #+# */
/* Updated: 2024/05/08 19:27:44 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:41:05 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,18 +35,11 @@ typedef size_t t_usize;
typedef float t_f32;
typedef double t_f64;
typedef int t_file;
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
typedef int t_file;
typedef bool t_error;
void me_abort(t_str msg);
void me_exit(t_i32 code);
void me_free(void *ptr);
void print_trace(void);
#define ERROR 1

View file

@ -14,7 +14,7 @@
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/alloc/alloc.h"
#include "me/mem/mem.h"
#include "me/vec/vec_buf_str.h"
#include <stdlib.h>
@ -43,7 +43,7 @@ t_error vec_buf_str_push(t_vec_buf_str *vec, t_buffer_str element)
new_capacity = (vec->capacity * 3) / 2 + 1;
while (vec->len + 1 > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_buffer_str));
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_buffer_str));
vec->capacity = new_capacity;
}
vec->buffer[vec->len] = element;
@ -63,7 +63,7 @@ t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity)
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_buffer_str));
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_buffer_str));
vec->capacity = new_capacity;
}
return (NO_ERROR);
@ -100,5 +100,5 @@ void vec_buf_str_free(t_vec_buf_str vec)
vec.len--;
}
}
me_free(vec.buffer);
mem_free(vec.buffer);
}

View file

@ -14,7 +14,7 @@
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/alloc/alloc.h"
#include "me/mem/mem.h"
#include "me/vec/vec_str.h"
#include <stdlib.h>
@ -43,7 +43,7 @@ t_error vec_str_push(t_vec_str *vec, t_str element)
new_capacity = (vec->capacity * 3) / 2 + 1;
while (vec->len + 1 > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_str));
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_str));
vec->capacity = new_capacity;
}
vec->buffer[vec->len] = element;
@ -63,7 +63,7 @@ t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity)
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_str));
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_str));
vec->capacity = new_capacity;
}
return (NO_ERROR);
@ -100,5 +100,5 @@ void vec_str_free(t_vec_str vec)
vec.len--;
}
}
me_free(vec.buffer);
mem_free(vec.buffer);
}

View file

@ -14,7 +14,7 @@
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/alloc/alloc.h"
#include "me/mem/mem.h"
#include "me/vec/vec_u8.h"
#include <stdlib.h>
@ -43,7 +43,7 @@ t_error vec_u8_push(t_vec_u8 *vec, t_u8 element)
new_capacity = (vec->capacity * 3) / 2 + 1;
while (vec->len + 1 > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_u8));
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_u8));
vec->capacity = new_capacity;
}
vec->buffer[vec->len] = element;
@ -63,7 +63,7 @@ t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity)
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = me_realloc_array(vec->buffer, new_capacity, sizeof(t_u8));
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_u8));
vec->capacity = new_capacity;
}
return (NO_ERROR);
@ -100,5 +100,5 @@ void vec_u8_free(t_vec_u8 vec)
vec.len--;
}
}
me_free(vec.buffer);
mem_free(vec.buffer);
}

View file

@ -1,7 +1,3 @@
alloc/alloc
alloc/alloc_dumb
alloc/alloc_get_page_from_bloc
alloc/get_arena
blx/blx
blx/blx_create_fontsheet
blx/blx_handlers
@ -54,15 +50,7 @@ img/qoi/qoi_decode
img/qoi/qoi_encode
img/qoi/qoi_fs
img/qoi/qoi_utils
list/list_add_back
list/list_add_front
list/list_alloc_node
list/list_free_all
list/list_free_one
list/list_get_last
list/list_iter
list/list_map
list/list_size
mem/allocator
mem/mem_alloc
mem/mem_alloc_array
mem/mem_compare

View file

@ -1,83 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */
/* Updated: 2024/05/12 15:02:27 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 <stdio.h>
#include <stdlib.h>
void __libc_free(void *ptr);
// 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);
// me_free(ptr);
// return (ret);
// }
// }
//
// void me_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);
// }

View file

@ -1,140 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_dumb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 15:02:37 by maiboyer #+# #+# */
/* Updated: 2024/05/12 17:09:41 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/alloc/alloc.h"
#include "me/alloc/alloc_dumb_internal.h"
#include "me/fs/putendl_fd.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include <stdlib.h>
void *__libc_malloc(size_t size);
void __libc_free(void *ptr);
t_ptr_table *get_table(void)
{
static t_ptr_table *table = NULL;
if (table == NULL)
{
table = __libc_malloc(sizeof(*table));
if (table == NULL)
(me_putendl_fd("Failed to alloc ptr array", 2), exit(1));
mem_set_zero(table, sizeof(*table));
}
return (table);
}
t_ptr *find_ptr(void *ptr)
{
t_usize i;
t_ptr_table *t;
t = get_table();
i = 0;
while (t)
{
while (i < PTR_LENS)
{
if (t->table[i].ptr == ptr)
return (&t->table[i]);
i++;
}
t = t->next;
}
return (NULL);
}
void print_pages_info(void)
{
}
#include <stdio.h>
void *me_malloc(t_usize size)
{
t_ptr_table *table;
t_ptr *ret;
// printf("ALLOC %zu\n", size);
ret = find_ptr(NULL);
if (ret == NULL)
{
table = get_table();
while (table->next != NULL)
table = table->next;
table->next = __libc_malloc(size);
if (table->next == NULL)
(me_abort("Failed to alloc ptr array"));
mem_set_zero(table->next, sizeof(*table));
ret = &table->next->table[0];
}
ret->ptr = __libc_malloc(size);
if (ret->ptr == NULL)
me_abort("Failed to malloc!");
ret->size = size;
mem_set_zero(ret->ptr, size);
return (ret->ptr);
}
void *me_calloc(t_usize elem_count, t_usize elem_size)
{
// printf("CALLOC %zu * %zu\n", elem_count, elem_size);
if (elem_size != 0 && elem_count > SIZE_MAX / elem_size)
return (me_abort("calloc overflow!"), NULL);
return (me_malloc(elem_size * elem_count));
}
void *me_realloc(void *ptr, t_usize size)
{
t_ptr *p;
t_ptr tmp;
if (ptr == NULL)
return (me_malloc(size));
// printf("REALLOC %p %zu\n", ptr, size);
p = find_ptr(ptr);
if (p == NULL)
return (me_abort("realloc with wrong ptr"), NULL);
if (p->size >= size)
return (p->ptr);
tmp.size = size;
tmp.ptr = __libc_malloc(size);
if (tmp.ptr == NULL)
me_abort("failed to malloc...");
mem_copy(tmp.ptr, p->ptr, p->size);
__libc_free(p->ptr);
*p = tmp;
return (tmp.ptr);
}
void *me_realloc_array(void *ptr, t_usize elem_size, t_usize elem_count)
{
// printf("CALLOC %zu * %zu\n", elem_count, elem_size);
if (elem_size != 0 && elem_count > SIZE_MAX / elem_size)
return (me_abort("realloc_array overflow !"), NULL);
return (me_realloc(ptr, elem_size * elem_count));
}
void me_free(void *ptr)
{
t_ptr *p;
// printf("FREE\n");
p = find_ptr(ptr);
if (p != NULL)
{
__libc_free(p->ptr);
p->ptr = NULL;
p->size = 0;
}
}

View file

@ -1,11 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_get_page_from_bloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/08 19:11:20 by maiboyer #+# #+# */
/* Updated: 2024/05/09 08:04:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -1,191 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_arena.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */
/* Updated: 2024/05/12 15:02:02 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/alloc/alloc.h"
#include "me/alloc/alloc_internal.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);
//
// 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

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/14 18:01:06 by maiboyer #+# #+# */
/* Updated: 2023/12/31 20:14:31 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:39:41 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@
#include "me/blx/inputs.h"
#include "me/blx/xdata.h"
#include "me/types.h"
#include "me/mem/mem.h"
#include "me/vec/vec_u8.h"
#include "me/printf/printf.h"
#include <mlx.h>
@ -29,7 +30,7 @@ t_blx blx_initialize(t_run_function func, t_free_function free_fn,
{
t_blx ctx;
ctx = (t_blx){.func = func, .app = data, .me_free = free_fn};
ctx = (t_blx){.func = func, .app = data, .mem_free = free_fn};
ctx.mlx = mlx_init();
if (ctx.mlx == NULL)
(me_eprintf("Error:\nfailed to inialize blx (mlx) !\n"), exit(1));
@ -59,14 +60,14 @@ void blx_free(t_blx app)
blx_sprite_free(app._data.screen);
blx_sprite_free(app._data.font);
mlx_do_key_autorepeaton(app.mlx);
if (app.me_free)
app.me_free(app.app);
if (app.mem_free)
app.mem_free(app.app);
if (app.win)
mlx_destroy_window(app.mlx, app.win);
if (app.mlx)
{
mlx_destroy_display(app.mlx);
me_free(app.mlx);
mem_free(app.mlx);
}
vec_u8_free(app.inputs.keysyms_held);
vec_u8_free(app.inputs.keysyms_pressed);

View file

@ -6,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:52:12 by maiboyer #+# #+# */
/* Updated: 2024/05/07 15:04:07 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 15:33:43 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#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_len.h"
@ -26,39 +25,30 @@ t_error str_reserve(t_buffer_str *buf, t_usize size)
if (buf == NULL)
return (ERROR);
if (buf->capacity >= size)
return (NO_ERROR);
while (size > buf->capacity)
{
new_capacity = (buf->capacity * 3) / 2 + 1;
temp_buffer = mem_realloc(buf->buf, new_capacity);
if (temp_buffer == NULL)
return (true);
buf->buf = temp_buffer;
buf->capacity = new_capacity;
}
temp_buffer = mem_realloc_array(buf->buf, new_capacity, sizeof(char));
if (temp_buffer == NULL)
return (ERROR);
buf->buf = temp_buffer;
buf->capacity = new_capacity;
return (NO_ERROR);
}
bool push_str_buffer(t_buffer_str *buf, t_const_str to_push)
t_error push_str_buffer(t_buffer_str *buf, t_const_str to_push)
{
t_usize to_push_len;
t_str temp_buffer;
t_usize new_capacity;
if (buf == NULL || to_push == NULL)
return (true);
return (ERROR);
to_push_len = str_len(to_push);
while (buf->len + to_push_len + 2 > buf->capacity)
{
new_capacity = (buf->capacity * 3) / 2 + 1;
temp_buffer = mem_realloc(buf->buf, new_capacity);
if (temp_buffer == NULL)
return (true);
buf->buf = temp_buffer;
buf->capacity = new_capacity;
}
if (str_reserve(buf, buf->len + to_push_len + 2))
return (ERROR);
buf->len += to_push_len;
str_l_cat(buf->buf, to_push, buf->capacity);
return (false);
return (NO_ERROR);
}
bool push_str_char(t_buffer_str *buf, char to_push)
@ -84,7 +74,7 @@ t_buffer_str alloc_new_buffer(t_usize capacity)
if (capacity == 0)
capacity = 16;
buf = mem_alloc(sizeof(char) * capacity);
buf = mem_alloc_array(sizeof(char), capacity);
if (buf == NULL)
{
out.buf = NULL;

View file

@ -6,13 +6,13 @@
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 17:38:21 by maix #+# #+# */
/* Updated: 2023/12/11 19:10:26 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:39:59 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/buffered_str/buf_str.h"
#include "me/gnl/gnl.h"
#include "me/mem/mem_alloc.h"
#include "me/mem/mem.h"
#include "me/mem/mem_move.h"
#include "me/string/str_len.h"
#include "me/types.h"
@ -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)))
{
me_free(temp_buffer);
mem_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)
;
me_free(temp_buffer);
mem_free(temp_buffer);
if (flags.error || flags.empty_read)
{
me_free(buf.buf);
mem_free(buf.buf);
return (*error = true, (t_buffer_str){0});
}
return (buf);

View file

@ -6,7 +6,7 @@
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 20:02:12 by maix #+# #+# */
/* Updated: 2023/12/11 19:09:32 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:40:16 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
#include "me/hash/sip/sip_utils.h"
#include "me/num/u64.h"
#include "me/num/usize.h"
#include "me/mem/mem.h"
#include <stdio.h>
#include <stdlib.h>
@ -101,6 +102,6 @@ t_u64 sip13_finish(t_sip13 *self)
state.v2 ^= 0xff;
compress(&state);
compress(&state);
me_free(self);
mem_free(self);
return (state.v0 ^ state.v1 ^ state.v2 ^ state.v3);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/24 19:06:05 by maiboyer #+# #+# */
/* Updated: 2023/12/24 19:18:01 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:40:29 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
#include "me/fs/read_to_vec.h"
#include "me/fs/write.h"
#include "me/img/qoi.h"
#include "me/mem/mem.h"
#include <stdlib.h>
t_i32 qoi_write(t_const_str filename, const void *data,
@ -32,7 +33,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);
me_free(encoded);
mem_free(encoded);
return (size);
}

View file

@ -1,22 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_add_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:38:45 by maiboyer #+# #+# */
/* Updated: 2023/12/09 15:02:42 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/list/list_add_back.h"
#include "me/list/list_get_last.h"
void list_add_back(t_list **list, t_list *new)
{
if (*list)
list_get_last(*list)->next = new;
else
*list = new;
}

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_add_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:15:23 by maiboyer #+# #+# */
/* Updated: 2023/12/09 15:02:50 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/list/list_add_front.h"
#include "me/list/list_alloc_node.h"
void list_add_front(t_list **lst, t_list *new)
{
new->next = *lst;
*lst = new;
}

View file

@ -1,26 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_alloc_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 19:57:28 by maiboyer #+# #+# */
/* Updated: 2023/12/09 18:13:05 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/list/list_alloc_node.h"
#include "me/mem/mem_alloc.h"
t_list *list_alloc_node(void *content)
{
t_list *out;
out = mem_alloc(sizeof(t_list) * 1);
if (out == NULL)
return (NULL);
out->content = content;
out->next = NULL;
return (out);
}

View file

@ -1,22 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_free_one.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:30:20 by maiboyer #+# #+# */
/* Updated: 2023/12/09 15:03:42 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/list/list_free_one.h"
#include <stdlib.h>
void list_free_one(t_list *lst, void (*del)(void *))
{
if (lst == NULL || del == NULL)
return ;
del(lst->content);
me_free(lst);
}

View file

@ -1,25 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_get_last.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:37:08 by maiboyer #+# #+# */
/* Updated: 2023/12/09 15:03:49 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/list/list_get_last.h"
t_list *list_get_last(t_list *list)
{
t_list *head;
head = list;
if (head == NULL)
return (NULL);
while (head->next)
head = head->next;
return (head);
}

View file

@ -1,22 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_iter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:39:05 by maiboyer #+# #+# */
/* Updated: 2023/12/09 15:03:55 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/list/list_iter.h"
void list_iter(t_list *list, void (*f)(void *))
{
while (list)
{
f(list->content);
list = list->next;
}
}

View file

@ -1,40 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:40:24 by maiboyer #+# #+# */
/* Updated: 2023/12/09 15:05:20 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/list/list_alloc_node.h"
#include "me/list/list_free_all.h"
#include "me/list/list_map.h"
#include <stdlib.h>
t_list *list_map(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
void *data;
t_list new;
t_list *cursor;
new = (t_list){.next = NULL, .content = NULL};
cursor = &new;
while (lst)
{
data = f(lst->content);
cursor->next = list_alloc_node(data);
if (cursor->next == NULL)
{
del(data);
list_free_all(&new.next, del);
return (NULL);
}
cursor = cursor->next;
lst = lst->next;
}
return (new.next);
}

View file

@ -1,28 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_size.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 20:23:19 by maiboyer #+# #+# */
/* Updated: 2023/12/09 15:05:00 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/list/list_size.h"
t_usize list_size(t_list *lst)
{
t_list *head;
t_usize idx;
head = lst;
idx = 0;
while (head)
{
head = head->next;
idx++;
}
return (idx);
}

View file

@ -1,30 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_free_all.c :+: :+: :+: */
/* allocator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 21:35:20 by maiboyer #+# #+# */
/* Updated: 2023/12/09 15:03:34 by maiboyer ### ########.fr */
/* Created: 2024/05/14 18:26:27 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:28:24 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/list/list_free_all.h"
#include <stdlib.h>
#include "aq/allocator.h"
#include "aq/libc_wrapper.h"
void list_free_all(t_list **lst, void (*del)(void *))
t_allocator *global_allocator(void)
{
t_list *tmp;
if (lst == NULL || del == NULL)
return ;
while (*lst)
static t_allocator global_alloc = {};
static bool init = false;
if (!init)
{
del((*lst)->content);
tmp = *lst;
*lst = (*lst)->next;
me_free(tmp);
init = true;
global_alloc = lc_init();
}
*lst = NULL;
return (&global_alloc);
}
void uninit_global_allocator(void)
{
t_allocator *allocator;
allocator = global_allocator();
allocator->uninit(allocator);
}

View file

@ -6,14 +6,25 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 14:47:49 by maiboyer #+# #+# */
/* Updated: 2024/05/07 12:45:31 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:32:31 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc.h"
#include "me/alloc/alloc.h"
#include "me/mem/_allocator.h"
void *mem_alloc(t_usize size)
void *mem_alloc(t_usize size)
{
return (me_malloc(size));
t_allocator *a;
a = global_allocator();
return (a->alloc(a, size));
}
void mem_free(void *ptr)
{
t_allocator *a;
a = global_allocator();
return (a->free(a, ptr));
}

View file

@ -6,15 +6,17 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 15:53:21 by maiboyer #+# #+# */
/* Updated: 2024/05/07 12:46:04 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:30:27 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/alloc/alloc.h"
#include "me/mem/mem_alloc_array.h"
#include <stdlib.h>
#include "me/mem/_allocator.h"
void *mem_alloc_array(t_usize item_count, t_usize item_size)
void *mem_alloc_array(t_usize size, t_usize count)
{
return (me_calloc(item_count, item_size));
t_allocator *a;
a = global_allocator();
return (a->alloc_array(a, size, count));
}

View file

@ -6,14 +6,24 @@
/* 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 */
/* Updated: 2024/05/14 18:32:06 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_realloc.h"
#include "me/alloc/alloc.h"
#include "me/mem/_allocator.h"
void *mem_realloc(void *ptr, t_usize size)
{
return (me_realloc(ptr, size));
t_allocator *a;
a = global_allocator();
return (a->realloc(a, ptr, size));
}
void *mem_realloc_array(void *ptr, t_usize size, t_usize count)
{
t_allocator *a;
a = global_allocator();
return (a->realloc_array(a, ptr, size, count));
}

View file

@ -6,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
/* Updated: 2024/05/09 07:43:10 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:42:15 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#define _GNU_SOURCE
#include "me/alloc/alloc_internal.h"
#include "me/fs/putendl_fd.h"
#include "me/fs/putstr_fd.h"
#include "me/types.h"
@ -91,7 +90,6 @@ void me_abort(t_str msg)
if (msg == NULL)
msg = "No message (msg was NULL)";
me_putendl_fd("Memory information:", 2);
print_pages_info();
me_putstr_fd("Abort: ", 2);
me_putendl_fd(msg, 2);
print_trace();

View file

@ -6,86 +6,16 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */
/* Updated: 2024/05/12 17:05:27 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:42:41 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/alloc/alloc_dumb_internal.h"
#include "me/alloc/alloc_internal.h"
#include "me/fs/putendl_fd.h"
#include "me/fs/putnbr_fd.h"
#include "me/fs/putstr_fd.h"
#include "me/mem/_allocator.h"
#include "me/types.h"
#include <stdlib.h>
void __libc_free(void *ptr);
// void uninit_allocator(void)
// {
// t_mpage *page;
// void *tmp;
// t_mblock *block;
// t_usize count_block;
//
// page = get_head_arena();
// count_block = 0;
// block = page->first;
// while (block)
// {
// if (block->used)
// count_block += 1;
// tmp = block->next;
// __libc_free(block);
// block = tmp;
// }
// while (page)
// {
// tmp = page->next;
// __libc_free(page->data);
// __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 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_exit(t_i32 exit_code)
{
uninit_allocator();
uninit_global_allocator();
exit(exit_code);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/05/09 16:55:16 by rparodi ### ########.fr */
/* Updated: 2024/05/14 18:42:59 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
#include "me/os/process.h"
#include "me/string/str_find_chr.h"
#include "me/string/str_n_compare.h"
#include "me/mem/mem.h"
#include "me/string/str_split.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
@ -73,8 +74,8 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
}
sp_index = 0;
while (splitted_path[sp_index])
me_free(splitted_path[sp_index++]);
me_free(splitted_path);
mem_free(splitted_path[sp_index++]);
mem_free(splitted_path);
return (NO_ERROR);
}
@ -99,7 +100,7 @@ t_error find_binary(t_spawn_info *info, t_process *process)
}
if (access(s.buf, X_OK | R_OK) == 0)
{
me_free(info->binary_path);
mem_free(info->binary_path);
info->binary_path = s.buf;
return (NO_ERROR);
}
@ -119,7 +120,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);
me_free(info.binary_path);
mem_free(info.binary_path);
}
t_error spawn_process(t_spawn_info info, t_process *process)

View file

@ -6,11 +6,11 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 18:12:11 by maiboyer #+# #+# */
/* Updated: 2023/12/11 19:18:48 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:43:13 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_set.h"
#include "me/mem/mem.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/string/str_len.h"
@ -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);
me_free(start_num);
mem_free(start_num);
}

View file

@ -6,11 +6,11 @@
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 01:44:35 by maix #+# #+# */
/* Updated: 2023/12/11 19:19:27 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:43:24 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem.h"
#include "me/mem/mem_set.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
@ -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);
me_free(start_num);
mem_free(start_num);
}

View file

@ -6,11 +6,11 @@
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 01:44:35 by maix #+# #+# */
/* Updated: 2023/12/11 19:19:59 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:43:39 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem.h"
#include "me/mem/mem_set.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
@ -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);
me_free(start_num);
mem_free(start_num);
}

View file

@ -6,11 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:57:04 by maiboyer #+# #+# */
/* Updated: 2023/12/01 21:20:07 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:43:44 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/buffered_str/buf_str.h"
#include "me/mem/mem.h"
#include "me/convert/atoi.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/matchers/matchers.h"
@ -99,7 +100,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);
me_free(truc);
mem_free(truc);
*c_idx = *nxt + 1;
return ((t_printf_arg){
.p_args = extra.p_args,

View file

@ -6,14 +6,13 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 21:05:47 by maiboyer #+# #+# */
/* Updated: 2023/12/01 21:49:51 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:43:56 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include <stdlib.h>
#include "me/mem/mem.h"
void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs,
t_usize value)
@ -23,7 +22,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 = (me_free(*strs.out), (t_str)mem_alloc_array(1, 1));
*strs.out = (mem_free(*strs.out), (t_str)mem_alloc_array(1, 1));
else
*strs.out = "";
*strs.pretty = "";

View file

@ -99,7 +99,7 @@ t_usize me_printf(t_const_str fmt, ...)
va_end(args);
len = str_len(str);
write(1, str, len);
me_free(str);
mem_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);
me_free(str);
mem_free(str);
return (len);
}
*/

View file

@ -101,8 +101,8 @@ int main(void) {
printf("\n");
printf("\n");
}
me_free(dest_libc);
me_free(dest_ft);
mem_free(dest_libc);
mem_free(dest_ft);
}
}
R*/

View file

@ -97,8 +97,8 @@ int main(void) {
printf("\n");
printf("\n");
}
me_free(dest_libc);
me_free(dest_ft);
mem_free(dest_libc);
mem_free(dest_ft);
}
}
R*/

View file

@ -6,12 +6,11 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/17 15:56:59 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:52:08 by maiboyer ### ########.fr */
/* Updated: 2024/05/14 18:44:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc.h"
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem.h"
#include "me/string/str_l_copy.h"
#include "me/string/str_split.h"
#include <stdlib.h>
@ -43,7 +42,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)
me_free(*(to_free++));
mem_free(*(to_free++));
return (NULL);
}