add free function (may be broken), add pointer to first allocation in memory bloc.

This commit is contained in:
2024-12-01 03:19:21 +01:00
parent fd59310dad
commit 7cddc28d28
7 changed files with 136 additions and 45 deletions

7
.gitignore vendored
View File

@ -1,6 +1,5 @@
.objs .objs
libft_malloc*.so libft_malloc*.so
libft/libft.a *.o
libft/*.o *.a
libft/ft_printf/*.o a.out

View File

@ -6,7 +6,7 @@
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ # # By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
# Updated: 2024/11/30 18:07:44 by tomoron ### ########.fr # # Updated: 2024/12/01 03:02:40 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -17,7 +17,8 @@ NAME := libft_malloc_$(HOSTTYPE).so
CC = cc CC = cc
SRCS_NAMES = malloc.c \ SRCS_NAMES = malloc.c \
show_alloc_mem.c show_alloc_mem.c\
free.c
SRCS_DIR = srcs SRCS_DIR = srcs
@ -33,7 +34,7 @@ LFT = libft/libft.a
LFT_DIR = libft/ LFT_DIR = libft/
all: libft_malloc.so all: exec #libft_malloc.so
exec: $(OBJS_DIR) $(OBJS) $(LFT) exec: $(OBJS_DIR) $(OBJS) $(LFT)
$(CC) -o a.out $(FLAGS) main.c $(OBJS) $(LFT) $(CC) -o a.out $(FLAGS) main.c $(OBJS) $(LFT)

5
main.c
View File

@ -2,4 +2,9 @@
int main(void) int main(void)
{ {
void *ptr;
ptr = ft_malloc(1013);
ft_free(ptr);
show_alloc_mem();
} }

View File

@ -6,15 +6,94 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/30 18:46:07 by tomoron #+# #+# */ /* Created: 2024/11/30 18:46:07 by tomoron #+# #+# */
/* Updated: 2024/11/30 22:39:16 by tomoron ### ########.fr */ /* Updated: 2024/12/01 03:16:19 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "includes/malloc.h" #include "includes/malloc.h"
t_mem_bloc *get_alloc_bloc(t_alloc *alloc, t_mem_bloc *first, size_t size)
{
while(first)
{
if((t_ul)alloc >= (t_ul)first && ((t_ul)alloc - (t_ul)(first + 1)) <= size)
return(first);
first = first->next;
}
return(0);
}
int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur)
{
t_alloc *prev;
if(cur == *alloc)
return(1);
prev = 0;
while(cur)
{
if(cur->next == *alloc)
{
*res = cur;
return(1);
}
if(cur->next > *alloc && cur <= *alloc && ((t_ul)alloc - (t_ul)cur) <= cur->size)
{
*alloc = cur;
*res = prev;
return(1);
}
prev = cur;
cur = cur->next;
}
return(0);
}
int free_prealloc(t_alloc *alloc, t_mem_bloc **main_bloc, size_t size)
{
t_mem_bloc *bloc;
t_alloc *prev;
bloc = get_alloc_bloc(alloc, *main_bloc, size);
if(!bloc)
return(0);
if(!get_prev_alloc(&alloc, &prev, bloc->first))
return(1);
bloc->space_left -= alloc->size + sizeof(t_alloc);
if(bloc->first == alloc)
bloc->first = alloc->next;
else if(prev)
prev->next = alloc->next;
if(!bloc->first)
{
if(*main_bloc == bloc)
*main_bloc = bloc->next;
munmap(bloc, size);
}
return(1);
}
void free_large(t_alloc *alloc)
{
t_alloc *prev;
if(!get_prev_alloc(&alloc, &prev, g_allocs.large))
return ;
if(alloc == g_allocs.large)
g_allocs.large = alloc->next;
else if(prev)
prev->next = alloc->next;
munmap(alloc, alloc->size + sizeof(t_alloc));
}
void ft_free(void *ptr) void ft_free(void *ptr)
{ {
t_alloc *alloc; t_alloc *alloc;
alloc = (t_alloc *)ptr - 1; alloc = (t_alloc *)ptr - 1;
if(free_prealloc(alloc, &g_allocs.tiny, TINY_BLOC_SIZE))
return ;
if(free_prealloc(alloc, &g_allocs.small, SMALL_BLOC_SIZE))
return ;
free_large(alloc);
} }

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/26 16:38:01 by tomoron #+# #+# */ /* Created: 2024/11/26 16:38:01 by tomoron #+# #+# */
/* Updated: 2024/11/30 13:53:11 by tomoron ### ########.fr */ /* Updated: 2024/12/01 03:08:41 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -36,18 +36,23 @@ typedef struct s_mem_bloc
{ {
size_t space_left; //8 size_t space_left; //8
struct s_mem_bloc *next; //8 struct s_mem_bloc *next; //8
} t_mem_bloc; //size 16 t_alloc *first; //8
char padding[8];
} t_mem_bloc; //size 32
typedef struct s_allocations typedef struct s_allocations
{ {
t_mem_bloc *tiny; t_mem_bloc *tiny;
t_mem_bloc *small; t_mem_bloc *small;
t_mem_bloc *large; t_alloc *large;
} t_allocations; } t_allocations;
typedef unsigned long t_ul;
extern t_allocations g_allocs; extern t_allocations g_allocs;
void *ft_malloc(size_t size); void *ft_malloc(size_t size);
void show_alloc_mem(); void show_alloc_mem();
void ft_free(void *ptr);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/23 17:19:59 by tomoron #+# #+# */ /* Created: 2024/11/23 17:19:59 by tomoron #+# #+# */
/* Updated: 2024/11/30 18:01:25 by tomoron ### ########.fr */ /* Updated: 2024/12/01 03:05:08 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,19 +17,19 @@
t_allocations g_allocs; t_allocations g_allocs;
void *get_memory(size_t size) void *get_memory(size_t size, int no_write)
{ {
void *ptr; t_mem_bloc *bloc;
t_mem_bloc *bloc_info;
ptr = mmap(0, size, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, bloc = mmap(0, size, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0); -1, 0);
if (ptr == MAP_FAILED) if (bloc == MAP_FAILED)
return (0); return (0);
bloc_info = ptr; if(no_write)
bloc_info->space_left = size - sizeof(t_mem_bloc); return(bloc);
bloc_info->next = 0; bloc->space_left = size - sizeof(t_mem_bloc);
return (ptr); bloc->next = 0;
return (bloc);
} }
t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev, t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev,
@ -37,8 +37,8 @@ t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev,
{ {
t_alloc *tmp; t_alloc *tmp;
if ((unsigned long)addr % 16) if ((t_ul)addr % 16)
addr = (t_alloc *)((char *)addr + (16 - ((unsigned long)addr % 16))); addr = (t_alloc *)((char *)addr + (16 - ((t_ul)addr % 16)));
addr->size = size; addr->size = size;
bloc->space_left -= (size + sizeof(t_alloc)); bloc->space_left -= (size + sizeof(t_alloc));
if (prev) if (prev)
@ -58,11 +58,11 @@ t_alloc *get_suitable_addr_in_bloc(t_mem_bloc *bloc, size_t size)
size_t space_left; size_t space_left;
size_t free_space; size_t free_space;
tmp = (void *)bloc + 16; tmp = bloc->first; //alloc when first alloc has been removed
space_left = bloc->space_left; space_left = bloc->space_left;
while (tmp->next) while (tmp->next)
{ {
free_space = ((unsigned long)tmp->next - (unsigned long)tmp) - (tmp->size + sizeof(t_alloc)); free_space = ((t_ul)tmp->next - (t_ul)tmp) - (tmp->size + sizeof(t_alloc));
if (free_space >= size + sizeof(t_alloc)) if (free_space >= size + sizeof(t_alloc))
return (reserve_addr( return (reserve_addr(
(void *)((char *)tmp->next + tmp->size + sizeof(t_alloc)), (void *)((char *)tmp->next + tmp->size + sizeof(t_alloc)),
@ -86,9 +86,10 @@ t_mem_bloc *create_new_bloc(int is_small, t_mem_bloc **bloc, t_mem_bloc *prev)
mmap_size = SMALL_BLOC_SIZE; mmap_size = SMALL_BLOC_SIZE;
else else
mmap_size = TINY_BLOC_SIZE; mmap_size = TINY_BLOC_SIZE;
new = get_memory(mmap_size); new = get_memory(mmap_size, 0);
if (!new) if (!new)
return (0); return (0);
new->first = (t_alloc *)(new + 1);
if (prev) if (prev)
prev->next = new; prev->next = new;
if (!*bloc) if (!*bloc)
@ -124,17 +125,18 @@ void *pre_allocated(size_t size, t_mem_bloc **bloc, int is_small)
void *ft_malloc(size_t size) void *ft_malloc(size_t size)
{ {
t_mem_bloc *new; t_alloc *new;
if (size <= TINY_MALLOC) if (size <= TINY_MALLOC)
return (pre_allocated(size, &(g_allocs.tiny), 0)); return (pre_allocated(size, &(g_allocs.tiny), 0));
else if (size <= SMALL_MALLOC) else if (size <= SMALL_MALLOC)
return (pre_allocated(size, &(g_allocs.small), 1)); return (pre_allocated(size, &(g_allocs.small), 1));
new = get_memory(size + sizeof(t_mem_bloc)); new = get_memory(size + sizeof(t_alloc), 1);
if (!new) if (!new)
return (0); return (0);
new->size = size;
new->next = g_allocs.large; new->next = g_allocs.large;
g_allocs.large = new; g_allocs.large = new;
return ((char *)new + sizeof(t_mem_bloc)); return ((char *)new + sizeof(t_alloc));
} }

View File

@ -6,15 +6,15 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/30 12:19:34 by tomoron #+# #+# */ /* Created: 2024/11/30 12:19:34 by tomoron #+# #+# */
/* Updated: 2024/11/30 15:50:14 by tomoron ### ########.fr */ /* Updated: 2024/12/01 03:05:08 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "includes/malloc.h" #include "includes/malloc.h"
void put_ulnbr_base(unsigned long nbr, char *base) void put_ulnbr_base(t_ul nbr, char *base)
{ {
unsigned long base_len; t_ul base_len;
base_len = ft_strlen(base); base_len = ft_strlen(base);
if(nbr >= base_len) if(nbr >= base_len)
@ -35,9 +35,9 @@ size_t show_allocs(t_alloc *alloc)
{ {
nb_bytes += alloc->size; nb_bytes += alloc->size;
write(1, "0x", 2); write(1, "0x", 2);
put_ulnbr_base((unsigned long)alloc + sizeof(t_alloc), "0123456789ABCDEF"); put_ulnbr_base((t_ul)alloc + sizeof(t_alloc), "0123456789ABCDEF");
write(1, " - 0x", 5); write(1, " - 0x", 5);
put_ulnbr_base((unsigned long)alloc +sizeof(t_alloc) + alloc->size, "0123456789ABCDEF"); put_ulnbr_base((t_ul)alloc +sizeof(t_alloc) + alloc->size, "0123456789ABCDEF");
write(1, " : ", 3); write(1, " : ", 3);
put_ulnbr_base(alloc->size, "0123456789"); put_ulnbr_base(alloc->size, "0123456789");
write(1, " bytes\n", 7); write(1, " bytes\n", 7);
@ -55,9 +55,9 @@ size_t show_pre_allocated(char *type, t_mem_bloc *bloc)
{ {
write(1, type, ft_strlen(type)); write(1, type, ft_strlen(type));
write(1, " : 0x", 5); write(1, " : 0x", 5);
put_ulnbr_base((unsigned long)bloc, "0123456789ABCDEF"); put_ulnbr_base((t_ul)bloc, "0123456789ABCDEF");
write(1, "\n", 1); write(1, "\n", 1);
nb_bytes += show_allocs((t_alloc *)(bloc + 1)); nb_bytes += show_allocs(bloc->first);
bloc = bloc->next; bloc = bloc->next;
} }
return(nb_bytes); return(nb_bytes);
@ -65,25 +65,25 @@ size_t show_pre_allocated(char *type, t_mem_bloc *bloc)
size_t show_large() size_t show_large()
{ {
t_mem_bloc *bloc; t_alloc *alloc;
size_t total_size; size_t total_size;
bloc = g_allocs.large; alloc = g_allocs.large;
total_size = 0; total_size = 0;
while(bloc) while(alloc)
{ {
write(1, "LARGE : 0x", 10); write(1, "LARGE : 0x", 10);
put_ulnbr_base((unsigned long)bloc, "0123456789ABCDEF"); put_ulnbr_base((t_ul)alloc, "0123456789ABCDEF");
write(1,"\n", 1); write(1,"\n", 1);
write(1, "0x", 2); write(1, "0x", 2);
put_ulnbr_base((unsigned long)(bloc + 1), "0123456789ABCDEF"); put_ulnbr_base((t_ul)(alloc + 1), "0123456789ABCDEF");
write(1, " - 0x", 5); write(1, " - 0x", 5);
put_ulnbr_base((unsigned long)(bloc + 1) + bloc->space_left, "0123456789ABCDEF"); put_ulnbr_base((t_ul)(alloc + 1) + alloc->size, "0123456789ABCDEF");
write(1, " : ", 3); write(1, " : ", 3);
put_ulnbr_base(bloc->space_left, "0123456789"); put_ulnbr_base(alloc->size, "0123456789");
write(1, " bytes\n", 7); write(1, " bytes\n", 7);
total_size += bloc->space_left; total_size += alloc->size;
bloc = bloc->next; alloc = alloc->next;
} }
return(total_size); return(total_size);
} }