src/FixedArray.h
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 15:52:44 2013 +0100 (2013-06-10)
changeset 265 3a903b89a514
parent 263 ba85a47ee414
permissions -rw-r--r--
Fixed type checking for multidimensional arrays.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn 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  * SourcePawn 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  * SourcePawn. 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     size_t objSize() {
    51         return SizeFor(length());
    52     }
    53 
    54     static inline size_t SizeFor(unsigned length) {
    55         return sizeof(FixedArray) + sizeof(Object *) * length;
    56     }
    57 
    58     static inline FixedArray *cast(Object *obj) {
    59         assert(!obj || obj->is(MapKind_FixedArray));
    60         return reinterpret_cast<FixedArray *>(obj);
    61     }
    62 };
    63 
    64 // A fixed-length array containing raw bytes.
    65 class ByteArray : public Object
    66 {
    67     uint32 length_;
    68 
    69   public:
    70     static ByteArray *New(Zone *zone, unsigned length, Heap::Tenure tenure = Heap::Tenure_Default);
    71     static ByteArray *New(Zone *zone, Handle<Map> map, unsigned length, Heap::Tenure tenure = Heap::Tenure_Default);
    72 
    73     unsigned length() {
    74         return length_;
    75     }
    76     Address raw() {
    77         return Address(this) + offsetToElements();
    78     }
    79     size_t objSize() {
    80         return SizeFor(length());
    81     }
    82 
    83     static inline size_t offsetToElements() {
    84         return sizeof(ByteArray);
    85     }
    86 
    87     static inline size_t SizeFor(unsigned length) {
    88         return sizeof(ByteArray) + sizeof(uint8) * length;
    89     }
    90 
    91     static inline ByteArray *cast(Object *obj) {
    92         assert(!obj || obj->is(MapKind_ByteArray));
    93         return reinterpret_cast<ByteArray *>(obj);
    94     }
    95 };
    96 
    97 }
    98 
    99 #endif // _include_jitcraft_fixedarray_h_