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