Skip to content

Commit

Permalink
type int8 to int16, change mutex name all check ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Ycaro02 authored and Ycaro02 committed Feb 22, 2024
1 parent 8f9ef58 commit d0302fb
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 55 deletions.
18 changes: 10 additions & 8 deletions include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# include <stddef.h> /* NULL */
# include <sys/mman.h> /* mmap */
# include <fcntl.h> /* open */
# include <sys/types.h> /* int8_t */
# include <sys/types.h> /* int16_t */
# include "basic_define.h" /* Color ... */
# include "../libft/libft.h" /* Libft without stdlib */

Expand Down Expand Up @@ -95,6 +95,8 @@ enum type_block_e {
GARBAGE_COLLECTOR_FREE=128, /* Free all page at the end of program */
};


# define SANITIZE_TYPE (PRE_ALLOCATE + ALLOCATION_TRACE + ENABLE_COLOR + DETECT_LEAK + GARBAGE_COLLECTOR_FREE)
typedef enum type_block_e e_type;

/**
Expand Down Expand Up @@ -124,7 +126,7 @@ typedef struct s_block {
} t_block;

typedef struct s_page {
int8_t type; /* type of page allocate TINY, SMALL, LARGE, debug storing info */
int16_t type; /* type of page allocate TINY, SMALL, LARGE, debug storing info */
int fd; /* fd deb file */
size_t size; /* size of total page, multiple of get_page_size */
size_t size_free; /* size free in bytes*/
Expand All @@ -140,7 +142,7 @@ typedef struct s_page {
extern t_page *g_data;

/* Global mutex to be thread safe */
extern pthread_mutex_t g_libft_malloc_mutex;
extern pthread_mutex_t g_malloc_mutex;

/********************************************************************
* Library function *
Expand All @@ -167,7 +169,7 @@ void free_meta_data();

/* free.c */
void free_meta_block(t_block* block, t_page *data);
int8_t page_empty(t_page *block);
int16_t page_empty(t_page *block);
void free_page(t_page *data);

/* page_gestion.c */
Expand All @@ -190,12 +192,12 @@ t_block *try_add_block(char type, size_t size);
void block_add_back(t_block **lst, t_block *block);

/* trace_alloc.c */
void write_block_info(t_block *block, size_t size, int8_t call, int fd);
void write_function_name(int8_t call, int fd);
void write_block_info(t_block *block, size_t size, int16_t call, int fd);
void write_function_name(int16_t call, int fd);

/* handle env.c */
int handle_env_variable(int8_t *special_flag);
int8_t check_debug_flag(int8_t flag);
int handle_env_variable(int16_t *special_flag);
int16_t check_debug_flag(int16_t flag);
int get_debug_fd();

/* need to declare getenv for bonus, basicly this declaration is in stdlib.h */
Expand Down
12 changes: 6 additions & 6 deletions src/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void free_meta_data()
if (!g_data)
return ;
ft_printf_fd(1, GREEN"Free meta data called\n"RESET);
pthread_mutex_lock(&g_libft_malloc_mutex);
pthread_mutex_lock(&g_malloc_mutex);
if (check_debug_flag(ALLOCATION_TRACE)) {
save_fd = get_debug_fd();
}
Expand All @@ -24,7 +24,7 @@ void free_meta_data()
if (save_fd != -1) {
close(save_fd);
}
pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
/* after investigate we don't need to destroy mutex, structure don't do some allocation
, destroy function just set invalid value to prevent bad call*/
}
Expand All @@ -33,7 +33,7 @@ void free_meta_data()
void free_page(t_page *data) { munmap(data, data->size); }

/* @brief check for empty page (all block size == 0) and not pre allocated page */
int8_t page_empty(t_page *data)
int16_t page_empty(t_page *data)
{
t_block *block;
if (data->type & PRE_ALLOCATE)
Expand Down Expand Up @@ -123,18 +123,18 @@ static int call_free(void *ptr)
void free(void *ptr)
{
// ft_printf_fd(1, "%sFree called %p %s\n", YELLOW, ptr, RESET);
pthread_mutex_lock(&g_libft_malloc_mutex);
pthread_mutex_lock(&g_malloc_mutex);
if (check_debug_flag(ALLOCATION_TRACE))
write_function_name(FREE_CALL, get_debug_fd()); /* Only for call history */
if (!ptr) {
ft_printf_fd(get_debug_fd(), "%sCall free NULL need to disable this%s\n", RED, RESET);
pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
return ;
}
else if (call_free(ptr) == FALSE) {
ft_printf_fd(2, RED"Free: Invalid pointer %p \n"RESET, ptr);
}
pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
// ft_printf_fd(2, "Invalid ptr - block_size == %p\n", ptr - BLOCK_SIZE);
// ft_printf_fd(2, "Invalid ptr == %p\n", ptr);
}
6 changes: 3 additions & 3 deletions src/handle_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static char *check_env_variable(char *to_check)
}

