This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapmemory.cpp
90 lines (81 loc) · 2.28 KB
/
heapmemory.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
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
#include <algorithm>
#include <iostream>
#include "heapmemory.h"
HeapChunk::HeapChunk(unsigned long startAddr, unsigned long size,
std::string callStack, HowardType *type) {
this->startAddr = startAddr;
this->size = size;
this->endAddr = startAddr + size;
this->callStack = callStack;
this->type = type;
}
unsigned long HeapChunk::getStartAddr() {
return this->startAddr;
}
unsigned long HeapChunk::getEndAddr() {
return this->endAddr;
}
unsigned long HeapChunk::getSize() {
return this->size;
}
std::string HeapChunk::getCallStack() {
return this->callStack;
}
HowardType * HeapChunk::getType() {
return this->type;
}
/* Heap */
Heap::Heap() {
this->heapChunks = new std::vector<HeapChunk*>();
}
//
// Functor: Helper to find heap chunks
struct FindHeapChunkByAddress: std::unary_function<HeapChunk*, bool> {
private:
unsigned long address;
public:
FindHeapChunkByAddress(const unsigned long addr) :
address(addr) {
}
bool operator()(HeapChunk* chunk) {
return chunk->getStartAddr() <= address
&& address <= chunk->getEndAddr();
}
};
std::vector<HeapChunk*>::iterator Heap::findHeapChunkByAddress(
unsigned long address) {
return std::find_if(this->heapChunks->begin(), this->heapChunks->end(),
FindHeapChunkByAddress(address));
}
bool Heap::addHeapChunk(HeapChunk*chunk) {
this->heapChunks->push_back(chunk);
return true;
}
void Heap::printHeap() {
std::vector<HeapChunk*>::iterator it = this->heapChunks->begin();
std::cout << "Heap:" << std::endl;
for (; this->heapChunks->end() != it; it++) {
std::cout << "\tmem chunk:" << std::endl;
std::cout << "\t\tstartAddr: " << std::hex << (*it)->getStartAddr()
<< " endAddr: " << std::hex << (*it)->getEndAddr() << " size: "
<< std::dec << (*it)->getSize() << " type: "
<< (*it)->getType()->getType() << " member: "
<< (*it)->getType()->getMembers()->size() << " cs: "
<< (*it)->getCallStack() << std::endl;
}
}
std::vector<HeapChunk*>::iterator Heap::getHeapChunksEndIt() {
return this->heapChunks->end();
}
bool Heap::removeHeapChunkByAddress(unsigned long address) {
std::vector<HeapChunk*>::iterator it = this->findHeapChunkByAddress(
address);
if (this->getHeapChunksEndIt() != it) {
HeapChunk *chunk = (*it);
delete chunk;
this->heapChunks->erase(it);
return true;
} else {
return false;
}
}