Merge remote-tracking branch 'origin/master'

This commit is contained in:
EniumRaphael 2024-07-11 14:53:32 +02:00
commit 4f6e96d0e0
89 changed files with 1890 additions and 2678 deletions

View file

@ -6,7 +6,7 @@
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ # # By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/04/28 17:28:30 by maiboyer #+# #+# # # Created: 2024/04/28 17:28:30 by maiboyer #+# #+# #
# Updated: 2024/07/05 18:00:14 by maiboyer ### ########.fr # # Updated: 2024/07/10 21:02:49 by maiboyer ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -19,7 +19,7 @@ link_group = -Wl,--start-group $(1) -Wl,--end-group
BUILD_DIR ?= $(shell realpath ./build/) BUILD_DIR ?= $(shell realpath ./build/)
# Flags # Flags
CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD -lreadline -I./includes -I./output/include -I./stdme/output/include -rdynamic -Wl,-E CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD -I./includes -I./output/include -I./stdme/output/include -rdynamic -Wl,-E
# CFLAGS += -fsanitize=address -fno-omit-frame-pointer -fsanitize-address-use-after-return=runtime -fno-common -fsanitize-address-use-after-scope # CFLAGS += -fsanitize=address -fno-omit-frame-pointer -fsanitize-address-use-after-return=runtime -fno-common -fsanitize-address-use-after-scope
# Sources # Sources
# LIB = ./libft/ft_bzero.c \ # LIB = ./libft/ft_bzero.c \

View file

@ -11,9 +11,9 @@
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef ALLOC_H #ifndef ALLOC_H
#define ALLOC_H # define ALLOC_H
#include "me/types.h" # include "me/types.h"
void *me_malloc(t_usize size); void *me_malloc(t_usize size);
void *me_calloc(t_usize elem_count, t_usize elem_size); void *me_calloc(t_usize elem_count, t_usize elem_size);

View file

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

View file

@ -11,21 +11,23 @@
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef ALLOCATOR_H #ifndef ALLOCATOR_H
#define ALLOCATOR_H # define ALLOCATOR_H
#include "me/types.h" # include "me/types.h"
typedef struct s_allocator t_allocator; typedef struct s_allocator t_allocator;
typedef void *(*t_allocator_alloc)(t_allocator *self, t_usize size); typedef void *(*t_allocator_alloc)(t_allocator *self,
typedef void *(*t_allocator_alloc_array)(t_allocator *self, t_usize size, t_usize size);
t_usize count); typedef void *(*t_allocator_alloc_array)(t_allocator *self,
typedef void *(*t_allocator_realloc)(t_allocator *self, void *ptr, t_usize size, t_usize count);
t_usize requested_size); typedef void *(*t_allocator_realloc)(t_allocator *self,
typedef void *(*t_allocator_realloc_array)(t_allocator *self, void *ptr, void *ptr, t_usize requested_size);
t_usize requested_size, typedef void *(*t_allocator_realloc_array)(t_allocator *self,
void *ptr, t_usize requested_size,
t_usize requested_count); t_usize requested_count);
typedef void (*t_allocator_free)(t_allocator *self, void *ptr); typedef void (*t_allocator_free)(t_allocator *self,
void *ptr);
typedef void (*t_allocator_uninit)(t_allocator *self); typedef void (*t_allocator_uninit)(t_allocator *self);
struct s_allocator struct s_allocator

View file

@ -6,48 +6,52 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 22:20:30 by maiboyer #+# #+# */ /* Created: 2024/05/12 22:20:30 by maiboyer #+# #+# */
/* Updated: 2024/05/17 15:34:26 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 16:40:57 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef INTERNAL_VG_FUNCS_H #ifndef INTERNAL_VG_FUNCS_H
#define INTERNAL_VG_FUNCS_H # define INTERNAL_VG_FUNCS_H
#include "me/types.h" # include "me/types.h"
#if !defined(NVALGRIND) || defined(VGHEADER)
# if !defined(NVALGRIND) || defined(VGHEADER)
# ifdef NVALGRIND # ifdef NVALGRIND
# undef NVALGRIND # undef NVALGRIND
# endif # endif
# define VGFUNCS # define VGFUNCS
# include <valgrind/memcheck.h> # include <valgrind/memcheck.h>
# include <valgrind/valgrind.h> # include <valgrind/valgrind.h>
#endif # endif
#define ZEROED_POOL 1 # define ZEROED_POOL 1
#define ZEROED_ALLOC 1 # define ZEROED_ALLOC 1
static inline t_usize redzone_size(void) static inline t_usize redzone_size(void)
{ {
return (8); return (8);
} }
#ifdef VGFUNCS # ifdef VGFUNCS
static inline bool vg_running(void) static inline bool vg_running(void)
{ {
return (RUNNING_ON_VALGRIND != 0); return (RUNNING_ON_VALGRIND != 0);
} }
#else # else
static inline bool vg_running(void) static inline bool vg_running(void)
{ {
return (false); return (false);
} }
#endif # endif
#define MEMPOOL_FLAG_MALLOCLIKE 1 # define MEMPOOL_FLAG_MALLOCLIKE 1
#define MEMPOOL_FLAG_AUTOFREE 2 # define MEMPOOL_FLAG_AUTOFREE 2
void vg_block_malloc(void *ptr, t_usize size); void vg_block_malloc(void *ptr, t_usize size);
void vg_block_resize(void *ptr, t_usize oldsize, t_usize newsize); void vg_block_resize(void *ptr, t_usize oldsize,
t_usize newsize);
void vg_block_free(void *ptr); void vg_block_free(void *ptr);
void vg_mem_no_access(void *ptr, t_usize size); void vg_mem_no_access(void *ptr, t_usize size);

View file

@ -11,10 +11,10 @@
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef LIBC_WRAPPER_H #ifndef LIBC_WRAPPER_H
#define LIBC_WRAPPER_H # define LIBC_WRAPPER_H
#include "aq/allocator.h" # include "aq/allocator.h"
#include "me/types.h" # include "me/types.h"
void *lc_malloc(t_allocator *self, t_usize size); void *lc_malloc(t_allocator *self, t_usize size);
void *lc_calloc(t_allocator *self, t_usize size, t_usize count); void *lc_calloc(t_allocator *self, t_usize size, t_usize count);

View file

@ -11,9 +11,9 @@
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MELLOC_H #ifndef MELLOC_H
#define MELLOC_H # define MELLOC_H
#include "aq/allocator.h" # include "aq/allocator.h"
void *m_malloc(t_allocator *self, t_usize size); void *m_malloc(t_allocator *self, t_usize size);
void *m_alloc_array(t_allocator *self, t_usize size, t_usize count); void *m_alloc_array(t_allocator *self, t_usize size, t_usize count);

View file

@ -6,19 +6,19 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 15:27:46 by maiboyer #+# #+# */ /* Created: 2024/05/15 15:27:46 by maiboyer #+# #+# */
/* Updated: 2024/05/16 14:58:27 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 16:55:59 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MELLOC_INTERAL_H #ifndef MELLOC_INTERAL_H
#define MELLOC_INTERAL_H # define MELLOC_INTERAL_H
#include "aq/allocator.h" # include "aq/allocator.h"
#include "me/types.h" # include "me/types.h"
#define PAGE_LIST_MAX 255 # define PAGE_LIST_MAX 255
#define PAGE_POW_2 12 # define PAGE_POW_2 12
#define PAGE_ALIGN 3 # define PAGE_ALIGN 3
typedef struct s_chunk typedef struct s_chunk
{ {
@ -50,15 +50,36 @@ struct s_allocator_melloc
t_page_list *list; t_page_list *list;
}; };
t_error alloc_page_list(t_page_list **out);
void *m_malloc(struct s_allocator_melloc *self, t_usize size); void *m_malloc(struct s_allocator_melloc *self, t_usize size);
void *m_alloc_array(struct s_allocator_melloc *self, t_usize size, void *m_alloc_array(struct s_allocator_melloc *self, \
t_usize count); t_usize size, t_usize count);
void *m_realloc(struct s_allocator_melloc *self, void *ptr, t_usize min_size); void *m_realloc(struct s_allocator_melloc *self, \
void *m_realloc_array(struct s_allocator_melloc *self, void *ptr, t_usize size, void *ptr, t_usize min_size);
t_usize count); void *m_realloc_array(struct s_allocator_melloc *self, \
void *ptr, t_usize size, t_usize count);
void m_free(struct s_allocator_melloc *self, void *ptr); void m_free(struct s_allocator_melloc *self, void *ptr);
void m_uninit(struct s_allocator_melloc *self); void m_uninit(struct s_allocator_melloc *self);
t_chunk *find_chunk_of_size(struct s_allocator_melloc *alloc, t_usize size);
t_chunk *get_first_block(t_page *page);
t_chunk *get_next_block(t_chunk *chunk, bool find_zero);
t_chunk *split_block(t_chunk *chunk, t_usize size);
void merge_blocks(t_page *page);
t_error alloc_page_list(t_page_list **out);
t_error alloc_new_page(struct s_allocator_melloc *alloc, t_usize page_size);
static inline t_usize round_to_pow2(t_usize val, t_usize pow)
{
pow = (1 << pow) - 1;
return ((val + pow) & (~pow));
}
static inline void *m_alloc_error(struct s_allocator_melloc *self, t_str msg)
{
self->uninit((void *)self);
me_abort(msg);
return (NULL);
}
#endif /* MELLOC_INTERAL_H */ #endif /* MELLOC_INTERAL_H */

View file

@ -1,12 +1,17 @@
alloc
get_arena
lc_alloc/functions1 lc_alloc/functions1
lc_alloc/functions2 lc_alloc/functions2
me_alloc/find_block
me_alloc/functions1 me_alloc/functions1
me_alloc/functions2 me_alloc/functions2
me_alloc/internals
me_alloc/merge_blocks
me_alloc/pages
me_alloc/realloc
vg/dummy_block vg/dummy_block
vg/dummy_mempool
vg/dummy_mem_status vg/dummy_mem_status
vg/dummy_mempool
vg/dummy_mempool_bis
vg/valgrind_block vg/valgrind_block
vg/valgrind_mempool
vg/valgrind_mem_status vg/valgrind_mem_status
vg/valgrind_mempool
vg/valgrind_mempool_bis

View file

