]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/MemoryPool.cpp
remove old deprecated rcracki
[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 racrcki_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                 printf( "Debug: nAvailPhys: %llu\n", nAvailPhys );\r
41                 printf( "Debug: bytesSaved: %d\n", bytesSaved );\r
42         }\r
43 \r
44         if ( maxMem > 0 && maxMem < nAvailPhys )\r
45                 nAvailPhys = maxMem;\r
46         \r
47         m_nMemMax = nAvailPhys - bytesSaved;    // Leave memory for CChainWalkSet       \r
48 \r
49         if (m_nMemMax < 16 * 1024 * 1024)\r
50                 m_nMemMax = 16 * 1024 * 1024;\r
51 }\r
52 \r
53 CMemoryPool::~CMemoryPool()\r
54 {\r
55         if (m_pMem != NULL)\r
56         {\r
57                 delete [] m_pMem;\r
58                 m_pMem = NULL;\r
59                 m_nMemSize = 0;\r
60         }\r
61 }\r
62 \r
63 unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, uint64& nAllocatedSize)\r
64 {\r
65         if (nFileLen <= m_nMemSize)\r
66         {\r
67                 nAllocatedSize = nFileLen;\r
68                 return m_pMem;\r
69         }\r
70 \r
71         unsigned int nTargetSize;\r
72         if (nFileLen < m_nMemMax)\r
73                 nTargetSize = nFileLen;\r
74         else\r
75                 nTargetSize = m_nMemMax;\r
76 \r
77         // Free existing memory\r
78         if (m_pMem != NULL)\r
79         {\r
80                 delete [] m_pMem;\r
81                 m_pMem = NULL;\r
82                 m_nMemSize = 0;\r
83         }\r
84 \r
85         // Allocate new memory\r
86         //printf("allocating %u bytes memory\n", nTargetSize);\r
87         m_pMem = new (nothrow) unsigned char[nTargetSize];\r
88         while (m_pMem == NULL && nTargetSize >= 32 * 1024 * 1024 )\r
89         {\r
90            nTargetSize -= 16 * 1024 * 1024;\r
91            m_pMem = new (nothrow) unsigned char[nTargetSize];\r
92         }\r
93 \r
94         if (m_pMem != NULL)\r
95         {\r
96                 m_nMemSize = nTargetSize;\r
97                 nAllocatedSize = nTargetSize;\r
98                 return m_pMem;\r
99         }\r
100         else\r
101         {\r
102                 m_nMemSize = 0;\r
103                 nAllocatedSize = 0;\r
104                 return NULL;\r
105         }\r
106 }\r