-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFMManager.h
165 lines (137 loc) · 3.38 KB
/
FMManager.h
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
#include <iostream>
#include <new>
#include <assert.h>
using namespace std;
typedef struct tag_Header
{
char* bufferStart;
unsigned bufferSize;
void* head;
unsigned numOfFreeBlocks;
unsigned numOfAllocatedBlocks;
}Header;
typedef struct tag_Bookmark
{
void* nextEmptyBlock;
}Bookmark;
template<typename T>
class FMManager
{
private:
Header* m_head;
public:
FMManager();
FMManager(unsigned bufferSize);
~FMManager();
char* GetBufferStart();
T * Add();
void Remove(T *);
unsigned Count();
unsigned Capacity();
bool IsEmpty();
bool IsFull();
};
/** \brief
* FMManager default constructor
* Create the FMManager object. Initialize buffer to default size
*/
template<typename T>
FMManager<T>::FMManager()
{
m_head->bufferStart = NULL;
m_head->head = NULL;
cout << "\n\nERROR : Using the incorrect FMManager Constructor!!!\n";
}
/** \brief
* FMManager parameterized constructor
* Create the FMManager object. Initialize buffer to size provided as argument.
* \param
* bufferSize - The size of the buffer that needs to be allocated.
*/
template<typename T>
FMManager<T>::FMManager(unsigned bufferSize)
{
assert(sizeof(Bookmark) <= sizeof(T));
m_head = (Header*)malloc(sizeof(Header));
assert(m_head != NULL);
m_head->bufferSize = bufferSize;
m_head->bufferStart = (char*)malloc(sizeof(bufferSize * sizeof(char)));
assert(m_head->bufferStart != NULL);
m_head->numOfAllocatedBlocks = 0;
m_head->numOfFreeBlocks = 0 ;
m_head->head = m_head->bufferStart;
}
template<typename T>
FMManager<T>::~FMManager()
{
m_head->bufferStart = NULL;
delete m_head->bufferStart;
m_head->head = NULL;
delete m_head->head;
m_head = NULL;
delete m_head;
}
/** \brief
* Returns the start pointer of the buffer as char pointer.
* \return
* char*
*/
template<typename T>
char* FMManager<T>::GetBufferStart()
{
return m_head->bufferStart;
}
template<typename T>
unsigned FMManager<T>::Count()
{
return m_head->numOfAllocatedBlocks;
}
template<typename T>
unsigned FMManager<T>::Capacity()
{
return m_head->bufferSize/sizeof(T) ;
}
template<typename T>
bool FMManager<T>::IsEmpty()
{
return (m_head->numOfAllocatedBlocks == 0);
}
template<typename T>
bool FMManager<T>::IsFull()
{
return (m_head->numOfAllocatedBlocks == (m_head->bufferSize/sizeof(T)));
}
template <typename T>
T* FMManager<T>::Add()
{
void* result = m_head->head;
assert(m_head->numOfFreeBlocks >= 0);
if(m_head->numOfFreeBlocks == 0)
{
/**< There are no empty blocks. Allocate the next available space and return */
result = m_head->head;
m_head->head = (void*)((char*)m_head->head + sizeof(T));
}
else
{
/**< There are empty blocks. So allocate at head and update head to next block. */
Bookmark* curBlock = ((Bookmark*)(m_head->head));
m_head->head = curBlock->nextEmptyBlock;
m_head->numOfFreeBlocks--;
}
m_head->numOfAllocatedBlocks++;
assert(result != NULL);
return new(result) T();
}
template <typename T>
void FMManager<T>::Remove(T* obj)
{
assert(obj != NULL);
assert(m_head->numOfAllocatedBlocks >= 0);
obj->~T();
Bookmark* bkmk = (Bookmark*)((char*)obj + sizeof(Bookmark));
bkmk->nextEmptyBlock = m_head->head ;
m_head->head = (void*)bkmk;
m_head->numOfAllocatedBlocks--;
m_head->numOfFreeBlocks++;
}