Update to allocator
This commit is contained in:
parent
b96c5e1e66
commit
c461fe3323
10 changed files with 469 additions and 14 deletions
|
|
@ -6,11 +6,10 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/14 18:02:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/18 18:22:32 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/05/23 14:35:35 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/types.h"
|
||||
|
||||
|
|
@ -20,8 +19,10 @@
|
|||
#include "valgrind/valgrind.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
|
||||
|
||||
|
|
@ -250,16 +251,19 @@ void *m_alloc_array(struct s_allocator_melloc *self, t_usize size,
|
|||
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;
|
||||
|
||||
// eprintf("M_REALLOC\n");
|
||||
// if (self->list == NULL && alloc_page_list(&self->list))
|
||||
// return (m_alloc_error(self, "Unable to alloc page list"));
|
||||
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);
|
||||
|
|
@ -293,8 +297,7 @@ void *m_realloc(struct s_allocator_melloc *self, void *ptr, t_usize size)
|
|||
{
|
||||
old_size = chunk->size;
|
||||
chunk->size += next->size + sizeof(*next);
|
||||
vg_mem_defined(next, next->size + sizeof(*next));
|
||||
mem_set_zero(next, 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));
|
||||
|
|
@ -305,7 +308,7 @@ void *m_realloc(struct s_allocator_melloc *self, void *ptr, t_usize size)
|
|||
vg_mem_no_access(next, sizeof(*next));
|
||||
next = m_realloc(self, NULL, size);
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
mem_copy(ptr, next, chunk->size);
|
||||
mem_move(next, ptr, chunk->size);
|
||||
vg_mem_no_access(chunk, sizeof(*chunk));
|
||||
m_free(self, ptr);
|
||||
return (next);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/12 16:00:33 by rparodi #+# #+# */
|
||||
/* Updated: 2024/05/18 17:49:43 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/05/22 15:04:52 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -22,6 +22,8 @@
|
|||
|
||||
t_error ft_command_exec(t_node *self, t_i32 *ret);
|
||||
|
||||
t_error ft_command_exec(t_node *self, t_i32 *ret);
|
||||
|
||||
t_error semicolon_exec(t_node *first, t_node *second, t_i32 *ret_value)
|
||||
{
|
||||
if (!first && !second)
|
||||
|
|
|
|||
2
stdme/.gitignore
vendored
2
stdme/.gitignore
vendored
|
|
@ -53,4 +53,4 @@ dkms.conf
|
|||
|
||||
./generic
|
||||
.direnv
|
||||
test
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/14 18:26:27 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/22 14:58:57 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/05/22 15:21:28 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -25,8 +25,8 @@ t_allocator *global_allocator(void)
|
|||
if (!init)
|
||||
{
|
||||
init = true;
|
||||
//global_alloc = m_init();
|
||||
global_alloc = lc_init();
|
||||
global_alloc = m_init();
|
||||
// global_alloc = lc_init();
|
||||
}
|
||||
return (&global_alloc);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/19 17:08:45 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/05/22 15:05:19 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
124
stdme/test/calloc.c
Normal file
124
stdme/test/calloc.c
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
/* 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"
|
||||
84
stdme/test/malloc.c
Normal file
84
stdme/test/malloc.c
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/* 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"
|
||||
196
stdme/test/realloc.c
Normal file
196
stdme/test/realloc.c
Normal file
|
|
@ -0,0 +1,196 @@
|
|||
/* 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"
|
||||
28
stdme/test/redef_alloc.h
Normal file
28
stdme/test/redef_alloc.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 */
|
||||
18
stdme/test/test-skeleton.c
Normal file
18
stdme/test/test-skeleton.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* test-skeleton.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int do_test(int argc, char **argv);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
return (do_test(argc, argv));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue