fix alloc between two allocations, fix free (uninitialized value)

This commit is contained in:
2024-12-01 19:22:43 +01:00
parent 7cddc28d28
commit a15705401e
3 changed files with 20 additions and 5 deletions

9
main.c
View File

@ -1,10 +1,17 @@
#include "srcs/includes/malloc.h" #include "srcs/includes/malloc.h"
#include <stdio.h>
int main(void) int main(void)
{ {
void *ptr; void *ptr;
ptr = ft_malloc(1013); ft_malloc(1013);
printf("%p\n",ft_malloc(100));
ft_malloc(100);
ptr = ft_malloc(1000);
ft_malloc(100);
ft_malloc(100);
ft_free(ptr); ft_free(ptr);
ft_malloc(123);
show_alloc_mem(); show_alloc_mem();
} }

View File

@ -6,7 +6,7 @@
/* 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/01 03:16:19 by tomoron ### ########.fr */ /* Updated: 2024/12/01 18:47:12 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -54,6 +54,7 @@ int free_prealloc(t_alloc *alloc, t_mem_bloc **main_bloc, size_t size)
t_mem_bloc *bloc; t_mem_bloc *bloc;
t_alloc *prev; t_alloc *prev;
prev = 0;
bloc = get_alloc_bloc(alloc, *main_bloc, size); bloc = get_alloc_bloc(alloc, *main_bloc, size);
if(!bloc) if(!bloc)
return(0); return(0);

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/12/01 03:05:08 by tomoron ### ########.fr */ /* Updated: 2024/12/01 19:21:27 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -58,14 +58,21 @@ t_alloc *get_suitable_addr_in_bloc(t_mem_bloc *bloc, size_t size)
size_t space_left; size_t space_left;
size_t free_space; size_t free_space;
tmp = bloc->first; //alloc when first alloc has been removed tmp = bloc->first;
space_left = bloc->space_left; space_left = bloc->space_left;
if((t_ul)bloc->first - (t_ul)(bloc + 1) >= size + sizeof(t_alloc))
{
tmp = bloc->first;
bloc->first = reserve_addr((void *)(bloc + 1), size, 0, bloc) - 1;
bloc->first->next = tmp;
return(bloc->first + 1);
}
while (tmp->next) while (tmp->next)
{ {
free_space = ((t_ul)tmp->next - (t_ul)tmp) - (tmp->size + sizeof(t_alloc)); free_space = ((t_ul)tmp->next - (t_ul)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(
(void *)((char *)tmp->next + tmp->size + sizeof(t_alloc)), (void *)((char *)tmp + tmp->size + sizeof(t_alloc)),
size, tmp, bloc)); size, tmp, bloc));
space_left -= free_space; space_left -= free_space;
tmp = tmp->next; tmp = tmp->next;