-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafe.c
98 lines (82 loc) · 1.73 KB
/
safe.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include "safe.h"
#include "log.h"
static int __allocerr = 0;
static int __allocerr_reason = 0;
static void *localalloc[__LOCAL_ALLOC_SIZE] = {0};
static int safe = 0;
static size_t allocidx = 0;
static galloc *global_alloc = {0};
void __init_global_alloc() {
if(!(global_alloc = malloc(sizeof(galloc)))) exit(1);
}
void __registerret(void *ret, void *retbuf[1])
{
retbuf[0] = ret;
}
void *__getregisteredret(void *retbuf[1])
{
return retbuf[0];
}
void __setunsafe(void)
{
safe = 0;
}
void __setsafe(void)
{
safe = 1;
}
void __clearlocalalloc(void)
{
for(int i = 0; i < __LOCAL_ALLOC_SIZE; i++) localalloc[i] = NULL;
}
int __issafe(void)
{
return safe;
}
void *__pushtolocalalloc(void *ptr)
{
localalloc[allocidx++] = ptr;
logln(INFO, "|Global Allocator| Allocated memory safely and added it to the stack.");
return ptr;
}
void *__popfromlocalalloc(void)
{
void *ptr;
if(allocidx > 0 && ((ptr = localalloc[--allocidx]))) return ptr;
return NULL;
}
int *__getallocerr(void)
{
return &__allocerr;
}
int *__getallocerr_reason(void)
{
return &__allocerr_reason;
}
void __freelocalalloc(void)
{
void *tmpptr;
size_t count = 0;
while((tmpptr = __popfromlocalalloc()) != NULL) {
++count;
free(tmpptr);
tmpptr = NULL;
}
(count == 0) ?
logln(INFO, "|Global Allocator| Nothing to clear.") :
logln(INFO, "|Global Allocator| Freed memory safely. Cleared %lu objects", count);
}
void *__smalloc(size_t size)
{
void *ptr;
if(!(ptr = malloc(size))) goto cleanup;
if(allocidx < __LOCAL_ALLOC_SIZE) return __pushtolocalalloc(ptr);
__allocerr_reason = LOCAL_ALLOC_SIZE_EXCEEDED;
cleanup:
__allocerr = EMALLOC;
void *tmpptr;
while((tmpptr = __popfromlocalalloc()) != NULL) free(tmpptr);
return NULL;
}