fix norm and set chunk sizes

This commit is contained in:
2024-12-03 19:03:30 +01:00
parent 231b283e0d
commit 7dfa7eb049
10 changed files with 241 additions and 186 deletions

View File

@ -1,12 +1,11 @@
# **************************************************************************** #
# #
# **************************************************************************** # #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
# Updated: 2024/12/01 03:02:40 by tomoron ### ########.fr #
# Updated: 2024/12/03 15:25:19 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -17,8 +16,11 @@ NAME := libft_malloc_$(HOSTTYPE).so
CC = cc
SRCS_NAMES = malloc.c \
malloc_utils.c\
show_alloc_mem.c\
free.c
free.c\
realloc.c\
utils.c
SRCS_DIR = srcs
@ -34,7 +36,7 @@ LFT = libft/libft.a
LFT_DIR = libft/
all: exec #libft_malloc.so
all: libft_malloc.so
exec: $(OBJS_DIR) $(OBJS) $(LFT)
$(CC) -o a.out $(FLAGS) main.c $(OBJS) $(LFT)
@ -43,7 +45,7 @@ libft_malloc.so: $(NAME)
ln -sf $(NAME) libft_malloc.so
$(LFT):
$(MAKE) -C $(LFT_DIR)
make -j$(shell nproc) -C $(LFT_DIR)
$(NAME): $(OBJS_DIR) $(OBJS) $(LFT)
$(CC) -shared -o $(NAME) $(OBJS) $(LFT)

View File

@ -6,7 +6,7 @@
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
# Updated: 2024/11/27 17:45:42 by tomoron ### ########.fr #
# Updated: 2024/12/03 15:26:54 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -74,7 +74,7 @@ all: $(NAME)
$(NAME): ft_printf $(OBJS)
make --no-print-directory -C ./ft_printf
$(MAKE) --no-print-directory -C ./ft_printf
cp ./ft_printf/libftprintf.a $(NAME)
ar rcs $(NAME) $(OBJS)
@ -86,7 +86,7 @@ bonus: $(OBJS) $(OBJS_BONUS)
clean:
rm -f $(OBJS) $(OBJS_BONUS)
make --no-print-directory -C ./ft_printf fclean
$(MAKE) --no-print-directory -C ./ft_printf fclean
fclean: clean
rm -f $(NAME)

17
main.c
View File

@ -3,15 +3,14 @@
int main(void)
{
void *ptr;
int i;
ft_malloc(1013);
printf("%p\n",ft_malloc(100));
ft_malloc(100);
ptr = ft_malloc(1000);
ft_malloc(100);
ft_malloc(100);
ft_free(ptr);
ft_malloc(123);
i = 0;
while(i < 110)
{
malloc(256);
i++;
}
show_alloc_mem();
}

View File