@ -1,196 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:26:01 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/allocator.h"
#include "aq/libc_wrapper.h"
typedef struct s_allocator_page
{
void *data;
t_usize size;
} t_allocator_page;
typedef struct s_page_list
{
t_usize allocated;
t_allocator_page a[10];
struct s_page_list *next;
} t_page_list;
/*
void *me_malloc(t_usize size)
{
t_mblock *block;
size = usize_round_up_to(size, 16);
block = get_block_for_size(size);
if (block == NULL)
return (me_abort("Found no page for me_malloc"), NULL);
vg_mem_defined(block, sizeof(*block));
vg_mempool_alloc(POOL_ADDR, (void *)(((t_usize)block) + sizeof(*block)),
block->size); block->used = true; mem_set_zero((t_u8 *)block + sizeof(*block),
block->size); vg_mem_no_access(block, sizeof(*block)); return ((void
*)(((t_usize)block) + sizeof(*block)));
}
void *me_calloc(t_usize elem_size, t_usize elem_count)
{
if (elem_size != 0 && elem_count > SIZE_MAX / elem_size)
me_abort("calloc overflow !");
return (me_malloc(elem_size * elem_count));
}
void *me_realloc(void *ptr, t_usize new_size)
{
t_mblock *block;
void *ret;
t_usize old_size;
if (ptr == NULL)
return (me_malloc(new_size));
block = (void *)((t_usize)(ptr) - sizeof(*block));
vg_mem_defined(block, sizeof(*block));
VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, block->size);
block->used = true;
if (block->size <= new_size)
return (vg_mem_no_access(block, sizeof(*block)), ptr);
old_size = block->size;
vg_mem_no_access(block, sizeof(*block));
if (false && merge_block(block, new_size))
{
vg_mem_defined(block, sizeof(*block));
vg_mempool_resize(POOL_ADDR, ptr, block->size);
VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, block->size);
mem_set_zero((t_u8 *)ptr + old_size, block->size - old_size);
vg_mem_no_access(block, sizeof(*block));
return (ptr);
}
else
{
ret = me_malloc(new_size);
mem_copy(ret, ptr, block->size);
mem_free(ptr);
return (ret);
}
}
void *me_realloc_array(void *ptr, t_usize elem_size, t_usize elem_count)
{
if (elem_size != 0 && elem_count > SIZE_MAX / elem_size)
me_abort("realloc_array overflow !");
return (me_realloc(ptr, elem_size * elem_count));
}
void mem_free(void *ptr)
{
t_mblock *cur;
if (ptr == NULL)
return;
cur = (void *)(((t_usize)ptr) - sizeof(t_mblock));
vg_mempool_free(POOL_ADDR, ptr);
vg_mem_defined(cur, sizeof(*cur));
cur->used = false;
merge_block(cur, ~(t_usize)0);
vg_mem_no_access(cur, sizeof(*cur));
}
*/
// 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_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);
// mem_free(ptr);
// return (ret);
// }
// }
//
// void mem_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,362 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_arena.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */
/* Updated: 2024/05/14 16:52:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/alloc.h"
#include "aq/alloc_internal.h"
#include "aq/internal_vg_funcs.h"
#include "me/fs/putendl_fd.h"
#include "me/fs/putnbr_fd.h"
#include "me/fs/putstr_fd.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/num/usize.h"
#include <stdio.h>
#include <stdlib.h>
/*
void *__libc_malloc(size_t size);
void __libc_free(void *ptr);
t_mpage *alloc_page(t_usize size)
{
t_mpage *val;
size = usize_round_up_to(size + sizeof(t_mpage), PAGE_SIZE_DEFAULT);
val = __libc_malloc(size);
if (val == NULL)
return (NULL);
mem_set_zero(val, size);
val->next = NULL;
val->page_size = size - sizeof(*val);
val->first = (t_mblock *)(((t_usize)val) + sizeof(t_mpage));
val->first->page = val;
val->first->next = NULL;
val->first->used = false;
val->first->size = size - sizeof(t_mpage) - sizeof(t_mblock) * 2;
mem_copy(val->first->padding, BLOCK_PADDING, 7);
vg_mem_no_access(val, size);
return (val);
}
t_mpage *get_head_arena(void)
{
static t_mpage *val = NULL;
if (val == NULL &&
PAGE_SIZE_DEFAULT > sizeof(t_mpage) + sizeof(t_mblock) * 2 + 16)
{
vg_mempool_create(POOL_ADDR);
val = alloc_page(PAGE_SIZE_DEFAULT - sizeof(t_mpage));
}
if (val == NULL)
(me_putstr_fd("Failed to alloc first page", 2), exit(1));
return (val);
}
t_mblock *split_block(t_mblock *self, t_usize min_size)
{
t_usize remaining;
t_mblock *old_next;
vg_mem_defined(self, sizeof(*self));
min_size = usize_round_up_to(min_size, 16);
if (self->size > (min_size + sizeof(t_mblock) + 16))
{
remaining = self->size - min_size - sizeof(t_mblock);
printf("splitting %zu into %zu and %zu\n", self->size, min_size,
remaining);
self->size = min_size;
old_next = self->next;
self->next = (t_mblock *)(((t_usize)self) + sizeof(*self) + self->size);
vg_mem_defined(self->next, sizeof(*self->next));
self->next->page = self->page;
self->next->next = old_next;
self->next->used = false;
self->next->size = remaining;
mem_copy(self->next->padding, BLOCK_PADDING, 7);
vg_mem_no_access(self->next, sizeof(*self->next));
}
vg_mem_no_access(self, sizeof(*self));
return (self);
}
t_mblock *get_block_for_size(t_usize size)
{
t_mblock *last;
t_mblock *cur;
t_mpage *head;
last = NULL;
head = get_head_arena();
vg_mem_defined(head, sizeof(*head));
cur = head->first;
vg_mem_no_access(head, sizeof(*head));
while (cur)
{
vg_mem_defined(cur, sizeof(*cur));
if (!cur->used && cur->size >= size)
return (split_block(cur, size));
last = cur;
cur = cur->next;
vg_mem_no_access(last, sizeof(*last));
}
if (last == NULL)
return (NULL);
vg_mem_defined(last, sizeof(*last));
vg_mem_defined(last->page, sizeof(*last->page));
last->page->next = alloc_page(size + sizeof(t_mblock));
if (last->page->next == NULL)
me_abort("Failed to alloc page!");
vg_mem_defined(last->page->next, sizeof(*last->page->next));
last->next = last->page->next->first;
cur = last->page->next->first;
vg_mem_no_access(last->page->next, sizeof(*last->page->next));
vg_mem_no_access(last->page, sizeof(*last->page));
vg_mem_no_access(last, sizeof(*last));
return (split_block(cur, size));
}
void print_pages_info(void)
{
t_mpage *page;
t_mpage *old;
t_i32 page_nb;
page = get_head_arena();
page_nb = 0;
while (page != NULL)
{
vg_mem_defined(page, sizeof(*page));
me_putstr_fd("Page ", 2);
me_putnbr_fd(page_nb++, 2);
me_putstr_fd(" with size ", 2);
me_putnbr_fd((t_i32)page->page_size, 2);
me_putstr_fd("\n", 2);
old = page;
page = page->next;
vg_mem_no_access(old, sizeof(*old));
}
}
bool merge_block(t_mblock *self, t_usize min_size)
{
t_mblock *old_next;
vg_mem_defined(self, sizeof(*self));
vg_mem_defined(self->next, sizeof(*self->next));
if (!(self->next && !self->next->used && self->next->page == self->page &&
self->size + self->next->size + sizeof(t_mblock) >= min_size))
return (vg_mem_no_access(self->next, sizeof(*self->next)),
vg_mem_no_access(self, sizeof(*self)), false);
old_next = self->next;
self->size += sizeof(*self) + self->next->size;
self->next = self->next->next;
vg_mem_no_access(self, sizeof(*self));
vg_mem_no_access(old_next, sizeof(*old_next));
return (true);
}
void uninit_allocator(void)
{
t_mpage *page;
void *tmp;
t_mblock *block;
t_usize count_block;
page = get_head_arena();
count_block = 0;
vg_mem_defined(page, sizeof(*page));
block = page->first;
while (block)
{
vg_mem_defined(block, sizeof(*block));
if (block->used)
count_block += 1;
block = block->next;
}
while (page)
{
vg_mem_defined(page, sizeof(*page));
tmp = page->next;
__libc_free(page);
page = tmp;
}
if (count_block != 0)
(me_putnbr_fd(count_block, 2),
me_putendl_fd(" Blocks weren't freed when exiting !", 2));
}*/
//
// void free_ifn(void *ptr)
// {
// if (ptr != NULL)
// __libc_free(ptr);
// }
//
// t_mpage *alloc_page(t_usize size)
// {
// t_mpage *page;
// void *data;
// t_mblock *block;
//
// size = usize_round_up_to(size, PAGE_SIZE_DEFAULT);
// page = __libc_malloc(sizeof(t_mpage));
// block = __libc_malloc(sizeof(t_mblock));
// data = __libc_malloc(size);
// if (page == NULL || data == NULL || block == NULL ||
// PAGE_SIZE_DEFAULT <= (t_usize)0)
// return (free_ifn(page), free_ifn(data), free_ifn(block), NULL);
// page->data = data;
// page->next = NULL;
// page->page_size = size;
// page->first = block;
// block->offset = 0;
// block->page = page;
// block->next = NULL;
// block->used = false;
// block->size = size;
// mem_copy(block->padding, BLOCK_PADDING, 7);
// return (page);
// }
//
// t_mpage *get_head_arena(void)
// {
// static t_mpage *val = NULL;
//
// if (val == NULL)
// {
// val = alloc_page(PAGE_SIZE_DEFAULT);
// if (val == NULL)
// (me_putstr_fd("Failed to alloc first page", 2), exit(1));
// }
// return (val);
// }
//
// t_mblock *split_block(t_mblock *self, t_usize min_size)
// {
// t_usize remaining;
// t_mblock *new_next;
//
// min_size = usize_round_up_to(min_size, 16);
// if (self->size > min_size)
// {
// remaining = self->size - min_size - 1;
// new_next = __libc_malloc(sizeof(t_mblock));
// if (new_next == NULL)
// return (me_abort("Failed to alloc block"), NULL);
// printf("splitting %zu into %zu and %zu\n", self->size, min_size,
// remaining);
// self->size = min_size;
// new_next->page = self->page;
// new_next->next = self->next;
// new_next->offset = self->offset + self->size + 1;
// new_next->used = false;
// new_next->size = remaining;
// mem_copy(new_next->padding, BLOCK_PADDING, 7);
// self->next = new_next;
// }
// return (self);
// }
//
// t_mblock *get_block_for_size(t_usize size)
// {
// t_mblock *last;
// t_mblock *cur;
//
// last = NULL;
// cur = get_head_arena()->first;
// while (cur)
// {
// if (cur->page == NULL)
// me_abort("block doesn't have a page ?????");
// if (!cur->used && cur->size >= size)
// return (split_block(cur, size));
// last = cur;
// cur = cur->next;
// }
// if (last == NULL)
// return (NULL);
// last->page->next = alloc_page(size);
// if (last->page->next == NULL)
// me_abort("Failed to alloc page!");
// last->next = last->page->next->first;
// return (split_block(last->page->next->first, size));
// }
//
// t_mblock *get_block_from_ptr(void *ptr)
// {
// t_mpage *page;
// t_mblock *block;
//
// page = get_page_from_ptr(ptr);
// if (page == NULL)
// return (NULL);
// block = page->first;
// while (block && block->page == page)
// {
// if (((t_u8 *)page->data) + block->offset == (t_u8 *)ptr)
// return (block);
// block = block->next;
// }
// return (NULL);
// }
//
// t_mpage *get_page_from_ptr(void *ptr)
// {
// t_mpage *page;
//
// page = get_head_arena();
// while (page)
// {
// if ((t_u8 *)page->data <= (t_u8 *)ptr &&
// (t_u8 *)ptr < (((t_u8 *)page->data) + page->page_size))
// return (page);
// page = page->next;
// }
// return (NULL);
// }
//
// void print_pages_info(void)
// {
// t_mpage *page;
// t_i32 page_nb;
//
// page = get_head_arena();
// page_nb = 0;
// while (page != NULL)
// {
// me_putstr_fd("Page ", 2);
// me_putnbr_fd(page_nb++, 2);
// me_putstr_fd(" with size ", 2);
// me_putnbr_fd((t_i32)page->page_size, 2);
// me_putstr_fd("\n", 2);
// page = page->next;
// }
// }
//
// bool merge_next_block(t_mblock *cur, t_usize min_size)
// {
// t_mblock *next;
//
// if (cur->next != NULL && cur->page == cur->next->page && !cur->next->used &&
// cur->size + cur->next->size >= min_size)
// {
// cur->size += cur->next->size;
// next = cur->next;
// cur->next = cur->next->next;
// printf("merging two blocks %p and %p\n", cur, cur->next);
// __libc_free(next);
// return (NO_ERROR);
// }
// return (ERROR);
// }

View file

@ -38,7 +38,8 @@ void *lc_realloc(t_allocator *self, void *ptr, t_usize size)
return (__libc_realloc(ptr, size)); return (__libc_realloc(ptr, size));
} }
void *lc_realloc_array(t_allocator *self, void *ptr, t_usize size, t_usize elem) void *lc_realloc_array(t_allocator *self, void *ptr, t_usize size,
t_usize elem)
{ {
(void)(self); (void)(self);
return (__libc_reallocarray(ptr, size, elem)); return (__libc_reallocarray(ptr, size, elem));
@ -49,4 +50,3 @@ void lc_free(t_allocator *self, void *ptr)
(void)(self); (void)(self);
return (__libc_free(ptr)); return (__libc_free(ptr));
} }

View 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);
}

View file

@ -6,321 +6,41 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 18:02:12 by maiboyer #+# #+# */ /* 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/allocator.h"
#include "aq/internal_vg_funcs.h" #include "aq/internal_vg_funcs.h"
#include "aq/melloc_interal.h" #include "aq/melloc_interal.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
void *__libc_malloc(t_usize size); 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); 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) void *m_malloc(struct s_allocator_melloc *self, t_usize size)
{ {
return (m_realloc(self, NULL, 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) if (size != 0 && count > SIZE_MAX / size)
return (m_alloc_error(self, "Alloc array overflow")); return (m_alloc_error(self, "Alloc array overflow"));
return (m_realloc(self, NULL, size * count)); 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) void m_free(struct s_allocator_melloc *self, void *ptr)
{ {
t_chunk *chunk; t_chunk *chunk;
(void)(self); (void)(self);
if (ptr == NULL) if (ptr == NULL)
return; return ;
vg_block_free(ptr); vg_block_free(ptr);
chunk = ptr - sizeof(*chunk); chunk = ptr - sizeof(*chunk);
vg_mem_defined(chunk, sizeof(*chunk)); vg_mem_defined(chunk, sizeof(*chunk));
@ -351,9 +71,8 @@ void m_uninit(struct s_allocator_melloc *self)
idx++; idx++;
} }
list_next = list->next; list_next = list->next;
__libc_free(list); (__libc_free(list), vg_mempool_destroy(list), \
vg_mempool_destroy(list); vg_mem_no_access(list, sizeof(*list)));
vg_mem_no_access(list, sizeof(*list));
list = list_next; list = list_next;
} }
} }

