jitcraft/Platform.h
author David Anderson <dvander@alliedmods.net>
Sun Jul 01 18:21:15 2012 -0700 (2012-07-01)
changeset 23 8a31742781c1
parent 11 a350c2cc736a
child 28 a9665857cfd5
permissions -rw-r--r--
Implement ++ and -- operators.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_jitcraft_platform_h_
    19 #define _include_jitcraft_platform_h_
    20 
    21 #include "Utility.h"
    22 
    23 namespace jc {
    24 
    25 namespace OS {
    26     enum Protection {
    27         Prot_ReadWrite,         // rw-, for the heap
    28         Prot_ReadExecute,       // r-x, for code
    29         Prot_ReadWriteExecute   // rwx, if disabling r-x for code.
    30     };
    31 
    32     enum MapMode {
    33         Map_Reserve,            // don't ask for actual pages yet
    34         Map_Commit              // ask for pages right away
    35     };
    36 
    37     Address MapMemory(size_t amount, MapMode mode, Protection prot);
    38     bool CommitMemory(Address address, size_t amount, Protection prot);
    39     void UnmapMemory(Address address, size_t size);
    40 
    41     class AlignableMapping {
    42         Address base_;
    43         Address aligned_;
    44         size_t size_;
    45 
    46       public:
    47         AlignableMapping(Address ptr, size_t alignment, size_t size)
    48           : base_(ptr),
    49             size_(size)
    50         {
    51             aligned_ = base_;
    52             if (!IsAligned(aligned_, alignment))
    53                 aligned_ = AlignedBase(aligned_, alignment) + alignment;
    54         }
    55 
    56         AlignableMapping() : base_(NULL), aligned_(NULL), size_(0)
    57         {
    58         }
    59 
    60         Address base() const {
    61             return base_;
    62         }
    63         bool isValid() const {
    64             return !!base_;
    65         }
    66         size_t size() const {
    67             return size_;
    68         }
    69         Address aligned() const {
    70             return aligned_;
    71         }
    72     };
    73 
    74     AlignableMapping MapAlignedMemory(size_t amount, size_t alignment, MapMode mode);
    75     void UnmapAlignedMemory(Address address, size_t size);
    76 
    77     // The default alignment of memory returned via MapMemory().
    78     // :TODO: better ifdefs
    79 #ifdef _MSC_VER
    80     static const size_t kMapAlignment = 64 * kKB;
    81 #else
    82     static const size_t kMapAlignment = 4 * kKB;
    83 #endif
    84 
    85     // Internal OS:: use only.
    86     Address MapMemoryAt(Address address, size_t amount, MapMode mode, Protection prot);
    87     AlignableMapping Shared_MapAlignableMemory(size_t amount, size_t alignment, MapMode mode);
    88 }
    89 
    90 }
    91 
    92 #endif // _include_jitcraft_platform_h_