Normed most of the stdme
This commit is contained in:
parent
882d5cb5bb
commit
ac5458d42c
31 changed files with 149 additions and 617 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -16,7 +16,7 @@
|
|||
t_error me_open(t_const_str path, bool read, bool write, int *file_out)
|
||||
{
|
||||
int out;
|
||||
int flags;
|
||||
int flags;
|
||||
|
||||
flags = 0;
|
||||
if (read && write)
|
||||
|
|
@ -35,7 +35,7 @@ t_error me_open(t_const_str path, bool read, bool write, int *file_out)
|
|||
t_error me_open_truncate(t_const_str path, int *file_out)
|
||||
{
|
||||
int out;
|
||||
int flags;
|
||||
int flags;
|
||||
|
||||
unlink(path);
|
||||
flags = O_WRONLY | O_CREAT | O_TRUNC;
|
||||
|
|
@ -49,7 +49,7 @@ t_error me_open_truncate(t_const_str path, int *file_out)
|
|||
t_error me_open_create(t_const_str path, int *file_out)
|
||||
{
|
||||
int out;
|
||||
int flags;
|
||||
int flags;
|
||||
|
||||
flags = O_WRONLY | O_CREAT | O_APPEND;
|
||||
out = open(path, flags, 0666);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -21,7 +21,7 @@ bool read_to_vec(t_const_str path, t_vec_u8 *out)
|
|||
{
|
||||
t_u8 temp_buffer[READ_BUFFER_SIZE];
|
||||
t_isize read_amount;
|
||||
int f;
|
||||
int f;
|
||||
bool eof;
|
||||
t_usize current_size;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -101,7 +101,7 @@ static bool handle_leftovers(int fd, char *temp_buffer, t_string *buf)
|
|||
|
||||
t_string get_next_line(int fd, bool *error)
|
||||
{
|
||||
t_string buf;
|
||||
t_string buf;
|
||||
char *temp_buffer;
|
||||
t_copy_flags flags;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
#include "me/str/str.h"
|
||||
#include "me/types.h"
|
||||
|
||||
void hasher_write_str(t_hasher *hasher, t_str s)
|
||||
void hasher_write_str(t_hasher *hasher, t_str s)
|
||||
{
|
||||
hasher_write_bytes(hasher, (t_u8 *)s, str_len(s));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -21,7 +21,7 @@
|
|||
t_i32 qoi_write(t_const_str filename, const void *data,
|
||||
const t_qoi_desc *desc)
|
||||
{
|
||||
int f;
|
||||
int f;
|
||||
void *encoded;
|
||||
t_i32 size;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/14 18:26:27 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/07 19:10:07 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 17:48:46 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,23 +17,23 @@
|
|||
#include "me/types.h"
|
||||
#include <stdio.h>
|
||||
|
||||
t_allocator *global_allocator(void)
|
||||
t_allocator *global_allocator(void)
|
||||
{
|
||||
static t_allocator global_alloc = {};
|
||||
static bool init = false;
|
||||
static t_allocator global_alloc = {};
|
||||
static bool init = false;
|
||||
|
||||
if (!init)
|
||||
{
|
||||
init = true;
|
||||
global_alloc = m_init();
|
||||
// global_alloc = lc_init();
|
||||
}
|
||||
return (&global_alloc);
|
||||
}
|
||||
|
||||
__attribute__((destructor(200)))
|
||||
void uninit_global_allocator(void)
|
||||
void uninit_global_allocator(void)
|
||||
{
|
||||
t_allocator *allocator;
|
||||
t_allocator *allocator;
|
||||
|
||||
allocator = global_allocator();
|
||||
vg_mem_defined(allocator, sizeof(*allocator));
|
||||
|
|
|
|||
|
|
@ -6,16 +6,16 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/_allocator.h"
|
||||
#include "me/mem/mem.h"
|
||||
|
||||
void *mem_alloc(t_usize size)
|
||||
void *mem_alloc(t_usize size)
|
||||
{
|
||||
t_allocator *a;
|
||||
t_allocator *a;
|
||||
void *ret;
|
||||
|
||||
a = global_allocator();
|
||||
|
|
@ -25,9 +25,9 @@ void *mem_alloc(t_usize size)
|
|||
return (ret);
|
||||
}
|
||||
|
||||
void mem_free(void *ptr)
|
||||
void mem_free(void *ptr)
|
||||
{
|
||||
t_allocator *a;
|
||||
t_allocator *a;
|
||||
|
||||
a = global_allocator();
|
||||
return (a->free(a, ptr));
|
||||
|
|
|
|||
|
|
@ -6,16 +6,16 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/mem/_allocator.h"
|
||||
|
||||
void *mem_alloc_array(t_usize size, t_usize count)
|
||||
void *mem_alloc_array(t_usize size, t_usize count)
|
||||
{
|
||||
t_allocator *a;
|
||||
t_allocator *a;
|
||||
|
||||
a = global_allocator();
|
||||
return (a->alloc_array(a, size, count));
|
||||
|
|
|
|||
|
|
@ -6,23 +6,23 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/_allocator.h"
|
||||
|
||||
void *mem_realloc(void *ptr, t_usize size)
|
||||
void *mem_realloc(void *ptr, t_usize size)
|
||||
{
|
||||
t_allocator *a;
|
||||
t_allocator *a;
|
||||
|
||||
a = global_allocator();
|
||||
return (a->realloc(a, ptr, size));
|
||||
}
|
||||
|
||||
void *mem_realloc_array(void *ptr, t_usize size, t_usize count)
|
||||
void *mem_realloc_array(void *ptr, t_usize size, t_usize count)
|
||||
{
|
||||
t_allocator *a;
|
||||
t_allocator *a;
|
||||
|
||||
a = global_allocator();
|
||||
return (a->realloc_array(a, ptr, size, count));
|
||||
|
|
|
|||
|
|
@ -6,15 +6,15 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 11:04:51 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/07 11:06:22 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 17:49:05 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/num/usize.h"
|
||||
|
||||
t_usize usize_round_up_to(t_usize self, t_usize mul)
|
||||
t_usize usize_round_up_to(t_usize self, t_usize mul)
|
||||
{
|
||||
t_usize mod;
|
||||
t_usize mod;
|
||||
|
||||
if (mul == 0)
|
||||
return (self);
|
||||
|
|
|
|||
|
|
@ -6,12 +6,11 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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/putstr_fd.h"
|
||||
#include "me/types.h"
|
||||
|
|
@ -31,24 +30,24 @@
|
|||
# define BACKTRACE_DEEP 256
|
||||
# endif
|
||||
|
||||
static size_t convert_to_vma(t_usize addr)
|
||||
static size_t convert_to_vma(t_usize addr)
|
||||
{
|
||||
Dl_info info;
|
||||
struct link_map *link_map;
|
||||
Dl_info info;
|
||||
struct link_map *link_map;
|
||||
|
||||
dladdr1((void *)addr, &info, (void **)&link_map, RTLD_DL_LINKMAP);
|
||||
return (addr - link_map->l_addr);
|
||||
}
|
||||
|
||||
static void print_trace_inner(void **trace, t_str *messages, t_usize i)
|
||||
static void print_trace_inner(void **trace, t_str *messages, t_usize i)
|
||||
{
|
||||
char syscom[1024];
|
||||
t_i32 p;
|
||||
char syscom[1024];
|
||||
t_i32 p;
|
||||
|
||||
p = 0;
|
||||
fprintf(stderr, "[bt] #%-4zu\t", i);
|
||||
while (messages[i][p] != '(' && messages[i][p] != ' ' &&
|
||||
messages[i][p] != 0)
|
||||
messages[i][p] != 0)
|
||||
++p;
|
||||
fflush(stderr);
|
||||
snprintf(
|
||||
|
|
@ -60,12 +59,12 @@ static void print_trace_inner(void **trace, t_str *messages, t_usize i)
|
|||
fprintf(stderr, "%s\n", messages[i]);
|
||||
}
|
||||
|
||||
void print_trace(void)
|
||||
void print_trace(void)
|
||||
{
|
||||
void *trace[BACKTRACE_DEEP];
|
||||
t_str *messages;
|
||||
t_i32 size;
|
||||
t_i32 i;
|
||||
void *trace[BACKTRACE_DEEP];
|
||||
t_str *messages;
|
||||
t_i32 size;
|
||||
t_i32 i;
|
||||
|
||||
size = backtrace(trace, BACKTRACE_DEEP);
|
||||
messages = backtrace_symbols(trace, size);
|
||||
|
|
@ -79,20 +78,18 @@ void print_trace(void)
|
|||
}
|
||||
#else
|
||||
|
||||
void print_trace(void)
|
||||
void print_trace(void)
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void me_abort(t_str msg)
|
||||
void me_abort(t_str msg)
|
||||
{
|
||||
if (msg == NULL)
|
||||
msg = "No message (msg was NULL)";
|
||||
me_putendl_fd("Memory information:", 2);
|
||||
me_putstr_fd("Abort: ", 2);
|
||||
me_putendl_fd(msg, 2);
|
||||
print_trace();
|
||||
// me_exit(1);
|
||||
abort();
|
||||
me_exit(134);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/07 19:11:39 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 17:54:22 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -19,7 +19,7 @@
|
|||
// __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)
|
||||
{
|
||||
(get_stdin(), get_stdout(), get_stderr());
|
||||
exit(exit_code);
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* test-skeleton.c :+: :+: :+: */
|
||||
/* gnu_source.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/22 15:11:51 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/22 15:12:44 by maiboyer ### ########.fr */
|
||||
/* Created: 2024/07/10 17:53:46 by maiboyer #+# #+# */
|
||||
/* 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[])
|
||||
{
|
||||
return (do_test(argc, argv));
|
||||
}
|
||||
# define _GNU_SOURCE
|
||||
|
||||
#endif /* GNU_SOURCE_H */
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -26,11 +26,11 @@
|
|||
bool find_path(const t_str *s);
|
||||
bool find_null(const t_str *s);
|
||||
bool str_start_with(t_const_str s, t_const_str prefix);
|
||||
t_error handle_redirections(t_spawn_info *info, t_process *process);
|
||||
t_error handle_redirections(t_spawn_info *info, t_process *process);
|
||||
|
||||
t_error spawn_process_exec(t_spawn_info info, t_process *process)
|
||||
t_error spawn_process_exec(t_spawn_info info, t_process *process)
|
||||
{
|
||||
bool res;
|
||||
bool res;
|
||||
|
||||
if (info.forked_free)
|
||||
info.forked_free(info.forked_free_args);
|
||||
|
|
@ -52,11 +52,11 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
|
|||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
|
||||
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
|
||||
t_string *s)
|
||||
{
|
||||
t_str *splitted_path;
|
||||
t_usize sp_index;
|
||||
t_str *splitted_path;
|
||||
t_usize sp_index;
|
||||
|
||||
splitted_path = str_split(path + 5, ':');
|
||||
if (splitted_path == NULL)
|
||||
|
|
@ -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);
|
||||
sp_index++;
|
||||
if (access(s->buf, X_OK | R_OK) == 0)
|
||||
break;
|
||||
break ;
|
||||
}
|
||||
sp_index = 0;
|
||||
while (splitted_path[sp_index])
|
||||
|
|
@ -79,16 +79,16 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
|
|||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error find_binary(t_spawn_info *info, t_process *process)
|
||||
t_error find_binary(t_spawn_info *info, t_process *process)
|
||||
{
|
||||
t_usize p_idx;
|
||||
t_string s;
|
||||
t_usize p_idx;
|
||||
t_string s;
|
||||
|
||||
(void)(process);
|
||||
if (info->binary_path == NULL)
|
||||
return (ERROR);
|
||||
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)
|
||||
string_push(&s, info->binary_path);
|
||||
else
|
||||
|
|
@ -107,7 +107,8 @@ t_error find_binary(t_spawn_info *info, t_process *process)
|
|||
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)
|
||||
close(process->stdin.vals.ro.fd);
|
||||
|
|
@ -123,7 +124,7 @@ static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
|
|||
mem_free(info.binary_path);
|
||||
}
|
||||
|
||||
t_error spawn_process(t_spawn_info info, t_process *process)
|
||||
t_error spawn_process(t_spawn_info info, t_process *process)
|
||||
{
|
||||
if (handle_redirections(&info, process))
|
||||
return (cleanup(info, process, true), ERROR);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,12 +14,12 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
bool find_null(const t_str *s)
|
||||
bool find_null(const t_str *s)
|
||||
{
|
||||
return (s == NULL);
|
||||
}
|
||||
|
||||
bool str_start_with(t_const_str s, t_const_str prefix)
|
||||
bool str_start_with(t_const_str s, t_const_str prefix)
|
||||
{
|
||||
while (*prefix && *s)
|
||||
{
|
||||
|
|
@ -29,13 +29,13 @@ bool str_start_with(t_const_str s, t_const_str prefix)
|
|||
return (*prefix == '\0');
|
||||
}
|
||||
|
||||
bool find_path(const t_str *s)
|
||||
bool find_path(const t_str *s)
|
||||
{
|
||||
t_str ss;
|
||||
t_str ss;
|
||||
|
||||
if (*s == NULL)
|
||||
return (false);
|
||||
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] == '=');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/07 18:01:52 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/07 18:03:28 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 17:58:04 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,19 +15,20 @@
|
|||
#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)
|
||||
void me_printf_append_string(t_const_str to_write, t_usize to_write_len,
|
||||
void *p_args)
|
||||
{
|
||||
t_sprintf_arg *arg;
|
||||
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)
|
||||
void me_printf_write(t_const_str to_write, \
|
||||
t_usize to_write_len, void *p_args)
|
||||
{
|
||||
t_fprintf_arg *arg;
|
||||
t_fprintf_arg *arg;
|
||||
|
||||
arg = (t_fprintf_arg *)p_args;
|
||||
write_fd(arg->fd, (t_u8 *)to_write, to_write_len, NULL);
|
||||
|
|
|
|||
|
|
@ -6,15 +6,15 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/01 21:05:47 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/07 17:40:12 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 17:58:28 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
|
||||
void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs,
|
||||
t_usize value)
|
||||
void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs,
|
||||
t_usize value)
|
||||
{
|
||||
if (!value && data->extra.precision == 0 && (data->flags & PRECISION))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,21 +6,18 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/11 17:50:56 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/07 18:01:58 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 17:57:15 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/fs/fs.h"
|
||||
#include "me/fs/write.h"
|
||||
#include "me/printf/_internal_printf.h"
|
||||
#include "me/types.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
t_usize me_printf(t_const_str fmt, ...)
|
||||
t_usize me_printf(t_const_str fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
t_usize res;
|
||||
va_list args;
|
||||
t_usize res;
|
||||
|
||||
va_start(args, fmt);
|
||||
res = me_vprintf(fmt, &args);
|
||||
|
|
@ -28,45 +25,13 @@ t_usize me_printf(t_const_str fmt, ...)
|
|||
return (res);
|
||||
}
|
||||
|
||||
t_usize me_eprintf(t_const_str fmt, ...)
|
||||
t_usize me_eprintf(t_const_str fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
t_usize res;
|
||||
va_list args;
|
||||
t_usize res;
|
||||
|
||||
va_start(args, fmt);
|
||||
res = me_veprintf(fmt, &args);
|
||||
va_end(args);
|
||||
return (res);
|
||||
}
|
||||
|
||||
/*
|
||||
t_usize me_printf(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(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);
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
|
|
@ -7,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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,9 +16,9 @@
|
|||
#include "me/types.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
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;
|
||||
|
||||
if (fd == NULL || fmt == NULL || args == NULL)
|
||||
return (0);
|
||||
|
|
@ -29,10 +28,10 @@ t_usize me_vprintf_fd(t_fd *fd, t_const_str fmt, va_list *args)
|
|||
return (passthru.total_print);
|
||||
}
|
||||
|
||||
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;
|
||||
t_usize res;
|
||||
va_list args;
|
||||
t_usize res;
|
||||
|
||||
va_start(args, fmt);
|
||||
res = me_vprintf_fd(fd, fmt, &args);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/07 17:27:50 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/07 18:03:40 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 17:57:32 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,9 +17,9 @@
|
|||
#include "me/types.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
t_usize me_vprintf_str(t_string *buf, t_const_str fmt, va_list *args)
|
||||
t_usize me_vprintf_str(t_string *buf, t_const_str fmt, va_list *args)
|
||||
{
|
||||
t_sprintf_arg passthru;
|
||||
t_sprintf_arg passthru;
|
||||
|
||||
if (buf == NULL || fmt == NULL || args == NULL)
|
||||
return (0);
|
||||
|
|
@ -29,10 +29,10 @@ t_usize me_vprintf_str(t_string *buf, t_const_str fmt, va_list *args)
|
|||
return (passthru.total_print);
|
||||
}
|
||||
|
||||
t_usize me_printf_str(t_string *buf, t_const_str fmt, ...)
|
||||
t_usize me_printf_str(t_string *buf, t_const_str fmt, ...)
|
||||
{
|
||||
t_usize res;
|
||||
va_list args;
|
||||
t_usize res;
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
res = me_vprintf_str(buf, fmt, &args);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/09 14:57:28 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/07 18:00:14 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 17:56:24 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -16,11 +16,12 @@
|
|||
#include "me/types.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
void me_printf_write(t_const_str to_write, t_usize to_write_len, void *p_args);
|
||||
void me_printf_write(t_const_str to_write, \
|
||||
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;
|
||||
|
||||
if (fmt == NULL || args == NULL)
|
||||
return (0);
|
||||
|
|
@ -30,9 +31,9 @@ t_usize me_vprintf(t_const_str fmt, va_list *args)
|
|||
return (passthru.total_print);
|
||||
}
|
||||
|
||||
t_usize me_veprintf(t_const_str fmt, va_list *args)
|
||||
t_usize me_veprintf(t_const_str fmt, va_list *args)
|
||||
{
|
||||
t_fprintf_arg passthru;
|
||||
t_fprintf_arg passthru;
|
||||
|
||||
if (fmt == NULL || args == NULL)
|
||||
return (0);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -16,10 +16,10 @@
|
|||
#include "me/str/str.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_str str_clone(t_const_str source)
|
||||
t_str str_clone(t_const_str source)
|
||||
{
|
||||
t_str res;
|
||||
t_usize len;
|
||||
t_usize len;
|
||||
|
||||
len = str_len(source) + 1;
|
||||
res = mem_alloc_array(sizeof(*res), len);
|
||||
|
|
|
|||
|
|
@ -6,16 +6,15 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
// 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;
|
||||
|
||||
if (lhs == NULL || rhs == NULL)
|
||||
return (lhs == rhs);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -19,10 +19,10 @@
|
|||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_error str_reserve(t_string *buf, t_usize size)
|
||||
t_error str_reserve(t_string *buf, t_usize size)
|
||||
{
|
||||
t_str temp_buffer;
|
||||
t_usize new_capacity;
|
||||
t_usize new_capacity;
|
||||
|
||||
if (buf == NULL)
|
||||
return (ERROR);
|
||||
|
|
@ -42,9 +42,9 @@ t_error str_reserve(t_string *buf, t_usize size)
|
|||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error string_push(t_string *buf, t_const_str to_push)
|
||||
t_error string_push(t_string *buf, t_const_str to_push)
|
||||
{
|
||||
t_usize to_push_len;
|
||||
t_usize to_push_len;
|
||||
|
||||
if (buf == NULL || to_push == NULL)
|
||||
return (ERROR);
|
||||
|
|
@ -56,26 +56,26 @@ t_error string_push(t_string *buf, t_const_str to_push)
|
|||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
bool string_push_char(t_string *buf, char to_push)
|
||||
bool string_push_char(t_string *buf, char to_push)
|
||||
{
|
||||
char push_str[2];
|
||||
char push_str[2];
|
||||
|
||||
push_str[0] = to_push;
|
||||
push_str[1] = 0;
|
||||
return (string_push(buf, push_str));
|
||||
}
|
||||
|
||||
void string_clear(t_string *buf)
|
||||
void string_clear(t_string *buf)
|
||||
{
|
||||
mem_set_zero(buf->buf, buf->capacity);
|
||||
buf->len = 0;
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
|
||||
t_string string_new(t_usize capacity)
|
||||
t_string string_new(t_usize capacity)
|
||||
{
|
||||
t_string out;
|
||||
t_str buf;
|
||||
t_string out;
|
||||
t_str buf;
|
||||
|
||||
if (capacity == 0)
|
||||
capacity = 16;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/08 21:32:17 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/08 22:25:52 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 18:01:27 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,9 +15,10 @@
|
|||
#include "me/string/string.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_error string_insert(t_string *self, t_usize pos, t_str str)
|
||||
t_error string_insert(t_string *self, t_usize pos, t_str str)
|
||||
{
|
||||
t_usize len;
|
||||
t_usize len;
|
||||
|
||||
if (self == NULL || str == NULL || pos > self->len)
|
||||
return (ERROR);
|
||||
if (pos == self->len)
|
||||
|
|
@ -32,9 +33,9 @@ t_error string_insert(t_string *self, t_usize pos, t_str str)
|
|||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error string_insert_char(t_string *self, t_usize pos, char chr)
|
||||
t_error string_insert_char(t_string *self, t_usize pos, char chr)
|
||||
{
|
||||
char tmp[2];
|
||||
char tmp[2];
|
||||
|
||||
if (chr == '\0')
|
||||
return (ERROR);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/08 21:50:27 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/08 21:54:45 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 18:01:53 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,9 +15,9 @@
|
|||
#include "me/string/string.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_error string_remove(t_string *self, t_usize pos, char *out)
|
||||
t_error string_remove(t_string *self, t_usize pos, char *out)
|
||||
{
|
||||
char fake_out;
|
||||
char fake_out;
|
||||
|
||||
if (out == NULL)
|
||||
out = &fake_out;
|
||||
|
|
@ -30,7 +30,7 @@ t_error string_remove(t_string *self, t_usize pos, char *out)
|
|||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error string_clear_after(t_string *self, t_usize pos)
|
||||
t_error string_clear_after(t_string *self, t_usize pos)
|
||||
{
|
||||
if (self == NULL || pos >= self->len)
|
||||
return (ERROR);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/08 22:02:49 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/08 22:04:49 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 18:01:40 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
#include "me/string/string.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_error string_reserve(t_string *self, t_usize capacity)
|
||||
t_error string_reserve(t_string *self, t_usize capacity)
|
||||
{
|
||||
if (self == NULL)
|
||||
return (ERROR);
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
@ -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"
|
||||
|
|
@ -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"
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* redef_alloc.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/22 15:15:52 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/22 15:16:34 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef REDEF_ALLOC_H
|
||||
#define REDEF_ALLOC_H
|
||||
|
||||
#include "me/mem/mem.h"
|
||||
|
||||
#undef malloc
|
||||
#undef calloc
|
||||
#undef realloc
|
||||
#undef free
|
||||
|
||||
#define malloc(s) mem_alloc((s))
|
||||
#define calloc(s, l) mem_alloc_array((s), (l))
|
||||
#define realloc(p, t) mem_realloc((p), (t))
|
||||
#define free(p) mem_free((p))
|
||||
|
||||
#endif /* REDEF_ALLOC_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue