-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDalloc.hpp
More file actions
215 lines (190 loc) · 6.32 KB
/
Dalloc.hpp
File metadata and controls
215 lines (190 loc) · 6.32 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#pragma once
//Stop std::vector from checking to much
//#define _ITERATOR_DEBUG_LEVEL 0
#include <string>
#include<cassert>
#include<iostream>
#include <vector>
template<class T>
bool ErrMegToUser(T msg) {
static bool executed=false;
if (!executed) {
//executed = true;
std::cout << msg << '\n';
std::cout << "väntar på Enter"; std::cin.ignore(999, '\n');
}
return false;
}
template <class T>
std::string MsgAt(T file, size_t line) {
return std::string(" in ") + file + std::string(" at ") + std::to_string(line) + ' ';
}
template<class T>
class Dalloc;
template<class T>
class PoolObj;
template<class T>
bool CheckVector(PoolObj<T>* poolPtr);
template <class T>
class VectorHack;
template <class T>
void DestroyAllocatedList(VectorHack<T>* This);
//The PoolObj is allocated together with the normal object allocated.
//It is holding data about the object to make it possible to check errors in memory handling.
template<class T>
class PoolObj {
friend class Dalloc<T>;
friend bool CheckVector<T>(PoolObj<T>* poolPtr);
size_t n = (size_t)-1; //Size of item allocated(in T)
std::string allocFile = "";
size_t allocLine = (size_t)-1;
std::string deallocFile = std::string("");
size_t deallocLine = (size_t)-1;
PoolObj(size_t n, std::string name, int lineNumber) :
n(n),
allocFile(name),
allocLine(lineNumber) {}
T* Get() {
return reinterpret_cast<T*>((char*)this + sizeof(PoolObj<T>));
}
static PoolObj<T>* Get(T* ptr) {
return reinterpret_cast<PoolObj<T>*>(this - sizeof(PoolObj<T>));
}
std::string AllocIn() {
return std::string(" allocated ")
+ MsgAt(allocFile, allocLine);
}
};
template <class T>
class VectorHack {
friend void DestroyAllocatedList<T>(VectorHack<T>* This);
friend class Dalloc<T>;
std::vector<PoolObj<T>*> list;
//VectorHack() {
//}
~VectorHack() {
DestroyAllocatedList<T>(this);
}
//size_t size() {
// return list.size();
//}
};
template<class T>
class Dalloc {
std::allocator<char> byteAllocator;
std::allocator<T> stdalloc;
static VectorHack<T> allocatedList;
public:
#define STDVECTOR
#pragma region Behhövs för std::vecotr när jag kör med den!
#ifdef STDVECTOR
//En del är bortkommenterat då det inte behövdes när jag fått ordning på det
// och gjort #define _ITERATOR_DEBUG_LEVEL 0
using value_type = T;
//bool operator==(const Dalloc&) { return true; }
bool operator!=(const Dalloc&) { return false; }
//friend class Dalloc;
//template <class U>
//Dalloc(const Dalloc<U>& r) :stdalloc(r.stdalloc) { allocatedList = r.allocatedList; }
//template <class U>
//Dalloc& operator=(const Dalloc<U>& r) {
// stdalloc(r.stdalloc);
// allocatedList = r.allocatedList;
// return *this;
//}
//Only needed if std::vector is tested!
T* allocate(size_t n) {
return allocate(n, "", 40);
}
void deallocate(T* ptr, size_t n) {
deallocate(ptr, n, "XXX", 80);
}
#endif
#pragma region Behhövs för std::vecotr när jag kör med den!
T* allocate(size_t n, const char* file, int line) {
if (n == 0) {
ErrMegToUser("att allokera 0 bytes kan vara slöseri med minne!\n");
return nullptr;
}
PoolObj<T>* ptr = reinterpret_cast<PoolObj<T>*>(byteAllocator.allocate(sizeof(PoolObj<T>) + n * sizeof(T)));
new (ptr) PoolObj<T>(n, file, line);
memset(ptr->Get(), 1, n * sizeof(T)); // To be able to revognize uninitiated memory!
allocatedList.list.push_back(ptr);
return ptr->Get();
}
void deallocate(T* ptr, size_t n, const char* file, size_t line) {
if (!ptr)
return;
PoolObj<T>* poolPtr = reinterpret_cast<PoolObj<T>*>((char*)ptr - sizeof(PoolObj<T>));
auto findIt = std::find(allocatedList.list.begin(), allocatedList.list.end(), poolPtr);
if (findIt == allocatedList.list.end()) {
assert("" == std::string("Trying to delete a non-allocated item!") + MsgAt(file, line) + '\n');
return;
}
if (n != poolPtr->n) {
std::cout << std::string("deallocate N does not match allocate N \n")
+ poolPtr->AllocIn() + " deallocated " +MsgAt(file, line) + '\n';
__debugbreak();
}
auto aFile = poolPtr->allocFile;
auto aLine = poolPtr->allocLine;
auto deFile = poolPtr->deallocFile;
auto deLine = poolPtr->deallocLine;
if (deFile != "") {
auto msg = std::string("deleting twice ") + MsgAt(file, line);
msg += std::string("\nwas already deleted ") + MsgAt(deFile, deLine);
msg += std::string("\nwas allocated " + poolPtr->AllocIn() + '\n');
ErrMegToUser(msg);
__debugbreak();
return;
}
poolPtr->deallocFile = file;
poolPtr->deallocLine = line;
}
};
template<class T>
VectorHack<T> Dalloc<T>::allocatedList;
#undef allocate
#define allocate(x) allocate(x, __FILE__, __LINE__)
#undef deallocate
#define deallocate(x, y) deallocate(x, y, __FILE__, __LINE__)
#include "Dhelper.h"
template<class T>
bool CheckVector(PoolObj<T>* poolPtr) {
if (poolPtr->deallocFile == "") {
return ErrMegToUser(std::string("memory ") + poolPtr->AllocIn() + "was not deallocated!");
}
return true;
}
template<>
bool CheckVector<Dhelper>(PoolObj<Dhelper>* poolPtr) {
if (poolPtr->deallocFile == "") {
return ErrMegToUser(std::string("memory ") + poolPtr->AllocIn() + "was not deallocated!");
}
size_t n = poolPtr->n;
Dhelper* ptr = poolPtr->Get();
size_t i = 0;
i = DD;
i = NON;
i = 0;
for (; i < n; ++i,++ptr) {
if (ptr->FLAG != DD) break;
}
for (; i < n; ++i, ++ptr) {
if (ptr->FLAG != NON) break;
}
if (i != n) {
return ErrMegToUser(std::string("parts of the memory ") + poolPtr->AllocIn()
+ "\nand deallocated in " + MsgAt(poolPtr->allocFile, poolPtr->deallocLine)
+ "\nwas not destroyed correct!\n");
}
return true;
}
template <class T>
void DestroyAllocatedList(VectorHack<T>* This) {
std::vector<PoolObj<T>*>* allocList = &(This->list);
for (auto it : *allocList)
CheckVector(&*it);
//if (!CheckVector(&*it))
// return;
}