fix norm and set chunk sizes
This commit is contained in:
14
Makefile
14
Makefile
@ -1,12 +1,11 @@
|
|||||||
# **************************************************************************** #
|
# **************************************************************************** # #
|
||||||
# #
|
|
||||||
# ::: :::::::: #
|
# ::: :::::::: #
|
||||||
# Makefile :+: :+: :+: #
|
# Makefile :+: :+: :+: #
|
||||||
# +:+ +:+ +:+ #
|
# +:+ +:+ +:+ #
|
||||||
# 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/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
|
CC = cc
|
||||||
|
|
||||||
SRCS_NAMES = malloc.c \
|
SRCS_NAMES = malloc.c \
|
||||||
|
malloc_utils.c\
|
||||||
show_alloc_mem.c\
|
show_alloc_mem.c\
|
||||||
free.c
|
free.c\
|
||||||
|
realloc.c\
|
||||||
|
utils.c
|
||||||
|
|
||||||
SRCS_DIR = srcs
|
SRCS_DIR = srcs
|
||||||
|
|
||||||
@ -34,7 +36,7 @@ LFT = libft/libft.a
|
|||||||
|
|
||||||
LFT_DIR = libft/
|
LFT_DIR = libft/
|
||||||
|
|
||||||
all: exec #libft_malloc.so
|
all: 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)
|
||||||
@ -43,7 +45,7 @@ libft_malloc.so: $(NAME)
|
|||||||
ln -sf $(NAME) libft_malloc.so
|
ln -sf $(NAME) libft_malloc.so
|
||||||
|
|
||||||
$(LFT):
|
$(LFT):
|
||||||
$(MAKE) -C $(LFT_DIR)
|
make -j$(shell nproc) -C $(LFT_DIR)
|
||||||
|
|
||||||
$(NAME): $(OBJS_DIR) $(OBJS) $(LFT)
|
$(NAME): $(OBJS_DIR) $(OBJS) $(LFT)
|
||||||
$(CC) -shared -o $(NAME) $(OBJS) $(LFT)
|
$(CC) -shared -o $(NAME) $(OBJS) $(LFT)
|
||||||
|
@ -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/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)
|
$(NAME): ft_printf $(OBJS)
|
||||||
make --no-print-directory -C ./ft_printf
|
$(MAKE) --no-print-directory -C ./ft_printf
|
||||||
cp ./ft_printf/libftprintf.a $(NAME)
|
cp ./ft_printf/libftprintf.a $(NAME)
|
||||||
ar rcs $(NAME) $(OBJS)
|
ar rcs $(NAME) $(OBJS)
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ bonus: $(OBJS) $(OBJS_BONUS)
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJS) $(OBJS_BONUS)
|
rm -f $(OBJS) $(OBJS_BONUS)
|
||||||
make --no-print-directory -C ./ft_printf fclean
|
$(MAKE) --no-print-directory -C ./ft_printf fclean
|
||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
rm -f $(NAME)
|
rm -f $(NAME)
|
||||||
|
19
main.c
19
main.c
@ -3,15 +3,14 @@
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
void *ptr;
|
int i;
|
||||||
|
|
||||||
ft_malloc(1013);
|
i = 0;
|
||||||
printf("%p\n",ft_malloc(100));
|
while(i < 110)
|
||||||
ft_malloc(100);
|
{
|
||||||
ptr = ft_malloc(1000);
|
malloc(256);
|
||||||
ft_malloc(100);
|
i++;
|
||||||
ft_malloc(100);
|
}
|
||||||
ft_free(ptr);
|
|
||||||
ft_malloc(123);
|
|
||||||
show_alloc_mem();
|
show_alloc_mem();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
88
srcs/free.c
88
srcs/free.c
@ -6,109 +6,111 @@
|
|||||||
/* 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/02 19:54:00 by tomoron ### ########.fr */
|
/* Updated: 2024/12/03 18:59:13 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "includes/malloc.h"
|
#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;
|
size_t size;
|
||||||
|
|
||||||
if(is_small)
|
if (is_small)
|
||||||
size = SMALL_CHUNK_SIZE;
|
size = SMALL_CHUNK_SIZE;
|
||||||
else
|
else
|
||||||
size = TINY_CHUNK_SIZE;
|
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 - \
|
||||||
return(first);
|
(t_ul)(first + 1)) <= size)
|
||||||
|
return (first);
|
||||||
first = first->next;
|
first = first->next;
|
||||||
}
|
}
|
||||||
return(0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur)
|
int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur)
|
||||||
{
|
{
|
||||||
t_alloc *prev;
|
t_alloc *prev;
|
||||||
|
|
||||||
*res = 0;
|
*res = 0;
|
||||||
if(cur == *alloc)
|
if (cur == *alloc)
|
||||||
return(1);
|
return (1);
|
||||||
prev = 0;
|
prev = 0;
|
||||||
while(cur)
|
while (cur)
|
||||||
{
|
{
|
||||||
if(cur->next == *alloc)
|
if (cur->next == *alloc)
|
||||||
{
|
{
|
||||||
*res = cur;
|
*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;
|
*alloc = cur;
|
||||||
*res = prev;
|
*res = prev;
|
||||||
return(1);
|
return (1);
|
||||||
}
|
}
|
||||||
prev = cur;
|
prev = cur;
|
||||||
cur = cur->next;
|
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_mem_chunk *chunk;
|
||||||
t_alloc *prev;
|
t_alloc *prev;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
|
||||||
prev = 0;
|
|
||||||
chunk = get_alloc_chunk(alloc, *main_chunk, is_small);
|
chunk = get_alloc_chunk(alloc, *main_chunk, is_small);
|
||||||
if(!chunk)
|
if (!chunk)
|
||||||
return(0);
|
return (0);
|
||||||
if(!get_prev_alloc(&alloc, &prev, chunk->first))
|
if (!get_prev_alloc(&alloc, &prev, chunk->first))
|
||||||
return(1);
|
return (1);
|
||||||
chunk->space_left -= alloc->size + sizeof(t_alloc);
|
chunk->space_left -= alloc->size + sizeof(t_alloc);
|
||||||
if(chunk->first == alloc)
|
if (chunk->first == alloc)
|
||||||
chunk->first = alloc->next;
|
chunk->first = alloc->next;
|
||||||
else if(prev)
|
else if (prev)
|
||||||
prev->next = alloc->next;
|
prev->next = alloc->next;
|
||||||
if(!chunk->first)
|
if (!chunk->first)
|
||||||
{
|
{
|
||||||
if(*main_chunk == chunk)
|
if (*main_chunk == chunk)
|
||||||
*main_chunk = chunk->next;
|
*main_chunk = chunk->next;
|
||||||
if(is_small)
|
if (is_small)
|
||||||
size = SMALL_CHUNK_SIZE;
|
size = SMALL_CHUNK_SIZE;
|
||||||
else
|
else
|
||||||
size = TINY_CHUNK_SIZE;
|
size = TINY_CHUNK_SIZE;
|
||||||
munmap(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;
|
t_alloc *prev;
|
||||||
|
|
||||||
if(!get_prev_alloc(&alloc, &prev, g_allocs.large))
|
if (!get_prev_alloc(&alloc, &prev, g_allocs.large))
|
||||||
return ;
|
return ;
|
||||||
if(alloc == g_allocs.large)
|
if (alloc == g_allocs.large)
|
||||||
g_allocs.large = alloc->next;
|
g_allocs.large = alloc->next;
|
||||||
else if(prev)
|
else if (prev)
|
||||||
prev->next = alloc->next;
|
prev->next = alloc->next;
|
||||||
munmap(alloc, alloc->size + sizeof(t_alloc));
|
munmap(alloc, alloc->size + sizeof(t_alloc));
|
||||||
}
|
}
|
||||||
|
|
||||||
void free(void *ptr)
|
void free(void *ptr)
|
||||||
{
|
{
|
||||||
t_alloc *alloc;
|
t_alloc *alloc;
|
||||||
|
|
||||||
if(!ptr)
|
if (!ptr)
|
||||||
return ;
|
return ;
|
||||||
alloc = (t_alloc *)ptr - 1;
|
alloc = (t_alloc *)ptr - 1;
|
||||||
if(free_prealloc(alloc, &g_allocs.tiny, 0))
|
if (free_prealloc(alloc, &g_allocs.tiny, 0))
|
||||||
return ;
|
return ;
|
||||||
if(free_prealloc(alloc, &g_allocs.small, 1))
|
if (free_prealloc(alloc, &g_allocs.small, 1))
|
||||||
return ;
|
return ;
|
||||||
free_large(alloc);
|
free_large(alloc);
|
||||||
}
|
}
|
||||||
|
@ -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/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 PAGE_SIZE sysconf(_SC_PAGESIZE)
|
||||||
|
|
||||||
|
# define TINY_MALLOC 256
|
||||||
|
# define SMALL_MALLOC 4096
|
||||||
|
|
||||||
# define TINY_MALLOC 1024
|
# define TINY_CHUNK_SIZE align(((16 + TINY_MALLOC) * 100) + 32, PAGE_SIZE)
|
||||||
# define SMALL_MALLOC 4096
|
# define SMALL_CHUNK_SIZE align(((16 + SMALL_MALLOC) * 100) + 32, PAGE_SIZE)
|
||||||
|
|
||||||
# define TINY_CHUNK_SIZE (((16 + TINY_MALLOC) * 100) + 32)
|
|
||||||
# define SMALL_CHUNK_SIZE 524288
|
|
||||||
|
|
||||||
typedef struct s_alloc
|
typedef struct s_alloc
|
||||||
{
|
{
|
||||||
@ -45,15 +44,17 @@ typedef struct s_allocations
|
|||||||
{
|
{
|
||||||
t_mem_chunk *tiny;
|
t_mem_chunk *tiny;
|
||||||
t_mem_chunk *small;
|
t_mem_chunk *small;
|
||||||
t_alloc *large;
|
t_alloc *large;
|
||||||
} t_allocations;
|
} t_allocations;
|
||||||
|
|
||||||
typedef unsigned long t_ul;
|
typedef unsigned long t_ul;
|
||||||
|
|
||||||
extern t_allocations g_allocs;
|
extern t_allocations g_allocs;
|
||||||
|
|
||||||
|
size_t align(size_t nb, size_t align_nb);
|
||||||
|
|
||||||
void *malloc(size_t size);
|
void *malloc(size_t size);
|
||||||
void show_alloc_mem();
|
void show_alloc_mem(void);
|
||||||
void free(void *ptr);
|
void free(void *ptr);
|
||||||
void *realloc(void *ptr, size_t size);
|
void *realloc(void *ptr, size_t size);
|
||||||
|
|
||||||
|
@ -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/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_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)
|
void *get_memory(size_t size, int no_write)
|
||||||
{
|
{
|
||||||
t_mem_chunk *chunk;
|
t_mem_chunk *chunk;
|
||||||
@ -25,8 +29,8 @@ void *get_memory(size_t size, int no_write)
|
|||||||
-1, 0);
|
-1, 0);
|
||||||
if (chunk == MAP_FAILED)
|
if (chunk == MAP_FAILED)
|
||||||
return (0);
|
return (0);
|
||||||
if(no_write)
|
if (no_write)
|
||||||
return(chunk);
|
return (chunk);
|
||||||
chunk->space_left = size - sizeof(t_mem_chunk);
|
chunk->space_left = size - sizeof(t_mem_chunk);
|
||||||
chunk->next = 0;
|
chunk->next = 0;
|
||||||
return (chunk);
|
return (chunk);
|
||||||
@ -52,59 +56,7 @@ 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_chunk(t_mem_chunk *chunk, size_t size)
|
static void *pre_allocated(size_t size, t_mem_chunk **chunk, int is_small)
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
t_mem_chunk *tmp;
|
t_mem_chunk *tmp;
|
||||||
t_mem_chunk *prev;
|
t_mem_chunk *prev;
|
||||||
@ -138,7 +90,6 @@ void *malloc(size_t size)
|
|||||||
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_alloc), 1);
|
new = get_memory(size + sizeof(t_alloc), 1);
|
||||||
if (!new)
|
if (!new)
|
||||||
return (0);
|
return (0);
|
||||||
|
78
srcs/malloc_utils.c
Normal file
78
srcs/malloc_utils.c
Normal 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));
|
||||||
|
}
|
@ -6,72 +6,73 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/12/02 14:41:22 by tomoron #+# #+# */
|
/* 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"
|
#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)
|
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);
|
||||||
|
|
||||||
|
static void *realloc_recreate(t_alloc *alloc, size_t size)
|
||||||
{
|
{
|
||||||
void *new;
|
void *new;
|
||||||
|
|
||||||
new = malloc(size);
|
new = malloc(size);
|
||||||
if(alloc->size < size)
|
if (alloc->size < size)
|
||||||
size = alloc->size;
|
size = alloc->size;
|
||||||
ft_memcpy(new, alloc + 1, size);
|
ft_memcpy(new, alloc + 1, size);
|
||||||
free(alloc + 1);
|
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;
|
t_alloc *prev;
|
||||||
|
|
||||||
chunk = get_alloc_chunk(alloc, chunk, is_small);
|
chunk = get_alloc_chunk(alloc, chunk, is_small);
|
||||||
if(!chunk)
|
if (!chunk)
|
||||||
return(0);
|
return (0);
|
||||||
if(!get_prev_alloc(&alloc, &prev, chunk->first))
|
if (!get_prev_alloc(&alloc, &prev, chunk->first))
|
||||||
return(0);
|
return (0);
|
||||||
if(alloc->size >= size)
|
if (alloc->size >= size)
|
||||||
{
|
{
|
||||||
chunk->space_left += alloc->size - size;
|
chunk->space_left += alloc->size - size;
|
||||||
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)
|
|| (size > SMALL_CHUNK_SIZE && is_small == 1)
|
||||||
|| (t_ul)(alloc->next) - (t_ul)(alloc + 1) < size)
|
|| (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;
|
chunk->space_left -= size - alloc->size;
|
||||||
alloc->size = 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;
|
t_alloc *prev;
|
||||||
|
|
||||||
if(!get_prev_alloc(&alloc, &prev, g_allocs.large))
|
if (!get_prev_alloc(&alloc, &prev, g_allocs.large))
|
||||||
return(0);
|
return (0);
|
||||||
return(realloc_recreate(alloc, size));
|
return (realloc_recreate(alloc, size));
|
||||||
}
|
}
|
||||||
|
|
||||||
void *realloc(void *ptr, size_t size)
|
void *realloc(void *ptr, size_t size)
|
||||||
{
|
{
|
||||||
t_alloc *alloc;
|
t_alloc *alloc;
|
||||||
|
|
||||||
if(!ptr)
|
if (!ptr)
|
||||||
return(malloc(size));
|
return (malloc(size));
|
||||||
alloc = (t_alloc *)ptr - 1;
|
alloc = (t_alloc *)ptr - 1;
|
||||||
ptr = realloc_prealloc(alloc, g_allocs.tiny, 0, size);
|
ptr = realloc_prealloc(alloc, g_allocs.tiny, 0, size);
|
||||||
if(ptr)
|
if (ptr)
|
||||||
return(ptr);
|
return (ptr);
|
||||||
ptr = realloc_prealloc(alloc, g_allocs.tiny, 1, size);
|
ptr = realloc_prealloc(alloc, g_allocs.small, 1, size);
|
||||||
if(ptr)
|
if (ptr)
|
||||||
return(ptr);
|
return (ptr);
|
||||||
return(realloc_large(alloc, size));
|
return (realloc_large(alloc, size));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,18 +6,18 @@
|
|||||||
/* 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/02 15:24:45 by tomoron ### ########.fr */
|
/* Updated: 2024/12/03 18:50:25 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "includes/malloc.h"
|
#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;
|
t_ul base_len;
|
||||||
|
|
||||||
base_len = ft_strlen(base);
|
base_len = ft_strlen(base);
|
||||||
if(nbr >= base_len)
|
if (nbr >= base_len)
|
||||||
{
|
{
|
||||||
put_ulnbr_base(nbr / base_len, base);
|
put_ulnbr_base(nbr / base_len, base);
|
||||||
write(1, base + (nbr % base_len), 1);
|
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);
|
write(1, base + nbr, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t show_allocs(t_alloc *alloc)
|
static size_t show_allocs(t_alloc *alloc)
|
||||||
{
|
{
|
||||||
size_t nb_bytes;
|
size_t nb_bytes;
|
||||||
|
|
||||||
nb_bytes = 0;
|
nb_bytes = 0;
|
||||||
while(alloc)
|
while (alloc)
|
||||||
{
|
{
|
||||||
nb_bytes += alloc->size;
|
nb_bytes += alloc->size;
|
||||||
write(1, "0x", 2);
|
write(1, "0x", 2);
|
||||||
put_ulnbr_base((t_ul)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((t_ul)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);
|
||||||
alloc = alloc->next;
|
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;
|
size_t nb_bytes;
|
||||||
|
|
||||||
nb_bytes = 0;
|
nb_bytes = 0;
|
||||||
while(chunk)
|
while (chunk)
|
||||||
{
|
{
|
||||||
write(1, type, ft_strlen(type));
|
write(1, type, ft_strlen(type));
|
||||||
write(1, " : 0x", 5);
|
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);
|
nb_bytes += show_allocs(chunk->first);
|
||||||
chunk = chunk->next;
|
chunk = chunk->next;
|
||||||
}
|
}
|
||||||
return(nb_bytes);
|
return (nb_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t show_large()
|
static size_t show_large(void)
|
||||||
{
|
{
|
||||||
t_alloc *alloc;
|
t_alloc *alloc;
|
||||||
size_t total_size;
|
size_t total_size;
|
||||||
|
|
||||||
alloc = g_allocs.large;
|
alloc = g_allocs.large;
|
||||||
total_size = 0;
|
total_size = 0;
|
||||||
while(alloc)
|
while (alloc)
|
||||||
{
|
{
|
||||||
write(1, "LARGE : 0x", 10);
|
write(1, "LARGE : 0x", 10);
|
||||||
put_ulnbr_base((t_ul)alloc, "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((t_ul)(alloc + 1), "0123456789ABCDEF");
|
put_ulnbr_base((t_ul)(alloc + 1), "0123456789ABCDEF");
|
||||||
write(1, " - 0x", 5);
|
write(1, " - 0x", 5);
|
||||||
@ -85,12 +86,12 @@ size_t show_large()
|
|||||||
total_size += alloc->size;
|
total_size += alloc->size;
|
||||||
alloc = alloc->next;
|
alloc = alloc->next;
|
||||||
}
|
}
|
||||||
return(total_size);
|
return (total_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_alloc_mem()
|
void show_alloc_mem(void)
|
||||||
{
|
{
|
||||||
size_t total;
|
size_t total;
|
||||||
|
|
||||||
total = 0;
|
total = 0;
|
||||||
total += show_pre_allocated("TINY", g_allocs.tiny);
|
total += show_pre_allocated("TINY", g_allocs.tiny);
|
||||||
|
20
srcs/utils.c
Normal file
20
srcs/utils.c
Normal 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);
|
||||||
|
}
|
Reference in New Issue
Block a user