jitcraft/Spaces.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 03 00:36:48 2012 -0700 (2012-07-03)
changeset 33 95baf120f7d4
parent 12 75e385f68b7e
child 49 a92afa7484fd
permissions -rw-r--r--
Handle-ize BytecodeEmitter.
     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_spaces_h_
    19 #define _include_jitcraft_spaces_h_
    20 
    21 #include "Utility.h"
    22 #include "Platform.h"
    23 
    24 namespace jc {
    25 
    26 static const size_t kObjectAlignment = 8;
    27 
    28 // The heap stack is designed for structures which are known to not escape the
    29 // program stack, and can thus be quickly allocated in a LIFO manner.
    30 // 
    31 class HeapStack
    32 {
    33     OS::AlignableMapping map_;
    34     Address buffer_;
    35     Address pos_;
    36     Address end_;
    37 
    38   private:
    39     static const size_t kStackSize = 1024 * 1024;
    40 
    41   public:
    42     HeapStack();
    43     ~HeapStack();
    44     bool initialize();
    45 
    46   public:
    47     Address position() const {
    48         return pos_;
    49     }
    50     inline Address allocate(size_t nbytes);
    51     inline void unwind(Address to);
    52 };
    53 
    54 class HeapChunk
    55 {
    56     HeapChunk *next_;
    57 
    58     // Base address, from OS::AlignableMapping.
    59     Address base_;
    60 
    61     // End of the allocated region.
    62     Address end_;
    63 
    64     // Current allocation point.
    65     Address cursor_;
    66 
    67   public:
    68     static const size_t kMaxObjectSize = 1 * kGB;
    69     static const size_t kDefaultSize = 256 * kKB;
    70     static const size_t kAlignment = 256 * kKB;
    71 
    72     // All objects must be allocated in the first kAlignment bytes of the
    73     // chunk. Every object is at least 8 bytes (kPointerAlignment), so we
    74     // reserve one mark bit for every possible object address in the chunk
    75     // (including, for simplicity, unallocatable addresses at the start).
    76     //
    77     // The number of mark bits needed is:
    78     //
    79     // MaxObjectsPerChunk = kAlignment / kPointerAlignment 
    80     //                    = 32KB
    81     //
    82     // The number of bytes needed for these mark bits is:
    83     //
    84     // NumMarkBytes       = MaxObjectsPerChunk / 8
    85     //                    = 4KB
    86     //
    87     // The number of uint32s needed to store these bytes is:
    88     //
    89     // NumMarkWords       = NumMarkBytes / sizeof(uint32)
    90     //                    = 1024
    91     static const size_t kMaxObjectsPerChunk = kAlignment / kObjectAlignment;
    92     static const size_t kMarkBits = kMaxObjectsPerChunk;
    93     static const size_t kMarkBytes = kMarkBits / 8;
    94     static const size_t kMarkWords = kMarkBytes / sizeof(uint32);
    95 
    96     static inline size_t offsetToMarkBits()
    97     {
    98         return Align(sizeof(HeapChunk), kObjectAlignment);
    99     }
   100 
   101     static inline size_t offsetToFirst()
   102     {
   103         assert(IsAligned(kMarkBytes, kObjectAlignment));
   104         return offsetToMarkBits() + kMarkBytes;
   105     }
   106 
   107   public:
   108     static HeapChunk *New(HeapChunk *prev, size_t size);
   109     static void Destroy(HeapChunk *chunk);
   110 
   111   public:
   112     inline Address allocate(size_t nbytes);
   113 
   114     HeapChunk *next() {
   115         return next_;
   116     }
   117     Address base() {
   118         return base_;
   119     }
   120     Address end() {
   121         return end_;
   122     }
   123     size_t size() {
   124         return end() - base();
   125     }
   126     Address first() {
   127         return reinterpret_cast<Address>(this) + offsetToFirst();
   128     }
   129     uint32 *markBits() {
   130         return reinterpret_cast<uint32 *>(reinterpret_cast<Address>(this) +
   131                                           offsetToMarkBits());
   132     }
   133 };
   134 
   135 class OldSpace
   136 {
   137     HeapChunk *first_;
   138     HeapChunk *last_;
   139 
   140   public:
   141     OldSpace();
   142     ~OldSpace();
   143 
   144     bool initialize();
   145 
   146   public:
   147     inline Address allocate(size_t nbytes);
   148     Address allocateSlow(size_t nbytes);
   149 };
   150 
   151 }
   152 
   153 #endif // _include_jitcraft_spaces_h_