Skip to content

Commit

Permalink
norm: resolve memory allocator norm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-cskn committed Mar 29, 2024
1 parent 4dc9788 commit 48c2010
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 230 deletions.
58 changes: 0 additions & 58 deletions memory-allocator/README.md

This file was deleted.

10 changes: 5 additions & 5 deletions memory-allocator/aborter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* aborter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hcoskun <hcoskun@student.42.fr> +#+ +:+ +#+ */
/* By: hamza <hamza@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/16 15:14:08 by hamza #+# #+# */
/* Updated: 2023/12/17 13:46:15 by hcoskun ### ########.fr */
/* Updated: 2024/03/25 15:35:05 by hamza ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,7 +17,7 @@
* @brief frees all memory blocks which allocated by using safe_malloc
* function but does not call abort functions.
*/
void free_memory(void)
void free_memory(void)
{
t_memory_block *memory_blocks;
t_memory_block *next;
Expand All @@ -26,8 +26,8 @@ void free_memory(void)
while (memory_blocks != NULL)
{
next = memory_blocks->next;
FREE_MEMORY(memory_blocks->ptr);
FREE_MEMORY(memory_blocks);
free(memory_blocks->ptr);
free(memory_blocks);
memory_blocks = next;
}
get_memory_blocks()->next = NULL;
Expand Down
16 changes: 8 additions & 8 deletions memory-allocator/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* allocator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hcoskun <hcoskun@student.42.fr> +#+ +:+ +#+ */
/* By: hamza <hamza@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/16 15:13:21 by hamza #+# #+# */
/* Updated: 2023/12/17 15:22:52 by hcoskun ### ########.fr */
/* Updated: 2024/03/25 15:35:20 by hamza ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -44,7 +44,7 @@ int append_memory_block(void *ptr)

if (!ptr)
return (BAD_EXIT);
new = ALLOCATE_MEMORY(sizeof(t_memory_block));
new = malloc(sizeof(t_memory_block));
if (!new)
return (BAD_EXIT);
*new = (t_memory_block){.ptr = ptr, .next = NULL};
Expand Down Expand Up @@ -76,7 +76,7 @@ int remove_memory_block(void *ptr)
if (cur->ptr == ptr)
{
prev->next = cur->next;
FREE_MEMORY(cur);
free(cur);
return (GOOD_EXIT);
}
prev = cur;
Expand All @@ -100,15 +100,15 @@ void *safe_malloc(int size)
{
void *ptr;

ptr = ALLOCATE_MEMORY(size);
ptr = malloc(size);
if (!ptr)
{
abort_memory();
return (NULL);
}
if (!append_memory_block(ptr))
{
FREE_MEMORY(ptr);
free(ptr);
abort_memory();
return (NULL);
}
Expand All @@ -122,7 +122,7 @@ void *safe_malloc(int size)
void safe_free(void *ptr)
{
if (!ptr)
return;
return ;
remove_memory_block(ptr);
FREE_MEMORY(ptr);
free(ptr);
}
38 changes: 19 additions & 19 deletions memory-allocator/allocator.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allocator.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hamza <hamza@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/25 15:17:28 by hamza #+# #+# */
/* Updated: 2024/03/25 15:20:02 by hamza ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef ALLOCATOR_H
# define ALLOCATOR_H

# ifndef TESTER_MODE
# define TESTER_MODE 0
# endif

# if TESTER_MODE
# include "tester.h"
# define ALLOCATE_MEMORY fake_malloc
# define FREE_MEMORY fake_free
# else
# define ALLOCATE_MEMORY malloc
# define FREE_MEMORY free
# endif

/*
A struct to store memory blocks which allocated
by using malloc function.
Yes, it is a linked list.
*/
typedef struct s_memory_block {
void *ptr;
struct s_memory_block *next;
} t_memory_block;
typedef struct s_memory_block
{
void *ptr;
struct s_memory_block *next;
} t_memory_block;

void *safe_malloc(int size);
void safe_free(void *ptr);
t_memory_block *get_memory_blocks(void);
void free_memory(void);
void abort_memory(void);
void register_pre_abort_func(void (*abort_func)(void));
void register_post_abort_func(void (*abort_func)(void));
void register_pre_abort_func(void (*abort_func)(void));
void register_post_abort_func(void (*abort_func)(void));

#endif
103 changes: 0 additions & 103 deletions memory-allocator/tester.c

This file was deleted.

7 changes: 0 additions & 7 deletions memory-allocator/tester.h

This file was deleted.

30 changes: 0 additions & 30 deletions memory-allocator/tester.sh

This file was deleted.

0 comments on commit 48c2010

Please sign in to comment.