]> git.sesse.net Git - freerainbowtables/blob - Common/rt api/MemoryPool.cpp
merged paths
[freerainbowtables] / Common / rt api / 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()
11 {
12         m_pMem = NULL;
13         m_nMemSize = 0;
14
15         unsigned int nAvailPhys = GetAvailPhysMemorySize();
16         if (nAvailPhys < 16 * 1024 * 1024)
17         {
18                 nAvailPhys = 512 * 1024 * 1024; // There is atleast 256 mb available (Some Linux distros returns a really low GetAvailPhysMemorySize()
19         }
20         if (nAvailPhys < 16 * 1024 * 1024)
21                 m_nMemMax = nAvailPhys / 2;                                     // Leave some memory for CChainWalkSet
22         else
23                 m_nMemMax = nAvailPhys - 8 * 1024 * 1024;       // Leave some memory for CChainWalkSet  
24 }
25
26 CMemoryPool::~CMemoryPool()
27 {
28         if (m_pMem != NULL)     {
29 #ifdef _MEMORYDEBUG
30                 printf("Freeing %i bytes of memory\n", m_nMemSize);
31 #endif 
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                 nAllocatedSize = nFileLen;
42                 return m_pMem;
43         }
44
45         unsigned int nTargetSize;
46         if (nFileLen < m_nMemMax) {
47                 nTargetSize = nFileLen;
48         }
49         else {
50                 nTargetSize = m_nMemMax;
51         }
52         // Free existing memory
53         if (m_pMem != NULL)     {
54 #ifdef _MEMORYDEBUG
55                 printf("Freeing %i bytes of memory\n", m_nMemSize);
56 #endif 
57                 delete m_pMem;
58                 m_pMem = NULL;
59                 m_nMemSize = 0;
60         }
61
62         // Allocate new memory
63         //printf("allocating %u bytes memory\n", nTargetSize);
64         //      m_pMem = new unsigned char[nTargetSize];
65 #ifdef _MEMORYDEBUG
66                 printf("Allocating %i bytes of memory - ", nTargetSize);
67 #endif 
68
69         m_pMem = new (nothrow) unsigned char[nTargetSize];
70         while (m_pMem == NULL && nTargetSize >= 512 * 1024 * 1024 )     {
71 #ifdef _MEMORYDEBUG
72                 printf("failed!\n");
73                 printf("Allocating %i bytes of memory (backup) - ", nTargetSize);
74 #endif 
75            nTargetSize -= 16 * 1024 * 1024;
76            m_pMem = new (nothrow) unsigned char[nTargetSize];
77         }
78         if (m_pMem != NULL)     {
79 #ifdef _MEMORYDEBUG
80                 printf("success!\n");
81 #endif
82                 m_nMemSize = nTargetSize;
83                 nAllocatedSize = nTargetSize;
84                 return m_pMem;
85         }
86         else {
87                 nAllocatedSize = 0;
88                 return NULL;
89         }
90 }