-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgarbage_collector.c
More file actions
82 lines (74 loc) · 1.65 KB
/
Copy pathgarbage_collector.c
File metadata and controls
82 lines (74 loc) · 1.65 KB
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
#include <stdio.h>
#include <stdlib.h>
#define ALLOCATE 0
#define FETCHPOINTERS 1
// Define a structure to store allocation information
typedef struct Allocation
{
void *ptr;
struct Allocation *next;
} t_Allocation;
// Function to free all tracked allocations
void free_all(t_Allocation *g_allocations)
{
t_Allocation *current;
t_Allocation *next;
current = g_allocations;
while (current)
{
next = current->next;
free(current->ptr);
free(current);
current = next;
}
g_allocations = NULL;
}
// Function to allocate memory and keep track of it
t_Allocation *my_malloc(size_t size, int FUNCTION)
{
static t_Allocation *g_allocations = NULL;
t_Allocation *new_alloc;
if (FUNCTION)
return (g_allocations);
new_alloc = (t_Allocation *)malloc(sizeof(t_Allocation));
if (!new_alloc)
{
fprintf(stderr, "Memory allocation failed\n");
free_all(g_allocations);
exit(EXIT_FAILURE);
}
new_alloc->ptr = malloc(size);
if (!new_alloc->ptr)
{
fprintf(stderr, "Memory allocation failed\n");
free_all(g_allocations);
exit(EXIT_FAILURE);
}
new_alloc->next = g_allocations;
g_allocations = new_alloc;
return (new_alloc);
}
// TEST------------------------------------------------
int main(void)
{
int *arr;
char *str;
int i;
i = 0;
arr = (int *)my_malloc(10 * sizeof(int), ALLOCATE)->ptr;
str = (char *)my_malloc(50 * sizeof(char), ALLOCATE)->ptr;
while (i < 10)
{
arr[i] = i;
i++;
}
snprintf(str, 50, "Hello, World!");
printf("Array: ");
i = 0;
while (i < 10)
printf("%d ", arr[i++]);
printf("\nString: %s\n", str);
free_all(my_malloc(0, FETCHPOINTERS));
return (0);
}
//----------------------------------------------------