-
Notifications
You must be signed in to change notification settings - Fork 3
/
memory.c
100 lines (77 loc) · 1.71 KB
/
memory.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
99
100
/***************************************
$Header$
malloc/free wrappers to allow auditing.
***************************************/
/* COPYRIGHT */
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
static unsigned long bytes_in_use = 0;
static unsigned long max_bytes_in_use = 0;
/*++++++++++++++++++++++++++++++
Replacement for malloc()
++++++++++++++++++++++++++++++*/
void *Malloc(size_t n)
{
char *s, *r;
unsigned long *x;
size_t nn;
bytes_in_use += n;
if (bytes_in_use > max_bytes_in_use) {
max_bytes_in_use = bytes_in_use;
}
nn = n + 8;
s = (char *) malloc(nn);
x = (unsigned long *) s;
*x = n;
r = (void *) (s + 8);
return r;
}
/*++++++++++++++++++++++++++++++
Replacement for malloc()
++++++++++++++++++++++++++++++*/
void *Realloc(void *old, size_t n)
{
char *s, *r;
unsigned long *x;
size_t nn;
s = (char *)old - 8;
x = (unsigned long *) s;
bytes_in_use -= *x;
bytes_in_use += n;
if (bytes_in_use > max_bytes_in_use) {
max_bytes_in_use = bytes_in_use;
}
nn = n + 8;
s = (char *) realloc(s, nn);
x = (unsigned long *) s;
*x = n;
r = (void *) (s + 8);
return r;
}
/*++++++++++++++++++++++++++++++
Replacement for free()
++++++++++++++++++++++++++++++*/
void Free(void *x)
{
char *r, *s;
unsigned long *v;
size_t nn;
r = (char *) x;
s = (void *) (r - 8);
v = (unsigned long *) s;
nn = *v;
bytes_in_use -= nn;
free(s);
}
/*++++++++++++++++++++++++++++++
Print amount of memory used
++++++++++++++++++++++++++++++*/
void
print_memory_statistics(void)
{
fprintf(stderr,
"Bytes in use at end : %8lu\n"
"Maximum bytes in use : %8lu\n",
bytes_in_use, max_bytes_in_use);
}