src/FixedArray.cpp
changeset 86 32ad0523b309
parent 85 06a0015b6a5c
child 91 5f4402180204
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/FixedArray.cpp	Tue Jul 24 23:53:34 2012 -0700
     1.3 @@ -0,0 +1,76 @@
     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 +#include "Heap-inl.h"
    1.25 +
    1.26 +using namespace ke;
    1.27 +
    1.28 +static const unsigned kMaxFixedArrayLength = ((INT_MAX - sizeof(FixedArray)) / sizeof(ke::Object *));
    1.29 +
    1.30 +FixedArray *
    1.31 +FixedArray::New(Zone *zone, unsigned length, Heap::Tenure tenure)
    1.32 +{
    1.33 +    if (length >= kMaxFixedArrayLength) {
    1.34 +        zone->reportError(Message_ArrayTooBig);
    1.35 +        return NULL;
    1.36 +    }
    1.37 +
    1.38 +    Local<FixedArray> array(zone, FixedArray::cast(zone->allocate(MapKind_FixedArray, SizeFor(length), tenure)));
    1.39 +    if (!array)
    1.40 +        return NULL;
    1.41 +
    1.42 +    array->length_ = length;
    1.43 +    memset(array->vec(), 0, length * sizeof(Object *));
    1.44 +    return array;
    1.45 +}
    1.46 +
    1.47 +ByteArray *
    1.48 +ByteArray::New(Zone *zone, unsigned length, Heap::Tenure tenure)
    1.49 +{
    1.50 +    if (length >= kMaxFixedArrayLength) {
    1.51 +        zone->reportError(Message_ArrayTooBig);
    1.52 +        return NULL;
    1.53 +    }
    1.54 +
    1.55 +    Local<ByteArray> array(zone, ByteArray::cast(zone->allocate(MapKind_ByteArray, SizeFor(length), tenure)));
    1.56 +    if (!array)
    1.57 +        return NULL;
    1.58 +
    1.59 +    array->length_ = length;
    1.60 +    memset(array->raw(), 0, length);
    1.61 +    return array;
    1.62 +}
    1.63 +
    1.64 +ByteArray *
    1.65 +ByteArray::New(Zone *zone, Handle<Map> map, unsigned length, Heap::Tenure tenure)
    1.66 +{
    1.67 +    if (length >= kMaxFixedArrayLength) {
    1.68 +        zone->reportError(Message_ArrayTooBig);
    1.69 +        return NULL;
    1.70 +    }
    1.71 +
    1.72 +    Local<ByteArray> array(zone, ByteArray::cast(zone->allocate(map, SizeFor(length), tenure)));
    1.73 +    if (!array)
    1.74 +        return NULL;
    1.75 +
    1.76 +    array->length_ = length;
    1.77 +    memset(array->raw(), 0, length);
    1.78 +    return array;
    1.79 +}