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