View file

@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* internals.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 16:49:31 by maiboyer #+# #+# */
/* Updated: 2024/07/10 20:54:31 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 *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);
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);
}

View 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);
}
}

View 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));
}

View file

@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* realloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 16:48:19 by maiboyer #+# #+# */
/* Updated: 2024/07/10 17:25:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/allocator.h"
#include "aq/internal_vg_funcs.h"
#include "aq/melloc_interal.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include <errno.h>
static void *_realloc_inner_copy(struct s_allocator_melloc *self, void *ptr, \
t_usize size, t_chunk *next)
{
t_chunk *chk;
chk = ptr - sizeof(*chk);
vg_mem_no_access(next, sizeof(*next));
next = m_realloc(self, NULL, size);
vg_mem_defined(chk, sizeof(*chk));
mem_move(next, ptr, chk->size);
vg_mem_no_access(chk, sizeof(*chk));
m_free(self, ptr);
return (next);
}
static void *_realloc_inner(struct s_allocator_melloc *self, void *ptr, \
t_usize size)
{
t_usize old_size;
t_chunk *chk;
t_chunk *next;
chk = ptr - sizeof(*chk);
vg_mem_defined(chk, sizeof(*chk));
if (chk->size >= size)
return (vg_mem_no_access(chk, sizeof(*chk)), ptr);
next = get_next_block(chk, false);
(vg_mem_defined(next, sizeof(*next)), vg_mem_defined(chk, sizeof(*chk)));
if (next != NULL && !next->used && chk->size + next->size
+ sizeof(*next) >= size)
{
old_size = chk->size;
chk->size += next->size + sizeof(*next);
return (vg_mem_undefined(next, next->size + sizeof(*next)), \
vg_block_resize((void *)chk + sizeof(*chk), old_size, chk->size), \
vg_mem_no_access(chk, sizeof(*chk)), ptr);
}
else
return (_realloc_inner_copy(self, ptr, size, next));
}
static void *_realloc_alloc(struct s_allocator_melloc *self, t_usize size)
{
t_chunk *chunk;
chunk = find_chunk_of_size(self, size);
if (chunk == NULL)
{
if (alloc_new_page(self, size + sizeof(t_chunk) * 2))
return (m_alloc_error(self, "Unable to alloc page"));
chunk = find_chunk_of_size(self, size);
if (chunk == NULL)
return (m_alloc_error(self, "Unable to find block"));
}
vg_mem_defined((void *)chunk, sizeof(*chunk));
chunk->used = true;
vg_mem_defined((void *)chunk + sizeof(*chunk), chunk->size);
mem_set_zero((void *)chunk + sizeof(*chunk), chunk->size);
vg_block_malloc((void *)chunk + sizeof(*chunk), chunk->size);
vg_mem_no_access((void *)chunk, sizeof(*chunk));
return ((void *)chunk + sizeof(*chunk));
}
void *m_realloc(struct s_allocator_melloc *self, void *ptr, t_usize size)
{
if (size > INT32_MAX - sizeof(t_chunk) * 10)
return (errno = ENOMEM, NULL);
size = round_to_pow2(size, PAGE_ALIGN);
if (ptr != NULL && size == 0)
return (m_free(self, ptr), NULL);
if (ptr == NULL)
return (_realloc_alloc(self, size));
else
return (_realloc_inner(self, ptr, size));
}
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));
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 22:33:30 by maiboyer #+# #+# */ /* 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 #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 vg_mempool_resize(void *pool, void *ptr, t_usize size)
{ {
(void)(pool); (void)(pool);

View 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

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/12 22:33:30 by maiboyer #+# #+# */ /* 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,19 +15,6 @@
#ifdef VGFUNCS #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) void vg_mempool_resize(void *pool, void *ptr, t_usize size)
{ {
VALGRIND_MEMPOOL_CHANGE(pool, ptr, ptr, size); VALGRIND_MEMPOOL_CHANGE(pool, ptr, ptr, size);
@ -36,7 +23,6 @@ void vg_mempool_resize(void *pool, void *ptr, t_usize size)
void vg_mempool_create(void *pool) void vg_mempool_create(void *pool)
{ {
VALGRIND_CREATE_MEMPOOL(pool, 0, ZEROED_POOL); VALGRIND_CREATE_MEMPOOL(pool, 0, ZEROED_POOL);
} }
void vg_mempool_destroy(void *pool) void vg_mempool_destroy(void *pool)

View 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

View file

@ -36,14 +36,12 @@
valgrind.dev valgrind.dev
valgrind valgrind
gnumake gnumake
readline.out
readline.dev
generic_c.packages.${system}.default generic_c.packages.${system}.default
c_formatter_42.packages.${system}.default c_formatter_42.packages.${system}.default
llvmPackages.bintools llvmPackages.bintools
norminette norminette
rust-bin.stable.latest.default rust-bin.stable.latest.default
(tree-sitter.override {webUISupport = true;}) #(tree-sitter.override {webUISupport = true;})
nodejs nodejs
]; ];
VALGRIND_INC_OPT = "${pkgs.valgrind.dev}/include"; VALGRIND_INC_OPT = "${pkgs.valgrind.dev}/include";

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:20:25 by maiboyer #+# #+# */ /* Created: 2024/05/02 13:20:25 by maiboyer #+# #+# */
/* Updated: 2024/05/08 21:52:34 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 21:03:38 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,8 +17,6 @@
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <readline/readline.h>
typedef t_i32 t_signal; typedef t_i32 t_signal;
t_error install_signal(void); t_error install_signal(void);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:41:15 by rparodi #+# #+# */ /* Created: 2024/03/28 14:41:15 by rparodi #+# #+# */
/* Updated: 2024/05/09 16:28:26 by rparodi ### ########.fr */ /* Updated: 2024/07/10 21:02:59 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,12 +18,6 @@
#include "me/types.h" #include "me/types.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>

View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _line_functions.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 15:23:25 by maiboyer #+# #+# */
/* Updated: 2024/07/10 15:38:46 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef _LINE_FUNCTIONS_H
# define _LINE_FUNCTIONS_H
# include "line/_line_structs.h"
# include "me/fs/fs.h"
# include "me/types.h"
# include "me/vec/vec_str.h"
t_error line_edit_insert(t_line_state *state, char c);
t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd,
t_const_str prompt);
t_str line_edit_feed(t_line_state *state);
void line_edit_backspace(t_line_state *state);
void line_edit_delete(t_line_state *state);
void line_edit_history_next(t_line_state *state, t_history_direction dir);
void line_edit_move_end(t_line_state *state);
void line_edit_move_home(t_line_state *state);
void line_edit_move_left(t_line_state *state);
void line_edit_move_right(t_line_state *state);
void line_edit_stop(t_line_state *state);
t_str linenoise(t_const_str prompt);
t_str line_blocking_edit(t_fd *stdin_fd, t_fd *stdout_fd, t_const_str prompt);
bool line_history_add(t_const_str line);
t_error line_history_load(t_str name);
t_error line_history_save(t_str name);
void line_clear_screen(t_fd *output);
void line_refresh(t_line_state *state, t_line_flags flags);
void line_hide(t_line_state *state);
void line_show(t_line_state *state);
#endif /* _LINE_FUNCTIONS_H */

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _line_internal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 15:37:40 by maiboyer #+# #+# */
/* Updated: 2024/07/10 15:48:56 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef _LINE_INTERNAL_H
# define _LINE_INTERNAL_H
# include "line/_line_functions.h"
# include "me/fs/fs.h"
# include "me/string/string.h"
# include "me/types.h"
# include "me/vec/vec_str.h"
t_const_str get_unfinished_str(void);
t_raw_mode_state *get_raw_mode_state(void);
t_vec_str *get_history(void);
void free_history(void);
t_error gnl_wrapper(t_fd *fd, t_string *out);
void line_uninit_lib(void);
t_error line_get_cursor_position(t_fd *input, t_fd *output,
t_u32 *column_out);
t_u32 line_get_columns(t_fd *input, t_fd *output);
t_usize line_get_prompt_len(t_const_str s);
t_str line_no_tty_impl(void);
void line_print_key_codes(void);
t_error line_enable_raw_mode(t_fd *fd);
void line_disable_raw_mode(t_fd *fd);
#endif /* _LINE_INTERAL_H */

View file

@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _line_structs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 15:24:34 by maiboyer #+# #+# */
/* Updated: 2024/07/10 15:53:54 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef _LINE_STRUCTS_H
# define _LINE_STRUCTS_H
# include "me/fs/fs.h"
# include "me/string/string.h"
# include "me/types.h"
# include <termios.h>
typedef struct s_line_state t_line_state;
typedef struct s_raw_mode_state t_raw_mode_state;
typedef enum e_line_flags t_line_flags;
typedef enum e_key_action t_key_action;
typedef enum e_history_direction t_history_direction;
/// @param input_fd Terminal stdin file descriptor.
/// @param output_fd Terminal stdout file descriptor.
/// @param buf Edited line buffer.
/// @param prompt_len Prompt to display.
/// @param pos Prompt length.
/// @param prompt_len Current cursor position.
/// @param columns Number of columns in terminal.
/// @param history_index The history index we are currently editing.
struct s_line_state
{
t_fd *input_fd;
t_fd *output_fd;
t_string buf;
t_const_str prompt;
t_usize prompt_len;
t_usize pos;
t_usize columns;
t_i32 history_index;
};
struct s_raw_mode_state
{
bool enabled;
struct termios state;
};
enum e_key_action
{
K_KEY_NULL = 0,
K_CTRL_A = 1,
K_CTRL_B = 2,
K_CTRL_C = 3,
K_CTRL_D = 4,
K_CTRL_E = 5,
K_CTRL_F = 6,
K_CTRL_H = 8,
K_TAB = 9,
K_NEWLINE = 10,
K_CTRL_K = 11,
K_CTRL_L = 12,
K_ENTER = 13,
K_CTRL_N = 14,
K_CTRL_P = 16,
K_CTRL_T = 20,
K_CTRL_U = 21,
K_CTRL_W = 23,
K_ESC = 27,
K_BACKSPACE = 127
};
enum e_line_flags
{
REFRESH_CLEAN = 1 << 0,
REFRESH_WRITE = 1 << 1,
REFRESH_ALL = REFRESH_CLEAN | REFRESH_WRITE,
};
enum e_history_direction
{
HIST_PREV,
HIST_NEXT,
};
#endif /* _LINE_STRUCTS_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:07:02 by maiboyer #+# #+# */ /* Created: 2024/07/05 18:07:02 by maiboyer #+# #+# */
/* Updated: 2024/07/05 19:59:47 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 15:28:51 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,62 +14,9 @@
/// https://github.com/antirez/linenoise /// https://github.com/antirez/linenoise
#ifndef LINE_H #ifndef LINE_H
#define LINE_H # define LINE_H
#include "me/fs/fs.h" # include "line/_line_functions.h"
#include "me/types.h" # include "line/_line_structs.h"
#include <termios.h>
extern t_str linenoiseEditMore;
typedef struct s_line_state t_line_state;
/* The linenoiseState structure represents the state during line editing.
* We pass this state to functions implementing specific editing
* functionalities. */
struct s_line_state
{
t_fd *input_fd; /* Terminal stdin file descriptor. */
t_fd *output_fd; /* Terminal stdout file descriptor. */
t_str buf; /* Edited line buffer. */
t_usize buflen; /* Edited line buffer size. */
t_const_str prompt; /* Prompt to display. */
t_usize prompt_len; /* Prompt length. */
t_usize pos; /* Current cursor position. */
t_usize old_pos; /* Previous refresh cursor position. */
t_usize len; /* Current edited line length. */
t_usize columns; /* Number of columns in terminal. */
t_usize old_rows; /* Rows used by last refrehsed line (multiline mode) */
t_i32 history_index; /* The history index we are currently editing. */
};
struct s_raw_mode_state
{
bool enabled;
struct termios state;
};
typedef struct s_raw_mode_state t_raw_mode_state;
/* Non blocking API. */
int linenoiseEditStart(t_line_state *l, int stdin_fd, int stdout_fd, char *buf, t_usize buflen, const char *prompt);
char *linenoiseEditFeed(t_line_state *l);
void linenoiseEditStop(t_line_state *l);
void linenoiseHide(t_line_state *l);
void linenoiseShow(t_line_state *l);
/* Blocking API. */
char *linenoise(const char *prompt);
void linenoise_free(void *ptr);
/* History API. */
bool linenoise_history_add(t_const_str line);
t_error linenoiseHistorySave(t_str name);
t_error linenoise_history_load(t_str name);
/* Other utilities. */
void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void);
#endif /* LINE_H */ #endif /* LINE_H */

