fix realloc size detection when new size is larger than current chunk max size, add pthread mutex to make the functions thread safe
This commit is contained in:
5
main.c
5
main.c
@ -5,6 +5,11 @@ int main(void)
|
|||||||
{
|
{
|
||||||
void *ptr;
|
void *ptr;
|
||||||
|
|
||||||
|
printf("%zu\n", SMALL_CHUNK_SIZE);
|
||||||
ptr = malloc(1203);
|
ptr = malloc(1203);
|
||||||
|
show_alloc_mem();
|
||||||
|
ptr = realloc(ptr, 12045);
|
||||||
|
show_alloc_mem();
|
||||||
free(ptr);
|
free(ptr);
|
||||||
|
show_alloc_mem();
|
||||||
}
|
}
|
||||||
|
11
srcs/free.c
11
srcs/free.c
@ -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/04 18:52:39 by tomoron ### ########.fr */
|
/* Updated: 2024/12/05 17:01:32 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -111,9 +111,12 @@ void free(void *ptr)
|
|||||||
if (!ptr)
|
if (!ptr)
|
||||||
return ;
|
return ;
|
||||||
alloc = (t_alloc *)ptr - 1;
|
alloc = (t_alloc *)ptr - 1;
|
||||||
|
pthread_mutex_lock(&g_mallock);
|
||||||
if (free_prealloc(alloc, &g_allocs.tiny, 0))
|
if (free_prealloc(alloc, &g_allocs.tiny, 0))
|
||||||
return ;
|
pthread_mutex_unlock(&g_mallock);
|
||||||
if (free_prealloc(alloc, &g_allocs.small, 1))
|
else if (free_prealloc(alloc, &g_allocs.small, 1))
|
||||||
return ;
|
pthread_mutex_unlock(&g_mallock);
|
||||||
|
else
|
||||||
free_large(alloc);
|
free_large(alloc);
|
||||||
|
pthread_mutex_unlock(&g_mallock);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* 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/12/03 15:21:41 by tomoron ### ########.fr */
|
/* Updated: 2024/12/05 16:39:23 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,6 +16,7 @@
|
|||||||
# include <stddef.h>
|
# include <stddef.h>
|
||||||
# include <sys/mman.h>
|
# include <sys/mman.h>
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
|
# include <pthread.h>
|
||||||
# include "libft.h"
|
# include "libft.h"
|
||||||
|
|
||||||
# define PAGE_SIZE sysconf(_SC_PAGESIZE)
|
# define PAGE_SIZE sysconf(_SC_PAGESIZE)
|
||||||
@ -50,6 +51,7 @@ typedef struct s_allocations
|
|||||||
typedef unsigned long t_ul;
|
typedef unsigned long t_ul;
|
||||||
|
|
||||||
extern t_allocations g_allocs;
|
extern t_allocations g_allocs;
|
||||||
|
extern pthread_mutex_t g_mallock;
|
||||||
|
|
||||||
size_t align(size_t nb, size_t align_nb);
|
size_t align(size_t nb, size_t align_nb);
|
||||||
|
|
||||||
|
@ -6,16 +6,14 @@
|
|||||||
/* 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/03 19:01:33 by tomoron ### ########.fr */
|
/* Updated: 2024/12/05 17:01:08 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "includes/malloc.h"
|
#include "includes/malloc.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
t_allocations g_allocs;
|
t_allocations g_allocs;
|
||||||
|
pthread_mutex_t g_mallock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
t_alloc *get_suitable_addr_in_chunk(t_mem_chunk *chunk, size_t size);
|
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 *create_new_chunk(int is_small, t_mem_chunk **chunk, \
|
||||||
@ -84,17 +82,24 @@ static void *pre_allocated(size_t size, t_mem_chunk **chunk, int is_small)
|
|||||||
|
|
||||||
void *malloc(size_t size)
|
void *malloc(size_t size)
|
||||||
{
|
{
|
||||||
t_alloc *new;
|
t_alloc *res;
|
||||||
|
|
||||||
|
pthread_mutex_lock(&g_mallock);
|
||||||
if (size <= TINY_MALLOC)
|
if (size <= TINY_MALLOC)
|
||||||
return (pre_allocated(size, &(g_allocs.tiny), 0));
|
res = 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));
|
res = pre_allocated(size, &(g_allocs.small), 1);
|
||||||
new = get_memory(size + sizeof(t_alloc), 1);
|
else
|
||||||
if (!new)
|
{
|
||||||
return (0);
|
res = get_memory(size + sizeof(t_alloc), 1);
|
||||||
new->size = size;
|
if (res)
|
||||||
new->next = g_allocs.large;
|
{
|
||||||
g_allocs.large = new;
|
res->size = size;
|
||||||
return ((char *)new + sizeof(t_alloc));
|
res->next = g_allocs.large;
|
||||||
|
g_allocs.large = res;
|
||||||
|
res++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&g_mallock);
|
||||||
|
return (res);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/12/02 14:41:22 by tomoron #+# #+# */
|
/* Created: 2024/12/02 14:41:22 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/12/04 18:50:45 by tomoron ### ########.fr */
|
/* Updated: 2024/12/05 17:32:25 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -20,11 +20,15 @@ static void *realloc_recreate(t_alloc *alloc, size_t size)
|
|||||||
{
|
{
|
||||||
void *new;
|
void *new;
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&g_mallock);
|
||||||
new = malloc(size);
|
new = malloc(size);
|
||||||
|
pthread_mutex_lock(&g_mallock);
|
||||||
if (alloc->size < size)
|
if (alloc->size < size)
|
||||||
size = alloc->size;
|
size = alloc->size;
|
||||||
|
pthread_mutex_unlock(&g_mallock);
|
||||||
ft_memcpy(new, alloc + 1, size);
|
ft_memcpy(new, alloc + 1, size);
|
||||||
free(alloc + 1);
|
free(alloc + 1);
|
||||||
|
pthread_mutex_lock(&g_mallock);
|
||||||
return (new);
|
return (new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,8 +48,8 @@ static void *realloc_prealloc(t_alloc *alloc, t_mem_chunk *chunk, \
|
|||||||
alloc->size = size;
|
alloc->size = size;
|
||||||
return (alloc + 1);
|
return (alloc + 1);
|
||||||
}
|
}
|
||||||
if ((size > (size_t)TINY_CHUNK_SIZE && is_small == 0)
|
if ((size > (size_t)TINY_MALLOC && is_small == 0)
|
||||||
|| (size > SMALL_CHUNK_SIZE && is_small == 1)
|
|| (size > SMALL_MALLOC && is_small == 1)
|
||||||
|| (t_ul)(alloc->next) - (t_ul)(alloc + 1) < size)
|
|| (t_ul)(alloc->next) - (t_ul)(alloc + 1) < size)
|
||||||
return (realloc_recreate(alloc, size));
|
return (realloc_recreate(alloc, size));
|
||||||
chunk->space_left -= size - alloc->size;
|
chunk->space_left -= size - alloc->size;
|
||||||
@ -69,11 +73,12 @@ void *realloc(void *ptr, size_t size)
|
|||||||
if (!ptr)
|
if (!ptr)
|
||||||
return (malloc(size));
|
return (malloc(size));
|
||||||
alloc = (t_alloc *)ptr - 1;
|
alloc = (t_alloc *)ptr - 1;
|
||||||
|
pthread_mutex_lock(&g_mallock);
|
||||||
ptr = realloc_prealloc(alloc, g_allocs.tiny, 0, size);
|
ptr = realloc_prealloc(alloc, g_allocs.tiny, 0, size);
|
||||||
if (ptr)
|
if(!ptr)
|
||||||
return (ptr);
|
|
||||||
ptr = realloc_prealloc(alloc, g_allocs.small, 1, size);
|
ptr = realloc_prealloc(alloc, g_allocs.small, 1, size);
|
||||||
if (ptr)
|
if(!ptr)
|
||||||
|
ptr = realloc_large(alloc, size);
|
||||||
|
pthread_mutex_unlock(&g_mallock);
|
||||||
return(ptr);
|
return(ptr);
|
||||||
return (realloc_large(alloc, size));
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/11/30 12:19:34 by tomoron #+# #+# */
|
/* Created: 2024/11/30 12:19:34 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/12/03 18:50:25 by tomoron ### ########.fr */
|
/* Updated: 2024/12/05 17:05:48 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -94,9 +94,11 @@ void show_alloc_mem(void)
|
|||||||
size_t total;
|
size_t total;
|
||||||
|
|
||||||
total = 0;
|
total = 0;
|
||||||
|
pthread_mutex_lock(&g_mallock);
|
||||||
total += show_pre_allocated("TINY", g_allocs.tiny);
|
total += show_pre_allocated("TINY", g_allocs.tiny);
|
||||||
total += show_pre_allocated("SMALL", g_allocs.small);
|
total += show_pre_allocated("SMALL", g_allocs.small);
|
||||||
total += show_large();
|
total += show_large();
|
||||||
|
pthread_mutex_unlock(&g_mallock);
|
||||||
write(1, "Total : ", 8);
|
write(1, "Total : ", 8);
|
||||||
put_ulnbr_base(total, "0123456789");
|
put_ulnbr_base(total, "0123456789");
|
||||||
write(1, " bytes\n", 7);
|
write(1, " bytes\n", 7);
|
||||||
|
Reference in New Issue
Block a user