jitcraft/Array.h
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 19:38:53 2012 -0700 (2012-06-30)
changeset 12 75e385f68b7e
child 13 627b6b94e774
permissions -rw-r--r--
Refactored Array, Code to be Objects. Removed Object tagging.
     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_array_h_
    19 #define _include_jitcraft_array_h_
    20 
    21 #include "Heap.h"
    22 #include "Types.h"
    23 
    24 namespace jc {
    25 
    26 class ArrayMap;
    27 
    28 class Array : public Object
    29 {
    30     friend class ArrayMap;
    31 
    32     OBJECT_FIELD(uint32, nelements);
    33     OBJECT_FIELD(VoidPtr, elements);
    34 
    35   private:
    36     void init(unsigned nelements);
    37     void *vec() {
    38         return READ_FIELD(elements);
    39     }
    40 
    41   public:
    42     static Array *New(Zone *zone, ArrayMap *map, uint32 elements,
    43                       Heap::Tenure = Heap::Tenure_Default);
    44 
    45     uint32 nelements() {
    46         return READ_FIELD(nelements);
    47     }
    48     inline int *int32Vec();
    49     inline float *floatVec();
    50     inline Object **objVec();
    51 
    52     int readInt32(size_t index) {
    53         assert(index < nelements());
    54         return int32Vec()[index];
    55     }
    56     void writeInt32(size_t index, int i) {
    57         assert(index < nelements());
    58         int32Vec()[index] = i;
    59     }
    60     float readFloat(size_t index) {
    61         assert(index < nelements());
    62         return floatVec()[index];
    63     }
    64     void writeFloat(size_t index, float f) {
    65         assert(index < nelements());
    66         floatVec()[index] = f;
    67     }
    68     Object *readObject(size_t index) {
    69         assert(index < nelements());
    70         return objVec()[index];
    71     }
    72     void writeObject(Zone *zone, size_t index, Object *ptr) {
    73         assert(index < nelements());
    74         write(zone, &objVec()[index], ptr);
    75     }
    76 
    77     static Array *cast(Object *obj) {
    78         assert(obj->is(MapKind_Array));
    79         return reinterpret_cast<Array *>(obj);
    80     }
    81 };
    82 
    83 class ArrayMap : public Map
    84 {
    85     OBJECT_FIELD(Type *, type);
    86     OBJECT_FIELD(ArrayMap *, contained);
    87 
    88   private:
    89     void init(Type *type, ArrayMap *contained) {
    90         WRITE_FIELD_OBJ_NO_BARRIER(type, type);
    91         WRITE_FIELD_OBJ_NO_BARRIER(contained, contained);
    92     }
    93 
    94   public:
    95     static ArrayMap *New(Zone *zone, Type *type, ArrayMap *contained);
    96 
    97     Type *type() {
    98         return Type::cast(READ_FIELD_OBJ(type));
    99     }
   100     ArrayMap *contained() {
   101         return ArrayMap::cast(READ_FIELD_OBJ(contained));
   102     }
   103     
   104     static ArrayMap *cast(Object *obj) {
   105         assert(!obj || obj->is(MapKind_MetaArray));
   106         return reinterpret_cast<ArrayMap *>(obj);
   107     }
   108 };
   109 
   110 inline int *
   111 Array::int32Vec()
   112 {
   113     assert(ArrayMap::cast(map())->type()->levels() == 1);
   114     assert(ArrayMap::cast(map())->type()->contained()->isInt());
   115     return reinterpret_cast<int *>(vec());
   116 }
   117 
   118 inline float *
   119 Array::floatVec()
   120 {
   121     assert(ArrayMap::cast(map())->type()->levels() == 1);
   122     assert(ArrayMap::cast(map())->type()->contained()->isFloat());
   123     return reinterpret_cast<float *>(vec());
   124 }
   125 
   126 inline Object **
   127 Array::objVec()
   128 {
   129     assert(ArrayMap::cast(map())->type()->levels() > 1);
   130     return reinterpret_cast<Object **>(vec());
   131 }
   132 
   133 }
   134 
   135 #endif // _include_jitcraft_array_h_