src/FixedArray.h
changeset 86 32ad0523b309
parent 85 06a0015b6a5c
child 161 b3f0aaee735a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/FixedArray.h	Tue Jul 24 23:53:34 2012 -0700
     1.3 @@ -0,0 +1,89 @@
     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_fixedarray_h_
    1.23 +#define _include_jitcraft_fixedarray_h_
    1.24 +
    1.25 +#include "Heap.h"
    1.26 +
    1.27 +namespace ke {
    1.28 +
    1.29 +// A fixed-length array that contains Object * slots.
    1.30 +class FixedArray : public Object
    1.31 +{
    1.32 +    uint32 length_;
    1.33 +
    1.34 +  private:
    1.35 +    Object **vec() {
    1.36 +        return reinterpret_cast<Object **>(this + 1);
    1.37 +    }
    1.38 +
    1.39 +  public:
    1.40 +    static FixedArray *New(Zone *zone, unsigned length, Heap::Tenure tenure);
    1.41 +
    1.42 +    unsigned length() {
    1.43 +        return length_;
    1.44 +    }
    1.45 +    Object *at(size_t i) {
    1.46 +        assert(i < length());
    1.47 +        return vec()[i];
    1.48 +    }
    1.49 +    void set(Zone *zone, size_t i, Object *obj) {
    1.50 +        assert(i < length());
    1.51 +        write(zone, &vec()[i], obj);
    1.52 +    }
    1.53 +
    1.54 +    static size_t SizeFor(unsigned length) {
    1.55 +        return sizeof(FixedArray) + sizeof(Object *) * length;
    1.56 +    }
    1.57 +
    1.58 +    static inline FixedArray *cast(Object *obj) {
    1.59 +        assert(!obj || obj->is(MapKind_FixedArray));
    1.60 +        return reinterpret_cast<FixedArray *>(obj);
    1.61 +    }
    1.62 +};
    1.63 +
    1.64 +// A fixed-length array containing raw bytes.
    1.65 +class ByteArray : public Object
    1.66 +{
    1.67 +    uint32 length_;
    1.68 +
    1.69 +  public:
    1.70 +    static ByteArray *New(Zone *zone, unsigned length, Heap::Tenure tenure = Heap::Tenure_Default);
    1.71 +    static ByteArray *New(Zone *zone, Handle<Map> map, unsigned length, Heap::Tenure tenure = Heap::Tenure_Default);
    1.72 +
    1.73 +    unsigned length() {
    1.74 +        return length_;
    1.75 +    }
    1.76 +    Address raw() {
    1.77 +        return reinterpret_cast<uint8 *>(this + 1);
    1.78 +    }
    1.79 +
    1.80 +    static size_t SizeFor(unsigned length) {
    1.81 +        return Align(sizeof(ByteArray) + sizeof(uint8) * length, kObjectAlignment);
    1.82 +    }
    1.83 +
    1.84 +    static inline ByteArray *cast(Object *obj) {
    1.85 +        assert(!obj || obj->is(MapKind_ByteArray));
    1.86 +        return reinterpret_cast<ByteArray *>(obj);
    1.87 +    }
    1.88 +};
    1.89 +
    1.90 +}
    1.91 +
    1.92 +#endif // _include_jitcraft_fixedarray_h_