jitcraft/FixedArray.cpp
changeset 12 75e385f68b7e
child 13 627b6b94e774
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/jitcraft/FixedArray.cpp	Sat Jun 30 19:38:53 2012 -0700
     1.3 @@ -0,0 +1,73 @@
     1.4 +/*
     1.5 + * Copyright (C) 2012 David Anderson
     1.6 + *
     1.7 + * This file is part of JITCraft.
     1.8 + *
     1.9 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.10 + * the terms of the GNU General Public License as published by the Free
    1.11 + * Software Foundation, either version 3 of the License, or (at your option)
    1.12 + * any later version.
    1.13 + * 
    1.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License along with
    1.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.20 + */
    1.21 +#include <limits.h>
    1.22 +#include "FixedArray.h"
    1.23 +#include "Zone.h"
    1.24 +
    1.25 +using namespace jc;
    1.26 +
    1.27 +static const unsigned kMaxFixedArrayLength = ((INT_MAX - sizeof(FixedArray)) / sizeof(jc::Object *));
    1.28 +
    1.29 +void
    1.30 +FixedArray::init(unsigned length)
    1.31 +{
    1.32 +    WRITE_FIELD(length, length);
    1.33 +}
    1.34 +
    1.35 +FixedArray *
    1.36 +FixedArray::New(Zone *zone, unsigned length, Heap::Tenure tenure)
    1.37 +{
    1.38 +    // :TODO: error...
    1.39 +    if (length >= kMaxFixedArrayLength) {
    1.40 +        assert(false);
    1.41 +        return NULL;
    1.42 +    }
    1.43 +
    1.44 +    Object *obj = zone->allocate(MapKind_FixedArray, SizeFor(length), tenure);
    1.45 +    if (!obj)
    1.46 +        return NULL;
    1.47 +
    1.48 +    FixedArray *array = FixedArray::cast(obj);
    1.49 +    array->init(length);
    1.50 +    return array;
    1.51 +}
    1.52 +
    1.53 +void
    1.54 +ByteArray::init(unsigned length)
    1.55 +{
    1.56 +    WRITE_FIELD(length, length);
    1.57 +}
    1.58 +
    1.59 +ByteArray *
    1.60 +ByteArray::New(Zone *zone, unsigned length, Heap::Tenure tenure)
    1.61 +{
    1.62 +    // :TODO: error...
    1.63 +    if (length >= kMaxFixedArrayLength) {
    1.64 +        assert(false);
    1.65 +        return NULL;
    1.66 +    }
    1.67 +
    1.68 +    Object *obj = zone->allocate(MapKind_ByteArray, SizeFor(length), tenure);
    1.69 +    if (!obj)
    1.70 +        return NULL;
    1.71 +
    1.72 +    ByteArray *array = ByteArray::cast(obj);
    1.73 +    array->init(length);
    1.74 +    return array;
    1.75 +}
    1.76 +