src/Array.cpp
author David Anderson <dvander@alliedmods.net>
Sat Sep 15 19:33:24 2012 -0700 (2012-09-15)
changeset 186 03a663e96860
parent 185 10936f3a2ba3
child 187 9308d52205a0
permissions -rw-r--r--
Allow more empty array creation.
     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 "Heap.h"
    22 #include "Zone.h"
    23 #include "Structures.h"
    24 #include "Heap-inl.h"
    25 
    26 using namespace ke;
    27 
    28 ArrayMap *
    29 ArrayMap::New(Zone *zone, Handle<Type> type)
    30 {
    31     Local<Map> obj(zone, zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray));
    32     if (!obj)
    33         return NULL;
    34 
    35     Local<ArrayMap> map(zone, ArrayMap::cast(obj));
    36     map->type_ = type;
    37     return map;
    38 }
    39 
    40 ArrayMap *
    41 ArrayMap::Attach(Zone *zone, Handle<Type> type)
    42 {
    43     if (type->newMap())
    44         return ArrayMap::cast(type->newMap());
    45 
    46     Local<ArrayMap> map(zone, ArrayMap::New(zone, type));
    47     if (!map)
    48         return NULL;
    49 
    50     // Ensure the inner type has an ArrayMap if needed.
    51     if (type->contained()->isArray()) {
    52         Local<Type> contained(zone, type->contained());
    53         if (!ArrayMap::Attach(zone, contained))
    54             return NULL;
    55     }
    56 
    57     type->setNewMap(map);
    58     return map;
    59 }
    60 
    61 static inline size_t
    62 SizeFor(Type *type, unsigned elements)
    63 {
    64     if (type->contained()->isTraceable())
    65         return sizeof(Array) + elements * sizeof(ke::Object *);
    66     if (type->contained()->isInt32())
    67         return sizeof(Array) + elements * sizeof(int);
    68     if (type->contained()->primitive() == PrimitiveType_Char)
    69         return sizeof(Array) + elements * sizeof(char);
    70     assert(type->contained()->isFloat());
    71     return elements * sizeof(float);
    72 }
    73 
    74 Array *
    75 Array::New(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
    76 {
    77     assert(map->type()->isFixedLength());
    78 
    79     return Array::New(zone, map, map->type()->fixedLength(), tenure);
    80 }
    81 
    82 Array *
    83 Array::New(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
    84 {
    85     size_t elementBytes = SizeFor(map->type(), elements);
    86     size_t bytes = sizeof(Array) + elementBytes;
    87     Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
    88     if (!array)
    89         return NULL;
    90 
    91     array->nelements_ = elements;
    92     array->elements_ = (Array *)array + 1;
    93 
    94     if (map->type()->contained()->isArray()) {
    95         Local<ArrayMap> childMap(zone, ArrayMap::cast(map->type()->contained()->newMap()));
    96         Local<Array> child(zone);
    97         for (unsigned i = 0; i < elements; i++) {
    98             unsigned length = map->type()->contained()->isFixedLength()
    99                               ? map->type()->contained()->fixedLength()
   100                               : 0;
   101             child = Array::New(zone, childMap, length, tenure);
   102             if (!child)
   103                 return NULL;
   104 
   105             array->writeObject(zone, i, child);
   106         }
   107     } else if (map->type()->contained()->isStruct()) {
   108         Local<StructMap> childMap(zone, StructMap::cast(map->type()->contained()->newMap()));
   109         Local<Struct> child(zone);
   110         for (unsigned i = 0; i < elements; i++) {
   111             child = Struct::New(zone, childMap, tenure);
   112             if (!child)
   113                 return NULL;
   114 
   115             array->writeObject(zone, i, child);
   116         }
   117     } else {
   118         memset(array->elements_, 0, elementBytes);
   119     }
   120     return array;
   121 }
   122 
   123 Array *
   124 Array::NewString(Zone *zone, Handle<ArrayMap> map, Address buffer, size_t length, Heap::Tenure tenure)
   125 {
   126     Local<Array> array(zone, Array::New(zone, map, length, tenure));
   127     if (!array)
   128         return NULL;
   129 
   130     assert(array->length() >= length);
   131     memcpy(array->chars(), buffer, length);
   132     return array;
   133 }
   134 
   135 Array *
   136 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, uint32 nelements, void *buffer,
   137                    Heap::Tenure tenure)
   138 {
   139     Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
   140     if (!array)
   141         return NULL;
   142 
   143     array->nelements_ = nelements;
   144     array->elements_ = buffer;
   145     return array;
   146 }
   147