@ -6,27 +6,28 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/30 18:46:07 by tomoron #+# #+# */
/* Updated: 2024/12/02 19:54:00 by tomoron ### ########.fr */
/* Updated: 2024/12/03 18:59:13 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/malloc.h"
t_mem_chunk *get_alloc_chunk(t_alloc *alloc, t_mem_chunk *first,int is_small)
t_mem_chunk *get_alloc_chunk(t_alloc *alloc, t_mem_chunk *first, int is_small)
{
size_t size;
if(is_small)
if (is_small)
size = SMALL_CHUNK_SIZE;
else
size = TINY_CHUNK_SIZE;
while(first)
while (first)
{
if((t_ul)alloc >= (t_ul)first && ((t_ul)alloc - (t_ul)(first + 1)) <= size)
return(first);
if ((t_ul)alloc >= (t_ul)first && ((t_ul)alloc - \
(t_ul)(first + 1)) <= size)
return (first);
first = first->next;
}
return(0);
return (0);
}
int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur)
@ -34,67 +35,68 @@ int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur)
t_alloc *prev;
*res = 0;
if(cur == *alloc)
return(1);
if (cur == *alloc)
return (1);
prev = 0;
while(cur)
while (cur)
{
if(cur->next == *alloc)
if (cur->next == *alloc)
{
*res = cur;
return(1);
return (1);
}
if(cur->next > *alloc && cur <= *alloc && ((t_ul)alloc - (t_ul)cur) <= cur->size)
if (cur->next > *alloc && cur <= *alloc && ((t_ul)alloc - \
(t_ul)cur) <= cur->size)
{
*alloc = cur;
*res = prev;
return(1);
return (1);
}
prev = cur;
cur = cur->next;
}
return(0);
return (0);
}
int free_prealloc(t_alloc *alloc, t_mem_chunk **main_chunk, int is_small)
static int free_prealloc(t_alloc *alloc, t_mem_chunk **main_chunk, \
int is_small)
{
t_mem_chunk *chunk;
t_alloc *prev;
size_t size;
prev = 0;
chunk = get_alloc_chunk(alloc, *main_chunk, is_small);
if(!chunk)
return(0);
if(!get_prev_alloc(&alloc, &prev, chunk->first))
return(1);
if (!chunk)
return (0);
if (!get_prev_alloc(&alloc, &prev, chunk->first))
return (1);
chunk->space_left -= alloc->size + sizeof(t_alloc);
if(chunk->first == alloc)
if (chunk->first == alloc)
chunk->first = alloc->next;
else if(prev)
else if (prev)
prev->next = alloc->next;
if(!chunk->first)
if (!chunk->first)
{
if(*main_chunk == chunk)
if (*main_chunk == chunk)
*main_chunk = chunk->next;
if(is_small)
if (is_small)
size = SMALL_CHUNK_SIZE;
else
size = TINY_CHUNK_SIZE;
munmap(chunk, size);
}
return(1);
return (1);
}
void free_large(t_alloc *alloc)
static void free_large(t_alloc *alloc)
{
t_alloc *prev;
if(!get_prev_alloc(&alloc, &prev, g_allocs.large))
if (!get_prev_alloc(&alloc, &prev, g_allocs.large))
return ;
if(alloc == g_allocs.large)
if (alloc == g_allocs.large)
g_allocs.large = alloc->next;
else if(prev)
else if (prev)
prev->next = alloc->next;
munmap(alloc, alloc->size + sizeof(t_alloc));
}
@ -103,12 +105,12 @@ void free(void *ptr)
{
t_alloc *alloc;
if(!ptr)
if (!ptr)
return ;
alloc = (t_alloc *)ptr - 1;
if(free_prealloc(alloc, &g_allocs.tiny, 0))
if (free_prealloc(alloc, &g_allocs.tiny, 0))
return ;
if(free_prealloc(alloc, &g_allocs.small, 1))
if (free_prealloc(alloc, &g_allocs.small, 1))
return ;
free_large(alloc);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/26 16:38:01 by tomoron #+# #+# */
/* Updated: 2024/12/02 20:18:33 by tomoron ### ########.fr */
/* Updated: 2024/12/03 15:21:41 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,12 +20,11 @@
# define PAGE_SIZE sysconf(_SC_PAGESIZE)
# define TINY_MALLOC 1024
# define TINY_MALLOC 256
# define SMALL_MALLOC 4096
# define TINY_CHUNK_SIZE (((16 + TINY_MALLOC) * 100) + 32)
# define SMALL_CHUNK_SIZE 524288
# define TINY_CHUNK_SIZE align(((16 + TINY_MALLOC) * 100) + 32, PAGE_SIZE)
# define SMALL_CHUNK_SIZE align(((16 + SMALL_MALLOC) * 100) + 32, PAGE_SIZE)
typedef struct s_alloc
{
@ -52,8 +51,10 @@ typedef unsigned long t_ul;
extern t_allocations g_allocs;
size_t align(size_t nb, size_t align_nb);
void *malloc(size_t size);
void show_alloc_mem();
void show_alloc_mem(void);
void free(void *ptr);
void *realloc(void *ptr, size_t size);

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/23 17:19:59 by tomoron #+# #+# */
/* Updated: 2024/12/02 19:54:00 by tomoron ### ########.fr */
/* Updated: 2024/12/03 19:01:33 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,10 @@
t_allocations g_allocs;
t_alloc *get_suitable_addr_in_chunk(t_mem_chunk *chunk, size_t size);
t_mem_chunk *create_new_chunk(int is_small, t_mem_chunk **chunk, \
t_mem_chunk *prev);
void *get_memory(size_t size, int no_write)
{
t_mem_chunk *chunk;
@ -25,8 +29,8 @@ void *get_memory(size_t size, int no_write)
-1, 0);
if (chunk == MAP_FAILED)
return (0);
if(no_write)
return(chunk);
if (no_write)
return (chunk);
chunk->space_left = size - sizeof(t_mem_chunk);
chunk->next = 0;
return (chunk);
@ -52,59 +56,7 @@ t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev,
return (addr + 1);
}
t_alloc *get_suitable_addr_in_chunk(t_mem_chunk *chunk, size_t size)
{
t_alloc *tmp;
size_t space_left;
size_t free_space;
tmp = chunk->first;
space_left = chunk->space_left;
if((t_ul)chunk->first - (t_ul)(chunk + 1) >= size + sizeof(t_alloc))
{
tmp = chunk->first;
chunk->first = reserve_addr((void *)(chunk + 1), size, 0, chunk) - 1;
chunk->first->next = tmp;
return(chunk->first + 1);
}
while (tmp->next)
{
free_space = ((t_ul)tmp->next - (t_ul)tmp) - (tmp->size + sizeof(t_alloc));
if (free_space >= size + sizeof(t_alloc))
return (reserve_addr(
(void *)((char *)tmp + tmp->size + sizeof(t_alloc)),
size, tmp, chunk));
space_left -= free_space;
tmp = tmp->next;
}
if (space_left >= size + sizeof(t_alloc))
return (reserve_addr(
(t_alloc *)((char *)tmp + tmp->size + sizeof(t_alloc)),
size, tmp, chunk));
return (0);
}
t_mem_chunk *create_new_chunk(int is_small, t_mem_chunk **chunk, t_mem_chunk *prev)
{
t_mem_chunk *new;
size_t mmap_size;
if (is_small)
mmap_size = SMALL_CHUNK_SIZE;
else
mmap_size = TINY_CHUNK_SIZE;
new = get_memory(mmap_size, 0);
if (!new)
return (0);
new->first = (t_alloc *)(new + 1);
if (prev)
prev->next = new;
if (!*chunk)
*chunk = new;
return (new);
}
void *pre_allocated(size_t size, t_mem_chunk **chunk, int is_small)
static void *pre_allocated(size_t size, t_mem_chunk **chunk, int is_small)
{
t_mem_chunk *tmp;
t_mem_chunk *prev;
@ -138,7 +90,6 @@ void *malloc(size_t size)
return (pre_allocated(size, &(g_allocs.tiny), 0));
else if (size <= SMALL_MALLOC)
return (pre_allocated(size, &(g_allocs.small), 1));
new = get_memory(size + sizeof(t_alloc), 1);
if (!new)
return (0);

78
srcs/malloc_utils.c Normal file
View File

@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* malloc_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/03 18:44:32 by tomoron #+# #+# */
/* Updated: 2024/12/03 19:01:11 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/malloc.h"
void *get_memory(size_t size, int no_write);
t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev,
t_mem_chunk *chunk);
t_mem_chunk *create_new_chunk(int is_small, t_mem_chunk **chunk, \
t_mem_chunk *prev)
{
t_mem_chunk *new;
size_t mmap_size;
if (is_small)
mmap_size = SMALL_CHUNK_SIZE;
else
mmap_size = TINY_CHUNK_SIZE;
new = get_memory(mmap_size, 0);
if (!new)
return (0);
new->first = (t_alloc *)(new + 1);
if (prev)
prev->next = new;
if (!*chunk)
*chunk = new;
return (new);
}
static t_alloc *get_suitable_addr_in_chunk_middle_end(size_t space_left, \
t_alloc *tmp, size_t size, t_mem_chunk *chunk)
{
size_t free_space;
while (tmp->next)
{
free_space = ((t_ul)tmp->next - (t_ul)tmp) - \
(tmp->size + sizeof(t_alloc));
if (free_space >= size + sizeof(t_alloc))
return (reserve_addr(
(void *)((char *)tmp + tmp->size + sizeof(t_alloc)),
size, tmp, chunk));
space_left -= free_space;
tmp = tmp->next;
}
if (space_left >= size + sizeof(t_alloc))
return (reserve_addr(
(t_alloc *)((char *)tmp + tmp->size + sizeof(t_alloc)),
size, tmp, chunk));
return (0);
}
t_alloc *get_suitable_addr_in_chunk(t_mem_chunk *chunk, size_t size)
{
t_alloc *tmp;
size_t space_lft;
tmp = chunk->first;
space_lft = chunk->space_left;
if ((t_ul)chunk->first - (t_ul)(chunk + 1) >= size + sizeof(t_alloc))
{
tmp = chunk->first;
chunk->first = reserve_addr((void *)(chunk + 1), size, 0, chunk) - 1;
chunk->first->next = tmp;
return (chunk->first + 1);
}
return (get_suitable_addr_in_chunk_middle_end(space_lft, tmp, size, chunk));
}

View File

@ -6,72 +6,73 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/02 14:41:22 by tomoron #+# #+# */
/* Updated: 2024/12/02 19:54:00 by tomoron ### ########.fr */
/* Updated: 2024/12/03 18:57:50 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/malloc.h"
t_mem_chunk *get_alloc_chunk(t_alloc *alloc, t_mem_chunk *first, size_t size);
int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur);
void *realloc_recreate(t_alloc *alloc, size_t size)
static void *realloc_recreate(t_alloc *alloc, size_t size)
{
void *new;
new = malloc(size);
if(alloc->size < size)
if (alloc->size < size)
size = alloc->size;
ft_memcpy(new, alloc + 1, size);
free(alloc + 1);
return(new);
return (new);
}
void *realloc_prealloc(t_alloc *alloc, t_mem_chunk *chunk, int is_small, size_t size)
static void *realloc_prealloc(t_alloc *alloc, t_mem_chunk *chunk, \
int is_small, size_t size)
{
t_alloc *prev;
chunk = get_alloc_chunk(alloc, chunk, is_small);
if(!chunk)
return(0);
if(!get_prev_alloc(&alloc, &prev, chunk->first))
return(0);
if(alloc->size >= size)
if (!chunk)
return (0);
if (!get_prev_alloc(&alloc, &prev, chunk->first))
return (0);
if (alloc->size >= size)
{
chunk->space_left += alloc->size - size;
alloc->size = size;
return(alloc + 1);
return (alloc + 1);
}
if((size > (size_t)TINY_CHUNK_SIZE && is_small == 0)
if ((size > (size_t)TINY_CHUNK_SIZE && is_small == 0)
|| (size > SMALL_CHUNK_SIZE && is_small == 1)
|| (t_ul)(alloc->next) - (t_ul)(alloc + 1) < size)
return(realloc_recreate(alloc, size));
return (realloc_recreate(alloc, size));
chunk->space_left -= size - alloc->size;
alloc->size = size;
return(alloc + 1);
return (alloc + 1);
}
void *realloc_large(t_alloc *alloc, size_t size)
static void *realloc_large(t_alloc *alloc, size_t size)
{
t_alloc *prev;
if(!get_prev_alloc(&alloc, &prev, g_allocs.large))
return(0);
return(realloc_recreate(alloc, size));
if (!get_prev_alloc(&alloc, &prev, g_allocs.large))
return (0);
return (realloc_recreate(alloc, size));
}
void *realloc(void *ptr, size_t size)
{
t_alloc *alloc;
if(!ptr)
return(malloc(size));
if (!ptr)
return (malloc(size));
alloc = (t_alloc *)ptr - 1;
ptr = realloc_prealloc(alloc, g_allocs.tiny, 0, size);
if(ptr)
return(ptr);
ptr = realloc_prealloc(alloc, g_allocs.tiny, 1, size);
if(ptr)
return(ptr);
return(realloc_large(alloc, size));
if (ptr)
return (ptr);
ptr = realloc_prealloc(alloc, g_allocs.small, 1, size);
if (ptr)
return (ptr);
return (realloc_large(alloc, size));
}

View File

@ -6,18 +6,18 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/30 12:19:34 by tomoron #+# #+# */
/* Updated: 2024/12/02 15:24:45 by tomoron ### ########.fr */
/* Updated: 2024/12/03 18:50:25 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/malloc.h"
void put_ulnbr_base(t_ul nbr, char *base)
static void put_ulnbr_base(t_ul nbr, char *base)
{
t_ul base_len;
base_len = ft_strlen(base);
if(nbr >= base_len)
if (nbr >= base_len)
{
put_ulnbr_base(nbr / base_len, base);
write(1, base + (nbr % base_len), 1);
@ -26,32 +26,33 @@ void put_ulnbr_base(t_ul nbr, char *base)
write(1, base + nbr, 1);
}
size_t show_allocs(t_alloc *alloc)
static size_t show_allocs(t_alloc *alloc)
{
size_t nb_bytes;
nb_bytes = 0;
while(alloc)
while (alloc)
{
nb_bytes += alloc->size;
write(1, "0x", 2);
put_ulnbr_base((t_ul)alloc + sizeof(t_alloc), "0123456789ABCDEF");
write(1, " - 0x", 5);
put_ulnbr_base((t_ul)alloc +sizeof(t_alloc) + alloc->size, "0123456789ABCDEF");
put_ulnbr_base((t_ul)alloc + sizeof(t_alloc) + alloc->size, \
"0123456789ABCDEF");
write(1, " : ", 3);
put_ulnbr_base(alloc->size, "0123456789");
write(1, " bytes\n", 7);
alloc = alloc->next;
}
return(nb_bytes);
return (nb_bytes);
}
size_t show_pre_allocated(char *type, t_mem_chunk *chunk)
static size_t show_pre_allocated(char *type, t_mem_chunk *chunk)
{
size_t nb_bytes;
nb_bytes = 0;
while(chunk)
while (chunk)
{
write(1, type, ft_strlen(type));
write(1, " : 0x", 5);
@ -60,21 +61,21 @@ size_t show_pre_allocated(char *type, t_mem_chunk *chunk)
nb_bytes += show_allocs(chunk->first);
chunk = chunk->next;
}
return(nb_bytes);
return (nb_bytes);
}
size_t show_large()
static size_t show_large(void)
{
t_alloc *alloc;
size_t total_size;
alloc = g_allocs.large;
total_size = 0;
while(alloc)
while (alloc)
{
write(1, "LARGE : 0x", 10);
put_ulnbr_base((t_ul)alloc, "0123456789ABCDEF");
write(1,"\n", 1);
write(1, "\n", 1);
write(1, "0x", 2);
put_ulnbr_base((t_ul)(alloc + 1), "0123456789ABCDEF");
write(1, " - 0x", 5);
@ -85,10 +86,10 @@ size_t show_large()
total_size += alloc->size;
alloc = alloc->next;
}
return(total_size);
return (total_size);
}
void show_alloc_mem()
void show_alloc_mem(void)
{
size_t total;

20
srcs/utils.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/03 13:20:54 by tomoron #+# #+# */
/* Updated: 2024/12/03 13:23:31 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/malloc.h"
size_t align(size_t nb, size_t align_nb)
{
if (nb % align_nb)
nb = nb + (align_nb - (nb % align_nb));
return (nb);
}