]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/MemoryPool.cpp
merged paths
[freerainbowtables] / Client Applications / rcracki_mt / MemoryPool.cpp
1 /*
2  * rcracki_mt is a multithreaded implementation and fork of the original 
3  * RainbowCrack
4  *
5  * Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
6  * Copyright Martin Westergaard Jørgensen <martinwj2005@gmail.com>
7  * Copyright 2009, 2010 Daniël Niggebrugge <niggebrugge@fox-it.com>
8  * Copyright 2009, 2010 James Nobis <frt@quelrod.net>
9  * Copyright 2010 uroskn
10  *
11  * This file is part of rcracki_mt.
12  *
13  * rcracki_mt is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * rcracki_mt is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with rcracki_mt.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27 #include "MemoryPool.h"
28 #include "Public.h"
29
30 CMemoryPool::CMemoryPool(unsigned int bytesSaved, bool bDebug, uint64 maxMem)
31 {
32         m_pMem = NULL;
33         m_nMemSize = 0;
34         debug = bDebug;
35
36         uint64 nAvailPhys = GetAvailPhysMemorySize();
37
38         if ( debug )
39         {
40                 #ifdef _WIN32
41                         printf( "Debug: nAvailPhys: %I64u\n", nAvailPhys );
42                 #else
43                         printf( "Debug: nAvailPhys: %llu\n", nAvailPhys );
44                 #endif
45                 printf( "Debug: bytesSaved: %d\n", bytesSaved );
46         }
47
48         if ( maxMem > 0 && maxMem < nAvailPhys )
49                 nAvailPhys = maxMem;
50         
51         m_nMemMax = nAvailPhys - bytesSaved;    // Leave memory for CChainWalkSet       
52
53         if (m_nMemMax < 16 * 1024 * 1024)
54                 m_nMemMax = 16 * 1024 * 1024;
55 }
56
57 CMemoryPool::~CMemoryPool()
58 {
59         if (m_pMem != NULL)
60         {
61                 delete [] m_pMem;
62                 m_pMem = NULL;
63                 m_nMemSize = 0;
64         }
65 }
66
67 unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, uint64& nAllocatedSize)
68 {
69         if (nFileLen <= m_nMemSize)
70         {
71                 nAllocatedSize = nFileLen;
72                 return m_pMem;
73         }
74
75         unsigned int nTargetSize;
76         if (nFileLen < m_nMemMax)
77                 nTargetSize = nFileLen;
78         else
79                 nTargetSize = m_nMemMax;
80
81         // Free existing memory
82         if (m_pMem != NULL)
83         {
84                 delete [] m_pMem;
85                 m_pMem = NULL;
86                 m_nMemSize = 0;
87         }
88
89         // Allocate new memory
90         //printf("allocating %u bytes memory\n", nTargetSize);
91         m_pMem = new (nothrow) unsigned char[nTargetSize];
92         while (m_pMem == NULL && nTargetSize >= 32 * 1024 * 1024 )
93         {
94            nTargetSize -= 16 * 1024 * 1024;
95            m_pMem = new (nothrow) unsigned char[nTargetSize];
96         }
97
98         if (m_pMem != NULL)
99         {
100                 m_nMemSize = nTargetSize;
101                 nAllocatedSize = nTargetSize;
102                 return m_pMem;
103         }
104         else
105         {
106                 m_nMemSize = 0;
107                 nAllocatedSize = 0;
108                 return NULL;
109         }
110 }