Changeset 785 in dev


Ignore:
Timestamp:
Jan 30, 2011, 12:08:38 AM (13 years ago)
Author:
イグトランス (egtra)
Message:

旧Windows対応箇所で、毎回の関数呼出を行なわないようにした。タイミングによって、GetModuleHandle内のHeap関数呼出が落ちる現象に遭遇したため。

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/egtra/ab5.0/abdev/OldWindowsHelperImpl.cpp

    r784 r785  
    11#include "stdafx.h"
    22
    3 static OSVERSIONINFO GetVersionEx2()
     3#define GET_PROC_ADDRESS(hmod, functionName) reinterpret_cast<decltype(functionName)*>(GetProcAddress((hmod), #functionName))
     4
     5namespace
     6{
     7
     8OSVERSIONINFO GetVersionEx2()
    49{
    510    OSVERSIONINFO vi = {sizeof vi};
     
    1015static OSVERSIONINFO const vi = GetVersionEx2();
    1116
    12 #define KERNEL32 (GetModuleHandle(TEXT("kernel32")))
     17HMODULE GetKernelModule()
     18{
     19    static HMODULE hmod = GetModuleHandle(TEXT("kernel32"));
     20    return hmod;
     21}
    1322
    14 #define GET_PROC_ADDRESS(hmod, functionName) reinterpret_cast<decltype(functionName)*>(GetProcAddress((hmod), #functionName))
     23}
    1524
    1625extern "C"
     
    1827    BOOL WINAPI IsDebuggerPresent_Helper()
    1928    {
    20         if (auto IsDebuggerPresent_Real = GET_PROC_ADDRESS(KERNEL32, IsDebuggerPresent))
     29        static auto IsDebuggerPresent_Real = GET_PROC_ADDRESS(GetKernelModule(), IsDebuggerPresent);
     30        if (IsDebuggerPresent_Real)
    2131        {
    2232            return IsDebuggerPresent_Real();
     
    3040    void* WINAPI EncodePointer_Helper(void* p)
    3141    {
    32         if (auto EncodePointer_Real = GET_PROC_ADDRESS(KERNEL32, EncodePointer))
     42        static auto EncodePointer_Real = GET_PROC_ADDRESS(GetKernelModule(), EncodePointer);
     43        if (EncodePointer_Real)
    3344        {
    3445            return EncodePointer_Real(p);
     
    4253    void* WINAPI DecodePointer_Helper(void* p)
    4354    {
    44         if (auto DecodePointer_Real = GET_PROC_ADDRESS(KERNEL32, DecodePointer))
     55        static auto DecodePointer_Real = GET_PROC_ADDRESS(GetKernelModule(), DecodePointer);
     56        if (DecodePointer_Real)
    4557        {
    4658            return DecodePointer_Real(p);
     
    5466    BOOL WINAPI IsProcessorFeaturePresent_Helper(DWORD feture)
    5567    {
    56         if (auto IsProcessorFeaturePresent_Real = GET_PROC_ADDRESS(KERNEL32, IsProcessorFeaturePresent))
     68        static auto IsProcessorFeaturePresent_Real = GET_PROC_ADDRESS(GetKernelModule(), IsProcessorFeaturePresent);
     69        if (IsProcessorFeaturePresent_Real)
    5770        {
    5871            return IsProcessorFeaturePresent_Real(feture);
     
    6780    BOOL WINAPI HeapSetInformation_Helper(HANDLE hHeap, HEAP_INFORMATION_CLASS hic, void* information, SSIZE_T informationLength)
    6881    {
    69         if (auto HeapSetInformation_Real = GET_PROC_ADDRESS(KERNEL32, HeapSetInformation))
     82        static auto HeapSetInformation_Real = GET_PROC_ADDRESS(GetKernelModule(), HeapSetInformation);
     83        if (HeapSetInformation_Real)
    7084        {
    7185            return HeapSetInformation_Real(hHeap, hic, information, informationLength);
     
    7791    }
    7892
     93    namespace
     94    {
     95        typedef decltype(InitializeCriticalSectionAndSpinCount)* PInitializeCriticalSectionAndSpinCount;
     96
     97        // 関数内静的変数とSEHは両立できないようなので別の関数へ分離
     98        BOOL WINAPI InitializeCriticalSectionAndSpinCount_Helper2(PInitializeCriticalSectionAndSpinCount pfn, LPCRITICAL_SECTION lpcs, DWORD spinCount)
     99        {
     100            __try
     101            {
     102                if (pfn != nullptr)
     103                {
     104                    BOOL ret = pfn(lpcs, spinCount);
     105                    return vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
     106                        ? TRUE
     107                        : ret;
     108                }
     109                else
     110                {
     111                    ::InitializeCriticalSection(lpcs);
     112                    return TRUE;
     113                }
     114            }
     115            __except(EXCEPTION_EXECUTE_HANDLER)
     116            {
     117            }
     118            ::SetLastError(ERROR_OUTOFMEMORY);
     119            return FALSE;
     120        }
     121    }
     122
    79123    BOOL WINAPI InitializeCriticalSectionAndSpinCount_Helper(LPCRITICAL_SECTION lpcs, DWORD spinCount)
    80124    {
    81         __try
    82         {
    83             if (auto InitializeCriticalSectionAndSpinCount_Real = GET_PROC_ADDRESS(KERNEL32, InitializeCriticalSectionAndSpinCount))
    84             {
    85                 OSVERSIONINFO vi = {sizeof vi};
    86                 ::GetVersionEx(&vi);
    87                 BOOL ret = InitializeCriticalSectionAndSpinCount_Real(lpcs, spinCount);
    88                 return vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
    89                     ? TRUE
    90                     : ret;
    91             }
    92             else
    93             {
    94                 ::InitializeCriticalSection(lpcs);
    95                 return TRUE;
    96             }
    97         }
    98         __except(EXCEPTION_EXECUTE_HANDLER)
    99         {
    100         }
    101         return FALSE;
     125        static auto InitializeCriticalSectionAndSpinCount_Real = GET_PROC_ADDRESS(GetKernelModule(), InitializeCriticalSectionAndSpinCount);
     126        return InitializeCriticalSectionAndSpinCount_Helper2(InitializeCriticalSectionAndSpinCount_Real, lpcs, spinCount);
    102127    }
    103128
     
    109134    PSLIST_ENTRY WINAPI InterlockedPushEntrySList_Helper(PSLIST_HEADER head, PSLIST_ENTRY entry)
    110135    {
    111         auto InterlockedPushEntrySList_Real = GET_PROC_ADDRESS(KERNEL32, InterlockedPushEntrySList);
     136        static auto InterlockedPushEntrySList_Real = GET_PROC_ADDRESS(GetKernelModule(), InterlockedPushEntrySList);
    112137        assert(InterlockedPushEntrySList_Real != nullptr);
    113138        return InterlockedPushEntrySList_Real(head, entry);
     
    116141    PSLIST_ENTRY WINAPI InterlockedPopEntrySList_Helper(PSLIST_HEADER head)
    117142    {
    118         auto InterlockedPopEntrySList_Real = GET_PROC_ADDRESS(KERNEL32, InterlockedPopEntrySList);
     143        static auto InterlockedPopEntrySList_Real = GET_PROC_ADDRESS(GetKernelModule(), InterlockedPopEntrySList);
    119144        assert(InterlockedPopEntrySList_Real != nullptr);
    120145        return InterlockedPopEntrySList_Real(head);
Note: See TracChangeset for help on using the changeset viewer.