jitcraft/Objects.h
changeset 28 a9665857cfd5
child 52 22b67b4cf067
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/jitcraft/Objects.h	Mon Jul 02 23:37:12 2012 -0700
     1.3 @@ -0,0 +1,110 @@
     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 +// Garbage-collection objects require a header to describe their layout. This
    1.33 +// header is called a "map". The different kinds of maps are enumerated below.
    1.34 +class Map;
    1.35 +
    1.36 +enum MapKind
    1.37 +{
    1.38 +    // Represents Type objects.
    1.39 +    MapKind_Type,
    1.40 +
    1.41 +    // Represents basic data structures. When adding to this, don't forget
    1.42 +    // to update Zone::initialize(), or you'll get a NULL crash trying to
    1.43 +    // allocate, since these maps are cached.
    1.44 +    MapKind_FixedArray,
    1.45 +    MapKind_ByteArray,
    1.46 +    MapKind_Code,
    1.47 +    MapKind_Reference,
    1.48 +    MapKind_Module,
    1.49 +    MapKind_Function,
    1.50 +
    1.51 +    // Used to represent Arrays. This does not have a canonical map.
    1.52 +    MapKind_Array,
    1.53 +
    1.54 +    // These represent special Map subclasses that require special tracing.
    1.55 +    MapKinds_Special,
    1.56 +    MapKind_Meta,           // (default map class)
    1.57 +    MapKind_MetaArray,      // ArrayMap instances
    1.58 +    MapKinds_EndSpecial,
    1.59 +
    1.60 +    MapKinds_Total
    1.61 +};
    1.62 +
    1.63 +typedef void * VoidPtr;
    1.64 +
    1.65 +// All garbage collected objects must inherit from GCObject. Note that using
    1.66 +// virtual functions is illegal, as the |map| field would be displaced.
    1.67 +class Object
    1.68 +{
    1.69 +    Barriered<Map> map_;
    1.70 +
    1.71 +  public:
    1.72 +    void init(Map *map) {
    1.73 +        map_ = map;
    1.74 +    }
    1.75 +
    1.76 +    Map *map() {
    1.77 +        return map_;
    1.78 +    }
    1.79 +    inline bool is(MapKind map);
    1.80 +
    1.81 +    inline void write(Zone *zone, Object **ptr, Object *value);
    1.82 +};
    1.83 +
    1.84 +// Maps describe the layout of GCObjects. Every GCObject requires a map.
    1.85 +class Map : public Object
    1.86 +{
    1.87 +    MapKind kind_;
    1.88 +
    1.89 +  public:
    1.90 +    void setKind(MapKind kind) {
    1.91 +        kind_ = kind;
    1.92 +    }
    1.93 +    MapKind kind() {
    1.94 +        return kind_;
    1.95 +    }
    1.96 +
    1.97 +    static inline Map *cast(Object *obj) {
    1.98 +        // Note that ArrayMap is a map, but a special one: since it is non-
    1.99 +        assert(obj->map()->kind() > MapKinds_Special &&
   1.100 +               obj->map()->kind() < MapKinds_EndSpecial);
   1.101 +        return reinterpret_cast<Map *>(obj);
   1.102 +    }
   1.103 +};
   1.104 +
   1.105 +inline bool
   1.106 +Object::is(MapKind kind)
   1.107 +{
   1.108 +    return map()->kind() == kind;
   1.109 +}
   1.110 +
   1.111 +}
   1.112 +
   1.113 +#endif //_include_jitcraft_objects_h_