src/FixedArray.cpp
author David Anderson <dvander@alliedmods.net>
Sat Sep 15 17:39:45 2012 -0700 (2012-09-15)
changeset 184 387d76b1e981
parent 91 5f4402180204
child 188 bab29bcec7fe
permissions -rw-r--r--
Add support for array reference semantics.
     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 <limits.h>
    19 #include <string.h>
    20 #include "FixedArray.h"
    21 #include "Zone.h"
    22 #include "Heap-inl.h"
    23 
    24 using namespace ke;
    25 
    26 static const unsigned kMaxFixedArrayLength = ((INT_MAX - sizeof(FixedArray)) / sizeof(ke::Object *));
    27 
    28 FixedArray *
    29 FixedArray::New(Zone *zone, unsigned length, Heap::Tenure tenure)
    30 {
    31     if (length >= kMaxFixedArrayLength) {
    32         zone->reportError(Message_ArrayTooBig);
    33         return NULL;
    34     }
    35 
    36     Local<FixedArray> array(zone, FixedArray::cast(zone->allocate(MapKind_FixedArray, SizeFor(length), tenure)));
    37     if (!array)
    38         return NULL;
    39 
    40     array->length_ = length;
    41     memset(array->vec(), 0, length * sizeof(Object *));
    42     return array;
    43 }
    44 
    45 ByteArray *
    46 ByteArray::New(Zone *zone, unsigned length, Heap::Tenure tenure)
    47 {
    48     if (length >= kMaxFixedArrayLength) {
    49         zone->reportError(Message_ArrayTooBig);
    50         return NULL;
    51     }
    52 
    53     Local<ByteArray> array(zone, ByteArray::cast(zone->allocate(MapKind_ByteArray, SizeFor(length), tenure)));
    54     if (!array)
    55         return NULL;
    56 
    57     array->length_ = length;
    58     memset(array->raw(), 0, length);
    59     return array;
    60 }
    61 
    62 ByteArray *
    63 ByteArray::New(Zone *zone, Handle<Map> map, unsigned length, Heap::Tenure tenure)
    64 {
    65     if (length >= kMaxFixedArrayLength) {
    66         zone->reportError(Message_ArrayTooBig);
    67         return NULL;
    68     }
    69 
    70     Local<ByteArray> array(zone, ByteArray::cast(zone->allocate(map, SizeFor(length), tenure)));
    71     if (!array)
    72         return NULL;
    73 
    74     array->length_ = length;
    75     memset(array->raw(), 0, length);
    76     return array;
    77 }