Changeset 36


Ignore:
Timestamp:
Nov 5, 2011, 1:45:20 PM (12 years ago)
Author:
roman
Message:
 
Location:
trunk/Utilities/VirtualHeapPtr
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.cpp

    r34 r36  
    7272        _ATLTRY
    7373        {
    74                 p[-1] = n;
     74                p[-1] = n + 1;
    7575        }
    7676        _ATLCATCHALL()
     
    8888        _ATLTRY
    8989        {
    90                 p[2] = n;
     90                p[2] = n + 1;
    9191        }
    9292        _ATLCATCHALL()
  • trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.h

    r35 r36  
    77#pragma once
    88
    9 #include <psapi.h>
    10 
    11 #pragma comment(lib, "psapi.lib")
    12 
    139////////////////////////////////////////////////////////////
    1410// CGlobalVirtualAllocator
     
    2420                m_nPageSize(4 << 10) // 4L
    2521        {
    26                 PERFORMANCE_INFORMATION Information;
    27                 ZeroMemory(&Information, sizeof Information);
    28                 ATLVERIFY(GetPerformanceInfo(&Information, sizeof Information));
    29                 m_nPageSize = Information.PageSize;
     22                SYSTEM_INFO Information;
     23                GetSystemInfo(&Information);
     24                m_nPageSize = Information.dwPageSize;
    3025                ATLASSERT(m_nPageSize);
    3126                ATLASSERT(!(m_nPageSize & (m_nPageSize - 1)));
     
    171166                VOID* m_pvData;
    172167                SIZE_T m_nDataSize;
     168                VOID* m_pvPaddingData;
     169                SIZE_T m_nPaddingDataSize;
    173170
    174171        public:
     
    178175                        ATLASSERT(nDataSize > 0);
    179176                        const SIZE_T nPageSize = g_GlobalVirtualAllocator.GetPageSize();
     177                        const SIZE_T nAlignedDataSize = g_GlobalVirtualAllocator.Align(nDataSize);
    180178                        const SIZE_T nAllocationSize =
    181179                                g_GlobalVirtualAllocator.Align(sizeof (CDescriptor)) +
    182180                                CTraits::g_nHeadSanityPageCount * nPageSize +
    183                                 g_GlobalVirtualAllocator.Align(nDataSize) +
     181                                nAlignedDataSize +
    184182                                CTraits::g_nTailSanityPageCount * nPageSize +
    185183                                0;
     
    189187                        DWORD nCurrentProtection;
    190188                        ATLVERIFY(VirtualProtect(pDescriptor, sizeof *pDescriptor, PAGE_READWRITE, &nCurrentProtection));
    191                         pDescriptor->Initialize(nAllocationSize, nDataSize);
     189                        pDescriptor->m_nAllocationSize = nAllocationSize;
     190                        pDescriptor->m_pvData = (BYTE*) pDescriptor + g_GlobalVirtualAllocator.Align(sizeof *pDescriptor) + CTraits::g_nHeadSanityPageCount * nPageSize;
     191                        pDescriptor->m_nDataSize = nDataSize;
     192                        pDescriptor->m_nPaddingDataSize = nAlignedDataSize - nDataSize;
     193                        pDescriptor->m_pvPaddingData = (BYTE*) pDescriptor->m_pvData + nAlignedDataSize - pDescriptor->m_nPaddingDataSize;
     194                        ATLVERIFY(VirtualProtect(pDescriptor->m_pvData, pDescriptor->m_nDataSize, PAGE_READWRITE, &nCurrentProtection));
     195                        memset(pDescriptor->m_pvPaddingData, 0x77, pDescriptor->m_nPaddingDataSize);
     196                        ATLVERIFY(VirtualProtect(pDescriptor, sizeof *pDescriptor, PAGE_READONLY, &nCurrentProtection));
    192197                        return pDescriptor;
    193198                }
     
    198203                        return pDescriptor;
    199204                }
    200                 VOID Initialize(SIZE_T nAllocationSize, SIZE_T nDataSize)
    201                 {
    202                         const SIZE_T nPageSize = g_GlobalVirtualAllocator.GetPageSize();
    203                         m_nAllocationSize = nAllocationSize;
    204                         m_pvData = (BYTE*) this + g_GlobalVirtualAllocator.Align(sizeof *this) + CTraits::g_nHeadSanityPageCount * nPageSize;
    205                         m_nDataSize = nDataSize;
    206                         DWORD nCurrentProtection;
    207                         ATLVERIFY(VirtualProtect(m_pvData, m_nDataSize, PAGE_READWRITE, &nCurrentProtection));
    208                         //ATLASSERT(!IsBadWritePtr(m_pvData, m_nDataSize));
    209                         ATLVERIFY(VirtualProtect(this, sizeof *this, PAGE_READONLY, &nCurrentProtection));
    210                 }
     205                //VOID Initialize()
    211206                VOID Terminate()
    212207                {
     208                        _ATLTRY
     209                        {
     210                                ATLENSURE_THROW(IsPaddingValid(), HRESULT_FROM_WIN32(ERROR_INVALID_DATA));
     211                        }
     212                        _ATLCATCHALL()
     213                        {
     214                        }
    213215                        ATLVERIFY(VirtualFree(this, 0, MEM_RELEASE));
    214216                }
     
    220222                {
    221223                        return m_nDataSize;
     224                }
     225                BOOL IsPaddingValid() const throw()
     226                {
     227                        BYTE* pnPaddingData = (BYTE*) m_pvPaddingData;
     228                        for(SIZE_T nIndex = m_nPaddingDataSize; nIndex > 0; nIndex--, pnPaddingData++)
     229                                if(*pnPaddingData != 0x77)
     230                                        return FALSE;
     231                        return TRUE;
    222232                }
    223233        };
Note: See TracChangeset for help on using the changeset viewer.