src/Array.cpp
author David Anderson <dvander@alliedmods.net>
Tue Dec 25 17:02:07 2012 -0800 (2012-12-25)
changeset 239 572668ef742b
parent 236 072f1114ea5d
child 247 7721042bdb67
permissions -rw-r--r--
More const preparation: allow type casts to follow qualification.
     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 #include <string.h>
    20 #include "Array.h"
    21 #include "FixedArray.h"
    22 #include "Heap.h"
    23 #include "Zone.h"
    24 #include "Structures.h"
    25 #include "Heap-inl.h"
    26 
    27 using namespace ke;
    28 
    29 ArrayMap *
    30 ArrayMap::New(Zone *zone, Handle<ArrayType> type)
    31 {
    32     Local<Map> obj(zone, zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray));
    33     if (!obj)
    34         return NULL;
    35 
    36     Local<ArrayMap> map(zone, ArrayMap::cast(obj));
    37     map->type_ = type;
    38     return map;
    39 }
    40 
    41 ArrayMap *
    42 ArrayMap::Attach(Zone *zone, Handle<ArrayType> type)
    43 {
    44     if (type->newMap())
    45         return ArrayMap::cast(type->newMap());
    46 
    47     Local<ArrayMap> map(zone, ArrayMap::New(zone, type));
    48     if (!map)
    49         return NULL;
    50 
    51     // Ensure the inner type has an ArrayMap if needed.
    52     if (type->contained()->isArray()) {
    53         Local<ArrayType> contained(zone, ArrayType::cast(type->contained()));
    54         if (!ArrayMap::Attach(zone, contained))
    55             return NULL;
    56     }
    57 
    58     type->setNewMap(map);
    59     return map;
    60 }
    61 
    62 Array *
    63 Array::NewRaw(Zone *zone, Handle<ArrayMap> map, unsigned nelements, Heap::Tenure tenure)
    64 {
    65     assert(!map->type()->isFixedLength() || nelements == map->type()->fixedLength());
    66 
    67     if (nelements > (INT_MAX / SizeForElement(map->type()))) {
    68         zone->reportError(Message_ArraySizeTooBig);
    69         return NULL;
    70     }
    71 
    72     size_t bytes = sizeof(Array);
    73     if (map->type()->hasInlineElements())
    74         bytes += SizeForElement(map->type()) * nelements;
    75 
    76     Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
    77     if (!array)
    78         return NULL;
    79 
    80     array->length_ = 0;
    81     array->elements_ = NULL;
    82 
    83     if (map->type()->hasInlineElements()) {
    84         // This must be a fixed array containing no traceables.
    85         assert(nelements > 0);
    86         assert(map->type()->isFixedLength());
    87         assert(!map->type()->contained()->isTraceable());
    88 
    89         // Compute an interior |elements_| pointer, such that if elements_
    90         // is accessed like a FixedArray, we reach the appropriate vector.
    91         array->elements_ = Address((Array *)array + 1) - ByteArray::offsetToElements();
    92         array->length_ = nelements;
    93         memset(array->bytes(), 0, SizeForElement(map->type()) * nelements);
    94         return array;
    95     }
    96 
    97     // If there is nothing to allocate, we don't bother creating elements yet.
    98     if (!nelements)
    99         return array;
   100 
   101     if (!map->type()->contained()->isTraceable()) {
   102         size_t nbytes = SizeForElement(map->type()) * nelements;
   103         Local<ByteArray> elements(zone, ByteArray::New(zone, nbytes, tenure));
   104         if (!elements)
   105             return NULL;
   106 
   107         array->updateLengthAndElements(zone, nelements, elements);
   108         return array;
   109     }
   110 
   111     Local<FixedArray> elements(zone, FixedArray::New(zone, nelements, tenure));
   112     if (!elements)
   113         return NULL;
   114 
   115     array->updateLengthAndElements(zone, nelements, elements);
   116 
   117     // If this is an array of structs, we're required to allocate those structs now.
   118     if (map->type()->contained()->isStruct()) {
   119         Local<StructType> childType(zone, StructType::cast(map->type()->contained()));
   120         Local<StructMap> childMap(zone, childType->newMap());
   121         Local<Struct> child(zone);
   122         for (unsigned i = 0; i < nelements; i++) {
   123             child = Struct::New(zone, childMap, tenure);
   124             if (!child)
   125                 return NULL;
   126 
   127             elements->set(zone, i, child);
   128         }
   129     }
   130 
   131     return array;
   132 }
   133 
   134 static Array *
   135 ConstructSizedArray(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH],
   136                     unsigned level, Heap::Tenure tenure)
   137 {
   138     Local<Array> array(zone, Array::NewRaw(zone, map, dims[level], tenure));
   139     if (!array)
   140         return NULL;
   141 
   142     if (map->type()->contained()->isArray()) {
   143         Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
   144         Local<ArrayMap> childMap(zone, childType->newMap());
   145         Local<Array> child(zone);
   146 
   147         for (unsigned i = 0; i < unsigned(dims[level]); i++) {
   148             child = ConstructSizedArray(zone, childMap, dims, level + 1, tenure);
   149             if (!child)
   150                 return NULL;
   151 
   152             array->writeObject(zone, i, child);
   153         }
   154     }
   155 
   156     return array;
   157 }
   158 
   159 Array *
   160 Array::NewSized(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH], Heap::Tenure tenure)
   161 {
   162     for (size_t i = 0; i < map->type()->levels(); i++) {
   163         if (dims[i] < 0) {
   164             zone->reportError(Message_InvalidDimensionSize, dims[i], i);
   165             return NULL;
   166         }
   167     }
   168 
   169     return ConstructSizedArray(zone, map, dims, 0, tenure);
   170 }
   171 
   172 static Array *
   173 ConstructFixedArray(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
   174 {
   175     unsigned length = map->type()->fixedLength();
   176     Local<Array> array(zone, Array::NewRaw(zone, map, length, tenure));
   177     if (!array)
   178         return NULL;
   179 
   180     if (map->type()->contained()->isArray()) {
   181         Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
   182         Local<ArrayMap> childMap(zone, childType->newMap());
   183         Local<Array> child(zone);
   184 
   185         if (childMap->type()->isFixedLength()) {
   186             for (unsigned i = 0; i < length; i++) {
   187                 child = ConstructFixedArray(zone, childMap, tenure);
   188                 if (!child)
   189                     return NULL;
   190 
   191                 array->writeObject(zone, i, child);
   192             }
   193         } else {
   194             for (unsigned i = 0; i < length; i++) {
   195                 child = Array::NewRaw(zone, childMap, 0, tenure);
   196                 if (!child)
   197                     return NULL;
   198 
   199                 array->writeObject(zone, i, child);
   200             }
   201         }
   202     }
   203 
   204     return array;
   205 }
   206 
   207 Array *
   208 Array::NewFixed(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
   209 {
   210     return ConstructFixedArray(zone, map, tenure);
   211 }
   212 
   213 Array *
   214 Array::NewString(Zone *zone, Handle<ArrayMap> map, char *buffer, size_t length, Heap::Tenure tenure)
   215 {
   216     assert(map->type()->isCharArray());
   217 
   218     Local<Array> array(zone);
   219     
   220     if (map->type()->isFixedLength())
   221         array = Array::NewFixed(zone, map, tenure);
   222     else
   223         array = Array::New1D(zone, map, length + 1, tenure);
   224     if (!array)
   225         return NULL;
   226 
   227     assert(array->length() >= length + 1);
   228     memcpy(array->chars(), buffer, length + 1);
   229     return array;
   230 }
   231 
   232 Array *
   233 Array::New1D(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
   234 {
   235     assert(map->type()->levels());
   236     return Array::NewRaw(zone, map, elements, tenure);
   237 }
   238 
   239 Array *
   240 Array::NewEmpty(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
   241 {
   242     assert(!map->type()->isFixedLength());
   243 
   244     return Array::NewRaw(zone, map, 0, tenure);
   245 }
   246 
   247 Array *
   248 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, void *buffer, unsigned length,
   249                    Heap::Tenure tenure)
   250 {
   251     Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
   252     if (!array)
   253         return NULL;
   254 
   255     array->length_ = length;
   256     array->elements_ = Address(buffer) - ByteArray::offsetToElements();
   257     return array;
   258 }
   259 
   260 static bool
   261 CopyArrayElements(Zone *zone, Handle<Type> contained, Handle<Array> dest, Handle<Array> source)
   262 {
   263     if (contained->isTraceable()) {
   264         if (contained->isStruct()) {
   265             assert(dest->type()->contained() == contained);
   266             assert(source->length() == dest->length());
   267 
   268             Local<Struct> left(zone);
   269             Local<Struct> right(zone);
   270             for (unsigned i = 0; i < source->length(); i++) {
   271                 left = Struct::cast(dest->readObject(i));
   272                 right = Struct::cast(source->readObject(i));
   273                 if (!Struct::Copy(zone, left, right))
   274                     return false;
   275             }
   276         } else if (contained->isArray() && ArrayType::cast(contained)->isFixedLength()) {
   277             assert(ArrayType::cast(source->type())->fixedLength() ==
   278                    ArrayType::cast(contained)->fixedLength());
   279 
   280             Local<Array> left(zone);
   281             Local<Array> right(zone);
   282             for (unsigned i = 0; i < source->length(); i++) {
   283                 left = Array::cast(dest->readObject(i));
   284                 right = Array::cast(source->readObject(i));
   285                 if (!Array::ShallowCopy(zone, left, right))
   286                     return false;
   287             }
   288         } else {
   289             assert(dest->length() == source->length());
   290             for (unsigned i = 0; i < source->length(); i++)
   291                 dest->writeObject(zone, i, source->readObject(i));
   292         }
   293     } else {
   294         assert(contained->pod() == dest->type()->contained()->pod());
   295         assert(contained->pod() == PrimitiveType_Char
   296                ? source->length() <= dest->length()
   297                : source->length() == dest->length());
   298     
   299         assert(dest->length());
   300         memcpy(dest->bytes(), source->bytes(), source->length() * Array::SizeForElement(dest->type()));
   301     }
   302     return true;
   303 }
   304 
   305 // Shallow copy an array. Fixed slots in the destination are copied from
   306 // the source, but otherwise, references are assigned.
   307 bool
   308 Array::ShallowCopy(Zone *zone, Handle<Array> dest, Handle<Array> source)
   309 {
   310     Local<Type> contained(zone, dest->type()->contained());
   311 
   312     return CopyArrayElements(zone, contained, dest, source);
   313 }
   314 
   315 Array *
   316 Array::DeepCopy(Zone *zone, Handle<Array> source, Heap::Tenure tenure)
   317 {
   318     Local<ArrayMap> map(zone, ArrayMap::cast(source->map()));
   319 
   320     Local<Array> dest(zone, Array::NewRaw(zone, map, source->length(), tenure));
   321     if (!dest)
   322         return NULL;
   323 
   324     Local<Type> contained(zone, source->type()->contained());
   325     if (contained->isArray()) {
   326         assert(false);
   327         Local<Array> sub(zone);
   328         for (size_t i = 0; i < source->length(); i++) {
   329             sub = Array::cast(source->readObject(i));
   330             sub = DeepCopy(zone, sub, tenure);
   331             if (!sub)
   332                 return NULL;
   333             dest->writeObject(zone, i, sub);
   334         }
   335     } else {
   336         if (!CopyArrayElements(zone, contained, dest, source))
   337             return NULL;
   338     }
   339 
   340     return dest;
   341 }