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