View file

@ -1 +1,2 @@
line line
line_globals

File diff suppressed because it is too large Load diff

62
line/src/line_globals.c Normal file
View file

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* line_globals.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 15:47:12 by maiboyer #+# #+# */
/* Updated: 2024/07/10 15:56:48 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/types.h"
#include "line/_line_internal.h"
#include "line/_line_structs.h"
#include "me/mem/mem.h"
t_const_str get_unfinished_str(void)
{
return ("If you see this,"
" you are misusing the API"
" if it returns get_unfinished_str()"
" the user is yet editing the line."
" See the README file for more information.");
}
t_vec_str *get_history(void)
{
static t_vec_str history = {};
static bool init = false;
if (!init)
{
history = vec_str_new(256, (void (*)())mem_free);
init = true;
}
return (&history);
}
t_raw_mode_state *get_raw_mode_state(void)
{
static t_raw_mode_state state = {};
return (&state);
}
/* Free the history, but does not reset it. Only used when we have to
* exit() to avoid memory leaks are reported by valgrind & co. */
void free_history(void)
{
t_vec_str *history;
history = get_history();
vec_str_free(*history);
}
/* At exit we'll try to fix the terminal to the initial conditions. */
__attribute__((destructor)) void line_uninit_lib(void)
{
line_disable_raw_mode(get_stdin());
free_history();
}

View file

@ -6,13 +6,14 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/07/03 21:23:17 by maiboyer ### ########.fr */ /* Updated: 2024/07/08 21:11:43 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "app/env.h" #include "app/env.h"
#include "app/node.h" #include "app/node.h"
#include "app/signal_handler.h" #include "app/signal_handler.h"
#include "line/line.h"
#include "me/hashmap/hashmap_env.h" #include "me/hashmap/hashmap_env.h"
#include "me/str/str.h" #include "me/str/str.h"
#include "me/types.h" #include "me/types.h"
@ -128,14 +129,13 @@ void ft_take_args(t_utils *shcat)
while (1) while (1)
{ {
shcat->str_input = NULL; shcat->str_input = NULL;
cmd = readline((t_const_str)shcat->name_shell); cmd = linenoise((t_const_str)shcat->name_shell);
if (cmd == NULL) if (cmd == NULL)
ft_exit(shcat, 0); ft_exit(shcat, 0);
shcat->str_input = str_clone(cmd); shcat->str_input = cmd;
free(cmd); line_history_add(shcat->str_input);
shcat->current_node = parse_str(&shcat->parser, shcat->str_input); shcat->current_node = parse_str(&shcat->parser, shcat->str_input);
exec_shcat(shcat); exec_shcat(shcat);
add_history(shcat->str_input);
mem_free(shcat->str_input); mem_free(shcat->str_input);
} }
} }
@ -158,6 +158,9 @@ void free_myparser(t_parser self)
ts_parser_delete(self.parser); ts_parser_delete(self.parser);
} }
#define IGN_START "\1"
#define IGN_END "\2"
t_i32 main(t_i32 argc, t_str argv[], t_str envp[]) t_i32 main(t_i32 argc, t_str argv[], t_str envp[])
{ {
t_utils utils; t_utils utils;
@ -172,11 +175,11 @@ t_i32 main(t_i32 argc, t_str argv[], t_str envp[])
utils.env = create_env_map(); utils.env = create_env_map();
if (populate_env(utils.env, envp)) if (populate_env(utils.env, envp))
me_abort("Unable to build env hashmap"); me_abort("Unable to build env hashmap");
utils.name_shell = "\001\x1B[93m\002" utils.name_shell = "\x1B[93m"
"42sh" "42sh"
"\001\x1B[32m\002" "\x1B[32m"
">" ">"
"\001\x1B[0m\002" "\x1B[0m"
"$ "; "$ ";
ft_take_args(&utils); ft_take_args(&utils);
} }

View file

@ -6,14 +6,14 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:22:14 by maiboyer #+# #+# */ /* Created: 2024/05/02 13:22:14 by maiboyer #+# #+# */
/* Updated: 2024/05/23 17:07:03 by maiboyer ### ########.fr */ /* Updated: 2024/07/09 14:43:30 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "app/signal_handler.h" #include "app/signal_handler.h"
#include "me/fs/fs.h"
#include "me/printf/printf.h" #include "me/printf/printf.h"
#include "me/types.h" #include "me/types.h"
#include "readline/readline.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -22,10 +22,11 @@ void sigint_handle(int sig, siginfo_t *info, void *ucontext)
(void)(sig); (void)(sig);
(void)(info); (void)(info);
(void)(ucontext); (void)(ucontext);
printf("\n"); write_fd(get_stdout(), (void *)"\n", 1, NULL);
rl_replace_line("", 0); // TODO: change this to the linenoise verison
rl_on_new_line(); // rl_replace_line("", 0);
rl_redisplay(); // rl_on_new_line();
// rl_redisplay();
} }
void sigquit_handle(int sig, siginfo_t *info, void *ucontext) void sigquit_handle(int sig, siginfo_t *info, void *ucontext)
@ -33,10 +34,10 @@ void sigquit_handle(int sig, siginfo_t *info, void *ucontext)
(void)(sig); (void)(sig);
(void)(info); (void)(info);
(void)(ucontext); (void)(ucontext);
printf("\n"); write_fd(get_stdout(), (void *)"\n", 1, NULL);
rl_replace_line("", 0); // rl_replace_line("", 0);
rl_on_new_line(); // rl_on_new_line();
rl_redisplay(); // rl_redisplay();
} }
void sigsegv_handle(int sig, siginfo_t *info, void *ucontext) void sigsegv_handle(int sig, siginfo_t *info, void *ucontext)
@ -56,8 +57,8 @@ t_error install_signal(void)
data = (struct sigaction){}; data = (struct sigaction){};
data.sa_sigaction = sigint_handle; data.sa_sigaction = sigint_handle;
data.sa_flags = SA_SIGINFO | SA_NOCLDWAIT; data.sa_flags = SA_SIGINFO | SA_NOCLDWAIT;
if (sigaction(SIGINT, &data, NULL)) // if (sigaction(SIGINT, &data, NULL))
return (ERROR); // return (ERROR);
data.sa_sigaction = sigquit_handle; data.sa_sigaction = sigquit_handle;
if (sigaction(SIGQUIT, &data, NULL)) if (sigaction(SIGQUIT, &data, NULL))

View file

@ -6,14 +6,13 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:12:18 by maiboyer #+# #+# */ /* Created: 2024/05/19 15:12:18 by maiboyer #+# #+# */
/* Updated: 2024/05/24 15:03:40 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:50:18 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef FS_H #ifndef FS_H
#define FS_H #define FS_H
#include "me/fs/read_to_vec.h"
#include "me/types.h" #include "me/types.h"
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
@ -78,8 +77,8 @@ typedef enum e_file_open_option
/// `G` means group /// `G` means group
/// `U` means user /// `U` means user
/// `ALL` means all /// `ALL` means all
/// There are the raw permission, you can combine them to get the permission you want /// There are the raw permission, you can combine them to get the permission you
/// And there are "aliases" that are common permission set /// want And there are "aliases" that are common permission set
/// @note you can combine them with the `|` operator /// @note you can combine them with the `|` operator
typedef enum e_file_perm typedef enum e_file_perm
{ {
@ -139,7 +138,6 @@ union u_file_slot {
t_file file; t_file file;
}; };
/// @brief File slot structure /// @brief File slot structure
/// ty: the type of the slot /// ty: the type of the slot
/// slot: the slot itself /// slot: the slot itself
@ -164,9 +162,9 @@ typedef t_const_str t_mode;
/// @note this is a simple typedef because I hate the struct keyword /// @note this is a simple typedef because I hate the struct keyword
typedef struct stat t_stat; typedef struct stat t_stat;
/// @brief Directory entry structure /// @brief Directory entry structure
/// @note this is a simple typedef because I hate the struct keyword and it is always behind a pointer /// @note this is a simple typedef because I hate the struct keyword and it is
/// always behind a pointer
typedef struct dirent *t_dir_entry; typedef struct dirent *t_dir_entry;
/*_____ _ _ _______ ______ _____ _ _ _ /*_____ _ _ _______ ______ _____ _ _ _
@ -187,7 +185,6 @@ t_fd_array *get_fd_arrays(void);
/// @note Will abort if no slot is available /// @note Will abort if no slot is available
struct s_file_slot *get_unused_fd_slot(void); struct s_file_slot *get_unused_fd_slot(void);
/// @brief Close all slots /// @brief Close all slots
/// @note This is probably NOT what you want /// @note This is probably NOT what you want
void close_all_slots(void); void close_all_slots(void);
@ -197,7 +194,6 @@ void close_all_slots(void);
/// @note this is probably NOT what you want /// @note this is probably NOT what you want
void close_slot(struct s_file_slot *slot); void close_slot(struct s_file_slot *slot);
/* ______ _____ /* ______ _____
| ____| __ \ | ____| __ \
| |__ | | | | | |__ | | | |
@ -263,7 +259,6 @@ void put_char_fd(t_fd *fd, t_u8 c);
|_____/_____|_| \_\______\_____| |_| \____/|_| \_\ |_| |_____/_____|_| \_\______\_____| |_| \____/|_| \_\ |_|
*/ */
/// @brief Open a file /// @brief Open a file
/// @param[in] name the name of the file /// @param[in] name the name of the file
/// @param[out] dir the file structure to fill /// @param[out] dir the file structure to fill
@ -304,7 +299,8 @@ t_error open_file(t_str name, t_mode mode, t_file **file);
/// @param[in] size the size of the buffer /// @param[in] size the size of the buffer
/// @param[out] read_count the number of bytes read /// @param[out] read_count the number of bytes read
/// @return true on error, false otherwise /// @return true on error, false otherwise
t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count); t_error read_file(t_file *file, t_u8 *buffer, t_usize size,
t_isize *read_count);
/// @brief Write to a file /// @brief Write to a file
/// @param[in] file the file to write to /// @param[in] file the file to write to
@ -313,11 +309,25 @@ t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
/// @param[out] write_count the number of bytes written /// @param[out] write_count the number of bytes written
/// @return true on error, false otherwise /// @return true on error, false otherwise
/// @note write_count can be NULL /// @note write_count can be NULL
t_error write_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *write_count); t_error write_file(t_file *file, t_u8 *buffer, t_usize size,
t_isize *write_count);
/// @brief Close the underlying file stream /// @brief Close the underlying file stream
/// @param[in] file the file to close /// @param[in] file the file to close
/// @note Will close the file and free the slot /// @note Will close the file and free the slot
void close_file(t_file *file); void close_file(t_file *file);
/* _____ ______ _______ _______ ______ _____ _____
/ ____| ____|__ __|__ __| ____| __ \ / ____|
| | __| |__ | | | | | |__ | |__) | (___
| | |_ | __| | | | | | __| | _ / \___ \
| |__| | |____ | | | | | |____| | \ \ ____) |
\_____|______| |_| |_| |______|_| \_\_____/
*/
//TODO: Documentation!
t_fd *get_stdin(void);
t_fd *get_stdout(void);
t_fd *get_stderr(void);
#endif /* FS_H */ #endif /* FS_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 15:43:08 by maiboyer #+# #+# */ /* Created: 2024/01/03 15:43:08 by maiboyer #+# #+# */
/* Updated: 2024/01/06 18:39:58 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 18:25:23 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,14 +29,14 @@ enum e_redirection
union u_redirection union u_redirection
{ {
struct s_fd struct s_fd_redirection
{ {
int value; int value;
} fd; } fd;
struct s_piped struct s_piped_redirection
{ {
} piped; } piped;
struct s_inherited struct s_inherited_redirection
{ {
} inherited; } inherited;
}; };

View file

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _internal_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 17:36:38 by maiboyer #+# #+# */
/* Updated: 2024/07/07 18:01:17 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef _INTERNAL_PRINTF_H
#define _INTERNAL_PRINTF_H
#include "me/fs/fs.h"
#include "me/string/string.h"
#include "me/types.h"
typedef enum e_printf_flags t_printf_flags;
typedef enum e_printf_type t_printf_type;
typedef struct s_fprintf_arg t_fprintf_arg;
typedef struct s_printf_args t_printf_arg;
typedef struct s_printf_extra_args t_printf_extra_args;
typedef struct s_sprintf_arg t_sprintf_arg;
typedef void (*t_printf_func)(t_const_str to_write, t_usize to_write_len,
void *p_args);
struct s_fprintf_arg
{
t_usize total_print;
t_fd *fd;
};
struct s_sprintf_arg
{
t_usize total_print;
t_string *buffer;
};
enum e_printf_flags
{
PRECISION = 1 << 1,
ALIGN = 1 << 2,
ZERO_ALIGN = 1 << 3,
SIGN = 1 << 4,
};
enum e_printf_type
{
CHAR = 1 << 0,
STR = 1 << 1,
U64 = 1 << 2,
I64 = 1 << 3,
VOID_PTR = 1 << 4,
I32 = 1 << 5,
U32 = 1 << 6,
};
struct s_printf_extra_args
{
t_u64 precision;
t_u64 align;
bool left_align;
bool space_align;
bool pretty;
};
struct s_printf_args
{
void *argument;
void *p_args;
t_printf_extra_args extra;
t_printf_flags flags;
};
void me_printf_write(t_const_str to_write, t_usize to_write_len, void *p_args);
void me_printf_append_string(t_const_str to_write, t_usize to_write_len,
void *p_args);
#endif /* _INTERNAL_PRINTF_H */

