jitcraft/FixedArray.cpp
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 19:38:53 2012 -0700 (2012-06-30)
changeset 12 75e385f68b7e
child 13 627b6b94e774
permissions -rw-r--r--
Refactored Array, Code to be Objects. Removed Object tagging.
     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 
    22 using namespace jc;
    23 
    24 static const unsigned kMaxFixedArrayLength = ((INT_MAX - sizeof(FixedArray)) / sizeof(jc::Object *));
    25 
    26 void
    27 FixedArray::init(unsigned length)
    28 {
    29     WRITE_FIELD(length, length);
    30 }
    31 
    32 FixedArray *
    33 FixedArray::New(Zone *zone, unsigned length, Heap::Tenure tenure)
    34 {
    35     // :TODO: error...
    36     if (length >= kMaxFixedArrayLength) {
    37         assert(false);
    38         return NULL;
    39     }
    40 
    41     Object *obj = zone->allocate(MapKind_FixedArray, SizeFor(length), tenure);
    42     if (!obj)
    43         return NULL;
    44 
    45     FixedArray *array = FixedArray::cast(obj);
    46     array->init(length);
    47     return array;
    48 }
    49 
    50 void
    51 ByteArray::init(unsigned length)
    52 {
    53     WRITE_FIELD(length, length);
    54 }
    55 
    56 ByteArray *
    57 ByteArray::New(Zone *zone, unsigned length, Heap::Tenure tenure)
    58 {
    59     // :TODO: error...
    60     if (length >= kMaxFixedArrayLength) {
    61         assert(false);
    62         return NULL;
    63     }
    64 
    65     Object *obj = zone->allocate(MapKind_ByteArray, SizeFor(length), tenure);
    66     if (!obj)
    67         return NULL;
    68 
    69     ByteArray *array = ByteArray::cast(obj);
    70     array->init(length);
    71     return array;
    72 }
    73