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