src/FixedArray.cpp
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 01:04:43 2013 +0100 (2013-06-10)
changeset 263 ba85a47ee414
parent 258 241d082d6d89
permissions -rw-r--r--
Normalise line endings.
asherkin@263
     1
/*
asherkin@263
     2
 * Copyright (C) 2012 David Anderson
asherkin@263
     3
 *
asherkin@263
     4
 * This file is part of SourcePawn.
asherkin@263
     5
 *
asherkin@263
     6
 * SourcePawn is free software: you can redistribute it and/or modify it under
asherkin@263
     7
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     8
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
     9
 * any later version.
asherkin@263
    10
 * 
asherkin@263
    11
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    14
 *
asherkin@263
    15
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    16
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
asherkin@263
    17
 */
asherkin@263
    18
#include <limits.h>
asherkin@263
    19
#include <string.h>
asherkin@263
    20
#include "FixedArray.h"
asherkin@263
    21
#include "Zone.h"
asherkin@263
    22
#include "Heap-inl.h"
asherkin@263
    23
asherkin@263
    24
using namespace ke;
asherkin@263
    25
asherkin@263
    26
static const unsigned kMaxFixedArrayLength = ((INT_MAX - sizeof(FixedArray)) / sizeof(ke::Object *));
asherkin@263
    27
asherkin@263
    28
FixedArray *
asherkin@263
    29
FixedArray::New(Zone *zone, unsigned length, Heap::Tenure tenure)
asherkin@263
    30
{
asherkin@263
    31
    if (length >= kMaxFixedArrayLength) {
asherkin@263
    32
        zone->reportError(Message_ArrayTooBig);
asherkin@263
    33
        return NULL;
asherkin@263
    34
    }
asherkin@263
    35
asherkin@263
    36
    Local<FixedArray> array(zone, FixedArray::cast(zone->allocate(MapKind_FixedArray, SizeFor(length), tenure)));
asherkin@263
    37
    if (!array)
asherkin@263
    38
        return NULL;
asherkin@263
    39
asherkin@263
    40
    array->length_ = length;
asherkin@263
    41
    memset(array->vec(), 0, length * sizeof(Object *));
asherkin@263
    42
    return array;
asherkin@263
    43
}
asherkin@263
    44
asherkin@263
    45
ByteArray *
asherkin@263
    46
ByteArray::New(Zone *zone, unsigned length, Heap::Tenure tenure)
asherkin@263
    47
{
asherkin@263
    48
    if (length >= kMaxFixedArrayLength) {
asherkin@263
    49
        zone->reportError(Message_ArrayTooBig);
asherkin@263
    50
        return NULL;
asherkin@263
    51
    }
asherkin@263
    52
asherkin@263
    53
    Local<ByteArray> array(zone, ByteArray::cast(zone->allocate(MapKind_ByteArray, SizeFor(length), tenure)));
asherkin@263
    54
    if (!array)
asherkin@263
    55
        return NULL;
asherkin@263
    56
asherkin@263
    57
    array->length_ = length;
asherkin@263
    58
    memset(array->raw(), 0, length);
asherkin@263
    59
    return array;
asherkin@263
    60
}
asherkin@263
    61
asherkin@263
    62
ByteArray *
asherkin@263
    63
ByteArray::New(Zone *zone, Handle<Map> map, unsigned length, Heap::Tenure tenure)
asherkin@263
    64
{
asherkin@263
    65
    if (length >= kMaxFixedArrayLength) {
asherkin@263
    66
        zone->reportError(Message_ArrayTooBig);
asherkin@263
    67
        return NULL;
asherkin@263
    68
    }
asherkin@263
    69
asherkin@263
    70
    Local<ByteArray> array(zone, ByteArray::cast(zone->allocate(map, SizeFor(length), tenure)));
asherkin@263
    71
    if (!array)
asherkin@263
    72
        return NULL;
asherkin@263
    73
asherkin@263
    74
    array->length_ = length;
asherkin@263
    75
    memset(array->raw(), 0, length);
asherkin@263
    76
    return array;
asherkin@263
    77
}
asherkin@263
    78