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