-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_pool.cpp
More file actions
41 lines (31 loc) · 870 Bytes
/
Copy pathmemory_pool.cpp
File metadata and controls
41 lines (31 loc) · 870 Bytes
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
//
// Created by ali-masa on 2/19/20.
//
#include "memory_pool.h"
void * MemoryPool::allocate() {
if(this->m_firstFree == NULL)
return NULL;
void* pointer = this->m_firstFree;
this->m_firstFree =
*static_cast<void**>(this->m_firstFree);
return pointer;
}
void MemoryPool::deallocate(void *ptr) {
void** ptrNextHead = reinterpret_cast<void**>(
((int*)ptr)
);
*ptrNextHead = this->m_firstFree;
this->m_firstFree = ptrNextHead;
}
#define POOL_PTR(i) reinterpret_cast<void**>(m_pool+(i)*elementSize)
MemoryPool::MemoryPool(void *pntr, size_t poolSize, size_t elementSize) {
size_t elements =
poolSize/sizeof(elementSize);
m_firstFree = (int*) poolSize;
size_t i;
for(i = 0; i < elements; ++i)
{
*POOL_PTR(i) = POOL_PTR(i + 1);
}
*POOL_PTR(i) = NULL;
}