jitcraft/Array.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 65 7c942b5adebc
permissions -rw-r--r--
Added support for forwards.
     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 "Handles.h"
    23 #include "Types.h"
    24 
    25 namespace ke {
    26 
    27 class ArrayMap;
    28 
    29 class Array : public Object
    30 {
    31     friend class ArrayMap;
    32 
    33     uint32 nelements_;
    34     void *elements_;
    35 
    36   public:
    37     static Array *New(Zone *zone, Handle<ArrayMap> map, uint32 elements,
    38                       Heap::Tenure = Heap::Tenure_Default);
    39 
    40     uint32 nelements() {
    41         return nelements_;
    42     }
    43     void *vec() {
    44         return elements_;
    45     }
    46 
    47     inline int *int32Vec();
    48     inline float *floatVec();
    49     inline Object **objVec();
    50     inline char *chars();
    51 
    52     inline Type *type();
    53     inline size_t elementSize();
    54 
    55     int readInt32(size_t index) {
    56         assert(index < nelements());
    57         return int32Vec()[index];
    58     }
    59     void writeInt32(size_t index, int i) {
    60         assert(index < nelements());
    61         int32Vec()[index] = i;
    62     }
    63     char readChar(size_t index) {
    64         assert(index < nelements());
    65         return chars()[index];
    66     }
    67     void writeChar(size_t index, char c) {
    68         assert(index < nelements());
    69         chars()[index] = c;
    70     }
    71     float readFloat(size_t index) {
    72         assert(index < nelements());
    73         return floatVec()[index];
    74     }
    75     void writeFloat(size_t index, float f) {
    76         assert(index < nelements());
    77         floatVec()[index] = f;
    78     }
    79     inline void *slotPtr(size_t index);
    80     Object *readObject(size_t index) {
    81         assert(index < nelements());
    82         return objVec()[index];
    83     }
    84     void writeObject(Zone *zone, size_t index, Object *ptr) {
    85         assert(index < nelements());
    86         write(zone, &objVec()[index], ptr);
    87     }
    88 
    89     static inline size_t SizeOf(Array *array) {
    90         return sizeof(Array) + array->nelements() * array->elementSize();
    91     }
    92 
    93     static Array *cast(Object *obj) {
    94         assert(!obj || obj->is(MapKind_Array));
    95         return reinterpret_cast<Array *>(obj);
    96     }
    97 };
    98 
    99 class ArrayMap : public Map
   100 {
   101     Barriered<Type> type_;
   102     Barriered<ArrayMap> contained_;
   103 
   104   public:
   105     static ArrayMap *New(Zone *zone, Handle<Type> type, Handle<ArrayMap> contained);
   106 
   107     Type *type() {
   108         return type_;
   109     }
   110     ArrayMap *contained() {
   111         return contained_;
   112     }
   113 
   114     // GC Descriptor.
   115     static inline size_t offsetOfType() {
   116         return offsetof(ArrayMap, type_);
   117     }
   118     static inline size_t offsetOfContained() {
   119         return offsetof(ArrayMap, contained_);
   120     }
   121     // End GC Descriptor.
   122     
   123     static ArrayMap *cast(Object *obj) {
   124         assert(!obj || obj->is(MapKind_MetaArray));
   125         return reinterpret_cast<ArrayMap *>(obj);
   126     }
   127 };
   128 
   129 inline int *
   130 Array::int32Vec()
   131 {
   132     assert(type()->levels() == 1);
   133     assert(type()->contained()->isInt32());
   134     return reinterpret_cast<int *>(vec());
   135 }
   136 
   137 inline float *
   138 Array::floatVec()
   139 {
   140     assert(type()->levels() == 1);
   141     assert(type()->contained()->isFloat());
   142     return reinterpret_cast<float *>(vec());
   143 }
   144 
   145 inline Object **
   146 Array::objVec()
   147 {
   148     assert(type()->levels() > 1);
   149     return reinterpret_cast<Object **>(vec());
   150 }
   151 
   152 inline void *
   153 Array::slotPtr(size_t index)
   154 {
   155     assert(type()->contained()->primitive() == PrimitiveType_Int32 ||
   156            type()->contained()->primitive() == PrimitiveType_Float);
   157     assert(index < nelements());
   158     return reinterpret_cast<int *>(vec()) + index;
   159 }
   160 
   161 inline char *
   162 Array::chars()
   163 {
   164     assert(type()->levels() == 1);
   165     assert(type()->contained()->primitive() == PrimitiveType_Char);
   166     return reinterpret_cast<char *>(vec());
   167 }
   168 
   169 inline Type *
   170 Array::type()
   171 {
   172     return ArrayMap::cast(map())->type();
   173 }
   174 
   175 inline size_t
   176 Array::elementSize()
   177 {
   178     if (type()->isArray())
   179         return sizeof(Object *);
   180     switch (type()->contained()->primitive()) {
   181       case PrimitiveType_Float:
   182         return sizeof(float);
   183       case PrimitiveType_Int32:
   184         return sizeof(int);
   185       default:
   186         assert(type()->contained()->primitive() == PrimitiveType_Char);
   187         return sizeof(char);
   188     }
   189 }
   190 
   191 }
   192 
   193 #endif // _include_jitcraft_array_h_