diff -r a350c2cc736a -r 75e385f68b7e jitcraft/Array.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jitcraft/Array.cpp Sat Jun 30 19:38:53 2012 -0700 @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2012 David Anderson + * + * This file is part of JITCraft. + * + * JITCraft is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * JITCraft. If not, see http://www.gnu.org/licenses/. + */ +#include "Array.h" +#include "Heap.h" +#include "Zone.h" +#include "Heap-inl.h" + +using namespace jc; + +ArrayMap * +ArrayMap::New(Zone *zone, Type *type, ArrayMap *contained) +{ + // :TODO: use Local + Map *obj = zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray); + if (!obj) + return NULL; + + ArrayMap *map = ArrayMap::cast(obj); + map->init(type, contained); + return map; +} + +static inline size_t +SizeFor(Type *type, unsigned elements) +{ + if (type->levels() > 1) + return sizeof(Array) + elements * sizeof(jc::Object *); + if (type->contained()->isInt()) + return sizeof(Array) + elements * sizeof(int); + assert(type->contained()->isFloat()); + return sizeof(Array) + elements * sizeof(float); +} + +void +Array::init(uint32 nelements) +{ + WRITE_FIELD(nelements, nelements); + WRITE_FIELD(elements, VoidPtr(this + 1)); +} + +Array * +Array::New(Zone *zone, ArrayMap *map, uint32 elements, Heap::Tenure tenure) +{ + // :TODO: handles + size_t bytes = SizeFor(map->type(), elements); + Object *obj = zone->allocate(map, bytes, tenure); + if (!obj) + return NULL; + + Array *array = Array::cast(obj); + array->init(elements); + return Array::cast(obj); +}