2 * rcracki_mt is a multithreaded implementation and fork of the original
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
11 * This file is part of rcracki_mt.
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.
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.
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/>.
27 #include "MemoryPool.h"
30 CMemoryPool::CMemoryPool(unsigned int bytesSaved, bool bDebug, uint64 maxMem)
36 uint64 nAvailPhys = GetAvailPhysMemorySize();
41 printf( "Debug: nAvailPhys: %I64u\n", nAvailPhys );
43 printf( "Debug: nAvailPhys: %llu\n", nAvailPhys );
45 printf( "Debug: bytesSaved: %d\n", bytesSaved );
48 if ( maxMem > 0 && maxMem < nAvailPhys )
51 m_nMemMax = nAvailPhys - bytesSaved; // Leave memory for CChainWalkSet
53 if (m_nMemMax < 16 * 1024 * 1024)
54 m_nMemMax = 16 * 1024 * 1024;
57 CMemoryPool::~CMemoryPool()
67 unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, uint64& nAllocatedSize)
69 if (nFileLen <= m_nMemSize)
71 nAllocatedSize = nFileLen;
75 unsigned int nTargetSize;
76 if (nFileLen < m_nMemMax)
77 nTargetSize = nFileLen;
79 nTargetSize = m_nMemMax;
81 // Free existing memory
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 )
94 nTargetSize -= 16 * 1024 * 1024;
95 m_pMem = new (nothrow) unsigned char[nTargetSize];
100 m_nMemSize = nTargetSize;
101 nAllocatedSize = nTargetSize;