src/Platform-Windows.cpp
author David Anderson <dvander@alliedmods.net>
Sat Dec 22 23:18:57 2012 -0800 (2012-12-22)
changeset 233 bbb498ccb4ac
parent 86 32ad0523b309
child 251 ca99a81745fe
permissions -rw-r--r--
Re-add enum support.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    18  */
    19 
    20 // :TODO: move into utility-style include
    21 #define WIN32_LEAN_AND_MEAN
    22 #include <Windows.h>
    23 #include "Platform.h"
    24 
    25 using namespace ke;
    26 
    27 static inline DWORD
    28 ToProtBits(OS::Protection prot)
    29 {
    30     switch (prot) {
    31       case OS::Prot_ReadWrite:
    32         return PAGE_READWRITE;
    33       case OS::Prot_ReadWriteExecute:
    34         return PAGE_EXECUTE_READWRITE;
    35       default:
    36         assert(prot == OS::Prot_ReadExecute);
    37         return PAGE_EXECUTE_READ;
    38     }
    39 }
    40 
    41 Address
    42 OS::MapMemory(size_t amount, MapMode mode, Protection prot_)
    43 {
    44     DWORD prot = ToProtBits(prot_);
    45     DWORD type = MEM_RESERVE;
    46     if (mode == Map_Commit)
    47         type |= MEM_COMMIT;
    48 
    49     return (Address)VirtualAlloc(NULL, amount, type, prot);
    50 }
    51 
    52 bool
    53 OS::CommitMemory(Address address, size_t amount, Protection prot_)
    54 {
    55     DWORD prot = ToProtBits(prot_);
    56     return !!VirtualAlloc(address, amount, MEM_COMMIT, prot);
    57 }
    58 
    59 void
    60 OS::UnmapMemory(Address address, size_t size)
    61 {
    62     VirtualFree(address, 0, MEM_RELEASE);
    63 }
    64 
    65 static Address
    66 MapMemoryAt(Address address, size_t amount, OS::MapMode mode, OS::Protection prot_)
    67 {
    68     DWORD bits = ToProtBits(prot_);
    69     DWORD flags = MEM_RESERVE;
    70     if (mode == OS::Map_Commit)
    71         flags |= MEM_COMMIT;
    72 
    73     return (Address)VirtualAlloc((LPVOID)address, amount, flags, bits);
    74 }
    75 
    76 OS::AlignableMapping
    77 OS::MapAlignedMemory(size_t amount, size_t alignment, MapMode mode)
    78 {
    79     Address addr = MapMemory(amount, mode, OS::Prot_ReadWrite);
    80     if (!addr)
    81         return AlignableMapping(NULL, alignment, 0);
    82 
    83     if (!IsAligned(addr, alignment)) {
    84         UnmapMemory(addr, amount);
    85 
    86         // Try to grab the next available aligned address.
    87         addr = AlignedBase(addr, alignment);
    88         addr = MapMemoryAt(addr + alignment, amount, mode, OS::Prot_ReadWrite);
    89         while (!addr) {
    90             // Try to over-allocate to get the right region. This can race
    91             // with other threads; we expect this to be rare. If this turns
    92             // out to be a problem, the API is set up such that we can just
    93             // return two pointers: the base, and the aligned address into
    94             // the over-allocated region.
    95             addr = MapMemory(amount + alignment, mode, OS::Prot_ReadWrite);
    96             if (!addr)
    97                 return AlignableMapping(NULL, alignment, 0);
    98             UnmapMemory(addr, amount + alignment);
    99             addr = AlignedBase(addr, alignment);
   100             addr = MapMemoryAt(addr + alignment, amount, mode, OS::Prot_ReadWrite);
   101         }
   102     }
   103 
   104     return AlignableMapping(addr, alignment, amount);
   105 }
   106 
   107 void
   108 OS::UnmapAlignedMemory(Address address, size_t size)
   109 {
   110     OS::UnmapMemory(address, size);
   111 }