-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.c
98 lines (75 loc) · 1.38 KB
/
mem.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
/*
*
* mem.c
*
*
* 10/28/01
*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <memory.h>
#include "mem.h"
#ifdef MEMCHK
static int mcount = 0;
static int verb = 0;
/*
*
*/
void* MALLOC (int size)
{
void* a;
int realsize;
realsize = size + sizeof(int);
a = malloc (realsize);
mcount += realsize;
if (verb)
printf ("$$$ %d bytes MALLOCed at %08x\n", realsize, a);
*(int*)a = realsize;
return (void*)((char*)a + sizeof(int));
}
/*
*/
void FREE (void* address)
{
char* realaddress;
int size;
realaddress = ((char*)address) - sizeof(int);
size = *(int*)realaddress;
mcount -= size;
if (verb)
printf ("$$$ %d bytes FREEed at %08x\n", size, realaddress);
free (realaddress);
}
void imem () { mcount = 0; }
void pmem () { printf ("Memory unfreed now = %d bytes.\n", mcount); }
#else
void pmem (){}
void imem (){}
#endif
void* getmem (int size)
{
return (char*) MALLOC (size);
}
char* stringclone (char* s)
{
if (s) {
char* p = (char*) getmem (strlen (s) + 1);
strcpy (p, s);
return p;
}
return NULL;
}
void memfree (void* mem)
{
FREE (mem);
}
void* getmemc (int length, int elemsize)
{
int size = length * elemsize;
return (char*) MALLOC (size);
}
void relmem (void* mem)
{
FREE (mem);
}