src/Array.cpp
author David Anderson <danderson@mozilla.com>
Wed Jul 25 01:32:49 2012 -0700 (2012-07-25)
changeset 93 23189d937087
parent 91 5f4402180204
child 154 a85a0303bcb8
permissions -rw-r--r--
Fix opt clang build.
     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 #include <string.h>
    19 #include "Array.h"
    20 #include "Heap.h"
    21 #include "Zone.h"
    22 #include "Heap-inl.h"
    23 
    24 using namespace ke;
    25 
    26 ArrayMap *
    27 ArrayMap::New(Zone *zone, Handle<Type> type, Handle<ArrayMap> contained)
    28 {
    29     Local<Map> obj(zone, zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray));
    30     if (!obj)
    31         return NULL;
    32 
    33     Local<ArrayMap> map(zone, ArrayMap::cast(obj));
    34     map->type_ = type;
    35     map->contained_ = contained;
    36     return map;
    37 }
    38 
    39 static inline size_t
    40 SizeFor(Type *type, unsigned elements)
    41 {
    42     if (type->levels() > 1)
    43         return sizeof(Array) + elements * sizeof(ke::Object *);
    44     if (type->contained()->isInt32())
    45         return sizeof(Array) + elements * sizeof(int);
    46     if (type->contained()->primitive() == PrimitiveType_Char)
    47         return sizeof(Array) + elements * sizeof(char);
    48     assert(type->contained()->isFloat());
    49     return elements * sizeof(float);
    50 }
    51 
    52 Array *
    53 Array::New(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
    54 {
    55     size_t elementBytes = SizeFor(map->type(), elements);
    56     size_t bytes = sizeof(Array) + elementBytes;
    57     Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
    58     if (!array)
    59         return NULL;
    60 
    61     array->nelements_ = elements;
    62     array->elements_ = (Array *)array + 1;
    63     memset(array->elements_, 0, elementBytes);
    64     return array;
    65 }
    66 
    67 Array *
    68 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, uint32 nelements, void *buffer,
    69                    Heap::Tenure tenure)
    70 {
    71     Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
    72     if (!array)
    73         return NULL;
    74 
    75     array->nelements_ = nelements;
    76     array->elements_ = buffer;
    77     return array;
    78 }