jitcraft/Array.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 03 00:28:21 2012 -0700 (2012-07-03)
changeset 31 0f330cf7e09a
parent 27 28697d267222
child 44 c920f9328716
permissions -rw-r--r--
Handle-ize Array.
     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 jc {
    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 Array *cast(Object *obj) {
    90         assert(!obj || obj->is(MapKind_Array));
    91         return reinterpret_cast<Array *>(obj);
    92     }
    93 };
    94 
    95 class ArrayMap : public Map
    96 {
    97     Barriered<Type> type_;
    98     Barriered<ArrayMap> contained_;
    99 
   100   public:
   101     static ArrayMap *New(Zone *zone, Handle<Type> type, Handle<ArrayMap> contained);
   102 
   103     Type *type() {
   104         return type_;
   105     }
   106     ArrayMap *contained() {
   107         return contained_;
   108     }
   109     
   110     static ArrayMap *cast(Object *obj) {
   111         assert(!obj || obj->is(MapKind_MetaArray));
   112         return reinterpret_cast<ArrayMap *>(obj);
   113     }
   114 };
   115 
   116 inline int *
   117 Array::int32Vec()
   118 {
   119     assert(type()->levels() == 1);
   120     assert(type()->contained()->isInt32());
   121     return reinterpret_cast<int *>(vec());
   122 }
   123 
   124 inline float *
   125 Array::floatVec()
   126 {
   127     assert(type()->levels() == 1);
   128     assert(type()->contained()->isFloat());
   129     return reinterpret_cast<float *>(vec());
   130 }
   131 
   132 inline Object **
   133 Array::objVec()
   134 {
   135     assert(type()->levels() > 1);
   136     return reinterpret_cast<Object **>(vec());
   137 }
   138 
   139 inline void *
   140 Array::slotPtr(size_t index)
   141 {
   142     assert(type()->contained()->primitive() == PrimitiveType_Int32 ||
   143            type()->contained()->primitive() == PrimitiveType_Float);
   144     assert(index < nelements());
   145     return reinterpret_cast<int *>(vec()) + index;
   146 }
   147 
   148 inline char *
   149 Array::chars()
   150 {
   151     assert(type()->levels() == 1);
   152     assert(type()->contained()->primitive() == PrimitiveType_Char);
   153     return reinterpret_cast<char *>(vec());
   154 }
   155 
   156 inline Type *
   157 Array::type()
   158 {
   159     return ArrayMap::cast(map())->type();
   160 }
   161 
   162 inline size_t
   163 Array::elementSize()
   164 {
   165     if (type()->isArray())
   166         return sizeof(Object *);
   167     switch (type()->contained()->primitive()) {
   168       case PrimitiveType_Float:
   169         return sizeof(float);
   170       case PrimitiveType_Int32:
   171         return sizeof(int);
   172       default:
   173         assert(type()->contained()->primitive() == PrimitiveType_Char);
   174         return sizeof(char);
   175     }
   176 }
   177 
   178 }
   179 
   180 #endif // _include_jitcraft_array_h_