src/Heap-inl.h
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 93 23189d937087
permissions -rw-r--r--
Initial implementation of module imports.
     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 #ifndef _include_jitcraft_heap_inl_h_
    20 #define _include_jitcraft_heap_inl_h_
    21 
    22 #include "Heap.h"
    23 #include "Spaces-inl.h"
    24 
    25 namespace ke {
    26 
    27 static inline Object *
    28 InitAddressIntoObject(Address address, Map *map)
    29 {
    30 #ifndef NDEBUG
    31     uintptr_t untagged = reinterpret_cast<uintptr_t>(address);
    32     assert(IsAligned(untagged, kObjectAlignment));
    33 #endif
    34 
    35     Object *obj = reinterpret_cast<Object *>(address);
    36     obj->init(map);
    37     return obj;
    38 }
    39 
    40 inline Address
    41 Heap::newObject(size_t bytes, Tenure age)
    42 {
    43     if (bytes >= HeapChunk::kLargeObjectThreshold)
    44         return newLargeObject(bytes);
    45 
    46     if (age == Tenure_Stack) {
    47         if (Address addr = newStackObject(bytes))
    48             return addr;
    49     }
    50 
    51 #if 0
    52     if (age == Tenure_Default) {
    53         if (Address addr = newSpace_.allocate(bytes))
    54             return addr;
    55 
    56         scavenge();
    57         if (Address addr = newSpace_.allocate(bytes))
    58             return addr;
    59     }
    60 #endif
    61 
    62     // Try an old-space allocation.
    63     if (Address addr = newOldObject(bytes))
    64         return addr;
    65 
    66     // Uh-oh - perform a last-ditch collection.
    67     reclaim();
    68 
    69     return newOldObject(bytes);
    70 }
    71 
    72 inline Object *
    73 Heap::allocate(Handle<Map> map, size_t bytes, Tenure age)
    74 {
    75     size_t actualBytes = Align(bytes, kObjectAlignment);
    76 
    77     // All objects must be at least two words.
    78     assert(actualBytes >= kMinObjectSize);
    79 
    80     Address address = newObject(actualBytes, age);
    81     if (!address) {
    82         reportOutOfMemory();
    83         return NULL;
    84     }
    85     return InitAddressIntoObject(address, map);
    86 }
    87 
    88 inline Address
    89 Heap::newStackObject(size_t bytes)
    90 {
    91     return lifoSpace_.allocate(bytes);
    92 }
    93 
    94 inline Address
    95 Heap::newOldObject(size_t bytes)
    96 {
    97     return oldSpace_.allocate(bytes);
    98 }
    99 
   100 inline Address
   101 Heap::newLargeObject(size_t bytes)
   102 {
   103     if (Address address = largeSpace_.allocate(bytes))
   104         return address;
   105 
   106     reclaim();
   107 
   108     return largeSpace_.allocate(bytes);
   109 }
   110 
   111 inline void
   112 Object::write(Zone *zone, Object **ptr, Object *obj)
   113 {
   114     *ptr = obj;
   115 }
   116 
   117 }
   118 
   119 #endif // _include_jitcraft_heap_inl_h_
   120