2 * freerainbowtables is a project for generating, distributing, and using
3 * perfect rainbow tables
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
11 * This file is part of freerainbowtables.
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.
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.
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/>.
27 #include "MemoryPool.h"
30 CMemoryPool::CMemoryPool()
35 unsigned long nAvailPhys = GetAvailPhysMemorySize();
36 if (nAvailPhys < 16 * 1024 * 1024)
38 nAvailPhys = 512 * 1024 * 1024; // There is atleast 256 mb available (Some Linux distros returns a really low GetAvailPhysMemorySize()
40 if (nAvailPhys < 16 * 1024 * 1024)
41 m_nMemMax = nAvailPhys / 2; // Leave some memory for CChainWalkSet
43 m_nMemMax = nAvailPhys - 8 * 1024 * 1024; // Leave some memory for CChainWalkSet
46 CMemoryPool::~CMemoryPool()
51 printf("Freeing %i bytes of memory\n", m_nMemSize);
59 unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, uint64& nAllocatedSize)
61 if (nFileLen <= m_nMemSize)
63 nAllocatedSize = nFileLen;
67 unsigned int nTargetSize;
68 if (nFileLen < m_nMemMax)
69 nTargetSize = nFileLen;
71 nTargetSize = m_nMemMax;
73 // Free existing memory
77 printf("Freeing %i bytes of memory\n", m_nMemSize);
84 // Allocate new memory
85 //printf("allocating %u bytes memory\n", nTargetSize);
87 printf("Allocating %i bytes of memory - ", nTargetSize);
90 m_pMem = new (nothrow) unsigned char[nTargetSize];
91 while (m_pMem == NULL && nTargetSize >= 512 * 1024 * 1024 )
95 printf("Allocating %i bytes of memory (backup) - ", nTargetSize);
97 nTargetSize -= 16 * 1024 * 1024;
98 m_pMem = new (nothrow) unsigned char[nTargetSize];
104 printf("success!\n");
106 m_nMemSize = nTargetSize;
107 nAllocatedSize = nTargetSize;