-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathxlMemoryPool.cpp
79 lines (70 loc) · 2.06 KB
/
xlMemoryPool.cpp
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
///***************************************************************************
// File: MemoryPool.cpp
//
// Purpose: A memory pool is an array of characters that is pre-allocated,
// and used as temporary memory by the caller. The allocation
// algorithm is very simple. When a thread asks for some memory,
// the index into the array moves forward by that many bytes, and
// a pointer is returned to the previous index before the pointer
// was advanced. When a call comes to free all of the memory, the
// pointer is set back to the beginning of the array.
//
// Each pool has MEMORYSIZE bytes of storage space available to it
//
// Platform: Microsoft Windows
//
///***************************************************************************
#include "xlMemoryPool.h"
//
// Constructor creates the memory to be used by the pool
// and starts the index at the beginning.
//
MemoryPool::MemoryPool(void)
{
m_rgchMemBlock = new char[MEMORYSIZE];
m_ichOffsetMemBlock = 0;
m_dwOwner = (DWORD)-1;
}
//
// An empty destructor - see reasoning below
//
MemoryPool::~MemoryPool(void)
{
}
//
// Unable to delete the memory block when we delete the pool,
// as it may be still be in use due to a GrowPools() call; this
// method will actually delete the pool's memory
//
void MemoryPool::ClearPool(void)
{
delete [] m_rgchMemBlock;
}
//
// Advances the index forward by the given number of bytes.
// Should there not be enough memory, or the number of bytes
// is not allowed, this method will return 0. Can be called
// and used exactly as malloc().
//
LPSTR MemoryPool::GetTempMemory(int cBytes)
{
LPSTR lpMemory;
if (m_ichOffsetMemBlock + cBytes > MEMORYSIZE || cBytes <= 0)
{
return 0;
}
else
{
lpMemory = (LPSTR) m_rgchMemBlock + m_ichOffsetMemBlock;
m_ichOffsetMemBlock += cBytes;
return lpMemory;
}
}
//
// Frees all the temporary memory by setting the index for
// available memory back to the beginning
//
void MemoryPool::FreeAllTempMemory()
{
m_ichOffsetMemBlock = 0;
}