add malloc(ft_malloc for now) function

This commit is contained in:
2024-11-29 18:49:10 +01:00
parent f721d2e95d
commit 2a3d29eb36
3 changed files with 117 additions and 42 deletions

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/27 18:09:19 by tomoron ### ########.fr # # Updated: 2024/11/29 17:03:52 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -26,7 +26,7 @@ SRCS = $(addprefix $(SRCS_DIR)/, $(SRCS_NAMES))
OBJS = $(addprefix $(OBJS_DIR)/, $(SRCS_NAMES:.c=.o)) OBJS = $(addprefix $(OBJS_DIR)/, $(SRCS_NAMES:.c=.o))
FLAGS = -Wall -Wextra -Werror FLAGS = -Wall -Wextra -Werror -g
LFT = libft/libft.a LFT = libft/libft.a

View File

@ -6,27 +6,24 @@
/* 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/28 20:16:54 by tomoron ### ########.fr */ /* Updated: 2024/11/29 18:45:50 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MALLOC_H #ifndef MALLOC_H
# define MALLOC_H # define MALLOC_H
# ifndef DEBUG
# define DEBUG 1
# endif
#include <stddef.h> #include <stddef.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
#define TINY_MALLOC_ALL 16384 #define TINY_BLOC_SIZE 16384
#define SMALL_MALLOC_ALL 524288 #define SMALL_BLOC_SIZE 524288
#define TINY_MALLOC 1024 #define TINY_MALLOC 1024
#define SMALL_MALLOC 4096 #define SMALL_MALLOC 4096
typedef struct s_alloc typedef struct s_alloc
{ {
size_t size; //8 size_t size; //8
@ -35,7 +32,7 @@ typedef struct s_alloc
typedef struct s_mem_bloc typedef struct s_mem_bloc
{ {
t_alloc *first_alloc; //8 size_t space_left; //8
struct s_mem_bloc *next; //8 struct s_mem_bloc *next; //8
} t_mem_bloc; //size 16 } t_mem_bloc; //size 16
@ -46,6 +43,9 @@ typedef struct s_allocations
t_mem_bloc *large; t_mem_bloc *large;
} t_allocations; } t_allocations;
t_allocations g_allocs;
extern t_allocations g_allocs;
void *ft_malloc(size_t size); void *ft_malloc(size_t size);

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/28 20:22:55 by tomoron ### ########.fr */ /* Updated: 2024/11/29 18:48:26 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,53 +24,128 @@ void *get_memory(size_t size)
if(ptr == MAP_FAILED) if(ptr == MAP_FAILED)
return(0); return(0);
bloc_info = ptr; bloc_info = ptr;
bloc_info->first_alloc = 0; bloc_info->space_left = size - sizeof(t_mem_bloc);
bloc_info->next = 0; bloc_info->next = 0;
return(ptr); return(ptr);
} }
void *allocate_memory(size_t size) void *allocate_memory(size_t size, t_mem_bloc **bloc)
{ {
(void)size; t_mem_bloc *new;
return(0);
new = get_memory(size + sizeof(t_mem_bloc));
if(!new)
return(0);
new->next = *bloc;
*bloc = new;
return((char *)new + sizeof(t_mem_bloc));
} }
t_mem_bloc *get_mem_bloc(t_mem_bloc **bloc, size_t size) t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev, t_mem_bloc *bloc)
{
t_alloc *tmp;
if((unsigned long)addr % 16)
addr = (t_alloc *)((char *)addr + (16 - ((unsigned long)addr % 16))); //O_O
addr->size = size;
bloc->space_left -= (size + sizeof(t_alloc));
if(prev)
{
tmp = prev->next;
prev->next = addr;
addr->next = tmp;
}
else
addr->next = 0;
return(addr + 1);
}
t_alloc *get_suitable_addr_in_bloc(t_mem_bloc *bloc, size_t size)
{
t_alloc *tmp;
size_t space_left;
size_t free_space;
tmp = (void *)bloc + 16;
space_left = bloc->space_left;
while(tmp->next)
{
free_space = (tmp->next - tmp) - (tmp->size + sizeof(t_alloc)); //what
if(free_space >= size + sizeof(t_alloc))
return(reserve_addr(
(t_alloc *)((char *)tmp->next + tmp->size + sizeof(t_alloc)),
size, tmp, bloc)); // should be after
space_left -= free_space;
}
if(space_left >= size + sizeof(t_alloc))
return(reserve_addr(
(t_alloc *)((char *)tmp + tmp->size + sizeof(t_alloc)),
size, tmp, bloc)); //not sure about this function
return(0); //30 minutes later, I don't understand this
}
t_mem_bloc *create_new_bloc(int is_small,t_mem_bloc **bloc, t_mem_bloc *prev)
{
t_mem_bloc *new;
size_t mmap_size;
if(is_small)
mmap_size = SMALL_BLOC_SIZE;
else
mmap_size = TINY_BLOC_SIZE;
new = get_memory(mmap_size);
if(!new)
return(0);
if(prev)
prev->next = new;
if(!*bloc)
*bloc = new;
return(new);
}
void *pre_allocated(size_t size, t_mem_bloc **bloc, int is_small)
{ {
t_mem_bloc *tmp; t_mem_bloc *tmp;
t_mem_bloc *prev;
t_alloc *res;
tmp = *bloc;
} prev = *bloc;
res = 0;
void *pre_allocated(size_t size, s_mem_bloc **bloc) while(tmp)
{ {
t_mem_bloc *mem_bloc; if(tmp->space_left >= size)
t_alloc new; {
res = get_suitable_addr_in_bloc(tmp, size);
mem_bloc = get_mem_bloc(bloc, size) if(res)
return(res);
}
prev = tmp;
tmp = tmp->next;
}
tmp = create_new_bloc(is_small, bloc, prev);
if(!tmp)
return(0);
return(reserve_addr((void *)tmp + sizeof(t_mem_bloc), size, 0, tmp));
} }
void *ft_malloc(size_t size) void *ft_malloc(size_t size)
{ {
static t_allocations allocs; if(size <= TINY_MALLOC)
return(pre_allocated(size, &(g_allocs.tiny), 0));
if(size <= TINY_ALLOC) else if(size <= SMALL_MALLOC)
return(pre_allocated(size, &allocs->tiny)); return(pre_allocated(size, &(g_allocs.small), 1));
else if(size <= SMALL_ALLOC)
return(pre_allocated(size, &allocs->small));
else else
return(allocate_memory(size)); return(allocate_memory(size, &(g_allocs.large)));
return(0);
} }
#include <stdlib.h>
int main(void) int main(void)
{ {
// void *p1 = malloc(1); void *ptr;
// void *p2 = malloc(1); void *ptr2;
// void *p3 = malloc(1);
// (void)p3; ptr = ft_malloc(10);
// printf("%ld\n", p2 - p1); ptr2 = ft_malloc(2000);
printf("%p\n", ptr);
printf("%p\n", ptr2);
} }