View file

@ -6,14 +6,13 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:18:19 by maiboyer #+# #+# */ /* Created: 2023/11/16 18:18:19 by maiboyer #+# #+# */
/* Updated: 2023/11/18 19:11:23 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:38:52 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef FORMATTER_H #ifndef FORMATTER_H
# define FORMATTER_H # define FORMATTER_H
# include "me/printf/printf.h" # include "me/printf/_internal_printf.h"
# include "me/types.h"
void printf_x_low(t_printf_arg data, t_printf_func f); void printf_x_low(t_printf_arg data, t_printf_func f);
void printf_x_up(t_printf_arg data, t_printf_func f); void printf_x_up(t_printf_arg data, t_printf_func f);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:58:41 by maiboyer #+# #+# */ /* Created: 2023/11/16 17:58:41 by maiboyer #+# #+# */
/* Updated: 2023/12/01 21:24:21 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:38:49 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
# define UTILS_H # define UTILS_H
# include "me/printf/matchers/matchers.h" # include "me/printf/matchers/matchers.h"
# include "me/printf/printf.h" # include "me/printf/_internal_printf.h"
# include "me/types.h" # include "me/types.h"
# include <stdarg.h> # include <stdarg.h>

View file

@ -6,14 +6,14 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:09:07 by maiboyer #+# #+# */ /* Created: 2023/11/16 18:09:07 by maiboyer #+# #+# */
/* Updated: 2023/11/18 18:10:33 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:38:46 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MATCHERS_H #ifndef MATCHERS_H
# define MATCHERS_H # define MATCHERS_H
# include "me/printf/printf.h" # include "me/printf/_internal_printf.h"
# include "me/types.h" # include "me/types.h"
# include <stdarg.h> # include <stdarg.h>
# define PRINTF_BUFFER_CHUNK 20 # define PRINTF_BUFFER_CHUNK 20

View file

@ -6,64 +6,23 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:10:27 by maiboyer #+# #+# */ /* Created: 2023/11/16 18:10:27 by maiboyer #+# #+# */
/* Updated: 2024/07/05 19:54:25 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:37:02 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef PRINTF_H #ifndef PRINTF_H
# define PRINTF_H #define PRINTF_H
# include "me/types.h" #include "me/types.h"
# include <stdarg.h> #include <stdarg.h>
# ifndef FS_H #ifndef FS_H
typedef struct s_fd t_fd; typedef struct s_fd t_fd;
# endif #endif
typedef struct s_fprintf_arg #ifndef STRING_H
{ typedef struct s_string t_string;
t_usize total_print; #endif
int fd;
} t_fprintf_arg;
typedef enum e_printf_flags
{
PRECISION = 1 << 1,
ALIGN = 1 << 2,
ZERO_ALIGN = 1 << 3,
SIGN = 1 << 4,
} t_printf_flags;
typedef enum e_printf_type
{
CHAR = 1 << 0,
STR = 1 << 1,
U64 = 1 << 2,
I64 = 1 << 3,
VOID_PTR = 1 << 4,
I32 = 1 << 5,
U32 = 1 << 6,
} t_printf_type;
typedef struct s_printf_extra_args
{
t_u64 precision;
t_u64 align;
bool left_align;
bool space_align;
bool pretty;
} t_printf_extra_args;
typedef struct s_printf_args
{
void *argument;
void *p_args;
t_printf_extra_args extra;
t_printf_flags flags;
} t_printf_arg;
typedef void (*t_printf_func)(t_const_str to_write,
t_usize to_write_len, void *p_args);
/// @brief Print a formatted string to stdout /// @brief Print a formatted string to stdout
/// @param fmt the format string /// @param fmt the format string
@ -89,7 +48,6 @@ t_usize me_vprintf(t_const_str fmt, va_list *args);
/// @return the number of characters printed /// @return the number of characters printed
t_usize me_veprintf(t_const_str fmt, va_list *args); t_usize me_veprintf(t_const_str fmt, va_list *args);
/// @brief Print a formatted string to the given fd /// @brief Print a formatted string to the given fd
/// @param fmt the format string /// @param fmt the format string
/// @param ... the arguments to format /// @param ... the arguments to format
@ -102,4 +60,18 @@ t_usize me_printf_fd(t_fd *, t_const_str fmt, ...);
/// @return the number of characters printed /// @return the number of characters printed
t_usize me_vprintf_fd(t_fd *, t_const_str fmt, va_list *args); t_usize me_vprintf_fd(t_fd *, t_const_str fmt, va_list *args);
/// @brief print a formatted string to a buffer
/// @param buffer the buffer to append to
/// @param fmt the format string
/// @param ... the arguments to format
/// @return the number of characters printed
t_usize me_printf_str(t_string *buffer, t_const_str fmt, ...);
/// @brief print a formatted string to a buffer
/// @param buffer the buffer to append to
/// @param fmt the format string
/// @param args the arguments to format
/// @return the number of characters printed
t_usize me_vprintf_str(t_string *buffer, t_const_str fmt, va_list *args);
#endif #endif

View file

@ -6,13 +6,13 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:54:28 by maiboyer #+# #+# */ /* Created: 2023/11/16 17:54:28 by maiboyer #+# #+# */
/* Updated: 2024/04/30 14:14:42 by maiboyer ### ########.fr */ /* Updated: 2024/07/08 21:58:11 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef STRING_H #ifndef STRING_H
# define STRING_H #define STRING_H
# include "me/types.h" #include "me/types.h"
typedef struct s_string typedef struct s_string
{ {
@ -27,7 +27,6 @@ typedef struct s_string
/// @return true if it failed, false otherwise /// @return true if it failed, false otherwise
t_error string_push(t_string *buf, t_const_str to_push); t_error string_push(t_string *buf, t_const_str to_push);
/// @brief Push a character to a buffer /// @brief Push a character to a buffer
/// @param buf the string to be pushed to /// @param buf the string to be pushed to
/// @param to_push the character to push /// @param to_push the character to push
@ -74,4 +73,33 @@ static inline char string_pop(t_string *buf)
return (c); return (c);
} }
/// @brief Insert a string into self
/// @param self the string to insert into
/// @param pos the index to start inserting at
/// @param str the string to insert
/// @return Error in case pos was over the string length, self was equal to NULL
/// or str was equal to NULL
t_error string_insert(t_string *self, t_usize pos, t_str str);
/// @brief Insert a char into self
/// @param self the string to insert into
/// @param pos the index to start inserting at
/// @param str the character to insert
/// @return Error in case pos was over the string length, self was equal to NULL
/// or chr was '\0'
t_error string_insert_char(t_string *self, t_usize pos, char chr);
/// @brief Clear the string, keeping everything before `pos`
/// @param self the string to operate on
/// @param pos the position to start remove from
/// @return returns an Error if self is null or pos is after the string length
t_error string_clear_after(t_string *self, t_usize pos);
/// @brief Remove a single character from the string, putting it in `out`
/// @param self the string to operate on
/// @param pos the position to start remove
/// @param out[out] the place to put the removed character, if any. Can be NULL
/// @return returns an Error if self is null or pos is after the string length
t_error string_remove(t_string *self, t_usize pos, char *out);
#endif #endif

View file

@ -73,6 +73,7 @@ os/pipe
os/process os/process
os/process_inner os/process_inner
os/process_inner2 os/process_inner2
printf/callbacks
printf/formatter/char printf/formatter/char
printf/formatter/decimal printf/formatter/decimal
printf/formatter/hex printf/formatter/hex
@ -85,6 +86,8 @@ printf/formatter/utils3
printf/formatter/utils_numbers printf/formatter/utils_numbers
printf/matchers printf/matchers
printf/printf printf/printf
printf/printf_fd
printf/printf_str
printf/vprintf printf/vprintf
str/str_clone str/str_clone
str/str_compare str/str_compare
@ -103,3 +106,6 @@ str/str_split
str/str_substring str/str_substring
str/str_trim str/str_trim
string/mod string/mod
string/string_insert
string/string_remove
string/string_reserve

View file

@ -6,19 +6,20 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:53:50 by maiboyer #+# #+# */ /* Created: 2024/05/19 15:53:50 by maiboyer #+# #+# */
/* Updated: 2024/05/30 16:02:12 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 19:17:05 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/types.h"
#include "me/fs/fs.h" #include "me/fs/fs.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/str/str.h" #include "me/str/str.h"
#include "me/types.h"
#include "unistd.h"
#include <stdio.h>
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
t_fd_array *get_fd_arrays(void) t_fd_array *get_fd_arrays(void)
{ {
@ -45,7 +46,7 @@ struct s_file_slot *get_unused_fd_slot(void)
return (NULL); return (NULL);
} }
void close_all_slots(void) __attribute__((destructor(201))) void close_all_slots(void)
{ {
t_usize i; t_usize i;
t_fd_array *arr; t_fd_array *arr;
@ -63,11 +64,11 @@ void close_slot(struct s_file_slot *slot)
if (slot->ty == SLOT_UNUSED) if (slot->ty == SLOT_UNUSED)
; ;
else if (slot->ty == SLOT_FD) else if (slot->ty == SLOT_FD)
close_fd(&slot->slot.fd); (mem_free(slot->slot.fd.name), close_fd(&slot->slot.fd));
else if (slot->ty == SLOT_DIR) else if (slot->ty == SLOT_DIR)
close_dir(&slot->slot.dir); (mem_free(slot->slot.dir.name), close_dir(&slot->slot.dir));
else if (slot->ty == SLOT_FILE) else if (slot->ty == SLOT_FILE)
close_file(&slot->slot.file); (mem_free(slot->slot.file.name), close_file(&slot->slot.file));
else else
(void)!write(2, "Unknown SLOT type", 17); (void)!write(2, "Unknown SLOT type", 17);
mem_set_zero(slot, sizeof(*slot)); mem_set_zero(slot, sizeof(*slot));
@ -112,8 +113,12 @@ t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count) t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count)
{ {
t_isize ret; t_isize ret;
t_isize false_ret;
if (fd == NULL || buffer == NULL || read_count == NULL || fd->fd == -1 || !(fd->perms & FD_READ)) if (read_count == NULL)
read_count = &false_ret;
if (fd == NULL || buffer == NULL || fd->fd == -1 ||
!(fd->perms & FD_READ))
return (ERROR); return (ERROR);
ret = read(fd->fd, buffer, size); ret = read(fd->fd, buffer, size);
if (ret == -1) if (ret == -1)
@ -152,10 +157,10 @@ void close_fd(t_fd *fd)
struct s_file_slot *slot; struct s_file_slot *slot;
if (fd == NULL) if (fd == NULL)
return ; return;
if (close(fd->fd) == -1) if (close(fd->fd) == -1)
return ; return;
slot = (void*)(fd) - offsetof(struct s_file_slot, slot.fd); slot = (void *)(fd)-offsetof(struct s_file_slot, slot.fd);
mem_set_zero(slot, sizeof(*slot)); mem_set_zero(slot, sizeof(*slot));
} }
@ -178,7 +183,7 @@ void put_number_fd(t_fd *fd, t_u64 number)
void put_string_fd(t_fd *fd, t_const_str string) void put_string_fd(t_fd *fd, t_const_str string)
{ {
write_fd(fd, (t_u8*)string, str_len(string), NULL); write_fd(fd, (t_u8 *)string, str_len(string), NULL);
} }
void put_char_fd(t_fd *fd, t_u8 c) void put_char_fd(t_fd *fd, t_u8 c)
@ -229,10 +234,10 @@ void close_dir(t_dir *dir)
struct s_file_slot *slot; struct s_file_slot *slot;
if (dir == NULL) if (dir == NULL)
return ; return;
if (closedir(dir->ptr) == -1) if (closedir(dir->ptr) == -1)
return ; return;
slot = (void*)(dir) - offsetof(struct s_file_slot, slot.dir); slot = (void *)(dir)-offsetof(struct s_file_slot, slot.dir);
mem_set_zero(slot, sizeof(*slot)); mem_set_zero(slot, sizeof(*slot));
} }
@ -260,7 +265,8 @@ t_error open_file(t_str name, t_mode mode, t_file **file)
return (NO_ERROR); return (NO_ERROR);
} }
t_error write_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *write_count) t_error write_file(t_file *file, t_u8 *buffer, t_usize size,
t_isize *write_count)
{ {
t_isize ret; t_isize ret;
t_isize fake_ret; t_isize fake_ret;
@ -280,7 +286,8 @@ t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
{ {
t_isize ret; t_isize ret;
if (file == NULL || buffer == NULL || read_count == NULL || file->ptr == NULL) if (file == NULL || buffer == NULL || read_count == NULL ||
file->ptr == NULL)
return (ERROR); return (ERROR);
ret = fread(buffer, size, 1, file->ptr); ret = fread(buffer, size, 1, file->ptr);
if (ret == -1) if (ret == -1)
@ -294,9 +301,74 @@ void close_file(t_file *file)
struct s_file_slot *slot; struct s_file_slot *slot;
if (file == NULL) if (file == NULL)
return ; return;
if (fclose(file->ptr) == -1) if (fclose(file->ptr) == -1)
return ; return;
slot = (void*)(file) - offsetof(struct s_file_slot, slot.file); slot = (void *)(file)-offsetof(struct s_file_slot, slot.file);
mem_set_zero(slot, sizeof(*slot)); mem_set_zero(slot, sizeof(*slot));
} }
/* _____ ______ _______ _______ ______ _____ _____
/ ____| ____|__ __|__ __| ____| __ \ / ____|
| | __| |__ | | | | | |__ | |__) | (___
| | |_ | __| | | | | | __| | _ / \___ \
| |__| | |____ | | | | | |____| | \ \ ____) |
\_____|______| |_| |_| |______|_| \_\_____/
*/
t_fd *get_stdin(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{
slot = get_unused_fd_slot();
out = &slot->slot.fd;
out->fd = STDIN_FILENO;
out->perms = FD_READ;
out->name = str_clone("<stdin>");
slot->ty = SLOT_FD;
value = out;
}
return (value);
}
t_fd *get_stdout(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{
slot = get_unused_fd_slot();
out = &slot->slot.fd;
out->fd = STDOUT_FILENO;
out->perms = FD_WRITE;
out->name = str_clone("<stdout>");
slot->ty = SLOT_FD;
value = out;
}
return (value);
}
t_fd *get_stderr(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{
slot = get_unused_fd_slot();
out = &slot->slot.fd;
out->fd = STDERR_FILENO;
out->perms = FD_WRITE;
out->name = str_clone("<stderr>");
slot->ty = SLOT_FD;
value = out;
}
return (value);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 15:29:38 by maiboyer #+# #+# */ /* Created: 2023/11/03 15:29:38 by maiboyer #+# #+# */
/* Updated: 2024/01/06 18:19:11 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:43:58 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/24 18:38:47 by maiboyer #+# #+# */ /* Created: 2023/12/24 18:38:47 by maiboyer #+# #+# */
/* Updated: 2023/12/30 18:15:58 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:44:22 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */ /* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 17:38:21 by maix #+# #+# */ /* Created: 2023/11/23 17:38:21 by maix #+# #+# */
/* Updated: 2024/05/14 18:39:59 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:45:42 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 19:17:11 by maiboyer #+# #+# */ /* Created: 2024/05/04 19:17:11 by maiboyer #+# #+# */
/* Updated: 2024/05/04 19:18:26 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:46:17 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/24 19:06:05 by maiboyer #+# #+# */ /* Created: 2023/12/24 19:06:05 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:40:29 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:46:34 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,31 +6,31 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 18:26:27 by maiboyer #+# #+# */ /* Created: 2024/05/14 18:26:27 by maiboyer #+# #+# */
/* Updated: 2024/05/22 15:21:28 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:48:46 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "aq/internal_vg_funcs.h"
#include "me/types.h"
#include "aq/allocator.h" #include "aq/allocator.h"
#include "aq/internal_vg_funcs.h"
#include "aq/libc_wrapper.h" #include "aq/libc_wrapper.h"
#include "aq/melloc.h" #include "aq/melloc.h"
#include "me/types.h"
#include <stdio.h>
t_allocator *global_allocator(void) t_allocator *global_allocator(void)
{ {
static t_allocator global_alloc = {}; static t_allocator global_alloc = {};
static bool init = false; static bool init = false;
if (!init) if (!init)
{ {
init = true; init = true;
global_alloc = m_init(); global_alloc = m_init();
// global_alloc = lc_init();
} }
return (&global_alloc); return (&global_alloc);
} }
__attribute__((destructor(200)))
void uninit_global_allocator(void) void uninit_global_allocator(void)
{ {
t_allocator *allocator; t_allocator *allocator;

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 14:47:49 by maiboyer #+# #+# */ /* Created: 2023/12/06 14:47:49 by maiboyer #+# #+# */
/* Updated: 2024/05/22 15:01:06 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:48:20 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 15:53:21 by maiboyer #+# #+# */ /* Created: 2023/11/06 15:53:21 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:30:27 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:47:03 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 12:46:18 by maiboyer #+# #+# */ /* Created: 2024/05/07 12:46:18 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:32:06 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:47:21 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:04:51 by maiboyer #+# #+# */ /* Created: 2024/05/07 11:04:51 by maiboyer #+# #+# */
/* Updated: 2024/05/07 11:06:22 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:49:05 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,12 +6,11 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */ /* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
/* Updated: 2024/05/16 17:26:27 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:54:01 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#define _GNU_SOURCE #include "./gnu_source.h"
#include "me/fs/putendl_fd.h" #include "me/fs/putendl_fd.h"
#include "me/fs/putstr_fd.h" #include "me/fs/putstr_fd.h"
#include "me/types.h" #include "me/types.h"
@ -89,10 +88,8 @@ void me_abort(t_str msg)
{ {
if (msg == NULL) if (msg == NULL)
msg = "No message (msg was NULL)"; msg = "No message (msg was NULL)";
me_putendl_fd("Memory information:", 2);
me_putstr_fd("Abort: ", 2); me_putstr_fd("Abort: ", 2);
me_putendl_fd(msg, 2); me_putendl_fd(msg, 2);
print_trace(); print_trace();
// me_exit(1); me_exit(134);
abort();
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */ /* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */
/* Updated: 2024/05/22 15:05:19 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:54:22 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,9 +15,12 @@
#include "me/types.h" #include "me/types.h"
#include <stdlib.h> #include <stdlib.h>
// If you are looking at why some stuff aren't closed, they are using the
// __attribute__((dtor)) to be run at exit for example:
// - close_all_slots
// - uninit global allocator
void me_exit(t_i32 exit_code) void me_exit(t_i32 exit_code)
{ {
close_all_slots(); (get_stdin(), get_stdout(), get_stderr());
uninit_global_allocator();
exit(exit_code); exit(exit_code);
} }

View file

@ -1,18 +1,18 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* test-skeleton.c :+: :+: :+: */ /* gnu_source.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/22 15:11:51 by maiboyer #+# #+# */ /* Created: 2024/07/10 17:53:46 by maiboyer #+# #+# */
/* Updated: 2024/05/22 15:12:44 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:54:49 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
int do_test(int argc, char **argv); #ifndef GNU_SOURCE_H
# define GNU_SOURCE_H
int main(int argc, char *argv[]) # define _GNU_SOURCE
{
return (do_test(argc, argv)); #endif /* GNU_SOURCE_H */
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */ /* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/05/19 14:57:20 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 18:04:36 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -70,7 +70,7 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
string_push(s, info->binary_path); string_push(s, info->binary_path);
sp_index++; sp_index++;
if (access(s->buf, X_OK | R_OK) == 0) if (access(s->buf, X_OK | R_OK) == 0)
break; break ;
} }
sp_index = 0; sp_index = 0;
while (splitted_path[sp_index]) while (splitted_path[sp_index])
@ -88,7 +88,7 @@ t_error find_binary(t_spawn_info *info, t_process *process)
if (info->binary_path == NULL) if (info->binary_path == NULL)
return (ERROR); return (ERROR);
s = string_new(256); s = string_new(256);
if (str_start_with(info->binary_path, "/") || if (str_start_with(info->binary_path, "/") || \
str_find_chr(info->binary_path, '/') != NULL) str_find_chr(info->binary_path, '/') != NULL)
string_push(&s, info->binary_path); string_push(&s, info->binary_path);
else else
@ -107,7 +107,8 @@ t_error find_binary(t_spawn_info *info, t_process *process)
return (string_free(s), ERROR); return (string_free(s), ERROR);
} }
static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process) static void cleanup(t_spawn_info info, t_process *process, \
bool cleanup_process)
{ {
if (cleanup_process && process->stdin.tag != INVALID) if (cleanup_process && process->stdin.tag != INVALID)
close(process->stdin.vals.ro.fd); close(process->stdin.vals.ro.fd);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */ /* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */
/* Updated: 2024/05/19 14:51:07 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 18:05:03 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -36,6 +36,6 @@ bool find_path(const t_str *s)
if (*s == NULL) if (*s == NULL)
return (false); return (false);
ss = *s; ss = *s;
return (ss[0] == 'P' && ss[1] == 'A' && ss[2] == 'T' && ss[3] == 'H' && return (ss[0] == 'P' && ss[1] == 'A' && ss[2] == 'T' && ss[3] == 'H' && \
ss[4] == '='); ss[4] == '=');
} }

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* callbacks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 18:01:52 by maiboyer #+# #+# */
/* Updated: 2024/07/10 17:58:04 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/fs.h"
#include "me/printf/_internal_printf.h"
#include "me/string/string.h"
#include "me/types.h"
void me_printf_append_string(t_const_str to_write, t_usize to_write_len,
void *p_args)
{
t_sprintf_arg *arg;
arg = p_args;
arg->total_print += to_write_len;
string_push(arg->buffer, to_write);
}
void me_printf_write(t_const_str to_write, \
t_usize to_write_len, void *p_args)
{
t_fprintf_arg *arg;
arg = (t_fprintf_arg *)p_args;
write_fd(arg->fd, (t_u8 *)to_write, to_write_len, NULL);
arg->total_print += to_write_len;
}

View file

@ -6,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 18:12:11 by maiboyer #+# #+# */ /* Created: 2023/11/18 18:12:11 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:43:13 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:39:40 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/printf/formatter/utils.h" #include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h" #include "me/str/str.h"
#include "me/str/str.h" #include "me/str/str.h"
#include <stdlib.h> #include <stdlib.h>

View file

@ -6,14 +6,13 @@
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */ /* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 01:44:35 by maix #+# #+# */ /* Created: 2023/11/18 01:44:35 by maix #+# #+# */
/* Updated: 2024/05/14 18:43:24 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:39:44 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/printf/formatter/utils.h" #include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h" #include "me/str/str.h"
#include "me/str/str.h" #include "me/str/str.h"
#include <stdio.h> #include <stdio.h>

View file

@ -6,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */ /* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */
/* Updated: 2023/12/11 19:19:03 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/printf/formatter/utils.h" #include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h" #include "me/str/str.h"
#define HEX_INLINE_BUF 17 #define HEX_INLINE_BUF 17

View file

@ -6,13 +6,12 @@
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */ /* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 01:19:18 by maix #+# #+# */ /* Created: 2023/11/18 01:19:18 by maix #+# #+# */
/* Updated: 2023/12/11 19:17:23 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/printf/formatter/utils.h" #include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h" #include "me/str/str.h"
#include <stdio.h> #include <stdio.h>
#define OCT_INLINE_BUF 23 #define OCT_INLINE_BUF 23

View file

@ -6,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */ /* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */
/* Updated: 2023/12/11 19:20:42 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/printf/formatter/utils.h" #include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h" #include "me/str/str.h"
#define PTR_INLINE_BUF 17 #define PTR_INLINE_BUF 17

View file

@ -6,14 +6,13 @@
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */ /* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 01:44:35 by maix #+# #+# */ /* Created: 2023/11/18 01:44:35 by maix #+# #+# */
/* Updated: 2024/05/14 18:43:39 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/printf/formatter/utils.h" #include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h" #include "me/str/str.h"
#include "me/str/str.h" #include "me/str/str.h"
#include <stdio.h> #include <stdio.h>

View file

@ -6,16 +6,14 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:57:04 by maiboyer #+# #+# */ /* Created: 2023/11/16 17:57:04 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:43:44 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/string/string.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/convert/atoi.h" #include "me/convert/atoi.h"
#include "me/printf/formatter/utils.h" #include "me/printf/formatter/utils.h"
#include "me/printf/matchers/matchers.h" #include "me/printf/matchers/matchers.h"
#include "me/printf/printf.h"
#include "me/str/str.h" #include "me/str/str.h"
#include "me/str/str.h" #include "me/str/str.h"
#include "me/types.h" #include "me/types.h"

View file

@ -6,14 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:00:07 by maiboyer #+# #+# */ /* Created: 2023/11/16 18:00:07 by maiboyer #+# #+# */
/* Updated: 2023/12/01 21:48:22 by maiboyer ### ########.fr */ /* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/string/string.h"
#include "me/char/char.h" #include "me/char/char.h"
#include "me/printf/formatter/utils.h" #include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/types.h" #include "me/types.h"
void set_var_for_pad_and_stuff(t_pad_and_stuff_args *a, t_printf_arg *d) void set_var_for_pad_and_stuff(t_pad_and_stuff_args *a, t_printf_arg *d)

View file

@ -6,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 21:05:47 by maiboyer #+# #+# */ /* Created: 2023/12/01 21:05:47 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:43:56 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:58:28 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/printf/formatter/utils.h"
void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs, void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs,
t_usize value) t_usize value)

View file

@ -6,14 +6,13 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:07:40 by maiboyer #+# #+# */ /* Created: 2023/11/16 18:07:40 by maiboyer #+# #+# */
/* Updated: 2023/12/11 19:11:51 by maiboyer ### ########.fr */ /* Updated: 2024/07/08 19:37:12 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/printf/formatter/formatter.h" #include "me/printf/formatter/formatter.h"
#include "me/printf/matchers/matchers.h" #include "me/printf/matchers/matchers.h"
#include "me/printf/printf.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -22,15 +21,15 @@ t_matcher_list *get_matchers(void)
{ {
static t_matcher_list printf_matchers = (t_matcher_list){ static t_matcher_list printf_matchers = (t_matcher_list){
.data = { .data = {
{.matcher = "o", .matcher_len = 1, .arg_type = U64, .f = &printf_o}, {.matcher = "o", .matcher_len = 1, .arg_type = U64, .f = printf_o},
{.matcher = "c", .matcher_len = 1, .arg_type = CHAR, .f = &printf_c}, {.matcher = "c", .matcher_len = 1, .arg_type = CHAR, .f = printf_c},
{.matcher = "s", .matcher_len = 1, .arg_type = STR, .f = &printf_s}, {.matcher = "s", .matcher_len = 1, .arg_type = STR, .f = printf_s},
{.matcher = "p", .matcher_len = 1, .arg_type = VOID_PTR, .f = &printf_p}, {.matcher = "p", .matcher_len = 1, .arg_type = VOID_PTR, .f = printf_p},
{.matcher = "d", .matcher_len = 1, .arg_type = I32, .f = &printf_d}, {.matcher = "d", .matcher_len = 1, .arg_type = I32, .f = printf_d},
{.matcher = "i", .matcher_len = 1, .arg_type = I32, .f = &printf_d}, {.matcher = "i", .matcher_len = 1, .arg_type = I32, .f = printf_d},
{.matcher = "u", .matcher_len = 1, .arg_type = U32, .f = &printf_u}, {.matcher = "u", .matcher_len = 1, .arg_type = U32, .f = printf_u},
{.matcher = "x", .matcher_len = 1, .arg_type = U32, .f = &printf_x_low}, {.matcher = "x", .matcher_len = 1, .arg_type = U32, .f = printf_x_low},
{.matcher = "X", .matcher_len = 1, .arg_type = U32, .f = &printf_x_up}, {.matcher = "X", .matcher_len = 1, .arg_type = U32, .f = printf_x_up},
}, },
.next = NULL, .next = NULL,
}; };
@ -55,7 +54,7 @@ bool insert_matcher(t_matcher matcher)
return (true); return (true);
} }
} }
matchers->next = malloc(sizeof(t_matcher_list) * 1); matchers->next = mem_alloc(sizeof(t_matcher_list));
} }
return (false); return (false);
} }
@ -74,7 +73,7 @@ t_matcher *find_matcher(t_const_str fmt, t_matcher_list *matchers,
matcher = &matchers->data[matcher_index]; matcher = &matchers->data[matcher_index];
if (matcher->f) if (matcher->f)
{ {
if (!mem_compare(&fmt[*c_idx], matcher->matcher, if (mem_compare(&fmt[*c_idx], matcher->matcher,
matcher->matcher_len)) matcher->matcher_len))
{ {
*c_idx += matcher->matcher_len; *c_idx += matcher->matcher_len;

View file

@ -6,115 +6,32 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/11 17:50:56 by maiboyer #+# #+# */ /* Created: 2023/11/11 17:50:56 by maiboyer #+# #+# */
/* Updated: 2024/02/09 14:58:10 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:57:15 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/string/string.h"
#include "me/fs/write.h"
#include "me/printf/formatter/formatter.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/matchers/matchers.h"
#include "me/printf/printf.h" #include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/types.h" #include "me/types.h"
#include <limits.h>
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
// p_args is an t_string;
static void me_printf_add_to_string(t_const_str to_write, t_usize to_write_len,
void *p_args)
{
t_string *out_buf;
out_buf = (t_string *)p_args;
(void)(to_write_len);
string_push(out_buf, to_write);
}
t_str me_printf_str(t_const_str fmt, va_list *arguments)
{
t_string out;
out = string_new(str_len(fmt));
if (out.buf == NULL)
{
return (NULL);
}
me_printf_str_inner(fmt, &me_printf_add_to_string, arguments, (void *)&out);
return (out.buf);
}
void me_printf_write(t_const_str to_write, t_usize to_write_len,
void *p_args)
{
t_fprintf_arg *arg;
arg = (t_fprintf_arg *)p_args;
me_write(arg->fd, (t_u8 *)to_write, to_write_len);
arg->total_print += to_write_len;
}
t_usize me_printf(t_const_str fmt, ...) t_usize me_printf(t_const_str fmt, ...)
{ {
va_list args; va_list args;
t_fprintf_arg passthru; t_usize res;
passthru = (t_fprintf_arg){
.fd = 1,
.total_print = 0,
};
va_start(args, fmt); va_start(args, fmt);
me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru); res = me_vprintf(fmt, &args);
va_end(args); va_end(args);
return (passthru.total_print); return (res);
} }
t_usize me_eprintf(t_const_str fmt, ...) t_usize me_eprintf(t_const_str fmt, ...)
{ {
va_list args; va_list args;
t_fprintf_arg passthru; t_usize res;
passthru = (t_fprintf_arg){
.fd = 2,
.total_print = 0,
};
va_start(args, fmt);
me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru);
va_end(args);
return (passthru.total_print);
}
/*
t_usize me_printf(t_const_str fmt, ...)
{
va_list args;
t_str str;
t_usize len;
va_start(args, fmt); va_start(args, fmt);
str = me_printf_str(fmt, &args); res = me_veprintf(fmt, &args);
va_end(args); va_end(args);
len = str_len(str); return (res);
write(1, str, len);
mem_free(str);
return (len);
} }
t_usize me_eprintf(t_const_str fmt, ...)
{
va_list args;
t_str str;
t_usize len;
va_start(args, fmt);
str = me_printf_str(fmt, &args);
va_end(args);
len = str_len(str);
write(2, str, len);
mem_free(str);
return (len);
}
*/

View file

@ -1,4 +1,3 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
@ -7,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 19:55:09 by maiboyer #+# #+# */ /* Created: 2024/07/05 19:55:09 by maiboyer #+# #+# */
/* Updated: 2024/07/05 19:57:23 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:55:18 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,16 +16,14 @@
#include "me/types.h" #include "me/types.h"
#include <stdarg.h> #include <stdarg.h>
void me_printf_write(t_const_str to_write, t_usize to_write_len, void *p_args);
t_usize me_vprintf_fd(t_fd *fd, t_const_str fmt, va_list *args) t_usize me_vprintf_fd(t_fd *fd, t_const_str fmt, va_list *args)
{ {
t_fprintf_arg passthru; t_fprintf_arg passthru;
passthru = (t_fprintf_arg){ if (fd == NULL || fmt == NULL || args == NULL)
.fd = fd->fd, return (0);
.total_print = 0, passthru.fd = fd;
}; passthru.total_print = 0;
me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru); me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru);
return (passthru.total_print); return (passthru.total_print);
} }
@ -34,14 +31,10 @@ t_usize me_vprintf_fd(t_fd *fd, t_const_str fmt, va_list *args)
t_usize me_printf_fd(t_fd *fd, t_const_str fmt, ...) t_usize me_printf_fd(t_fd *fd, t_const_str fmt, ...)
{ {
va_list args; va_list args;
t_fprintf_arg passthru; t_usize res;
passthru = (t_fprintf_arg){
.fd = fd->fd,
.total_print = 0,
};
va_start(args, fmt); va_start(args, fmt);
me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru); res = me_vprintf_fd(fd, fmt, &args);
va_end(args); va_end(args);
return (passthru.total_print); return (res);
} }

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printf_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 17:27:50 by maiboyer #+# #+# */
/* Updated: 2024/07/10 17:57:32 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/printf/_internal_printf.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/string/string.h"
#include "me/types.h"
#include <stdarg.h>
t_usize me_vprintf_str(t_string *buf, t_const_str fmt, va_list *args)
{
t_sprintf_arg passthru;
if (buf == NULL || fmt == NULL || args == NULL)
return (0);
passthru.buffer = buf;
passthru.total_print = 0;
me_printf_str_inner(fmt, &me_printf_append_string, args, &passthru);
return (passthru.total_print);
}
t_usize me_printf_str(t_string *buf, t_const_str fmt, ...)
{
t_usize res;
va_list args;
va_start(args, fmt);
res = me_vprintf_str(buf, fmt, &args);
va_end(args);
return (res);
}

View file

@ -6,34 +6,27 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 14:57:28 by maiboyer #+# #+# */ /* Created: 2024/02/09 14:57:28 by maiboyer #+# #+# */
/* Updated: 2024/02/09 15:00:39 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 17:56:24 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/string/string.h" #include "me/fs/fs.h"
#include "me/fs/write.h"
#include "me/printf/formatter/formatter.h"
#include "me/printf/formatter/utils.h" #include "me/printf/formatter/utils.h"
#include "me/printf/matchers/matchers.h"
#include "me/printf/printf.h" #include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/types.h" #include "me/types.h"
#include <limits.h>
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
void me_printf_write(t_const_str to_write, t_usize to_write_len, void me_printf_write(t_const_str to_write, \
void *p_args); t_usize to_write_len, void *p_args);
t_usize me_vprintf(t_const_str fmt, va_list *args) t_usize me_vprintf(t_const_str fmt, va_list *args)
{ {
t_fprintf_arg passthru; t_fprintf_arg passthru;
passthru = (t_fprintf_arg){ if (fmt == NULL || args == NULL)
.fd = 1, return (0);
.total_print = 0, passthru.fd = get_stdout();
}; passthru.total_print = 0;
me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru); me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru);
return (passthru.total_print); return (passthru.total_print);
} }
@ -42,10 +35,10 @@ t_usize me_veprintf(t_const_str fmt, va_list *args)
{ {
t_fprintf_arg passthru; t_fprintf_arg passthru;
passthru = (t_fprintf_arg){ if (fmt == NULL || args == NULL)
.fd = 2, return (0);
.total_print = 0, passthru.fd = get_stderr();
}; passthru.total_print = 0;
me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru); me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru);
return (passthru.total_print); return (passthru.total_print);
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 16:05:48 by maiboyer #+# #+# */ /* Created: 2023/11/06 16:05:48 by maiboyer #+# #+# */
/* Updated: 2024/05/18 16:34:33 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 18:00:28 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/04 18:53:47 by maiboyer #+# #+# */ /* Created: 2023/11/04 18:53:47 by maiboyer #+# #+# */
/* Updated: 2024/05/04 18:37:40 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 18:00:43 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/str/str.h" #include "me/str/str.h"
// PLEASE FIX THIS FUNCTION IF NEEDED !
bool str_compare(t_const_str lhs, t_const_str rhs) bool str_compare(t_const_str lhs, t_const_str rhs)
{ {
t_usize index; t_usize index;

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:52:12 by maiboyer #+# #+# */ /* Created: 2023/11/16 17:52:12 by maiboyer #+# #+# */
/* Updated: 2024/05/18 18:06:37 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 18:02:36 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -69,7 +69,7 @@ void string_clear(t_string *buf)
{ {
mem_set_zero(buf->buf, buf->capacity); mem_set_zero(buf->buf, buf->capacity);
buf->len = 0; buf->len = 0;
return; return ;
} }
t_string string_new(t_usize capacity) t_string string_new(t_usize capacity)

View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* string_insert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 21:32:17 by maiboyer #+# #+# */
/* Updated: 2024/07/10 18:01:27 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/str/str.h"
#include "me/string/string.h"
#include "me/types.h"
t_error string_insert(t_string *self, t_usize pos, t_str str)
{
t_usize len;
if (self == NULL || str == NULL || pos > self->len)
return (ERROR);
if (pos == self->len)
return (string_push(self, str));
len = str_len(str);
if (string_reserve(self, self->len + len))
return (ERROR);
mem_move(&self->buf[pos + len], &self->buf[pos], self->len - pos);
mem_copy(&self->buf[pos], str, len);
self->len += len;
self->buf[self->len] = '\0';
return (NO_ERROR);
}
t_error string_insert_char(t_string *self, t_usize pos, char chr)
{
char tmp[2];
if (chr == '\0')
return (ERROR);
tmp[0] = chr;
tmp[1] = '\0';
return (string_insert(self, pos, tmp));
}

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* string_remove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 21:50:27 by maiboyer #+# #+# */
/* Updated: 2024/07/10 18:01:53 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/str/str.h"
#include "me/string/string.h"
#include "me/types.h"
t_error string_remove(t_string *self, t_usize pos, char *out)
{
char fake_out;
if (out == NULL)
out = &fake_out;
if (self == NULL || pos >= self->len)
return (ERROR);
*out = self->buf[pos];
mem_move(&self->buf[pos], &self->buf[pos] + 1, self->len - pos);
self->len--;
self->buf[self->len] = '\0';
return (NO_ERROR);
}
t_error string_clear_after(t_string *self, t_usize pos)
{
if (self == NULL || pos >= self->len)
return (ERROR);
mem_set_zero(&self->buf[pos], self->len - pos);
self->len = pos;
self->buf[self->len] = '\0';
return (NO_ERROR);
}

View file

@ -1,28 +1,25 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* redef_alloc.h :+: :+: :+: */ /* string_reserve.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/22 15:15:52 by maiboyer #+# #+# */ /* Created: 2024/07/08 22:02:49 by maiboyer #+# #+# */
/* Updated: 2024/05/22 15:16:34 by maiboyer ### ########.fr */ /* Updated: 2024/07/10 18:01:40 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef REDEF_ALLOC_H
#define REDEF_ALLOC_H
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/string/string.h"
#include "me/types.h"
#undef malloc t_error string_reserve(t_string *self, t_usize capacity)
#undef calloc {
#undef realloc if (self == NULL)
#undef free return (ERROR);
if (self->capacity >= capacity)
#define malloc(s) mem_alloc((s)) return (NO_ERROR);
#define calloc(s, l) mem_alloc_array((s), (l)) self->buf = mem_realloc(self->buf, capacity);
#define realloc(p, t) mem_realloc((p), (t)) return (NO_ERROR);
#define free(p) mem_free((p)) }
#endif /* REDEF_ALLOC_H */

View file

@ -1,124 +0,0 @@
/* Copyright (C) 2000-2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <error.h>
#include <limits.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include "./redef_alloc.h"
/* Number of samples per size. */
#define N 5000
static void fixed_test(int size)
{
char *ptrs[N];
int i;
for (i = 0; i < N; ++i)
{
int j;
ptrs[i] = (char *)calloc(1, size);
if (ptrs[i] == NULL)
break;
for (j = 0; j < size; ++j)
{
if (ptrs[i][j] != '\0')
error(EXIT_FAILURE, 0,
"byte not cleared (size %d, element %d, byte %d)", size,
i, j);
ptrs[i][j] = '\xff';
}
}
while (i-- > 0)
free(ptrs[i]);
}
static void random_test(void)
{
char *ptrs[N];
int i;
for (i = 0; i < N; ++i)
{
int j;
int n = 1 + random() % 10;
int elem = 1 + random() % 100;
int size = n * elem;
ptrs[i] = (char *)calloc(n, elem);
if (ptrs[i] == NULL)
break;
for (j = 0; j < size; ++j)
{
if (ptrs[i][j] != '\0')
error(EXIT_FAILURE, 0,
"byte not cleared (size %d, element %d, byte %d)", size,
i, j);
ptrs[i][j] = '\xff';
}
}
while (i-- > 0)
free(ptrs[i]);
}
static void null_test(void)
{
/* If the size is 0 the result is implementation defined. Just make
sure the program doesn't crash. The result of calloc is
deliberately ignored, so do not warn about that. */
calloc(0, 0);
calloc(0, UINT_MAX);
calloc(UINT_MAX, 0);
calloc(0, ~((size_t)0));
calloc(~((size_t)0), 0);
}
static int do_test(int argc, char **argv)
{
(void)(argc);
(void)(argv);
/* We are allocating blocks with `calloc' and check whether every
block is completely cleared. We first try this for some fixed
times and then with random size. */
fixed_test(15);
fixed_test(5);
fixed_test(17);
fixed_test(6);
fixed_test(31);
fixed_test(96);
random_test();
null_test();
return 0;
}
#define TEST_FUNCTION do_test()
#include "./test-skeleton.c"

View file

@ -1,84 +0,0 @@
/* Copyright (C) 1999-2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "./redef_alloc.h"
static int errors = 0;
static void merror(const char *msg)
{
++errors;
printf("Error: %s\n", msg);
}
static int do_test(int argc, char **argv)
{
(void)(argc);
(void)(argv);
void *p, *q;
int save;
srandom(time(NULL));
errno = 0;
p = malloc(-1);
save = errno;
if (p != NULL)
merror("malloc (-1) succeeded.");
if (p == NULL && save != ENOMEM)
merror("errno is not set correctly");
p = malloc(10);
if (p == NULL)
merror("malloc (10) failed.");
/* realloc (p, 0) == free (p). */
p = realloc(p, 0);
if (p != NULL)
merror("realloc (p, 0) failed.");
p = malloc(0);
if (p == NULL)
merror("malloc (0) failed.");
p = realloc(p, 0);
if (p != NULL)
merror("realloc (p, 0) failed.");
p = malloc(513 * 1024);
if (p == NULL)
merror("malloc (513K) failed.");
q = malloc(-512 * 1024);
if (q != NULL)
merror("malloc (-512K) succeeded.");
free(p);
return errors != 0;
}
#include "./test-skeleton.c"

