jitcraft/Array.cpp
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 22:45:25 2012 -0700 (2012-06-30)
changeset 16 23627f8bc421
parent 13 627b6b94e774
child 20 e020a14e21be
permissions -rw-r--r--
Added support for arbitrary expression references.
     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 jc;
    24 
    25 ArrayMap *
    26 ArrayMap::New(Zone *zone, Type *type, ArrayMap *contained)
    27 {
    28     // :TODO: use Local
    29     Map *obj = zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray);
    30     if (!obj)
    31         return NULL;
    32 
    33     ArrayMap *map = 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(jc::Object *);
    44     if (type->contained()->isInt())
    45         return sizeof(Array) + elements * sizeof(int);
    46     assert(type->contained()->isFloat());
    47     return sizeof(Array) + elements * sizeof(float);
    48 }
    49 
    50 Array *
    51 Array::New(Zone *zone, ArrayMap *map, uint32 elements, Heap::Tenure tenure)
    52 {
    53     // :TODO: handles
    54     size_t bytes = SizeFor(map->type(), elements);
    55     Object *obj = zone->allocate(map, bytes, tenure);
    56     if (!obj)
    57         return NULL;
    58 
    59     Array *array = Array::cast(obj);
    60     array->nelements_ = elements;
    61     array->elements_ = VoidPtr(array + 1);
    62     return Array::cast(obj);
    63 }