/* @brief basic check flag function return bool value */
inline int8_t check_debug_flag(int8_t flag) { return ((g_data->type & flag) == flag); }
inline int16_t check_debug_flag(int16_t flag) { return ((g_data->type & flag) == flag); }

/* @brief get debug file fd */
inline int get_debug_fd() { return (g_data->fd); }
Expand All @@ -29,7 +29,7 @@ void display_env_var(char *env, char *to_check)
ft_printf_fd(1, "%s%s %s: %s\n"RESET, color, to_check, str, env);
}

static int8_t bool_check_env(char *to_check, int8_t flag)
static int16_t bool_check_env(char *to_check, int16_t flag)
{
char *env = getenv(to_check);

Expand All @@ -38,7 +38,7 @@ static int8_t bool_check_env(char *to_check, int8_t flag)
return ((env != NULL) * flag);
}

int handle_env_variable(int8_t *special_flag)
int handle_env_variable(int16_t *special_flag)
{
int fd = -1;
char *trace_file = check_env_variable(MALLOC_TRACE_ENV);
Expand Down
43 changes: 23 additions & 20 deletions src/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@
/**
* Declare globale t_page pointer, the head of pages linked-list
*/
t_page *g_data = NULL;
pthread_mutex_t g_libft_malloc_mutex = PTHREAD_MUTEX_INITIALIZER;
// pthread_mutex_t g_libft_malloc_mutex;
t_page *g_data = NULL;
pthread_mutex_t g_malloc_mutex = PTHREAD_MUTEX_INITIALIZER;

static void handle_debug_flag(t_page *page)
{
if (check_debug_flag(ALLOCATION_TRACE)) {
ft_printf_fd(get_debug_fd(), GREEN"Create first small page (mmap called) %p\n"RESET, page); /* Only for call history */
}
if (check_debug_flag(GARBAGE_COLLECTOR_FREE)) {
atexit(free_meta_data);
}
if (check_debug_flag(DETECT_LEAK)) {
atexit(check_for_leak);
}
}
/* @brief Init first pre allocate page for tiny and small block */
inline static int8_t init_first_page()
static int16_t init_first_page()
{
/* Variable creation in scope to avoid useless stack allocation when g_data != NULL*/
if (!g_data) {
t_page *page;
int fd = -1;
int8_t special_flag = PRE_ALLOCATE;
int16_t special_flag = PRE_ALLOCATE;

fd = handle_env_variable(&special_flag);
page = init_page(TINY, 0, special_flag);
if (!page) {
pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
return (FALSE);
}
page->fd = fd;
Expand All @@ -31,19 +42,11 @@ inline static int8_t init_first_page()
/* first page->type contain all debug context no more needed */
page = init_page(SMALL, 0, PRE_ALLOCATE);
if (!page) {
pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
return (FALSE);
}
page_add_back(&g_data, page);
if (check_debug_flag(ALLOCATION_TRACE)) {
ft_printf_fd(get_debug_fd(), GREEN"Create first small page (mmap called) %p\n"RESET, page); /* Only for call history */
}
if (check_debug_flag(GARBAGE_COLLECTOR_FREE)) {
atexit(free_meta_data);
}
if (check_debug_flag(DETECT_LEAK)) {
atexit(check_for_leak);
}
handle_debug_flag(page);
}
return (TRUE);
}
Expand All @@ -60,15 +63,15 @@ void *malloc(size_t size)
e_type type;
t_block *block;

pthread_mutex_lock(&g_libft_malloc_mutex);
pthread_mutex_lock(&g_malloc_mutex);

if (size <= 0) { /* maybe to change malloc 1 for 0 input ? */
pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
return (NULL);
}

if(init_first_page() == FALSE) {
pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
return (NULL);
}

Expand All @@ -80,6 +83,6 @@ void *malloc(size_t size)
write_block_info(block, size, MALLOC_CALL, get_debug_fd());
}

pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
return (((void *) block) + BLOCK_SIZE);
}
5 changes: 5 additions & 0 deletions src/page_gestion.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ inline size_t get_page_size(e_type type, size_t size)
return (page_size[type >> 1]);
}

/*
* Don't worry about 'type >> 1', pre allocate/debug value in type, is given in different args to prevent this
* in init_first page when we call init_page function
*/

/** @brief Init page with mmmap call
* @param e_type enum represent the type of desired block
* @param size_t size: size of desired block allocation in bytes
Expand Down
12 changes: 6 additions & 6 deletions src/realloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ static void *exec_realloc(t_block *block, size_t size)
size_t i = 0, block_size = block->size;;
char *content = (char *)((void *)block + BLOCK_SIZE), *new_ptr = NULL;

pthread_mutex_unlock(&g_libft_malloc_mutex); /* unlock before malloc call */
pthread_mutex_unlock(&g_malloc_mutex); /* unlock before malloc call */
new_ptr = malloc(block->size + size);
pthread_mutex_lock(&g_libft_malloc_mutex); /* lock after malloc call cause unlock at the end of realloc, maybe can optimise that */
pthread_mutex_lock(&g_malloc_mutex); /* lock after malloc call cause unlock at the end of realloc, maybe can optimise that */

