]> git.sesse.net Git - freerainbowtables/blobdiff - Client Applications/rcracki_mt/MemoryPool.cpp
UINT4 -> uint32
[freerainbowtables] / Client Applications / rcracki_mt / MemoryPool.cpp
index 2129daa1884970c007dc6628fd512deaeb6506aa..5c634c02df4c8cfa5515706731f76422b9c29805 100644 (file)
-/*
-   RainbowCrack - a general propose implementation of Philippe Oechslin's faster time-memory trade-off technique.
-
-   Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
-*/
-
-#include "MemoryPool.h"
-#include "Public.h"
-
-CMemoryPool::CMemoryPool(unsigned int bytesForChainWalkSet)
-{
-       m_pMem = NULL;
-       m_nMemSize = 0;
-
-       unsigned int nAvailPhys = GetAvailPhysMemorySize();
-       if (nAvailPhys < 32 * 1024 * 1024)
-       {
-               nAvailPhys = 256 * 1024 * 1024; // There is atleast 256 mb available (Some Linux distros returns a really low GetAvailPhysMemorySize())
-       }
-       
-       m_nMemMax = nAvailPhys - bytesForChainWalkSet;  // Leave memory for CChainWalkSet       
-
-       if (m_nMemMax < 16 * 1024 * 1024)
-               m_nMemMax = 16 * 1024 * 1024;
-
-}
-
-CMemoryPool::~CMemoryPool()
-{
-       if (m_pMem != NULL)
-       {
-               delete m_pMem;
-               m_pMem = NULL;
-               m_nMemSize = 0;
-       }
-}
-
-unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, unsigned int& nAllocatedSize)
-{
-       if (nFileLen <= m_nMemSize)
-       {
-               nAllocatedSize = nFileLen;
-               return m_pMem;
-       }
-
-       unsigned int nTargetSize;
-       if (nFileLen < m_nMemMax)
-               nTargetSize = nFileLen;
-       else
-               nTargetSize = m_nMemMax;
-
-       // Free existing memory
-       if (m_pMem != NULL)
-       {
-               delete m_pMem;
-               m_pMem = NULL;
-               m_nMemSize = 0;
-       }
-
-       // Allocate new memory
-       //printf("allocating %u bytes memory\n", nTargetSize);
-       m_pMem = new (nothrow) unsigned char[nTargetSize];
-       while (m_pMem == NULL && nTargetSize >= 32 * 1024 * 1024 )
-       {
-          nTargetSize -= 16 * 1024 * 1024;
-          m_pMem = new (nothrow) unsigned char[nTargetSize];
-       }
-
-       if (m_pMem != NULL)
-       {
-               m_nMemSize = nTargetSize;
-               nAllocatedSize = nTargetSize;
-               return m_pMem;
-       }
-       else
-       {
-               m_nMemSize = 0;
-               nAllocatedSize = 0;
-               return NULL;
-       }
-}
+/*\r
+ * rcracki_mt is a multithreaded implementation and fork of the original \r
+ * RainbowCrack\r
+ *\r
+ * Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>\r
+ * Copyright Martin Westergaard Jørgensen <martinwj2005@gmail.com>\r
+ * Copyright 2009, 2010 Daniël Niggebrugge <niggebrugge@fox-it.com>\r
+ * Copyright 2009, 2010 James Nobis <frt@quelrod.net>\r
+ * Copyright 2010 uroskn\r
+ *\r
+ * This file is part of rcracki_mt.\r
+ *\r
+ * rcracki_mt is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * rcracki_mt is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with rcracki_mt.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+\r
+#include "MemoryPool.h"\r
+#include "Public.h"\r
+\r
+CMemoryPool::CMemoryPool(unsigned int bytesSaved, bool bDebug, uint64 maxMem)\r
+{\r
+       m_pMem = NULL;\r
+       m_nMemSize = 0;\r
+       debug = bDebug;\r
+\r
+       uint64 nAvailPhys = GetAvailPhysMemorySize();\r
+\r
+       if ( debug )\r
+       {\r
+               #ifdef _WIN32\r
+                       printf( "Debug: nAvailPhys: %I64u\n", nAvailPhys );\r
+               #else\r
+                       printf( "Debug: nAvailPhys: %llu\n", nAvailPhys );\r
+               #endif\r
+               printf( "Debug: bytesSaved: %d\n", bytesSaved );\r
+       }\r
+\r
+       if ( maxMem > 0 && maxMem < nAvailPhys )\r
+               nAvailPhys = maxMem;\r
+       \r
+       m_nMemMax = nAvailPhys - bytesSaved;    // Leave memory for CChainWalkSet       \r
+\r
+       if (m_nMemMax < 16 * 1024 * 1024)\r
+               m_nMemMax = 16 * 1024 * 1024;\r
+}\r
+\r
+CMemoryPool::~CMemoryPool()\r
+{\r
+       if (m_pMem != NULL)\r
+       {\r
+               delete [] m_pMem;\r
+               m_pMem = NULL;\r
+               m_nMemSize = 0;\r
+       }\r
+}\r
+\r
+unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, uint64& nAllocatedSize)\r
+{\r
+       if (nFileLen <= m_nMemSize)\r
+       {\r
+               nAllocatedSize = nFileLen;\r
+               return m_pMem;\r
+       }\r
+\r
+       unsigned int nTargetSize;\r
+       if (nFileLen < m_nMemMax)\r
+               nTargetSize = nFileLen;\r
+       else\r
+               nTargetSize = m_nMemMax;\r
+\r
+       // Free existing memory\r
+       if (m_pMem != NULL)\r
+       {\r
+               delete [] m_pMem;\r
+               m_pMem = NULL;\r
+               m_nMemSize = 0;\r
+       }\r
+\r
+       // Allocate new memory\r
+       //printf("allocating %u bytes memory\n", nTargetSize);\r
+       m_pMem = new (nothrow) unsigned char[nTargetSize];\r
+       while (m_pMem == NULL && nTargetSize >= 32 * 1024 * 1024 )\r
+       {\r
+               nTargetSize -= 16 * 1024 * 1024;\r
+               m_pMem = new (nothrow) unsigned char[nTargetSize];\r
+       }\r
+\r
+       if (m_pMem != NULL)\r
+       {\r
+               m_nMemSize = nTargetSize;\r
+               nAllocatedSize = nTargetSize;\r
+               return m_pMem;\r
+       }\r
+       else\r
+       {\r
+               m_nMemSize = 0;\r
+               nAllocatedSize = 0;\r
+               return NULL;\r
+       }\r
+}\r