View file

@ -1,196 +0,0 @@
/* Copyright (C) 2013-2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aq/melloc_interal.h"
#include "./redef_alloc.h"
void FAIL_EXIT1(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
exit(1);
}
static int do_test(int argc, char **argv)
{
(void)(argv);
(void)(argv);
void *p;
unsigned char *p1, *p2, *p3;
unsigned char *c;
int save, i, ok;
errno = 0;
p1 = realloc(NULL, 50);
memset(p1, 0x10, 50);
for (i = 0; i < 50; i++)
{
if (p1[i] != 0x10)
FAIL_EXIT1("memset didn't set (p[%i] = %#02x)\n", i, p1[i]);
}
p2 = malloc(50);
p3 = realloc(p1, 150);
if (p1 == p3)
FAIL_EXIT1("P1 == P2 \n");
for (i = 0; i < 50; i++)
{
if (p3[i] != 0x10)
FAIL_EXIT1("realloc didn't preserve after move (p3[%i] = %#02x)\n", i, p3[i]);
}
// /* realloc (NULL, ...) behaves similarly to malloc (C89). */
// p = realloc(NULL, -1);
// save = errno;
//
// if (p != NULL)
// FAIL_EXIT1("realloc (NULL, -1) succeeded.\n");
//
// /* errno should be set to ENOMEM on failure (POSIX). */
// if (p == NULL && save != ENOMEM)
// FAIL_EXIT1("errno is not set correctly\n");
//
// errno = 0;
//
// /* realloc (NULL, ...) behaves similarly to malloc (C89). */
// p = realloc(NULL, 10);
// save = errno;
//
// if (p == NULL)
// FAIL_EXIT1("realloc (NULL, 10) failed.\n");
//
// free(p);
//
// p = calloc(20, 1);
// if (p == NULL)
// FAIL_EXIT1("calloc (20, 1) failed.\n");
//
// /* Check increasing size preserves contents (C89). */
// p = realloc(p, 200);
// if (p == NULL)
// FAIL_EXIT1("realloc (p, 200) failed.\n");
//
// c = p;
// ok = 1;
//
// for (i = 0; i < 20; i++)
// {
// if (c[i] != 0)
// ok = 0;
// }
//
// if (ok == 0)
// FAIL_EXIT1("first 20 bytes were not cleared\n");
//
// free(p);
//
// p = realloc(NULL, 100);
// if (p == NULL)
// FAIL_EXIT1("realloc (NULL, 100) failed.\n");
//
// memset(p, 0xff, 100);
//
// /* Check decreasing size preserves contents (C89). */
// p = realloc(p, 16);
// if (p == NULL)
// FAIL_EXIT1("realloc (p, 16) failed.\n");
//
// c = p;
// ok = 1;
//
// for (i = 0; i < 16; i++)
// {
// if (c[i] != 0xff)
// ok = 0;
// }
//
// if (ok == 0)
// FAIL_EXIT1("first 16 bytes were not correct\n");
//
// /* Check failed realloc leaves original untouched (C89). */
// c = realloc(p, -1);
// if (c != NULL)
// FAIL_EXIT1("realloc (p, -1) succeeded.\n");
//
// c = p;
// ok = 1;
//
// for (i = 0; i < 16; i++)
// {
// if (c[i] != 0xff)
// ok = 0;
// }
//
// if (ok == 0)
// FAIL_EXIT1("first 16 bytes were not correct after failed realloc\n");
//
// /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */
// p = realloc(p, 0);
// if (p != NULL)
// FAIL_EXIT1("realloc (p, 0) returned non-NULL.\n");
//
// /* realloc (NULL, 0) acts like malloc (0) (glibc). */
// p = realloc(NULL, 0);
// if (p == NULL)
// FAIL_EXIT1("realloc (NULL, 0) returned NULL.\n");
//
// free(p);
//
// printf("WTF\n");
//
// /* Smoke test to make sure that allocations do not move if they have
// enough
// space to expand in the chunk. */
// for (size_t sz = 3; sz < 256 * 1024; sz += 2048)
// {
// p = realloc(NULL, sz);
// if (p == NULL)
// FAIL_EXIT1("realloc (NULL, %zu) returned NULL.\n", sz);
// size_t newsz = ((t_chunk *)((void *)(p) - sizeof(t_chunk)))->size;
// printf("size: %zu, usable size: %zu, extra: %zu\n", sz, newsz,
// newsz - sz);
// uintptr_t oldp = (uintptr_t)p;
// void *new_p = realloc(p, newsz);
// if ((uintptr_t)new_p != oldp)
// FAIL_EXIT1(
// "Expanding (%zu bytes) to usable size (%zu) moved block\n", sz,
// newsz);
// free(new_p);
//
// /* We encountered a large enough extra size at least once. */
// if (newsz - sz > 1024)
// break;
// }
return 0;
}
#define TEST_FUNCTION do_test()
#include "./test-skeleton.c"

View file

@ -1,12 +0,0 @@
{
leak readline
Memcheck:Leak
...
fun:readline
}
{
leak add_history
Memcheck:Leak
...
fun:add_history
}