add realloc, rename bloc to chunk, start thinking of chunk sizes

This commit is contained in:
2024-12-02 20:18:59 +01:00
parent a15705401e
commit 231b283e0d
5 changed files with 163 additions and 70 deletions

View File

@ -6,14 +6,20 @@
/* 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/12/01 18:47:12 by tomoron ### ########.fr */ /* Updated: 2024/12/02 19:54:00 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) t_mem_chunk *get_alloc_chunk(t_alloc *alloc, t_mem_chunk *first,int is_small)
{ {
size_t size;
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) if((t_ul)alloc >= (t_ul)first && ((t_ul)alloc - (t_ul)(first + 1)) <= size)
@ -27,6 +33,7 @@ int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur)
{ {
t_alloc *prev; t_alloc *prev;
*res = 0;
if(cur == *alloc) if(cur == *alloc)
return(1); return(1);
prev = 0; prev = 0;
@ -49,27 +56,32 @@ int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur)
return(0); return(0);
} }
int free_prealloc(t_alloc *alloc, t_mem_bloc **main_bloc, size_t size) int free_prealloc(t_alloc *alloc, t_mem_chunk **main_chunk, int is_small)
{ {
t_mem_bloc *bloc; t_mem_chunk *chunk;
t_alloc *prev; t_alloc *prev;
size_t size;
prev = 0; prev = 0;
bloc = get_alloc_bloc(alloc, *main_bloc, size); chunk = get_alloc_chunk(alloc, *main_chunk, is_small);
if(!bloc) if(!chunk)
return(0); return(0);
if(!get_prev_alloc(&alloc, &prev, bloc->first)) if(!get_prev_alloc(&alloc, &prev, chunk->first))
return(1); return(1);
bloc->space_left -= alloc->size + sizeof(t_alloc); chunk->space_left -= alloc->size + sizeof(t_alloc);
if(bloc->first == alloc) if(chunk->first == alloc)
bloc->first = alloc->next; chunk->first = alloc->next;
else if(prev) else if(prev)
prev->next = alloc->next; prev->next = alloc->next;
if(!bloc->first) if(!chunk->first)
{ {
if(*main_bloc == bloc) if(*main_chunk == chunk)
*main_bloc = bloc->next; *main_chunk = chunk->next;
munmap(bloc, size); if(is_small)
size = SMALL_CHUNK_SIZE;
else
size = TINY_CHUNK_SIZE;
munmap(chunk, size);
} }
return(1); return(1);
} }
@ -87,14 +99,16 @@ void free_large(t_alloc *alloc)
munmap(alloc, alloc->size + sizeof(t_alloc)); munmap(alloc, alloc->size + sizeof(t_alloc));
} }
void ft_free(void *ptr) void free(void *ptr)
{ {
t_alloc *alloc; t_alloc *alloc;
alloc = (t_alloc *)ptr - 1; if(!ptr)
if(free_prealloc(alloc, &g_allocs.tiny, TINY_BLOC_SIZE))
return ; return ;
if(free_prealloc(alloc, &g_allocs.small, SMALL_BLOC_SIZE)) alloc = (t_alloc *)ptr - 1;
if(free_prealloc(alloc, &g_allocs.tiny, 0))
return ;
if(free_prealloc(alloc, &g_allocs.small, 1))
return ; return ;
free_large(alloc); 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/12/01 03:08:41 by tomoron ### ########.fr */ /* Updated: 2024/12/02 20:18:33 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,30 +20,31 @@
# define PAGE_SIZE sysconf(_SC_PAGESIZE) # define PAGE_SIZE sysconf(_SC_PAGESIZE)
# define TINY_BLOC_SIZE (PAGE_SIZE * 4)
# define SMALL_BLOC_SIZE 524288
# define TINY_MALLOC 1024 # define TINY_MALLOC 1024
# define SMALL_MALLOC 4096 # define SMALL_MALLOC 4096
# define TINY_CHUNK_SIZE (((16 + TINY_MALLOC) * 100) + 32)
# define SMALL_CHUNK_SIZE 524288
typedef struct s_alloc typedef struct s_alloc
{ {
size_t size; //8 size_t size; //8
struct s_alloc *next; //8 struct s_alloc *next; //8
} t_alloc; //size 16 } t_alloc; //size 16
typedef struct s_mem_bloc typedef struct s_mem_chunk
{ {
size_t space_left; //8 size_t space_left; //8
struct s_mem_bloc *next; //8 struct s_mem_chunk *next; //8
t_alloc *first; //8 t_alloc *first; //8
char padding[8]; char padding[8];
} t_mem_bloc; //size 32 } t_mem_chunk; //size 32
typedef struct s_allocations typedef struct s_allocations
{ {
t_mem_bloc *tiny; t_mem_chunk *tiny;
t_mem_bloc *small; t_mem_chunk *small;
t_alloc *large; t_alloc *large;
} t_allocations; } t_allocations;
@ -51,8 +52,9 @@ typedef unsigned long t_ul;
extern t_allocations g_allocs; extern t_allocations g_allocs;
void *ft_malloc(size_t size); void *malloc(size_t size);
void show_alloc_mem(); void show_alloc_mem();
void ft_free(void *ptr); void free(void *ptr);
void *realloc(void *ptr, size_t size);
#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/12/01 19:21:27 by tomoron ### ########.fr */ /* Updated: 2024/12/02 19:54:00 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,28 +19,28 @@ t_allocations g_allocs;
void *get_memory(size_t size, int no_write) void *get_memory(size_t size, int no_write)
{ {
t_mem_bloc *bloc; t_mem_chunk *chunk;
bloc = mmap(0, size, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, chunk = mmap(0, size, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0); -1, 0);
if (bloc == MAP_FAILED) if (chunk == MAP_FAILED)
return (0); return (0);
if(no_write) if(no_write)
return(bloc); return(chunk);
bloc->space_left = size - sizeof(t_mem_bloc); chunk->space_left = size - sizeof(t_mem_chunk);
bloc->next = 0; chunk->next = 0;
return (bloc); return (chunk);
} }
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,
t_mem_bloc *bloc) t_mem_chunk *chunk)
{ {
t_alloc *tmp; t_alloc *tmp;
if ((t_ul)addr % 16) if ((t_ul)addr % 16)
addr = (t_alloc *)((char *)addr + (16 - ((t_ul)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)); chunk->space_left -= (size + sizeof(t_alloc));
if (prev) if (prev)
{ {
tmp = prev->next; tmp = prev->next;
@ -52,20 +52,20 @@ t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev,
return (addr + 1); return (addr + 1);
} }
t_alloc *get_suitable_addr_in_bloc(t_mem_bloc *bloc, size_t size) t_alloc *get_suitable_addr_in_chunk(t_mem_chunk *chunk, size_t size)
{ {
t_alloc *tmp; t_alloc *tmp;
size_t space_left; size_t space_left;
size_t free_space; size_t free_space;
tmp = bloc->first; tmp = chunk->first;
space_left = bloc->space_left; space_left = chunk->space_left;
if((t_ul)bloc->first - (t_ul)(bloc + 1) >= size + sizeof(t_alloc)) if((t_ul)chunk->first - (t_ul)(chunk + 1) >= size + sizeof(t_alloc))
{ {
tmp = bloc->first; tmp = chunk->first;
bloc->first = reserve_addr((void *)(bloc + 1), size, 0, bloc) - 1; chunk->first = reserve_addr((void *)(chunk + 1), size, 0, chunk) - 1;
bloc->first->next = tmp; chunk->first->next = tmp;
return(bloc->first + 1); return(chunk->first + 1);
} }
while (tmp->next) while (tmp->next)
{ {
@ -73,64 +73,64 @@ t_alloc *get_suitable_addr_in_bloc(t_mem_bloc *bloc, size_t size)
if (free_space >= size + sizeof(t_alloc)) if (free_space >= size + sizeof(t_alloc))
return (reserve_addr( return (reserve_addr(
(void *)((char *)tmp + tmp->size + sizeof(t_alloc)), (void *)((char *)tmp + tmp->size + sizeof(t_alloc)),
size, tmp, bloc)); size, tmp, chunk));
space_left -= free_space; space_left -= free_space;
tmp = tmp->next; tmp = tmp->next;
} }
if (space_left >= size + sizeof(t_alloc)) if (space_left >= size + sizeof(t_alloc))
return (reserve_addr( return (reserve_addr(
(t_alloc *)((char *)tmp + tmp->size + sizeof(t_alloc)), (t_alloc *)((char *)tmp + tmp->size + sizeof(t_alloc)),
size, tmp, bloc)); size, tmp, chunk));
return (0); return (0);
} }
t_mem_bloc *create_new_bloc(int is_small, t_mem_bloc **bloc, t_mem_bloc *prev) t_mem_chunk *create_new_chunk(int is_small, t_mem_chunk **chunk, t_mem_chunk *prev)
{ {
t_mem_bloc *new; t_mem_chunk *new;
size_t mmap_size; size_t mmap_size;
if (is_small) if (is_small)
mmap_size = SMALL_BLOC_SIZE; mmap_size = SMALL_CHUNK_SIZE;
else else
mmap_size = TINY_BLOC_SIZE; mmap_size = TINY_CHUNK_SIZE;
new = get_memory(mmap_size, 0); new = get_memory(mmap_size, 0);
if (!new) if (!new)
return (0); return (0);
new->first = (t_alloc *)(new + 1); new->first = (t_alloc *)(new + 1);
if (prev) if (prev)
prev->next = new; prev->next = new;
if (!*bloc) if (!*chunk)
*bloc = new; *chunk = new;
return (new); return (new);
} }
void *pre_allocated(size_t size, t_mem_bloc **bloc, int is_small) void *pre_allocated(size_t size, t_mem_chunk **chunk, int is_small)
{ {
t_mem_bloc *tmp; t_mem_chunk *tmp;
t_mem_bloc *prev; t_mem_chunk *prev;
t_alloc *res; t_alloc *res;
tmp = *bloc; tmp = *chunk;
prev = *bloc; prev = *chunk;
res = 0; res = 0;
while (tmp) while (tmp)
{ {
if (tmp->space_left >= size) if (tmp->space_left >= size)
{ {
res = get_suitable_addr_in_bloc(tmp, size); res = get_suitable_addr_in_chunk(tmp, size);
if (res) if (res)
return (res); return (res);
} }
prev = tmp; prev = tmp;
tmp = tmp->next; tmp = tmp->next;
} }
tmp = create_new_bloc(is_small, bloc, prev); tmp = create_new_chunk(is_small, chunk, prev);
if (!tmp) if (!tmp)
return (0); return (0);
return (reserve_addr((void *)tmp + sizeof(t_mem_bloc), size, 0, tmp)); return (reserve_addr((void *)tmp + sizeof(t_mem_chunk), size, 0, tmp));
} }
void *ft_malloc(size_t size) void *malloc(size_t size)
{ {
t_alloc *new; t_alloc *new;

77
srcs/realloc.c Normal file
View File

@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* realloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#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)
{
void *new;
new = malloc(size);
if(alloc->size < size)
size = alloc->size;
ft_memcpy(new, alloc + 1, size);
free(alloc + 1);
return(new);
}
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)
{
chunk->space_left += alloc->size - size;
alloc->size = size;
return(alloc + 1);
}
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));
chunk->space_left -= size - alloc->size;
alloc->size = size;
return(alloc + 1);
}
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));
}
void *realloc(void *ptr, size_t size)
{
t_alloc *alloc;
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));
}

View File

@ -6,7 +6,7 @@
/* 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/12/01 03:05:08 by tomoron ### ########.fr */ /* Updated: 2024/12/02 15:24:45 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -46,19 +46,19 @@ size_t show_allocs(t_alloc *alloc)
return(nb_bytes); return(nb_bytes);
} }
size_t show_pre_allocated(char *type, t_mem_bloc *bloc) size_t show_pre_allocated(char *type, t_mem_chunk *chunk)
{ {
size_t nb_bytes; size_t nb_bytes;
nb_bytes = 0; nb_bytes = 0;
while(bloc) while(chunk)
{ {
write(1, type, ft_strlen(type)); write(1, type, ft_strlen(type));
write(1, " : 0x", 5); write(1, " : 0x", 5);
put_ulnbr_base((t_ul)bloc, "0123456789ABCDEF"); put_ulnbr_base((t_ul)chunk, "0123456789ABCDEF");
write(1, "\n", 1); write(1, "\n", 1);
nb_bytes += show_allocs(bloc->first); nb_bytes += show_allocs(chunk->first);
bloc = bloc->next; chunk = chunk->next;
} }
return(nb_bytes); return(nb_bytes);
} }
@ -98,5 +98,5 @@ void show_alloc_mem()
total += show_large(); total += show_large();
write(1, "Total : ", 8); write(1, "Total : ", 8);
put_ulnbr_base(total, "0123456789"); put_ulnbr_base(total, "0123456789");
write(1, " bytes\n", 6); write(1, " bytes\n", 7);
} }