jitcraft/FixedArray.h
author David Anderson <dvander@alliedmods.net>
Sun Jul 08 16:49:17 2012 -0700 (2012-07-08)
changeset 63 dd57743d220e
parent 59 b7d61ddb3c8d
child 85 06a0015b6a5c
permissions -rw-r--r--
Added support for forwards.
     1 /* vim: set ts=4 sw=4 tw=99 et
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_jitcraft_fixedarray_h_
    20 #define _include_jitcraft_fixedarray_h_
    21 
    22 #include "Heap.h"
    23 
    24 namespace ke {
    25 
    26 // A fixed-length array that contains Object * slots.
    27 class FixedArray : public Object
    28 {
    29     uint32 length_;
    30 
    31   private:
    32     Object **vec() {
    33         return reinterpret_cast<Object **>(this + 1);
    34     }
    35 
    36   public:
    37     static FixedArray *New(Zone *zone, unsigned length, Heap::Tenure tenure);
    38 
    39     unsigned length() {
    40         return length_;
    41     }
    42     Object *at(size_t i) {
    43         assert(i < length());
    44         return vec()[i];
    45     }
    46     void set(Zone *zone, size_t i, Object *obj) {
    47         assert(i < length());
    48         write(zone, &vec()[i], obj);
    49     }
    50 
    51     static size_t SizeFor(unsigned length) {
    52         return sizeof(FixedArray) + sizeof(Object *) * length;
    53     }
    54 
    55     static inline FixedArray *cast(Object *obj) {
    56         assert(!obj || obj->is(MapKind_FixedArray));
    57         return reinterpret_cast<FixedArray *>(obj);
    58     }
    59 };
    60 
    61 // A fixed-length array containing raw bytes.
    62 class ByteArray : public Object
    63 {
    64     uint32 length_;
    65 
    66   public:
    67     static ByteArray *New(Zone *zone, unsigned length, Heap::Tenure tenure = Heap::Tenure_Default);
    68 
    69     unsigned length() {
    70         return length_;
    71     }
    72     Address raw() {
    73         return reinterpret_cast<uint8 *>(this + 1);
    74     }
    75 
    76     static size_t SizeFor(unsigned length) {
    77         return Align(sizeof(ByteArray) + sizeof(uint8) * length, kObjectAlignment);
    78     }
    79 
    80     static inline ByteArray *cast(Object *obj) {
    81         assert(!obj || obj->is(MapKind_ByteArray));
    82         return reinterpret_cast<ByteArray *>(obj);
    83     }
    84 };
    85 
    86 }
    87 
    88 #endif // _include_jitcraft_fixedarray_h_