src/Structures.h
changeset 147 9c544823d090
parent 146 dc7d3b7a918c
child 152 153d0cafc1fd
     1.1 --- a/src/Structures.h	Sat Aug 04 17:51:43 2012 -0700
     1.2 +++ b/src/Structures.h	Sat Aug 04 19:33:06 2012 -0700
     1.3 @@ -22,6 +22,7 @@
     1.4  #include "Objects.h"
     1.5  #include "Handles.h"
     1.6  #include "Vector.h"
     1.7 +#include "Heap.h"
     1.8  
     1.9  namespace ke {
    1.10  
    1.11 @@ -46,7 +47,7 @@
    1.12      unsigned traceableOffsetCount() const {
    1.13          return offsets_.length();
    1.14      }
    1.15 -    const unsigned *offsets() {
    1.16 +    const unsigned *offsets() const {
    1.17          return offsets_.buffer();
    1.18      }
    1.19  
    1.20 @@ -96,6 +97,69 @@
    1.21      }
    1.22  };
    1.23  
    1.24 +class StructMap : public Map
    1.25 +{
    1.26 +    unsigned structureSize_;
    1.27 +    unsigned traceOffsetCount_;
    1.28 +    Barriered<Type> type_;
    1.29 +
    1.30 +  public:
    1.31 +    static StructMap *New(Zone *zone, const StructureBufferBuilder &builder);
    1.32 +
    1.33 +    unsigned structureSize() {
    1.34 +        return structureSize_;
    1.35 +    }
    1.36 +    unsigned traceOffsetCount() {
    1.37 +        return traceOffsetCount_;
    1.38 +    }
    1.39 +    const unsigned *traceOffsets() {
    1.40 +        return reinterpret_cast<unsigned *>(this + 1);
    1.41 +    }
    1.42 +    size_t size() {
    1.43 +        return Align(sizeof(StructMap) + sizeof(unsigned) * traceOffsetCount(), kObjectAlignment);
    1.44 +    }
    1.45 +    Type *type() {
    1.46 +        return type_;
    1.47 +    }
    1.48 +    void setType(Type *type) {
    1.49 +        assert(!type_);
    1.50 +        type_ = type;
    1.51 +    }
    1.52 +
    1.53 +    // Begin GC descriptors.
    1.54 +    static inline size_t offsetOfType() {
    1.55 +        return OFFSETOF(StructMap, type_);
    1.56 +    }
    1.57 +    // End GC descriptors.
    1.58 +
    1.59 +    static inline StructMap *cast(Object *obj) {
    1.60 +        assert(!obj || obj->is(MapKind_MetaStruct));
    1.61 +        return reinterpret_cast<StructMap *>(obj);
    1.62 +    }
    1.63 +};
    1.64 +
    1.65 +class Struct : public Object
    1.66 +{
    1.67 +  public:
    1.68 +    static Struct *New(Zone *zone, Handle<StructMap> map, Heap::Tenure tenure = Heap::Tenure_Default);
    1.69 +
    1.70 +    size_t size() {
    1.71 +        return Align(sizeof(Struct) + map()->structureSize(), kObjectAlignment);
    1.72 +    }
    1.73 +    StructMap *map() {
    1.74 +        return StructMap::cast(Object::map());
    1.75 +    }
    1.76 +
    1.77 +    static inline size_t offsetOfSlotOffset(unsigned offset) {
    1.78 +        return sizeof(Struct) + offset;
    1.79 +    }
    1.80 +
    1.81 +    static inline Struct *cast(Object *obj) {
    1.82 +        assert(!obj || obj->is(MapKind_Struct));
    1.83 +        return reinterpret_cast<Struct *>(obj);
    1.84 +    }
    1.85 +};
    1.86 +
    1.87  }
    1.88  
    1.89  #endif // _include_jc_structure_h_