]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/MemoryPool.cpp
2129daa1884970c007dc6628fd512deaeb6506aa
[freerainbowtables] / Client Applications / rcracki_mt / MemoryPool.cpp
1 /*
2    RainbowCrack - a general propose implementation of Philippe Oechslin's faster time-memory trade-off technique.
3
4    Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
5 */
6
7 #include "MemoryPool.h"
8 #include "Public.h"
9
10 CMemoryPool::CMemoryPool(unsigned int bytesForChainWalkSet)
11 {
12         m_pMem = NULL;
13         m_nMemSize = 0;
14
15         unsigned int nAvailPhys = GetAvailPhysMemorySize();
16         if (nAvailPhys < 32 * 1024 * 1024)
17         {
18                 nAvailPhys = 256 * 1024 * 1024; // There is atleast 256 mb available (Some Linux distros returns a really low GetAvailPhysMemorySize())
19         }
20         
21         m_nMemMax = nAvailPhys - bytesForChainWalkSet;  // Leave memory for CChainWalkSet       
22
23         if (m_nMemMax < 16 * 1024 * 1024)
24                 m_nMemMax = 16 * 1024 * 1024;
25
26 }
27
28 CMemoryPool::~CMemoryPool()
29 {
30         if (m_pMem != NULL)
31         {
32                 delete m_pMem;
33                 m_pMem = NULL;
34                 m_nMemSize = 0;
35         }
36 }
37
38 unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, unsigned int& nAllocatedSize)
39 {
40         if (nFileLen <= m_nMemSize)
41         {
42                 nAllocatedSize = nFileLen;
43                 return m_pMem;
44         }
45
46         unsigned int nTargetSize;
47         if (nFileLen < m_nMemMax)
48                 nTargetSize = nFileLen;
49         else
50                 nTargetSize = m_nMemMax;
51
52         // Free existing memory
53         if (m_pMem != NULL)
54         {
55                 delete m_pMem;
56                 m_pMem = NULL;
57                 m_nMemSize = 0;
58         }
59
60         // Allocate new memory
61         //printf("allocating %u bytes memory\n", nTargetSize);
62         m_pMem = new (nothrow) unsigned char[nTargetSize];
63         while (m_pMem == NULL && nTargetSize >= 32 * 1024 * 1024 )
64         {
65            nTargetSize -= 16 * 1024 * 1024;
66            m_pMem = new (nothrow) unsigned char[nTargetSize];
67         }
68
69         if (m_pMem != NULL)
70         {
71                 m_nMemSize = nTargetSize;
72                 nAllocatedSize = nTargetSize;
73                 return m_pMem;
74         }
75         else
76         {
77                 m_nMemSize = 0;
78                 nAllocatedSize = 0;
79                 return NULL;
80         }
81 }