src/Heap-inl.h
changeset 86 32ad0523b309
parent 69 dfce0539a77d
child 93 23189d937087
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Heap-inl.h	Tue Jul 24 23:53:34 2012 -0700
     1.3 @@ -0,0 +1,118 @@
     1.4 +/* vim: set ts=4 sw=4 tw=99 et:
     1.5 + *
     1.6 + * Copyright (C) 2012 David Anderson
     1.7 + *
     1.8 + * This file is part of JITCraft.
     1.9 + *
    1.10 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.11 + * the terms of the GNU General Public License as published by the Free
    1.12 + * Software Foundation, either version 3 of the License, or (at your option)
    1.13 + * any later version.
    1.14 + * 
    1.15 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License along with
    1.20 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.21 + */
    1.22 +#ifndef _include_jitcraft_heap_inl_h_
    1.23 +#define _include_jitcraft_heap_inl_h_
    1.24 +
    1.25 +#include "Heap.h"
    1.26 +#include "Spaces-inl.h"
    1.27 +
    1.28 +namespace ke {
    1.29 +
    1.30 +static inline Object *
    1.31 +InitAddressIntoObject(Address address, Map *map)
    1.32 +{
    1.33 +    uintptr_t untagged = reinterpret_cast<uintptr_t>(address);
    1.34 +    assert(IsAligned(untagged, kObjectAlignment));
    1.35 +
    1.36 +    Object *obj = reinterpret_cast<Object *>(address);
    1.37 +    obj->init(map);
    1.38 +    return obj;
    1.39 +}
    1.40 +
    1.41 +inline Address
    1.42 +Heap::newObject(size_t bytes, Tenure age)
    1.43 +{
    1.44 +    if (bytes >= HeapChunk::kLargeObjectThreshold)
    1.45 +        return newLargeObject(bytes);
    1.46 +
    1.47 +    if (age == Tenure_Stack) {
    1.48 +        if (Address addr = newStackObject(bytes))
    1.49 +            return addr;
    1.50 +    }
    1.51 +
    1.52 +#if 0
    1.53 +    if (age == Tenure_Default) {
    1.54 +        if (Address addr = newSpace_.allocate(bytes))
    1.55 +            return addr;
    1.56 +
    1.57 +        scavenge();
    1.58 +        if (Address addr = newSpace_.allocate(bytes))
    1.59 +            return addr;
    1.60 +    }
    1.61 +#endif
    1.62 +
    1.63 +    // Try an old-space allocation.
    1.64 +    if (Address addr = newOldObject(bytes))
    1.65 +        return addr;
    1.66 +
    1.67 +    // Uh-oh - perform a last-ditch collection.
    1.68 +    reclaim();
    1.69 +
    1.70 +    return newOldObject(bytes);
    1.71 +}
    1.72 +
    1.73 +inline Object *
    1.74 +Heap::allocate(Handle<Map> map, size_t bytes, Tenure age)
    1.75 +{
    1.76 +    size_t actualBytes = Align(bytes, kObjectAlignment);
    1.77 +
    1.78 +    // All objects must be at least two words.
    1.79 +    assert(actualBytes >= sizeof(Object *) * 2);
    1.80 +
    1.81 +    Address address = newObject(actualBytes, age);
    1.82 +    if (!address) {
    1.83 +        reportOutOfMemory();
    1.84 +        return NULL;
    1.85 +    }
    1.86 +    return InitAddressIntoObject(address, map);
    1.87 +}
    1.88 +
    1.89 +inline Address
    1.90 +Heap::newStackObject(size_t bytes)
    1.91 +{
    1.92 +    return lifoSpace_.allocate(bytes);
    1.93 +}
    1.94 +
    1.95 +inline Address
    1.96 +Heap::newOldObject(size_t bytes)
    1.97 +{
    1.98 +    return oldSpace_.allocate(bytes);
    1.99 +}
   1.100 +
   1.101 +inline Address
   1.102 +Heap::newLargeObject(size_t bytes)
   1.103 +{
   1.104 +    if (Address address = largeSpace_.allocate(bytes))
   1.105 +        return address;
   1.106 +
   1.107 +    reclaim();
   1.108 +
   1.109 +    return largeSpace_.allocate(bytes);
   1.110 +}
   1.111 +
   1.112 +inline void
   1.113 +Object::write(Zone *zone, Object **ptr, Object *obj)
   1.114 +{
   1.115 +    *ptr = obj;
   1.116 +}
   1.117 +
   1.118 +}
   1.119 +
   1.120 +#endif // _include_jitcraft_heap_inl_h_
   1.121 +