jitcraft/Heap-inl.h
changeset 11 a350c2cc736a
child 12 75e385f68b7e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/jitcraft/Heap-inl.h	Fri Jun 29 19:35:17 2012 -0700
     1.3 @@ -0,0 +1,102 @@
     1.4 +/*
     1.5 + * Copyright (C) 2012 David Anderson
     1.6 + *
     1.7 + * This file is part of JITCraft.
     1.8 + *
     1.9 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.10 + * the terms of the GNU General Public License as published by the Free
    1.11 + * Software Foundation, either version 3 of the License, or (at your option)
    1.12 + * any later version.
    1.13 + * 
    1.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License along with
    1.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.20 + */
    1.21 +#ifndef _include_jitcraft_heap_inl_h_
    1.22 +#define _include_jitcraft_heap_inl_h_
    1.23 +
    1.24 +#include "Heap.h"
    1.25 +
    1.26 +namespace jc {
    1.27 +
    1.28 +static inline Object *
    1.29 +InitAddressIntoObject(Address address, Map *map)
    1.30 +{
    1.31 +    uintptr_t untagged = reinterpret_cast<uintptr_t>(address);
    1.32 +    assert(IsAligned(untagged, kObjectAlignment));
    1.33 +
    1.34 +    Address tagged = reinterpret_cast<Address>(untagged | kObjectTag);
    1.35 +    GCObject *obj = reinterpret_cast<GCObject *>(tagged);
    1.36 +    obj->init(map);
    1.37 +    return obj;
    1.38 +}
    1.39 +
    1.40 +Address
    1.41 +Heap::newObject(size_t bytes, Tenure age)
    1.42 +{
    1.43 +#if 0
    1.44 +    if (bytes >= kLargeObjectThreshold)
    1.45 +        return newLargeObject(bytes);
    1.46 +#endif
    1.47 +
    1.48 +#if 0
    1.49 +    if (age == Tenure_Default) {
    1.50 +        if (Address addr = newSpace_.allocate(bytes))
    1.51 +            return addr;
    1.52 +
    1.53 +        scavenge();
    1.54 +        if (Address addr = newSpace_.allocate(bytes))
    1.55 +            return addr;
    1.56 +    }
    1.57 +#endif
    1.58 +
    1.59 +    // Try an old-space allocation.
    1.60 +    if (Address addr = newOldObject(bytes))
    1.61 +        return addr;
    1.62 +
    1.63 +    // Uh-oh - perform a collection.
    1.64 +    assert(false);
    1.65 +
    1.66 +#if 0
    1.67 +    return newOldObject(bytes);
    1.68 +#endif
    1.69 +    return NULL;
    1.70 +}
    1.71 +
    1.72 +Object *
    1.73 +Heap::allocate(Map *map, size_t bytes, Tenure age)
    1.74 +{
    1.75 +    size_t actualBytes = Align(bytes, kObjectAlignment);
    1.76 +    Address address = newObject(actualBytes, age);
    1.77 +    if (!address) {
    1.78 +        assert(false);
    1.79 +        return NULL;
    1.80 +    }
    1.81 +    return InitAddressIntoObject(address, map);
    1.82 +}
    1.83 +
    1.84 +Object *GCObject::read(Object **ptr)
    1.85 +{
    1.86 +    return *TYPED_FIELD_ADDRESS(Object *, ptr);
    1.87 +}
    1.88 +
    1.89 +void
    1.90 +GCObject::write(Zone *zone, Object **ptr, Object *obj)
    1.91 +{
    1.92 +    //env->writeBarrier(this, OBJ_FIELD_OFFSET(this, ptr), obj);
    1.93 +    *TYPED_FIELD_ADDRESS(Object *, ptr) = obj;
    1.94 +}
    1.95 +
    1.96 +void
    1.97 +GCObject::writeNoBarrier(Object **ptr, Object *obj)
    1.98 +{
    1.99 +    *TYPED_FIELD_ADDRESS(Object *, ptr) = obj;
   1.100 +}
   1.101 +
   1.102 +}
   1.103 +
   1.104 +#endif // _include_jitcraft_heap_inl_h_
   1.105 +