-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem.cpp
60 lines (52 loc) · 1.41 KB
/
mem.cpp
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
#include "piler2.h"
#define __STDC_LIMIT_MACROS
#include <stdint.h>
#ifdef _MSC_VER
#include <crtdbg.h>
#endif
static size_t AllocatedBytes;
static size_t PeakAllocatedBytes;
static const size_t numTrackingBytes = sizeof(size_t);
// Allocate memory, fail on error, track total
void *allocmem(size_t bytes)
{
// Ensure we can extend the allocation to include the size tracker
assert((SIZE_MAX - bytes) >= numTrackingBytes);
char *p = (char *) malloc(bytes + numTrackingBytes);
if (0 == p)
Quit("ERROR: Failed to allocate memory (%llu bytes).", bytes);
// Store the allocation size into the first size_t bytes
size_t *pSize = (size_t *) p;
*pSize = bytes;
AllocatedBytes += bytes;
if (AllocatedBytes > PeakAllocatedBytes)
PeakAllocatedBytes = AllocatedBytes;
// Advance the pointer beyond the tracker before returning
return p + numTrackingBytes;
}
void freemem(void *p)
{
if (0 == p)
return;
size_t *pSize = ((size_t *) p) - 1;
size_t numDataBytes = *pSize;
assert(numDataBytes <= AllocatedBytes);
AllocatedBytes -= numDataBytes;
// Also free the tracker
free((size_t *) p - 1);
}
void chkmem()
{
#ifdef _MSC_VER
assert(_CrtCheckMemory());
#endif
}
void *reallocmem(void *pOldAlloc, size_t bytes)
{
if (0 == pOldAlloc)
return allocmem(bytes);
size_t *pOldAllocSize = (size_t *) pOldAlloc - 1;
void *pNewAlloc = allocmem(bytes);
memcpy(pNewAlloc, pOldAlloc, *pOldAllocSize);
return pNewAlloc;
}