-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsm.cpp
More file actions
340 lines (296 loc) · 14.3 KB
/
sm.cpp
File metadata and controls
340 lines (296 loc) · 14.3 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "sm.h"
#include<map>
/*******************************************************************************************/
/* Variables */
/*******************************************************************************************/
unsigned int m_initialPoolSize; // Initial pool size to be created.
//vector<int> m_PoolSizes; // Vector of all the pool sizes created so far
map<unsigned int, PoolData_t*> m_PoolMap; // Mapping of size and pool
/*******************************************************************************************/
/* Functions */
/*******************************************************************************************/
//----------------------------------------------------------------------------------------------
// @name : initStorageManager
//
// @description : Creates pools on the basis of initial pool sizes specified.
//
// @param initialPoolSize : This is the size of each pool (in bytes) that will be allocated
// at startup.
// @param numPools : Number of pools specified at startup
// @param pools : Array containing size of different pools that need to be created
// at startup
//
// @returns : Nothing
//----------------------------------------------------------------------------------------------
void initStorageManager(const unsigned initialPoolSize, int numPools, const unsigned int *pools)
{
m_initialPoolSize = initialPoolSize;
unsigned int totalClaimedMemory = 0;
printf("StorageManager:: Initial Pools- ");
for (int i = 0; i < numPools; i++)
{
PoolData_t *poolData = (PoolData_t *)malloc(sizeof(PoolData_t));
if (poolData == nullptr)
{
printf("\n\n**MEMORY ERROR: initStorageManager: Failed to create pool %u!!\n\n", pools[i]);
abort();
}
memset(poolData, 0, sizeof(poolData));
size_t sizeOfPoolInBytes = pools[i] * sizeof(char) * initialPoolSize;
totalClaimedMemory += sizeOfPoolInBytes;
char *ptr = (char *)malloc(sizeOfPoolInBytes);
initializePoolData(sizeOfPoolInBytes, ptr, pools[i], poolData);
printf("%d ", pools[i]);
}
printf("\nStorageManager:: Pool init complete\n");
printf("Total claimed memory: %u MB\n\n", totalClaimedMemory/1000/1000);
}
//----------------------------------------------------------------------------------------------
// @name : createNewPool
//
// @description : Creates a new memory pool. This is called if an allocation
// is requested for a size for which there exists no pools. This
// allocates memory to the pool and initializes its data. The size
// of this pool will depend on the m_initialPoolSize which was
// initialized at the time of initStorageManager().
//
// @param sizeId : The size of allocation (in bytes) which was requested. This
// size is not the same as the pool size.
//
// @returns : Nothing
//----------------------------------------------------------------------------------------------
void createNewPool(unsigned sizeId)
{
PoolData_t *poolData = (PoolData_t *)malloc(sizeof(PoolData_t));
if (poolData == nullptr)
{
printf("\n\n**MEMORY ERROR: createNewPool: Failed to create pool %u!!\n\n", sizeId);
abort();
}
memset(poolData, 0, sizeof(poolData));
size_t sizeOfPoolInBytes = sizeId * sizeof(char) * m_initialPoolSize;
char *ptr = (char *)malloc(sizeOfPoolInBytes);
initializePoolData(sizeOfPoolInBytes, ptr, sizeId, poolData);
printf("Created new pool : %u\n", sizeId);
}
//----------------------------------------------------------------------------------------------
// @name : initializePoolData
//
// @description : Initialize the Pool specific data.
//
// @param sizeOfPoolInBytes : Total size of the pool (bytes) that needs to be initialized.
// @param ptr : Pointer to start of this memory pool
// @param sizeId : Size ID of this pool. (e.g- For a request of 8 bytes, a pool of
// sizeId=8 will be created)
// @param poolData : Pool specific data that has to be initialized
//
// @returns : Nothing
//----------------------------------------------------------------------------------------------
void initializePoolData(size_t sizeOfPoolInBytes, char *ptr, unsigned int sizeId, PoolData_t *poolData)
{
poolData->poolSize = sizeId;
poolData->startAddress = ptr;
poolData->endAddress = ptr + sizeOfPoolInBytes;
poolData->totalSize = sizeOfPoolInBytes;
poolData->remainingSpace = sizeOfPoolInBytes;
poolData->totalBlocks = (poolData->endAddress - poolData->startAddress) / poolData->poolSize;
poolData->freeBlocks = poolData->totalBlocks;
poolData->usedBlocks = 0;
poolData->nextFreeBlock = -1;
poolData->nextFreeBlockInSequence = 0;
poolData->isFreedBlockBlockAvailable = false;
poolData->totalAllocationsFromThisPool = 0;
m_PoolMap[sizeId] = poolData;
}
/*******************************************************************************************/
void expandPool(unsigned size)
{
//TODO
}
//----------------------------------------------------------------------------------------------
// @name : displayPoolInfo
//
// @description : Displays detailed information of each pool present in the system.
//
// @returns : Nothing
//----------------------------------------------------------------------------------------------
void displayPoolInfo()
{
map<unsigned int, PoolData_t*>::iterator it = m_PoolMap.begin();
printf("\n\n");
while (it != m_PoolMap.end())
{
PoolData_t *poolData = it->second;
unsigned int poolSize = it->first;
printf("Pool %u\n", poolSize);
printf(" totalAllocationsFromThisPool : %u\n", poolData->totalAllocationsFromThisPool);
printf(" startAddress : 0x%x\n", poolData->startAddress);
printf(" endAddress : 0x%x\n", poolData->endAddress);
printf(" totalSize : %u bytes\n", poolData->totalSize);
printf(" remainingSpace : %u bytes\n", poolData->remainingSpace);
printf(" totalBlocks : %u\n", poolData->totalBlocks);
printf(" freeBlocks : %u\n", poolData->freeBlocks);
printf(" usedBlocks : %u\n", poolData->usedBlocks);
printf("\n");
it++;
}
printf("\n** Total Pools: %d **\n", m_PoolMap.size());
}
/*******************************************************************************************/
void destroyStorageManager()
{
//TODO
}
//----------------------------------------------------------------------------------------------
// @name : SM_alloc
//
// @description : This function is called from SM_ALLOC_ARRAY macro. It checks if
// a pool of sizeID=size is present. If yes, then memory is allocated
// from that pool. If not, then a new pool is created with sizeID=size
// and memory is allocated from it.
//
// @param size : Size of memory requested for heap allocation.
//
// @returns : Pointer to start of the allocated memory (from Pool)
//----------------------------------------------------------------------------------------------
void * SM_alloc(size_t size)
{
//printf("SM_alloc called for %d bytes\n", size);
/* Find which pool to use. If pool of required size not present, create a pool */
PoolData_t *poolData = m_PoolMap[size];
if (poolData != nullptr)
{
/* Check if this pool has enough free blocks */
if (poolData->freeBlocks == 0)
{
printf("ERROR: Pool %u exhausted!\n", size);
abort();
//expandPool(size);
}
}
else
{
createNewPool(size);
poolData = m_PoolMap[size];
}
char *ptr = nullptr;
if (poolData->isFreedBlockBlockAvailable && poolData->nextFreeBlock >= 0)
{
/* Allocating a block which was freed earlier. */
ptr = findAddressFromBlock(poolData->nextFreeBlock, poolData);
poolData->isFreedBlockBlockAvailable = false;
poolData->nextFreeBlock = -1;
//printf(">> From freed block, poolData->nextFreeBlock = %u\n", poolData->nextFreeBlock);
}
else
{
/* Allocating from free blocks in sequnce */
ptr = findAddressFromBlock(poolData->nextFreeBlockInSequence, poolData);
//printf(">> From sequence\n");
poolData->nextFreeBlockInSequence++;
}
//printf("Allocated 0x%x (block %u) in pool %u\n", ptr, poolData->usedBlocks, poolData->poolSize);
/* Handle statistics */
poolData->freeBlocks--;
poolData->usedBlocks++;
poolData->remainingSpace -= size;
poolData->totalAllocationsFromThisPool++;
return ptr;
}
//----------------------------------------------------------------------------------------------
// @name : SM_dealloc
//
// @description : This function is called from SM_DEALLOC macro. It marks the
// memory pointed to by ptr as free. Actual de-allocation DOES NOT
// takes place. This memory block is then re-claimed for future
// allocations from this pool.
//
// @param ptr : Pointer to memory that needs to be freed.
//
// @returns : Nothing
//----------------------------------------------------------------------------------------------
void SM_dealloc(void *ptr)
{
if (ptr == nullptr)
{
return;
}
/* Find the pool in which this address lies. */
unsigned int poolSize = findPoolFromAddress(ptr);
//printf("Deallocating 0x%x from pool %u\n", ptr, poolSize);
/* Mark this address as free */
PoolData_t *poolData = m_PoolMap[poolSize];
poolData->freeBlocks++;
poolData->usedBlocks--;
poolData->nextFreeBlock = findBlockFromAddress((char *)ptr, poolData);
//printf("Setting nextFreeBlock=%u in pool %u\n", poolData->nextFreeBlock, poolData->poolSize);
poolData->isFreedBlockBlockAvailable = true;
poolData->remainingSpace += poolSize;
//printf("Deallocated 0x%x block (%u) from pool %u\n", ptr, poolData->nextFreeBlock, poolSize);
}
//----------------------------------------------------------------------------------------------
// @name : findPoolFromAddress
//
// @description : This function is used to determine the Pool (sizeId) in which
// this memory block (address pointed by ptr) lies. this is used
// at the time of deallocation.
// TODO: Find some efficient way to determine in
// which pool does this ptr lie
//
// @param ptr : Pointer to memory whose pool needs to be determined.
//
// @returns : sizeId of the pool in which this memory resides.
//----------------------------------------------------------------------------------------------
unsigned int findPoolFromAddress(void *ptr)
{
map<unsigned int, PoolData_t*>::iterator it = m_PoolMap.begin();
while (it != m_PoolMap.end())
{
PoolData_t *poolData = it->second;
//printf(" Checking 0x%x in pool %u [0x%x, 0x%x] \n", ptr, poolData->poolSize, poolData->startAddress, poolData->endAddress);
if (ptr >= poolData->startAddress && ptr <= poolData->endAddress)
{
return poolData->poolSize;
}
it++;
}
/* If we reached here, it means something is wrong! */
printf("\n\n**ERROR: 0x%x not present in any of the memory pools!!\n\n", ptr);
abort();
}
//----------------------------------------------------------------------------------------------
// @name : findAddressFromBlock
//
// @description : Given the index (param block) and the pool, determine
// the address of memory. This implements an efficient way to
// determine memory address using simple arithmetic operation.
// This becomes possible because the pools are of fixed size.
// Each block (allocated memory) in a pool is at a fixed distance
// from each other. This helps in a fast memory allocation.
//
// @param block : Index of the block in this pool
// @param poolData : Pool specific data
//
// @returns : Pointer to memory corresponding to the given block.
//----------------------------------------------------------------------------------------------
char *findAddressFromBlock(unsigned int block, PoolData_t *poolData)
{
return poolData->startAddress + (block * poolData->poolSize);
}
//----------------------------------------------------------------------------------------------
// @name : findBlockFromAddress
//
// @description : Given the memory address and the Pool specific data, it determines
// the block i.e, index of memory chunk in the given Pool. This is
// used at the time of de-allocation to quickly mark the next free
// memory block in the given Pool.
//
// @param addr : Memory address
// @param poolData : Pool specific data
//
// @returns : Block corresponding to the given address (addr).
//----------------------------------------------------------------------------------------------
unsigned int findBlockFromAddress(char *addr, PoolData_t *poolData)
{
return (addr - poolData->startAddress) / poolData->poolSize;
}