added the mandatory show_alloc_mem, and sorted the zones by increasing address in the linked lists
This commit is contained in:
parent
65447f4a0a
commit
db2b5f27bb
92 changed files with 176 additions and 20 deletions
3
Makefile
3
Makefile
|
|
@ -18,7 +18,8 @@ SRCS := $(SRCS_DIR)/ft_malloc.c \
|
|||
$(SRCS_DIR)/ft_calloc.c \
|
||||
$(SRCS_DIR)/ft_realloc.c \
|
||||
$(SRCS_DIR)/init_state.c \
|
||||
$(SRCS_DIR)/bonus_utils.c
|
||||
$(SRCS_DIR)/bonus_utils.c \
|
||||
$(SRCS_DIR)/utils.c
|
||||
|
||||
OBJS := $(SRCS:.c=.o)
|
||||
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* ft_malloc_public.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/25 12:31:43 by thrieg #+# #+# */
|
||||
/* Updated: 2025/11/28 18:26:47 by thrieg ### ########.fr */
|
||||
/* Updated: 2025/12/13 05:34:43 by thrieg ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -21,5 +21,6 @@ void free(void *ptr);
|
|||
void *calloc(size_t nmemb, size_t size);
|
||||
void *realloc(void *ptr, size_t size);
|
||||
void show_alloc_mem_ex(bool hexdump_free_zones);
|
||||
void show_alloc_mem();
|
||||
|
||||
#endif
|
||||
Binary file not shown.
BIN
libft/ft_atoi.o
BIN
libft/ft_atoi.o
Binary file not shown.
BIN
libft/ft_bzero.o
BIN
libft/ft_bzero.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
libft/ft_itoa.o
BIN
libft/ft_itoa.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
libft/ft_power.o
BIN
libft/ft_power.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
libft/ft_split.o
BIN
libft/ft_split.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
libft/ft_utoa.o
BIN
libft/ft_utoa.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
libft/libft.a
BIN
libft/libft.a
Binary file not shown.
Binary file not shown.
Binary file not shown.
2
main.c
2
main.c
|
|
@ -5,7 +5,7 @@
|
|||
#include <time.h>
|
||||
|
||||
/*
|
||||
** These match your allocator’s thresholds
|
||||
** match allocator’s thresholds
|
||||
** (only used here for shaping size distributions)
|
||||
*/
|
||||
#define TINY_MAX 64
|
||||
|
|
|
|||
71
main_show_alloc_mem.c
Normal file
71
main_show_alloc_mem.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main_show_alloc_mem.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/13 05:54:30 by thrieg #+# #+# */
|
||||
/* Updated: 2025/12/13 05:55:27 by thrieg ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "includes/ft_malloc_public.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *a = malloc(10);
|
||||
char *b = malloc(100);
|
||||
int *c = malloc(50 * sizeof(int));
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
a[i] = i;
|
||||
for (int i = 0; i < 100; ++i)
|
||||
b[i] = 0xAA;
|
||||
for (int i = 0; i < 50; ++i)
|
||||
c[i] = i * 2;
|
||||
|
||||
write(1, "\n\n\n\n\nafter first alloc: \n\n\n\n\n", sizeof("\n\n\n\n\nafter first alloc: \n\n\n\n\n") - 1);
|
||||
show_alloc_mem();
|
||||
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
|
||||
write(1, "\n\n\n\n\nafter free: \n\n\n\n\n", sizeof("\n\n\n\n\nafter free: \n\n\n\n\n") - 1);
|
||||
show_alloc_mem();
|
||||
|
||||
a = malloc(20);
|
||||
b = malloc(1000);
|
||||
c = malloc(500 * sizeof(int));
|
||||
|
||||
for (int i = 0; i < 20; ++i)
|
||||
a[i] = i;
|
||||
for (int i = 0; i < 1000; ++i)
|
||||
b[i] = 0xAA;
|
||||
for (int i = 0; i < 500; ++i)
|
||||
c[i] = i * 2;
|
||||
|
||||
write(1, "\n\n\n\n\nafter allocating again: \n\n\n\n\n", sizeof("\n\n\n\n\nafter allocating again: \n\n\n\n\n") - 1);
|
||||
show_alloc_mem();
|
||||
|
||||
a = realloc(a, 420); // move the block
|
||||
c = realloc(c, 504 * sizeof(int)); // expend
|
||||
b = realloc(b, 400); // shrink
|
||||
|
||||
write(1, "\n\n\n\n\nafter realloc: \n\n\n\n\n", sizeof("\n\n\n\n\nafter realloc: \n\n\n\n\n") - 1);
|
||||
show_alloc_mem();
|
||||
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
write(1, "\n\n\n\n\nafter free: \n\n\n\n\n", sizeof("\n\n\n\n\nafter free: \n\n\n\n\n") - 1);
|
||||
show_alloc_mem();
|
||||
return (0);
|
||||
}
|
||||
|
||||
// cc -g main_show_alloc_mem.c -L. -lft_malloc -o test_show
|
||||
// export LD_LIBRARY_PATH="$PWD"
|
||||
|
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* bonus_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/28 16:42:13 by thrieg #+# #+# */
|
||||
/* Updated: 2025/11/28 18:25:49 by thrieg ### ########.fr */
|
||||
/* Updated: 2025/12/13 04:42:15 by thrieg ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* init_state.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/18 16:44:13 by thrieg #+# #+# */
|
||||
/* Updated: 2025/12/08 15:32:55 by thrieg ### ########.fr */
|
||||
/* Updated: 2025/12/13 05:50:48 by thrieg ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -28,6 +28,43 @@ void init_env_variables()
|
|||
g_state.is_init = true;
|
||||
}
|
||||
|
||||
//inserts a page in the linked list whilst keeping the zones sorted in ascending address
|
||||
static void zone_insert_sorted(t_zone **head, t_zone *z)
|
||||
{
|
||||
t_zone *cur;
|
||||
|
||||
z->prev = NULL;
|
||||
z->next = NULL;
|
||||
|
||||
if (*head == NULL)
|
||||
{
|
||||
*head = z;
|
||||
return;
|
||||
}
|
||||
|
||||
cur = *head;
|
||||
|
||||
// insert before head
|
||||
if ((uintptr_t)z < (uintptr_t)cur)
|
||||
{
|
||||
z->next = cur;
|
||||
cur->prev = z;
|
||||
*head = z;
|
||||
return;
|
||||
}
|
||||
|
||||
// walk until the next node is after z (or end)
|
||||
while (cur->next && (uintptr_t)cur->next < (uintptr_t)z)
|
||||
cur = cur->next;
|
||||
|
||||
// insert after cur
|
||||
z->next = cur->next;
|
||||
z->prev = cur;
|
||||
if (cur->next)
|
||||
cur->next->prev = z;
|
||||
cur->next = z;
|
||||
}
|
||||
|
||||
// only call this for TINY or SMALL
|
||||
void *add_page(t_type type)
|
||||
{
|
||||
|
|
@ -51,10 +88,7 @@ void *add_page(t_type type)
|
|||
header->occupied = false;
|
||||
header->size = size - sizeof(t_zone) - sizeof(t_header);
|
||||
header->zone = ptr;
|
||||
ptr->next = g_state.tiny_zone; // tiny_zone can be NULL but that's fine
|
||||
if (g_state.tiny_zone)
|
||||
g_state.tiny_zone->prev = ptr;
|
||||
g_state.tiny_zone = ptr;
|
||||
zone_insert_sorted(&g_state.tiny_zone, ptr);
|
||||
}
|
||||
else if (type == E_SMALL)
|
||||
{
|
||||
|
|
@ -64,10 +98,7 @@ void *add_page(t_type type)
|
|||
header->occupied = false;
|
||||
header->size = size - sizeof(t_zone) - sizeof(t_header);
|
||||
header->zone = ptr;
|
||||
ptr->next = g_state.small_zone; // small_zone can be NULL but that's fine
|
||||
if (g_state.small_zone)
|
||||
g_state.small_zone->prev = ptr;
|
||||
g_state.small_zone = ptr;
|
||||
zone_insert_sorted(&g_state.small_zone, ptr);
|
||||
}
|
||||
return (ptr);
|
||||
}
|
||||
|
|
@ -83,14 +114,11 @@ void *add_large(size_t size)
|
|||
ptr->size = size;
|
||||
ptr->type = E_LARGE;
|
||||
ptr->prev = NULL;
|
||||
ptr->next = g_state.large_zone; // large_zone can be NULL but that's fine
|
||||
if (g_state.large_zone)
|
||||
g_state.large_zone->prev = ptr;
|
||||
g_state.large_zone = ptr;
|
||||
t_header *header = (t_header *)(ptr + 1);
|
||||
header->occupied = true;
|
||||
header->size = size - sizeof(t_zone) - sizeof(t_header);
|
||||
header->zone = ptr;
|
||||
zone_insert_sorted(&g_state.large_zone, ptr);
|
||||
return ((void *)(header + 1));
|
||||
}
|
||||
|
||||
|
|
|
|||
55
srcs/utils.c
Normal file
55
srcs/utils.c
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/13 04:41:06 by thrieg #+# #+# */
|
||||
/* Updated: 2025/12/13 06:00:35 by thrieg ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/ft_malloc.h"
|
||||
#include "../libft/ft_printf/ft_printf.h"
|
||||
|
||||
void print_allocs_zone(t_zone *zone)
|
||||
{
|
||||
ft_printf("%s : %p\n", zone->type == E_TINY ? "TINY" : (zone->type == E_SMALL ? "SMALL" : "LARGE"), zone);
|
||||
|
||||
char *zone_end = (char *)zone + zone->size;
|
||||
t_header *header = (t_header *)(zone + 1);
|
||||
while ((char *)header + sizeof(t_header) <= zone_end)
|
||||
{
|
||||
if (header->occupied)
|
||||
ft_printf("%p - %p : %u bytes\n", (char *)header + sizeof(header), (char *)header + sizeof(header) + header->size, (unsigned int)header->size);
|
||||
header = (t_header *)((char *)(header + 1) + header->size);
|
||||
}
|
||||
}
|
||||
|
||||
static t_zone *min_zone_ptr(t_zone *a, t_zone *b, t_zone *c)
|
||||
{
|
||||
t_zone *m = NULL;
|
||||
|
||||
if (a && (!m || (uintptr_t)a < (uintptr_t)m)) m = a;
|
||||
if (b && (!m || (uintptr_t)b < (uintptr_t)m)) m = b;
|
||||
if (c && (!m || (uintptr_t)c < (uintptr_t)m)) m = c;
|
||||
return m;
|
||||
}
|
||||
|
||||
void show_alloc_mem(void)
|
||||
{
|
||||
t_zone *t = g_state.tiny_zone;
|
||||
t_zone *s = g_state.small_zone;
|
||||
t_zone *l = g_state.large_zone;
|
||||
|
||||
while (t || s || l)
|
||||
{
|
||||
t_zone *m = min_zone_ptr(t, s, l);
|
||||
print_allocs_zone(m);
|
||||
|
||||
if (m == t) t = t->next;
|
||||
else if (m == s) s = s->next;
|
||||
else l = l->next;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue