]> git.sesse.net Git - freerainbowtables/blobdiff - Common/rt api/MemoryPool.cpp
Merge branch 'master' of git@gitorious.org:freerainbowtables-applications/freerainbow...
[freerainbowtables] / Common / rt api / MemoryPool.cpp
index 053db7a04b5fff229fe08a321768693291e7f9b9..a6745073f0e97e9953c510280ac32b405106de2f 100644 (file)
@@ -45,8 +45,10 @@ CMemoryPool::CMemoryPool()
 
 CMemoryPool::~CMemoryPool()
 {
-       if (m_pMem != NULL)
-       {
+       if (m_pMem != NULL)     {
+#ifdef _MEMORYDEBUG
+               printf("Freeing %i bytes of memory\n", m_nMemSize);
+#endif 
                delete [] m_pMem;
                m_pMem = NULL;
                m_nMemSize = 0;
@@ -55,21 +57,23 @@ CMemoryPool::~CMemoryPool()
 
 unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, unsigned int& nAllocatedSize)
 {
-       if (nFileLen <= m_nMemSize)
-       {
+       if (nFileLen <= m_nMemSize)     {
                nAllocatedSize = nFileLen;
                return m_pMem;
        }
 
        unsigned int nTargetSize;
-       if (nFileLen < m_nMemMax)
+       if (nFileLen < m_nMemMax) {
                nTargetSize = nFileLen;
-       else
+       }
+       else {
                nTargetSize = m_nMemMax;
-
+       }
        // Free existing memory
-       if (m_pMem != NULL)
-       {
+       if (m_pMem != NULL)     {
+#ifdef _MEMORYDEBUG
+               printf("Freeing %i bytes of memory\n", m_nMemSize);
+#endif 
                delete [] m_pMem;
                m_pMem = NULL;
                m_nMemSize = 0;
@@ -77,22 +81,29 @@ unsigned char* CMemoryPool::Allocate(unsigned int nFileLen, unsigned int& nAlloc
 
        // Allocate new memory
        //printf("allocating %u bytes memory\n", nTargetSize);
+       //      m_pMem = new unsigned char[nTargetSize];
+#ifdef _MEMORYDEBUG
+               printf("Allocating %i bytes of memory - ", nTargetSize);
+#endif 
+
        m_pMem = new (nothrow) unsigned char[nTargetSize];
-       while (m_pMem == NULL && nTargetSize >= 512 * 1024 * 1024 )
-       {
-               nTargetSize -= 16 * 1024 * 1024;
-               m_pMem = new (nothrow) unsigned char[nTargetSize];
+       while (m_pMem == NULL && nTargetSize >= 512 * 1024 * 1024 )     {
+#ifdef _MEMORYDEBUG
+               printf("failed!\n");
+               printf("Allocating %i bytes of memory (backup) - ", nTargetSize);
+#endif 
+          nTargetSize -= 16 * 1024 * 1024;
+          m_pMem = new (nothrow) unsigned char[nTargetSize];
        }
-
-       if (m_pMem != NULL)
-       {
+       if (m_pMem != NULL)     {
+#ifdef _MEMORYDEBUG
+               printf("success!\n");
+#endif
                m_nMemSize = nTargetSize;
                nAllocatedSize = nTargetSize;
                return m_pMem;
        }
-       else
-       {
-               m_nMemSize = 0;
+       else {
                nAllocatedSize = 0;
                return NULL;
        }