norm almost ok, just too many functions

This commit is contained in:
2024-11-29 19:11:41 +01:00
parent 2a3d29eb36
commit 59b60a0bd3
2 changed files with 87 additions and 86 deletions

View File

@ -6,47 +6,46 @@
/* 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/29 18:45:50 by tomoron ### ########.fr */ /* Updated: 2024/11/29 19:10:52 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MALLOC_H #ifndef MALLOC_H
# define MALLOC_H # define MALLOC_H
#include <stddef.h> # include <stddef.h>
#include <sys/mman.h> # include <sys/mman.h>
#include <unistd.h> # include <unistd.h>
#define TINY_BLOC_SIZE 16384 # define TINY_BLOC_SIZE 16384
#define SMALL_BLOC_SIZE 524288 # define SMALL_BLOC_SIZE 524288
#define TINY_MALLOC 1024
#define SMALL_MALLOC 4096
# define TINY_MALLOC 1024
# define SMALL_MALLOC 4096
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_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_mem_bloc; //size 16
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_mem_bloc *large;
} t_allocations; } t_allocations;
t_allocations g_allocs; t_allocations g_allocs;
extern t_allocations g_allocs; extern t_allocations g_allocs;
void *ft_malloc(size_t size); void *ft_malloc(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/11/29 18:48:26 by tomoron ### ########.fr */ /* Updated: 2024/11/29 19:00:29 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,41 +15,43 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
void *get_memory(size_t size) void *get_memory(size_t size)
{ {
void *ptr; void *ptr;
t_mem_bloc *bloc_info; t_mem_bloc *bloc_info;
ptr = mmap(0, size, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); ptr = mmap(0, size, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE,
if(ptr == MAP_FAILED) -1, 0);
return(0); if (ptr == MAP_FAILED)
return (0);
bloc_info = ptr; bloc_info = ptr;
bloc_info->space_left = size - sizeof(t_mem_bloc); 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, t_mem_bloc **bloc) void *allocate_memory(size_t size, t_mem_bloc **bloc)
{ {
t_mem_bloc *new; t_mem_bloc *new;
new = get_memory(size + sizeof(t_mem_bloc)); new = get_memory(size + sizeof(t_mem_bloc));
if(!new) if (!new)
return(0); return (0);
new->next = *bloc; new->next = *bloc;
*bloc = new; *bloc = new;
return((char *)new + sizeof(t_mem_bloc)); return ((char *)new + sizeof(t_mem_bloc));
} }
t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev, t_mem_bloc *bloc) t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev,
t_mem_bloc *bloc)
{ {
t_alloc *tmp; t_alloc *tmp;
if((unsigned long)addr % 16) if ((unsigned long)addr % 16)
addr = (t_alloc *)((char *)addr + (16 - ((unsigned long)addr % 16))); //O_O addr = (t_alloc *)((char *)addr + (16 - ((unsigned long)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)
{ {
tmp = prev->next; tmp = prev->next;
prev->next = addr; prev->next = addr;
@ -57,92 +59,92 @@ t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev, t_mem_bloc *blo
} }
else else
addr->next = 0; addr->next = 0;
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_bloc(t_mem_bloc *bloc, 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 = (void *)bloc + 16; tmp = (void *)bloc + 16;
space_left = bloc->space_left; space_left = bloc->space_left;
while(tmp->next) while (tmp->next)
{ {
free_space = (tmp->next - tmp) - (tmp->size + sizeof(t_alloc)); //what free_space = (tmp->next - 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(
(t_alloc *)((char *)tmp->next + tmp->size + sizeof(t_alloc)), (void *)((char *)tmp->next + tmp->size + sizeof(t_alloc)),
size, tmp, bloc)); // should be after size, tmp, bloc));
space_left -= free_space; space_left -= free_space;
} }
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)); //not sure about this function size, tmp, bloc));
return(0); //30 minutes later, I don't understand this return (0);
} }
t_mem_bloc *create_new_bloc(int is_small,t_mem_bloc **bloc, t_mem_bloc *prev) t_mem_bloc *create_new_bloc(int is_small, t_mem_bloc **bloc, t_mem_bloc *prev)
{ {
t_mem_bloc *new; t_mem_bloc *new;
size_t mmap_size; size_t mmap_size;
if(is_small) if (is_small)
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);
if(!new) if (!new)
return(0); return (0);
if(prev) if (prev)
prev->next = new; prev->next = new;
if(!*bloc) if (!*bloc)
*bloc = new; *bloc = 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_bloc **bloc, int is_small)
{ {
t_mem_bloc *tmp; t_mem_bloc *tmp;
t_mem_bloc *prev; t_mem_bloc *prev;
t_alloc *res; t_alloc *res;
tmp = *bloc; tmp = *bloc;
prev = *bloc; prev = *bloc;
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_bloc(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_bloc(is_small, bloc, 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_bloc), size, 0, tmp));
} }
void *ft_malloc(size_t size) void *ft_malloc(size_t size)
{ {
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));
else else
return(allocate_memory(size, &(g_allocs.large))); return (allocate_memory(size, &(g_allocs.large)));
} }
int main(void) int main(void)
{ {
void *ptr; void *ptr;
void *ptr2; void *ptr2;
ptr = ft_malloc(10); ptr = ft_malloc(10);
ptr2 = ft_malloc(2000); ptr2 = ft_malloc(2000);