if (!new_ptr) {
return (NULL);
Expand All @@ -22,7 +22,7 @@ static void *exec_realloc(t_block *block, size_t size)
}

/** @brief Check data type and empty space in page to know if realloc is needed **/
static int8_t need_realloc(t_page *page, t_block *block, size_t size)
static int16_t need_realloc(t_page *page, t_block *block, size_t size)
{
size_t align = size;
size_t new_size = block->size + size;
Expand All @@ -44,7 +44,7 @@ static int8_t need_realloc(t_page *page, t_block *block, size_t size)
*/
static void *check_for_realloc_block(t_page *prev, t_page *current, t_block *block, void *ptr, size_t size)
{
int8_t realloc_needed = FALSE;
int16_t realloc_needed = FALSE;
while (block) {
if (ptr == (void *)block + BLOCK_SIZE) {

Expand Down Expand Up @@ -112,12 +112,12 @@ void *realloc(void *ptr, size_t size)
free(ptr); /* thread safe in free */
return (NULL);
}
pthread_mutex_lock(&g_libft_malloc_mutex);
pthread_mutex_lock(&g_malloc_mutex);
new_ptr = get_block_addr(ptr, size);
if (!new_ptr) {
ft_printf_fd(2, RED"Realloc invalid pointer %p\n"RESET,ptr);
}
pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
return (new_ptr);
}

Expand Down
12 changes: 6 additions & 6 deletions src/show_alloc_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void display_hex_data(t_block *block, void *ptr)
}
}

static void display_type(int8_t type)
static void display_type(int16_t type)
{
if (type & TINY) {
ft_printf_fd(1, TINY_NAME);
Expand All @@ -38,7 +38,7 @@ static void display_type(int8_t type)
/** @brief Display page function, iter on each block and display his information
* @param t_page *data, pointer on page to display
*/
static size_t print_bloc(t_page *data, int8_t hex_flag)
static size_t print_bloc(t_page *data, int16_t hex_flag)
{
int total = 0;
t_block *block;
Expand Down Expand Up @@ -67,13 +67,13 @@ void show_alloc_mem_hex()
{
t_page *tmp = g_data;

pthread_mutex_lock(&g_libft_malloc_mutex); /* lock before read g_data */
pthread_mutex_lock(&g_malloc_mutex); /* lock before read g_data */
display_line(NULL, '-');
while (tmp) {
print_bloc(tmp, 1);
tmp = tmp->next;
}
pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
}


Expand All @@ -83,7 +83,7 @@ void show_alloc_mem()
t_page *tmp;
size_t total = 0;

pthread_mutex_lock(&g_libft_malloc_mutex); /* lock before read g_data */
pthread_mutex_lock(&g_malloc_mutex); /* lock before read g_data */
tmp = g_data;
display_line(NULL, '-');
while (tmp) {
Expand All @@ -92,5 +92,5 @@ void show_alloc_mem()
}
ft_printf_fd(1, "Total: %U bytes\n", total);
display_line(NULL, '-');
pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
}
12 changes: 6 additions & 6 deletions src/trace_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void display_realloc_block(t_block *block, e_type type, size_t size, int
}
}

void write_function_name(int8_t call, int fd)
void write_function_name(int16_t call, int fd)
{
if (check_debug_flag(ENABLE_COLOR)) {
char *color = GREEN;
Expand All @@ -64,9 +64,9 @@ void write_function_name(int8_t call, int fd)
handle_reset_color(fd);
}

void write_block_info(t_block *block, size_t size, int8_t call, int fd)
void write_block_info(t_block *block, size_t size, int16_t call, int fd)
{
int8_t type = detect_type(block->size);
int16_t type = detect_type(block->size);

handle_add_color(fd, YELLOW);
if (type & TINY) {
Expand Down Expand Up @@ -110,13 +110,13 @@ void check_for_leak()
if (!g_data)
return ;

pthread_mutex_lock(&g_libft_malloc_mutex); /* lock before read g_data */
pthread_mutex_lock(&g_malloc_mutex); /* lock before read g_data */
while (current) {
block = current->block;
while (block) {
if (block->size != 0 ) {
char *color = RED;
int8_t type = detect_type(block->size);
int16_t type = detect_type(block->size);
if (type & TINY) {
color = YELLOW;
} else if (type & SMALL) {
Expand All @@ -129,5 +129,5 @@ void check_for_leak()
}
current = current->next;;
}
pthread_mutex_unlock(&g_libft_malloc_mutex);
pthread_mutex_unlock(&g_malloc_mutex);
}

0 comments on commit d0302fb

Please sign in to comment.