fix norm and set chunk sizes

This commit is contained in:
2024-12-03 19:03:30 +01:00
parent 231b283e0d
commit 7dfa7eb049
10 changed files with 241 additions and 186 deletions

View File

@ -6,109 +6,111 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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;
else
size = TINY_CHUNK_SIZE;
while(first)
while (first)
{
if((t_ul)alloc >= (t_ul)first && ((t_ul)alloc - (t_ul)(first + 1)) <= size)
return(first);
if ((t_ul)alloc >= (t_ul)first && ((t_ul)alloc - \
(t_ul)(first + 1)) <= size)
return (first);
first = first->next;
}
return(0);
return (0);
}
int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur)
{
t_alloc *prev;
t_alloc *prev;
*res = 0;
if(cur == *alloc)
return(1);
if (cur == *alloc)
return (1);
prev = 0;
while(cur)
while (cur)
{
if(cur->next == *alloc)
if (cur->next == *alloc)
{
*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;
*res = prev;
return(1);
return (1);
}
prev = cur;
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_alloc *prev;
size_t size;
t_mem_chunk *chunk;
t_alloc *prev;
size_t size;
prev = 0;
chunk = get_alloc_chunk(alloc, *main_chunk, is_small);
if(!chunk)
return(0);
if(!get_prev_alloc(&alloc, &prev, chunk->first))
return(1);
if (!chunk)
return (0);
if (!get_prev_alloc(&alloc, &prev, chunk->first))
return (1);
chunk->space_left -= alloc->size + sizeof(t_alloc);
if(chunk->first == alloc)
if (chunk->first == alloc)
chunk->first = alloc->next;
else if(prev)
else if (prev)
prev->next = alloc->next;
if(!chunk->first)
if (!chunk->first)
{
if(*main_chunk == chunk)
if (*main_chunk == chunk)
*main_chunk = chunk->next;
if(is_small)
if (is_small)
size = SMALL_CHUNK_SIZE;
else
size = TINY_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;
if(!get_prev_alloc(&alloc, &prev, g_allocs.large))
t_alloc *prev;
if (!get_prev_alloc(&alloc, &prev, g_allocs.large))
return ;
if(alloc == g_allocs.large)
if (alloc == g_allocs.large)
g_allocs.large = alloc->next;
else if(prev)
else if (prev)
prev->next = alloc->next;
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 ;
alloc = (t_alloc *)ptr - 1;
if(free_prealloc(alloc, &g_allocs.tiny, 0))
if (free_prealloc(alloc, &g_allocs.tiny, 0))
return ;
if(free_prealloc(alloc, &g_allocs.small, 1))
if (free_prealloc(alloc, &g_allocs.small, 1))
return ;
free_large(alloc);
}