jitcraft/Heap-inl.h
author David Anderson <dvander@alliedmods.net>
Wed Jul 11 01:12:54 2012 -0700 (2012-07-11)
changeset 69 dfce0539a77d
parent 59 b7d61ddb3c8d
permissions -rw-r--r--
Added error reporting API and fixed all known error paths.
     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     uintptr_t untagged = reinterpret_cast<uintptr_t>(address);
    31     assert(IsAligned(untagged, kObjectAlignment));
    32 
    33     Object *obj = reinterpret_cast<Object *>(address);
    34     obj->init(map);
    35     return obj;
    36 }
    37 
    38 inline Address
    39 Heap::newObject(size_t bytes, Tenure age)
    40 {
    41     if (bytes >= HeapChunk::kLargeObjectThreshold)
    42         return newLargeObject(bytes);
    43 
    44     if (age == Tenure_Stack) {
    45         if (Address addr = newStackObject(bytes))
    46             return addr;
    47     }
    48 
    49 #if 0
    50     if (age == Tenure_Default) {
    51         if (Address addr = newSpace_.allocate(bytes))
    52             return addr;
    53 
    54         scavenge();
    55         if (Address addr = newSpace_.allocate(bytes))
    56             return addr;
    57     }
    58 #endif
    59 
    60     // Try an old-space allocation.
    61     if (Address addr = newOldObject(bytes))
    62         return addr;
    63 
    64     // Uh-oh - perform a last-ditch collection.
    65     reclaim();
    66 
    67     return newOldObject(bytes);
    68 }
    69 
    70 inline Object *
    71 Heap::allocate(Handle<Map> map, size_t bytes, Tenure age)
    72 {
    73     size_t actualBytes = Align(bytes, kObjectAlignment);
    74 
    75     // All objects must be at least two words.
    76     assert(actualBytes >= sizeof(Object *) * 2);
    77 
    78     Address address = newObject(actualBytes, age);
    79     if (!address) {
    80         reportOutOfMemory();
    81         return NULL;
    82     }
    83     return InitAddressIntoObject(address, map);
    84 }
    85 
    86 inline Address
    87 Heap::newStackObject(size_t bytes)
    88 {
    89     return lifoSpace_.allocate(bytes);
    90 }
    91 
    92 inline Address
    93 Heap::newOldObject(size_t bytes)
    94 {
    95     return oldSpace_.allocate(bytes);
    96 }
    97 
    98 inline Address
    99 Heap::newLargeObject(size_t bytes)
   100 {
   101     if (Address address = largeSpace_.allocate(bytes))
   102         return address;
   103 
   104     reclaim();
   105 
   106     return largeSpace_.allocate(bytes);
   107 }
   108 
   109 inline void
   110 Object::write(Zone *zone, Object **ptr, Object *obj)
   111 {
   112     *ptr = obj;
   113 }
   114 
   115 }
   116 
   117 #endif // _include_jitcraft_heap_inl_h_
   118