src/Platform-Posix.cpp
author David Anderson <dvander@alliedmods.net>
Mon Dec 24 02:49:20 2012 -0800 (2012-12-24)
changeset 234 26e3035cdffc
parent 92 e3974e7360e9
child 251 ca99a81745fe
permissions -rw-r--r--
Get string literals working again.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft 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  * Foobar 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  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include <sys/mman.h>
    20 #include "Platform.h"
    21 
    22 using namespace ke;
    23 
    24 static inline int
    25 ToProtBits(OS::Protection prot)
    26 {
    27     switch (prot) {
    28       case OS::Prot_ReadWrite:
    29         return PROT_READ | PROT_WRITE;
    30       case OS::Prot_ReadWriteExecute:
    31         return PROT_READ | PROT_WRITE | PROT_EXEC;
    32       default:
    33         assert(prot == OS::Prot_ReadExecute);
    34         return PROT_READ | PROT_EXEC;
    35     }
    36 }
    37 
    38 Address
    39 OS::MapMemory(size_t amount, MapMode mode, Protection prot_)
    40 {
    41     int prot = ToProtBits(prot_);
    42     void *addr = mmap(NULL, amount, prot, MAP_ANON | MAP_PRIVATE, -1, 0);
    43     if (addr == MAP_FAILED)
    44         return NULL;
    45 
    46     return Address(addr);
    47 }
    48 
    49 bool
    50 OS::CommitMemory(Address address, size_t amount, Protection prot_)
    51 {
    52     return true;
    53 }
    54 
    55 void
    56 OS::UnmapMemory(Address address, size_t size)
    57 {
    58     munmap(address, size);
    59 }
    60 
    61 OS::AlignableMapping
    62 OS::MapAlignedMemory(size_t amount, size_t alignment, MapMode mode)
    63 {
    64     assert(amount >= alignment);
    65     assert(IsPowerOfTwo(alignment));
    66 
    67     if (alignment == kPageAlignment) {
    68         Address address = MapMemory(amount, mode, Prot_ReadWrite);
    69         if (!address)
    70             return AlignableMapping();
    71         return AlignableMapping(address, alignment, amount);
    72     }
    73 
    74     size_t requested = Min(amount * 2, amount + 2 * alignment);
    75     Address address = MapMemory(requested, mode, Prot_ReadWrite);
    76     if (!address)
    77         return AlignableMapping();
    78 
    79     size_t begin = 0;
    80     Address aligned = AlignedBase(address, alignment);
    81     if (!IsAligned(address, alignment)) {
    82         // Shave off the first part of the unaligned region.
    83         aligned += alignment;
    84         begin = size_t(aligned - address);
    85         UnmapAlignedMemory(address, begin);
    86     }
    87 
    88     if (requested - begin > amount) {
    89         // Shave off the last part of the unaligned region.
    90         UnmapAlignedMemory(aligned + amount, requested - amount - begin);
    91     }
    92 
    93     return AlignableMapping(aligned, alignment, amount);
    94 }
    95 
    96 void
    97 OS::UnmapAlignedMemory(Address address, size_t size)
    98 {
    99     OS::UnmapMemory(address, size);
   100 }
   101