-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarbage_alloc.c
83 lines (72 loc) · 2.17 KB
/
garbage_alloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* ************************************************************************** */
/* LE - / */
/* / */
/* garbage_alloc.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: simrossi <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2019/04/10 19:46:00 by simrossi #+# ## ## #+# */
/* Updated: 2019/04/11 10:17:26 by simrossi ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "garbage.h"
t_garblst *g_garbage = NULL;
/*
** free_g_garbage_lst:
**
** Iterate over each g_garbage node to free the entire list.
** Set the g_garbage variable to NULL.
*/
void free_g_garbage_lst(void)
{
t_garblst *next_garb;
while (g_garbage)
{
next_garb = g_garbage->next;
free(g_garbage);
g_garbage = next_garb;
}
g_garbage = NULL;
}
/*
** garbage_free:
**
** Free a given pointer. Remove the allocated
** pointer of the g_garbage list.
*/
void garbage_free(void *ptr)
{
t_garblst *prev_garb;
t_garblst *curr_garb;
prev_garb = NULL;
curr_garb = g_garbage;
while (curr_garb && curr_garb->allocated != ptr)
{
prev_garb = curr_garb;
curr_garb = curr_garb->next;
}
if (!curr_garb)
return ;
if (prev_garb)
prev_garb->next = curr_garb->next;
else
g_garbage = g_garbage->next;
free(curr_garb);
}
/*
** garbage_malloc:
**
** Allocated a single pointer of the given size.
** Store the new allocated pointer in the g_garbage list.
*/
void *garbage_malloc(size_t size)
{
t_garblst *garb_item;
if (!(garb_item = (t_garblst *)malloc(sizeof(t_garblst) + size)))
return (NULL);
garb_item->allocated = garb_item + 1;
garb_item->next = g_garbage;
g_garbage = garb_item;
return (garb_item + 1);
}