src/Array.cpp
author David Anderson <dvander@alliedmods.net>
Fri Dec 28 17:13:51 2012 -0800 (2012-12-28)
changeset 247 7721042bdb67
parent 236 072f1114ea5d
child 256 3c184218462d
permissions -rw-r--r--
Implement dependent char 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 #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::NewDependent(Zone *zone, Handle<ArrayType> type)
    43 {
    44     Local<Map> obj(zone, zone->allocateMap(MapKind_DependentArray, sizeof(ArrayMap), MapKind_MetaArray));
    45     if (!obj)
    46         return NULL;
    47 
    48     Local<ArrayMap> map(zone, ArrayMap::cast(obj));
    49     map->type_ = type;
    50     return map;
    51 }
    52 
    53 ArrayMap *
    54 ArrayMap::Attach(Zone *zone, Handle<ArrayType> type)
    55 {
    56     if (type->newMap())
    57         return ArrayMap::cast(type->newMap());
    58 
    59     Local<ArrayMap> map(zone, ArrayMap::New(zone, type));
    60     if (!map)
    61         return NULL;
    62 
    63     // Ensure the inner type has an ArrayMap if needed.
    64     if (type->contained()->isArray()) {
    65         Local<ArrayType> contained(zone, ArrayType::cast(type->contained()));
    66         if (!ArrayMap::Attach(zone, contained))
    67             return NULL;
    68     }
    69 
    70     type->setNewMap(map);
    71     return map;
    72 }
    73 
    74 Array *
    75 Array::NewRaw(Zone *zone, Handle<ArrayMap> map, unsigned nelements, Heap::Tenure tenure)
    76 {
    77     assert(!map->type()->isFixedLength() || nelements == map->type()->fixedLength());
    78 
    79     if (nelements > (INT_MAX / SizeForElement(map->type()))) {
    80         zone->reportError(Message_ArraySizeTooBig);
    81         return NULL;
    82     }
    83 
    84     size_t bytes = sizeof(Array);
    85     if (map->type()->hasInlineElements())
    86         bytes += SizeForElement(map->type()) * nelements;
    87 
    88     Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
    89     if (!array)
    90         return NULL;
    91 
    92     array->length_ = 0;
    93     array->elements_ = NULL;
    94 
    95     if (map->type()->hasInlineElements()) {
    96         // This must be a fixed array containing no traceables.
    97         assert(nelements > 0);
    98         assert(map->type()->isFixedLength());
    99         assert(!map->type()->contained()->isTraceable());
   100 
   101         // Compute an interior |elements_| pointer, such that if elements_
   102         // is accessed like a FixedArray, we reach the appropriate vector.
   103         array->elements_ = Address((Array *)array + 1) - ByteArray::offsetToElements();
   104         array->length_ = nelements;
   105         memset(array->bytes(), 0, SizeForElement(map->type()) * nelements);
   106         return array;
   107     }
   108 
   109     // If there is nothing to allocate, we don't bother creating elements yet.
   110     if (!nelements)
   111         return array;
   112 
   113     if (!map->type()->contained()->isTraceable()) {
   114         size_t nbytes = SizeForElement(map->type()) * nelements;
   115         Local<ByteArray> elements(zone, ByteArray::New(zone, nbytes, tenure));
   116         if (!elements)
   117             return NULL;
   118 
   119         array->updateLengthAndElements(zone, nelements, elements);
   120         return array;
   121     }
   122 
   123     Local<FixedArray> elements(zone, FixedArray::New(zone, nelements, tenure));
   124     if (!elements)
   125         return NULL;
   126 
   127     array->updateLengthAndElements(zone, nelements, elements);
   128 
   129     // If this is an array of structs, we're required to allocate those structs now.
   130     if (map->type()->contained()->isStruct()) {
   131         Local<StructType> childType(zone, StructType::cast(map->type()->contained()));
   132         Local<StructMap> childMap(zone, childType->newMap());
   133         Local<Struct> child(zone);
   134         for (unsigned i = 0; i < nelements; i++) {
   135             child = Struct::New(zone, childMap, tenure);
   136             if (!child)
   137                 return NULL;
   138 
   139             elements->set(zone, i, child);
   140         }
   141     }
   142 
   143     return array;
   144 }
   145 
   146 static Array *
   147 ConstructSizedArray(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH],
   148                     unsigned level, Heap::Tenure tenure)
   149 {
   150     Local<Array> array(zone, Array::NewRaw(zone, map, dims[level], tenure));
   151     if (!array)
   152         return NULL;
   153 
   154     if (map->type()->contained()->isArray()) {
   155         Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
   156         Local<ArrayMap> childMap(zone, childType->newMap());
   157         Local<Array> child(zone);
   158 
   159         for (unsigned i = 0; i < unsigned(dims[level]); i++) {
   160             child = ConstructSizedArray(zone, childMap, dims, level + 1, tenure);
   161             if (!child)
   162                 return NULL;
   163 
   164             array->writeObject(zone, i, child);
   165         }
   166     }
   167 
   168     return array;
   169 }
   170 
   171 Array *
   172 Array::NewSized(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH], Heap::Tenure tenure)
   173 {
   174     for (size_t i = 0; i < map->type()->levels(); i++) {
   175         if (dims[i] < 0) {
   176             zone->reportError(Message_InvalidDimensionSize, dims[i], i);
   177             return NULL;
   178         }
   179     }
   180 
   181     return ConstructSizedArray(zone, map, dims, 0, tenure);
   182 }
   183 
   184 static Array *
   185 ConstructFixedArray(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
   186 {
   187     unsigned length = map->type()->fixedLength();
   188     Local<Array> array(zone, Array::NewRaw(zone, map, length, tenure));
   189     if (!array)
   190         return NULL;
   191 
   192     if (map->type()->contained()->isArray()) {
   193         Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
   194         Local<ArrayMap> childMap(zone, childType->newMap());
   195         Local<Array> child(zone);
   196 
   197         if (childMap->type()->isFixedLength()) {
   198             for (unsigned i = 0; i < length; i++) {
   199                 child = ConstructFixedArray(zone, childMap, tenure);
   200                 if (!child)
   201                     return NULL;
   202 
   203                 array->writeObject(zone, i, child);
   204             }
   205         } else {
   206             for (unsigned i = 0; i < length; i++) {
   207                 child = Array::NewRaw(zone, childMap, 0, tenure);
   208                 if (!child)
   209                     return NULL;
   210 
   211                 array->writeObject(zone, i, child);
   212             }
   213         }
   214     }
   215 
   216     return array;
   217 }
   218 
   219 Array *
   220 Array::NewFixed(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
   221 {
   222     return ConstructFixedArray(zone, map, tenure);
   223 }
   224 
   225 Array *
   226 Array::NewString(Zone *zone, Handle<ArrayMap> map, char *buffer, size_t length, Heap::Tenure tenure)
   227 {
   228     assert(map->type()->isCharArray());
   229 
   230     Local<Array> array(zone);
   231     
   232     if (map->type()->isFixedLength())
   233         array = Array::NewFixed(zone, map, tenure);
   234     else
   235         array = Array::New1D(zone, map, length + 1, tenure);
   236     if (!array)
   237         return NULL;
   238 
   239     assert(array->length() >= length + 1);
   240     memcpy(array->chars(), buffer, length + 1);
   241     return array;
   242 }
   243 
   244 Array *
   245 Array::New1D(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
   246 {
   247     assert(map->type()->levels());
   248     return Array::NewRaw(zone, map, elements, tenure);
   249 }
   250 
   251 Array *
   252 Array::NewEmpty(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
   253 {
   254     assert(!map->type()->isFixedLength());
   255 
   256     return Array::NewRaw(zone, map, 0, tenure);
   257 }
   258 
   259 Array *
   260 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, void *buffer, unsigned length,
   261                    Heap::Tenure tenure)
   262 {
   263     Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
   264     if (!array)
   265         return NULL;
   266 
   267     array->length_ = length;
   268     array->elements_ = Address(buffer) - ByteArray::offsetToElements();
   269     return array;
   270 }
   271 
   272 static bool
   273 CopyArrayElements(Zone *zone, Handle<Type> contained, Handle<Array> dest, Handle<Array> source)
   274 {
   275     if (contained->isTraceable()) {
   276         if (contained->isStruct()) {
   277             assert(dest->type()->contained() == contained);
   278             assert(source->length() == dest->length());
   279 
   280             Local<Struct> left(zone);
   281             Local<Struct> right(zone);
   282             for (unsigned i = 0; i < source->length(); i++) {
   283                 left = Struct::cast(dest->readObject(i));
   284                 right = Struct::cast(source->readObject(i));
   285                 if (!Struct::Copy(zone, left, right))
   286                     return false;
   287             }
   288         } else if (contained->isArray() && ArrayType::cast(contained)->isFixedLength()) {
   289             assert(ArrayType::cast(source->type())->fixedLength() ==
   290                    ArrayType::cast(contained)->fixedLength());
   291 
   292             Local<Array> left(zone);
   293             Local<Array> right(zone);
   294             for (unsigned i = 0; i < source->length(); i++) {
   295                 left = Array::cast(dest->readObject(i));
   296                 right = Array::cast(source->readObject(i));
   297                 if (!Array::ShallowCopy(zone, left, right))
   298                     return false;
   299             }
   300         } else {
   301             assert(dest->length() == source->length());
   302             for (unsigned i = 0; i < source->length(); i++)
   303                 dest->writeObject(zone, i, source->readObject(i));
   304         }
   305     } else {
   306         assert(contained->pod() == dest->type()->contained()->pod());
   307         assert(contained->pod() == PrimitiveType_Char
   308                ? source->length() <= dest->length()
   309                : source->length() == dest->length());
   310     
   311         assert(dest->length());
   312         memcpy(dest->bytes(), source->bytes(), source->length() * Array::SizeForElement(dest->type()));
   313     }
   314     return true;
   315 }
   316 
   317 // Shallow copy an array. Fixed slots in the destination are copied from
   318 // the source, but otherwise, references are assigned.
   319 bool
   320 Array::ShallowCopy(Zone *zone, Handle<Array> dest, Handle<Array> source)
   321 {
   322     Local<Type> contained(zone, dest->type()->contained());
   323 
   324     return CopyArrayElements(zone, contained, dest, source);
   325 }
   326 
   327 Array *
   328 Array::DeepCopy(Zone *zone, Handle<Array> source, Heap::Tenure tenure)
   329 {
   330     Local<ArrayMap> map(zone, ArrayMap::cast(source->map()));
   331 
   332     Local<Array> dest(zone, Array::NewRaw(zone, map, source->length(), tenure));
   333     if (!dest)
   334         return NULL;
   335 
   336     Local<Type> contained(zone, source->type()->contained());
   337     if (contained->isArray()) {
   338         assert(false);
   339         Local<Array> sub(zone);
   340         for (size_t i = 0; i < source->length(); i++) {
   341             sub = Array::cast(source->readObject(i));
   342             sub = DeepCopy(zone, sub, tenure);
   343             if (!sub)
   344                 return NULL;
   345             dest->writeObject(zone, i, sub);
   346         }
   347     } else {
   348         if (!CopyArrayElements(zone, contained, dest, source))
   349             return NULL;
   350     }
   351 
   352     return dest;
   353 }
   354 
   355 Array *
   356 DependentArray::New(Zone *zone, Handle<Array> source, Handle<ArrayMap> map, unsigned index)
   357 {
   358     Local<ArrayType> sourcetype(zone, source->type());
   359 
   360     // Right now we only support char arrays, to avoid nasty deep cloning
   361     // and to maybe cut off this feature as far as we can.
   362     assert(sourcetype->isCharArray());
   363     assert(map->type()->isCharArray());
   364 
   365     // We allow taking 0-length dependent strings. It's safe because any reads
   366     // or writes will fail.
   367     assert(index <= source->length());
   368 
   369     unsigned newlen = source->length() - index;
   370     unsigned eltsize = SizeForElement(sourcetype);
   371     
   372     if (!sourcetype->isFixedLength()) {
   373         // Simple case, just copy the array.
   374         Local<ArrayMap> map(zone, sourcetype->newMap());
   375         
   376         Local<Array> dest(zone, Array::NewRaw(zone, map, newlen, Heap::Tenure_Default));
   377         if (!dest)
   378             return NULL;
   379 
   380         memcpy(dest->bytes(),
   381                (Address)source->bytes() + (index * eltsize),
   382                newlen * eltsize);
   383         return dest;
   384     }
   385 
   386     Local<DependentArray> array(zone, DependentArray::cast(zone->allocate(map, sizeof(DependentArray), Heap::Tenure_Default)));
   387     if (!array)
   388         return NULL;
   389 
   390     // :GC: This is currently not safe for moving GC! Although we will mark
   391     // the object owning the interior pointer, the interior pointer could be
   392     // moved later. We will eventually need some way of dealing with this.
   393     Address elements = Address(source->bytes()) + (index * eltsize);
   394 
   395     array->root_ = source;
   396     array->length_ = newlen;
   397     array->elements_ = elements - ByteArray::offsetToElements();
   398     return array;
   399 }