jitcraft/Heap.h
changeset 28 a9665857cfd5
parent 27 28697d267222
child 49 a92afa7484fd
     1.1 --- a/jitcraft/Heap.h	Mon Jul 02 14:04:29 2012 -0700
     1.2 +++ b/jitcraft/Heap.h	Mon Jul 02 23:37:12 2012 -0700
     1.3 @@ -1,181 +1,81 @@
     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_objects_h_
    1.22 -#define _include_jitcraft_objects_h_
    1.23 -
    1.24 -#include "Utility.h"
    1.25 -#include "Spaces.h"
    1.26 -#include "Barriers.h"
    1.27 -
    1.28 -namespace jc {
    1.29 -
    1.30 -class Zone;
    1.31 -
    1.32 -// Macros for laying out objects.
    1.33 -#define OBJECT_FIELD(Type, name)                    \
    1.34 -    Type fld_##name##_;
    1.35 -
    1.36 -// :TODO: refactor these now that objects are no longer tagged.
    1.37 -#define RAW_FIELD_ADDRESS(ptr)                      \
    1.38 -    (reinterpret_cast<uintptr_t>(ptr))
    1.39 -#define TYPED_FIELD_ADDRESS(Type, ptr)              \
    1.40 -    (reinterpret_cast<Type *>(RAW_FIELD_ADDRESS(ptr)))
    1.41 -#define OBJ_FIELD_OFFSET(obj, ptr)                  \
    1.42 -    (reinterpret_cast<uintptr_t>(ptr) - reinterpret_cast<uintptr_t>(obj))
    1.43 -#define READ_FIELD(name)                            \
    1.44 -    read(&fld_##name##_)
    1.45 -#define WRITE_FIELD(name, val)                      \
    1.46 -    write(&fld_##name##_, val)
    1.47 -#define READ_FIELD_OBJ(name)                        \
    1.48 -    read(reinterpret_cast<Object **>(&fld_##name##_))
    1.49 -#define WRITE_FIELD_OBJ(env, name, val)             \
    1.50 -    write(env, reinterpret_cast<Object **>(&fld_##name##_), val)
    1.51 -#define WRITE_FIELD_OBJ_NO_BARRIER(name, val)  \
    1.52 -    writeNoBarrier(reinterpret_cast<Object **>(&fld_##name##_), val)
    1.53 -
    1.54 -// Garbage-collection objects require a header to describe their layout. This
    1.55 -// header is called a "map". The different kinds of maps are enumerated below.
    1.56 -class Map;
    1.57 -
    1.58 -enum MapKind
    1.59 -{
    1.60 -    // Represents Type objects.
    1.61 -    MapKind_Type,
    1.62 -
    1.63 -    // Represents basic data structures. When adding to this, don't forget
    1.64 -    // to update Zone::initialize(), or you'll get a NULL crash trying to
    1.65 -    // allocate, since these maps are cached.
    1.66 -    MapKind_FixedArray,
    1.67 -    MapKind_ByteArray,
    1.68 -    MapKind_Code,
    1.69 -    MapKind_Reference,
    1.70 -    MapKind_Module,
    1.71 -    MapKind_Function,
    1.72 -
    1.73 -    // Used to represent Arrays. This does not have a canonical map.
    1.74 -    MapKind_Array,
    1.75 -
    1.76 -    // These represent special Map subclasses that require special tracing.
    1.77 -    MapKinds_Special,
    1.78 -    MapKind_Meta,           // (default map class)
    1.79 -    MapKind_MetaArray,      // ArrayMap instances
    1.80 -    MapKinds_EndSpecial,
    1.81 -
    1.82 -    MapKinds_Total
    1.83 -};
    1.84 -
    1.85 -typedef void * VoidPtr;
    1.86 -
    1.87 -// All garbage collected objects must inherit from GCObject. Note that using
    1.88 -// virtual functions is illegal, as the |map| field would be displaced.
    1.89 -class Object
    1.90 -{
    1.91 -    Barriered<Map> map_;
    1.92 -
    1.93 +/*
    1.94 + * Copyright (C) 2012 David Anderson
    1.95 + *
    1.96 + * This file is part of JITCraft.
    1.97 + *
    1.98 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.99 + * the terms of the GNU General Public License as published by the Free
   1.100 + * Software Foundation, either version 3 of the License, or (at your option)
   1.101 + * any later version.
   1.102 + * 
   1.103 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
   1.104 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   1.105 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   1.106 + *
   1.107 + * You should have received a copy of the GNU General Public License along with
   1.108 + * JITCraft. If not, see http://www.gnu.org/licenses/.
   1.109 + */
   1.110 +#ifndef _include_jitcraft_heap_h_
   1.111 +#define _include_jitcraft_heap_h_
   1.112 +
   1.113 +#include "Utility.h"
   1.114 +#include "Spaces.h"
   1.115 +#include "Barriers.h"
   1.116 +#include "Objects.h"
   1.117 +#include "Handles.h"
   1.118 +
   1.119 +namespace jc {
   1.120 +
   1.121 +class Zone;
   1.122 +
   1.123 +// Heap-allocated objects go here. Heaps are thread-local.
   1.124 +class Heap
   1.125 +{
   1.126 +    Zone *zone_;
   1.127 +    HeapStack lifoSpace_;
   1.128 +    OldSpace mapSpace_;
   1.129 +    OldSpace oldSpace_;
   1.130 +
   1.131 +    Map *metaMap_;
   1.132 +
   1.133    public:
   1.134 -    void init(Map *map) {
   1.135 -        map_ = map;
   1.136 -    }
   1.137 +    enum Tenure {
   1.138 +        // Default, objects go into the nursery.
   1.139 +        Tenure_Default,
   1.140 +        
   1.141 +        // If objects are proven to not escape the stack, they can go into
   1.142 +        // a LIFO arena. Allocation on the LIFO arena is not guaranteed to
   1.143 +        // succeed; thus, objects may still be allocated in other arenas.
   1.144 +        Tenure_Stack,
   1.145 +
   1.146 +        // Objects that should not go into the nursery (i.e. are expected
   1.147 +        // to be long-living) go here.
   1.148 +        Tenure_Old
   1.149 +    };
   1.150 +
   1.151 +  private:
   1.152 +    Address newOldObject(size_t bytes);
   1.153 +    inline Address newStackObject(size_t bytes);
   1.154 +    inline Address newObject(size_t bytes, Tenure pretenure);
   1.155  
   1.156 -    Map *map() {
   1.157 -        return map_;
   1.158 -    }
   1.159 -    inline bool is(MapKind map);
   1.160 -
   1.161 -    inline void write(Zone *zone, Object **ptr, Object *value);
   1.162 -};
   1.163 -
   1.164 -// Maps describe the layout of GCObjects. Every GCObject requires a map.
   1.165 -class Map : public Object
   1.166 -{
   1.167 -    MapKind kind_;
   1.168 -
   1.169 -  public:
   1.170 -    void setKind(MapKind kind) {
   1.171 -        kind_ = kind;
   1.172 -    }
   1.173 -    MapKind kind() {
   1.174 -        return kind_;
   1.175 -    }
   1.176 -
   1.177 -    static inline Map *cast(Object *obj) {
   1.178 -        // Note that ArrayMap is a map, but a special one: since it is non-
   1.179 -        assert(obj->map()->kind() > MapKinds_Special &&
   1.180 -               obj->map()->kind() < MapKinds_EndSpecial);
   1.181 -        return reinterpret_cast<Map *>(obj);
   1.182 -    }
   1.183 -};
   1.184 -
   1.185 -// Heap-allocated objects go here. Heaps are thread-local.
   1.186 -class Heap
   1.187 -{
   1.188 -    Zone *zone_;
   1.189 -    HeapStack lifoSpace_;
   1.190 -    OldSpace mapSpace_;
   1.191 -    OldSpace oldSpace_;
   1.192 -
   1.193 -    Map *metaMap_;
   1.194 -
   1.195 -  public:
   1.196 -    enum Tenure {
   1.197 -        // Default, objects go into the nursery.
   1.198 -        Tenure_Default,
   1.199 -        
   1.200 -        // If objects are proven to not escape the stack, they can go into
   1.201 -        // a LIFO arena. Allocation on the LIFO arena is not guaranteed to
   1.202 -        // succeed; thus, objects may still be allocated in other arenas.
   1.203 -        Tenure_Stack,
   1.204 -
   1.205 -        // Objects that should not go into the nursery (i.e. are expected
   1.206 -        // to be long-living) go here.
   1.207 -        Tenure_Old
   1.208 -    };
   1.209 -
   1.210 -  private:
   1.211 -    Address newOldObject(size_t bytes);
   1.212 -    inline Address newStackObject(size_t bytes);
   1.213 -    inline Address newObject(size_t bytes, Tenure pretenure);
   1.214 -
   1.215 -  public:
   1.216 -    Heap(Zone *zone);
   1.217 -    bool initialize();
   1.218 -
   1.219 -    inline Object *allocate(Map *map, size_t byes, Tenure pretenure);
   1.220 -    inline Object *allocate(Handle<Map> map, size_t byes, Tenure pretenure);
   1.221 -    Map *allocateMap(MapKind kind, size_t bytes);
   1.222 -
   1.223 -    Map *metaMap() {
   1.224 -        return metaMap_;
   1.225 -    }
   1.226 -    inline Address lifoPosition() const {
   1.227 -        return lifoSpace_.position();
   1.228 -    }
   1.229 -    inline void unwindLifo(Address to) {
   1.230 -        lifoSpace_.unwind(to);
   1.231 -    }
   1.232 -};
   1.233 -
   1.234 -inline bool
   1.235 -Object::is(MapKind kind)
   1.236 -{
   1.237 -    return map()->kind() == kind;
   1.238 -}
   1.239 -
   1.240 -}
   1.241 -
   1.242 -#endif //_include_jitcraft_objects_h_
   1.243 +  public:
   1.244 +    Heap(Zone *zone);
   1.245 +    bool initialize();
   1.246 +
   1.247 +    inline Object *allocate(Handle<Map> map, size_t byes, Tenure pretenure);
   1.248 +    Map *allocateMap(MapKind kind, size_t bytes);
   1.249 +
   1.250 +    Map *metaMap() {
   1.251 +        return metaMap_;
   1.252 +    }
   1.253 +    inline Address lifoPosition() const {
   1.254 +        return lifoSpace_.position();
   1.255 +    }
   1.256 +    inline void unwindLifo(Address to) {
   1.257 +        lifoSpace_.unwind(to);
   1.258 +    }
   1.259 +};
   1.260 +
   1.261 +}
   1.262 +
   1.263 +#endif //_include_jitcraft_heap_h_