]> git.sesse.net Git - freerainbowtables/blob - Common/rt api/MemoryPool.cpp
7e17b2a6a49a39572bda55b4a79e374c946aeef0
[freerainbowtables] / Common / rt api / MemoryPool.cpp
1 /*
2  * freerainbowtables is a project for generating, distributing, and using
3  * perfect rainbow tables
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, 2011 James Nobis <frt@quelrod.net>
9  * Copyright 2010 uroskn
10  *
11  * This file is part of freerainbowtables.
12  *
13  * freerainbowtables 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  * freerainbowtables 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 freerainbowtables.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27 #include "MemoryPool.h"
28 #include "Public.h"
29
30 CMemoryPool::CMemoryPool()
31 {
32         m_pMem = NULL;
33         m_nMemSize = 0;
34
35         unsigned int nAvailPhys = GetAvailPhysMemorySize();
36         if (nAvailPhys < 16 * 1024 * 1024)
37         {
38                 nAvailPhys = 512 * 1024 * 1024; // There is atleast 256 mb available (Some Linux distros returns a really low GetAvailPhysMemorySize()
39         }
40         if (nAvailPhys < 16 * 1024 * 1024)
41                 m_nMemMax = nAvailPhys / 2;                                     // Leave some memory for CChainWalkSet
42         else
43                 m_nMemMax = nAvailPhys - 8 * 1024 * 1024;       // Leave some memory for CChainWalkSet  
44 }
45
46 CMemoryPool::~CMemoryPool()
47 {
48         if (m_pMem != NULL)     {
49 #ifdef _MEMORYDEBUG
50                 printf("Freeing %i bytes of memory\n", m_nMemSize);
51 #endif 
52                 delete [] m_pMem;
53                 m_pMem = NULL;
54                 m_nMemSize = 0;
55         }
56 }
57
58 unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, unsigned int& nAllocatedSize)
59 {
60         if (nFileLen <= m_nMemSize)     {
61                 nAllocatedSize = nFileLen;
62                 return m_pMem;
63         }
64
65         unsigned int nTargetSize;
66         if (nFileLen < m_nMemMax) {
67                 nTargetSize = nFileLen;
68         }
69         else {
70                 nTargetSize = m_nMemMax;
71         }
72         // Free existing memory
73         if (m_pMem != NULL)     {
74 #ifdef _MEMORYDEBUG
75                 printf("Freeing %i bytes of memory\n", m_nMemSize);
76 #endif 
77                 delete [] m_pMem;
78                 m_pMem = NULL;
79                 m_nMemSize = 0;
80         }
81
82         // Allocate new memory
83         //printf("allocating %u bytes memory\n", nTargetSize);
84         //      m_pMem = new unsigned char[nTargetSize];
85 #ifdef _MEMORYDEBUG
86                 printf("Allocating %i bytes of memory - ", nTargetSize);
87 #endif 
88
89         m_pMem = new (nothrow) unsigned char[nTargetSize];
90         while (m_pMem == NULL && nTargetSize >= 512 * 1024 * 1024 )     {
91 #ifdef _MEMORYDEBUG
92                 printf("failed!\n");
93                 printf("Allocating %i bytes of memory (backup) - ", nTargetSize);
94 #endif 
95            nTargetSize -= 16 * 1024 * 1024;
96            m_pMem = new (nothrow) unsigned char[nTargetSize];
97         }
98         if (m_pMem != NULL)     {
99 #ifdef _MEMORYDEBUG
100                 printf("success!\n");
101 #endif
102                 m_nMemSize = nTargetSize;
103                 nAllocatedSize = nTargetSize;
104                 return m_pMem;
105         }
106         else {
107                 nAllocatedSize = 0;
108                 return NULL;
109         }
110 }