Barst  2.0
A server that controls lab hardware.
mem pool.cpp
1 #include "mem pool.h"
2 
3 
4 
5 
6 CMemRing::CMemRing(__int64 ll_size, int nMinElems, int nMaxElems) : m_llSize(ll_size),
7  m_nMinElems(nMinElems), m_nMaxElems(nMaxElems)
8 {
9  SRingItem sRingItem;
10  InitializeCriticalSection(&m_hMemSafe);
11  for (int i = 0; i < m_nMinElems; ++i)
12  {
13  sRingItem.nCount = 0;
14  sRingItem.pMemory = malloc(m_llSize);
15  m_apMemory.push_back(sRingItem);
16  }
17 }
18 
19 CMemRing::~CMemRing()
20 {
21  DeleteCriticalSection(&m_hMemSafe);
22  for (int i = 0; i < m_apMemory.size(); ++i)
23  free(m_apMemory[i].pMemory);
24 }
25 
26 void *CMemRing::GetIndexMemory(int nIdx)
27 {
28  void * pHead = NULL;
29  EnterCriticalSection(&m_hMemSafe);
30  if (nIdx < m_apMemory.size())
31  {
32  pHead = m_apMemory[nIdx].pMemory;
33  m_apMemory[nIdx].nCount += 1;
34  }
35  LeaveCriticalSection(&m_hMemSafe);
36  return pHead;
37 }
38 
40 {
41  void * pHead = NULL;
42  EnterCriticalSection(&m_hMemSafe);
43  if (nIdx < m_apMemory.size())
44  pHead = m_apMemory[nIdx].pMemory;
45  LeaveCriticalSection(&m_hMemSafe);
46  return pHead;
47 }
48 
49 void CMemRing::ReleaseIndex(int nIdx)
50 {
51  EnterCriticalSection(&m_hMemSafe);
52  if (nIdx < m_apMemory.size())
53  m_apMemory[nIdx].nCount -= 1;
54  LeaveCriticalSection(&m_hMemSafe);
55 }
56 
57 void *CMemRing::GetFree(int *pnIdx)
58 {
59  void *pHead = NULL;
60  SRingItem sRingItem;
61  EnterCriticalSection(&m_hMemSafe);
62  for (int i = 0; i < m_apMemory.size(); ++i)
63  {
64  if (!m_apMemory[i].nCount)
65  {
66  *pnIdx = i;
67  pHead = m_apMemory[i].pMemory;
68  break;
69  }
70  }
71 
72  if (!pHead && m_apMemory.size() < m_nMaxElems) // we need to create new memory
73  {
74  sRingItem.nCount = 0;
75  pHead = sRingItem.pMemory = malloc(m_llSize);
76  *pnIdx = (int)m_apMemory.size();
77  m_apMemory.push_back(sRingItem);
78  }
79  LeaveCriticalSection(&m_hMemSafe);
80  return pHead;
81 }
82 
83 
84 //CMemPool::CMemPool()
85 //{
86 // m_aMemory.clear();
87 // InitializeCriticalSection(&m_rPoolSafe);
88 //}
89 //
90 //CMemPool::~CMemPool()
91 //{
92 // while (m_aMemory.size())
93 // {
94 // free(m_aMemory.back().pMemory);
95 // m_aMemory.pop_back();
96 // }
97 // DeleteCriticalSection(&m_rPoolSafe);
98 //}
99 //
100 //void* CMemPool::PoolAcquire(unsigned long long ullSize)
101 //{
102 // void* pMem= NULL;
103 // EnterCriticalSection(&m_rPoolSafe);
104 // for (int i= 0; i< m_aMemory.size();++i) // Find if we have that memory in pool
105 // {
106 // if (m_aMemory[i].ullSize == ullSize && !m_aMemory[i].bInUse)
107 // {
108 // pMem= m_aMemory[i].pMemory;
109 // m_aMemory[i].bInUse= true;
110 // break;
111 // }
112 // }
113 //
114 // if (!pMem) // If didn't find, create it
115 // {
116 // SMemoryItem sMemoryItem;
117 // sMemoryItem.bInUse= true;
118 // sMemoryItem.ullSize= ullSize;
119 // sMemoryItem.pMemory= malloc(ullSize);
120 // if (sMemoryItem.pMemory) // Success
121 // {
122 // pMem= sMemoryItem.pMemory;
123 // m_aMemory.push_back(sMemoryItem);
124 // }
125 // }
126 // LeaveCriticalSection(&m_rPoolSafe);
127 // return pMem;
128 //}
129 //
130 //void CMemPool::PoolRelease(const void* pHead)
131 //{
132 // if (!pHead)
133 // return;
134 // EnterCriticalSection(&m_rPoolSafe);
135 // for (int i= 0; i<m_aMemory.size(); ++i)
136 // {
137 // if (m_aMemory[i].pMemory== pHead)
138 // {
139 // m_aMemory[i].bInUse= false;
140 // break;
141 // }
142 // }
143 // LeaveCriticalSection(&m_rPoolSafe);
144 //}
145 //
146 //void CMemPool::PoolFreeUnused()
147 //{
148 // // Now delete the pool memory above used.
149 // EnterCriticalSection(&m_rPoolSafe);
150 // while (m_aMemory.size())
151 // {
152 // if (!m_aMemory.back().bInUse)
153 // {
154 // free(m_aMemory.back().pMemory);
155 // m_aMemory.pop_back();
156 // } else // Stop at the first in use memory.
157 // break;
158 // }
159 // LeaveCriticalSection(&m_rPoolSafe);
160 //}
void * GetIndexMemory(int nIdx)
Definition: mem pool.cpp:26
CMemRing(__int64 ll_size, int nMinElems, int nMaxElems)
Definition: mem pool.cpp:6
int nCount
Definition: mem pool.h:49
void * GetFree(int *pnIdx)
Definition: mem pool.cpp:57
void * pMemory
Definition: mem pool.h:47
void ReleaseIndex(int nIdx)
Definition: mem pool.cpp:49
const int m_nMaxElems
Definition: mem pool.h:82
const __int64 m_llSize
Definition: mem pool.h:78
const int m_nMinElems
Definition: mem pool.h:80
void * GetIndexMemoryUnsafe(int nIdx)
Definition: mem pool.cpp:39