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.
[email protected]
     1
/* vim: set ts=4 sw=4 tw=99 et:
[email protected]
     2
 *
[email protected]
     3
 * Copyright (C) 2012 David Anderson
[email protected]
     4
 *
[email protected]
     5
 * This file is part of JITCraft.
[email protected]
     6
 *
[email protected]
     7
 * JITCraft is free software: you can redistribute it and/or modify it under
[email protected]
     8
 * the terms of the GNU General Public License as published by the Free
[email protected]
     9
 * Software Foundation, either version 3 of the License, or (at your option)
[email protected]
    10
 * any later version.
[email protected]
    11
 * 
[email protected]
    12
 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
[email protected]
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
[email protected]
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
[email protected]
    15
 *
[email protected]
    16
 * You should have received a copy of the GNU General Public License along with
[email protected]
    17
 * JITCraft. If not, see http://www.gnu.org/licenses/.
[email protected]
    18
 */
[email protected]
    19
#ifndef _include_jitcraft_heap_inl_h_
[email protected]
    20
#define _include_jitcraft_heap_inl_h_
[email protected]
    21
[email protected]
    22
#include "Heap.h"
[email protected]
    23
#include "Spaces-inl.h"
[email protected]
    24
[email protected]
    25
namespace ke {
[email protected]
    26
[email protected]
    27
static inline Object *
[email protected]
    28
InitAddressIntoObject(Address address, Map *map)
[email protected]
    29
{
[email protected]
    30
#ifndef NDEBUG
[email protected]
    31
    uintptr_t untagged = reinterpret_cast<uintptr_t>(address);
[email protected]
    32
    assert(IsAligned(untagged, kObjectAlignment));
[email protected]
    33
#endif
[email protected]
    34
[email protected]
    35
    Object *obj = reinterpret_cast<Object *>(address);
[email protected]
    36
    obj->init(map);
[email protected]
    37
    return obj;
[email protected]
    38
}
[email protected]
    39
[email protected]
    40
inline Address
[email protected]
    41
Heap::newObject(size_t bytes, Tenure age)
[email protected]
    42
{
[email protected]
    43
    if (bytes >= HeapChunk::kLargeObjectThreshold)
[email protected]
    44
        return newLargeObject(bytes);
[email protected]
    45
[email protected]
    46
    if (age == Tenure_Stack) {
[email protected]
    47
        if (Address addr = newStackObject(bytes))
[email protected]
    48
            return addr;
[email protected]
    49
    }
[email protected]
    50
[email protected]
    51
#if 0
[email protected]
    52
    if (age == Tenure_Default) {
[email protected]
    53
        if (Address addr = newSpace_.allocate(bytes))
[email protected]
    54
            return addr;
[email protected]
    55
[email protected]
    56
        scavenge();
[email protected]
    57
        if (Address addr = newSpace_.allocate(bytes))
[email protected]
    58
            return addr;
[email protected]
    59
    }
[email protected]
    60
#endif
[email protected]
    61
[email protected]
    62
    // Try an old-space allocation.
[email protected]
    63
    if (Address addr = newOldObject(bytes))
[email protected]
    64
        return addr;
[email protected]
    65
[email protected]
    66
    // Uh-oh - perform a last-ditch collection.
[email protected]
    67
    reclaim();
[email protected]
    68
[email protected]
    69
    return newOldObject(bytes);
[email protected]
    70
}
[email protected]
    71
[email protected]
    72
inline Object *
[email protected]
    73
Heap::allocate(Handle<Map> map, size_t bytes, Tenure age)
[email protected]
    74
{
[email protected]
    75
    size_t actualBytes = Align(bytes, kObjectAlignment);
[email protected]
    76
[email protected]
    77
    // All objects must be at least two words.
[email protected]
    78
    assert(actualBytes >= kMinObjectSize);
[email protected]
    79
[email protected]
    80
    Address address = newObject(actualBytes, age);
[email protected]
    81
    if (!address) {
[email protected]
    82
        reportOutOfMemory();
[email protected]
    83
        return NULL;
[email protected]
    84
    }
[email protected]
    85
    return InitAddressIntoObject(address, map);
[email protected]
    86
}
[email protected]
    87
[email protected]
    88
inline Address
[email protected]
    89
Heap::newStackObject(size_t bytes)
[email protected]
    90
{
[email protected]
    91
    return lifoSpace_.allocate(bytes);
[email protected]
    92
}
[email protected]
    93
[email protected]
    94
inline Address
[email protected]
    95
Heap::newOldObject(size_t bytes)
[email protected]
    96
{
[email protected]
    97
    return oldSpace_.allocate(bytes);
[email protected]
    98
}
[email protected]
    99
[email protected]
   100
inline Address
[email protected]
   101
Heap::newLargeObject(size_t bytes)
[email protected]
   102
{
[email protected]
   103
    if (Address address = largeSpace_.allocate(bytes))
[email protected]
   104
        return address;
[email protected]
   105
[email protected]
   106
    reclaim();
[email protected]
   107
[email protected]
   108
    return largeSpace_.allocate(bytes);
[email protected]
   109
}
[email protected]
   110
[email protected]
   111
inline void
[email protected]
   112
Object::write(Zone *zone, Object **ptr, Object *obj)
[email protected]
   113
{
[email protected]
   114
    *ptr = obj;
[email protected]
   115
}
[email protected]
   116
[email protected]
   117
}
[email protected]
   118
[email protected]
   119
#endif // _include_jitcraft_heap_inl_h_
[email protected]
   120