-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryManager.cpp
More file actions
126 lines (117 loc) · 4.53 KB
/
MemoryManager.cpp
File metadata and controls
126 lines (117 loc) · 4.53 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: MemoryManager.cpp
* Author: Zafrir
*
* Created on 1 ינואר 2017, 16:18
*/
#include <iostream>
#include <vector>
#include <math.h>
#include "MemoryManager.h"
#include "MyAllocator.h"
MemoryManager::MemoryManager(size_t heapSize) {
pool = &MemPool::getInstane(heapSize);
}
MemoryManager::~MemoryManager() {
}
size_t MemoryManager::findBlock(void* ptr) {
for(int i = 0 ; blocksList.size() ; i++){
if(blocksList.at(i).getStartPtr()== ptr){
return i;
}
}
return -1;
}
void* MemoryManager::allocate(size_t size) {
size_t fixSize = pow(2,ceil(log2(size)));
if(freeList.empty()){
void* ptr = this->pool->getNextFreeLocationPtr(fixSize);
freeblock f(fixSize,ptr);
f.setAvailable(false);
if(blocksList.empty()){
cout<<"blockList is empty"<<endl;
blocksList.push_back(f);
}
else{
blocksList.push_back(f);
blocksList.at(blocksList.size()-2).setNext(&f);
}
return f.getStartPtr();
}
else{
for(int i = 0 ; i < freeList.size() ; i++){
if(freeList.at(i).getSize() == fixSize){
return freeList.at(i).getStartPtr();
}
if(freeList.at(i).getSize() > fixSize){
////here we need to disassemble chunks ///////
size_t oldSize = freeList.at(i).getSize();
size_t newSize = freeList.at(i).getSize() - fixSize;//TODO fix size for newSize
void* newPtr = (char*)freeList.at(i).getStartPtr() + fixSize;
freeblock f1(newSize , newPtr);
f1.setAvailable(true);
f1.setNext(freeList.at(i).getNext());
/////// update blockList//////////
size_t index = findBlock(freeList.at(i).getStartPtr());
blocksList.at(index).setSize(fixSize);
blocksList.at(index).setAvailable(false);
blocksList.at(index).setNext(&f1);
blocksList.insert(blocksList.begin()+index , f1);
///////// update freeList///////////
freeList.at(i).setSize(fixSize);
freeList.at(i).setNext(&f1);
freeList.push_back(f1);
freeblock tmp = freeList.at(i);
freeList.erase(freeList.begin()+i);
return tmp.getStartPtr();
}
}
void* ptr = this->pool->getNextFreeLocationPtr(fixSize);//allocate object on the heap
if(ptr!=nullptr){
freeblock f(fixSize,ptr);
f.setAvailable(false);
freeblock f1 = blocksList.at(blocksList.size()-1);
blocksList.pop_back();
f.setNext(&f1);
blocksList.push_back(f1);
blocksList.push_back(f);
}
else{ /////// if we need, here we need to merge chunks////////
for(int i = 0 ; i < freeList.size() ; i++){
freeblock temp=freeList.at(i);
size_t sumSize=fixSize;
int countIteration=0;
while(temp.getNext()->getAvailable()&& sumSize>0){
temp=*temp.getNext();
sumSize-=temp.getSize();
countIteration++;
}
if(sumSize<=0){
int index=findBlock(freeList.at(i).getStartPtr());
blocksList[index].setSize(fixSize);
blocksList[index].setAvailable(false);
blocksList[index].setNext(blocksList.at(index+countIteration-1).getNext());
for(int j=1;j<countIteration;j++){
blocksList.erase(blocksList.begin()+index+j);
}
//blocksList.erase(blocksList.begin()+index+countIteration);
for(int j=0;j<countIteration;j++){
freeList.erase(freeList.begin()+i+j);
}
return blocksList[index].getStartPtr();
}
}
}
}
}
MemoryManager::MemoryManager() {}
void MemoryManager::myfree(void* pointer) {
size_t index = findBlock(pointer);
blocksList[index].setAvailable(true);
freeList.push_back(blocksList.at(index));
}