fix norm and set chunk sizes
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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_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)
|
||||
{
|
||||
t_mem_chunk *chunk;
|
||||
@ -25,8 +29,8 @@ void *get_memory(size_t size, int no_write)
|
||||
-1, 0);
|
||||
if (chunk == MAP_FAILED)
|
||||
return (0);
|
||||
if(no_write)
|
||||
return(chunk);
|
||||
if (no_write)
|
||||
return (chunk);
|
||||
chunk->space_left = size - sizeof(t_mem_chunk);
|
||||
chunk->next = 0;
|
||||
return (chunk);
|
||||
@ -52,59 +56,7 @@ t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev,
|
||||
return (addr + 1);
|
||||
}
|
||||
|
||||
t_alloc *get_suitable_addr_in_chunk(t_mem_chunk *chunk, size_t size)
|
||||
{
|
||||
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)
|
||||
static void *pre_allocated(size_t size, t_mem_chunk **chunk, int is_small)
|
||||
{
|
||||
t_mem_chunk *tmp;
|
||||
t_mem_chunk *prev;
|
||||
@ -138,7 +90,6 @@ void *malloc(size_t size)
|
||||
return (pre_allocated(size, &(g_allocs.tiny), 0));
|
||||
else if (size <= SMALL_MALLOC)
|
||||
return (pre_allocated(size, &(g_allocs.small), 1));
|
||||
|
||||
new = get_memory(size + sizeof(t_alloc), 1);
|
||||
if (!new)
|
||||
return (0);
|
||||
|
Reference in New Issue
Block a user