-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoryOperators.cpp
More file actions
54 lines (41 loc) · 1.45 KB
/
memoryOperators.cpp
File metadata and controls
54 lines (41 loc) · 1.45 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
#include<iostream>
#include<string>
#include <cassert>
#include"heap.h"
#include"memoryOperators.h"
using namespace std;
const uint32_t MEMSYSTEM_SIGNATURE = 0xBDDDF00Du;
const uint32_t MEMSYSTEM_ENDMARKER = 0xBAADF00Du;
struct allocHeader{
uint32_t signature;
int allocID;
int size;
heap* pointerHeap;
allocHeader * pNext;
allocHeader * pPrev;
};
static int allocCounterForAllHeaps = 0;
//overloading operators new and delete
void * operator new(size_t size, heap * pointerHeap){
size_t iRequestedBytes = size + sizeof(allocHeader);
char * pointerMemory = (char *)malloc(iRequestedBytes);
allocHeader * pHeader = (allocHeader *)pointerMemory;
pHeader->signature = MEMSYSTEM_SIGNATURE;
pHeader->pointerHeap = pointerHeap;
pHeader->size = size;
pHeader->allocID = pointerHeap->nextValidAllocID();
allocCounterForAllHeaps++;
char * startMemBlock = pointerMemory + sizeof(allocHeader);
int * endMarker = (int*) (startMemBlock + size);
*endMarker = MEMSYSTEM_ENDMARKER;
pointerHeap->AddAllocation(size, pHeader);
return startMemBlock;
}
void operator delete (void * pointerMemory) {
allocCounterForAllHeaps--;
allocHeader * pHeader = (allocHeader *) ( (char *)pointerMemory - sizeof(allocHeader) );
int * endMarker = (int*) ((char*)pointerMemory + pHeader->size);
assert (*endMarker == MEMSYSTEM_ENDMARKER);
pHeader->pointerHeap->RemoveAllocation(pHeader);
free(pHeader);
}