src/StringPool.cpp
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 15:52:44 2013 +0100 (2013-06-10)
changeset 265 3a903b89a514
parent 263 ba85a47ee414
permissions -rw-r--r--
Fixed type checking for multidimensional arrays.
asherkin@263
     1
/*
asherkin@263
     2
 * Copyright (C) 2012 David Anderson
asherkin@263
     3
 *
asherkin@263
     4
 * This file is part of JITCraft.
asherkin@263
     5
 *
asherkin@263
     6
 * JITCraft 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
 * Foobar 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
 * JITCraft. If not, see http://www.gnu.org/licenses/.
asherkin@263
    17
 */
asherkin@263
    18
#include "Zone.h"
asherkin@263
    19
#include "StringPool.h"
asherkin@263
    20
#include "Strings.h"
asherkin@263
    21
#include "Spaces.h"
asherkin@263
    22
#include "Spaces-inl.h"
asherkin@263
    23
#include "Heap-inl.h"
asherkin@263
    24
asherkin@263
    25
using namespace ke;
asherkin@263
    26
asherkin@263
    27
StringPool::StringPool(Zone *zone)
asherkin@263
    28
  : zone_(zone),
asherkin@263
    29
    set_(ZoneAllocatorPolicy())
asherkin@263
    30
{
asherkin@263
    31
}
asherkin@263
    32
asherkin@263
    33
StringPool::~StringPool()
asherkin@263
    34
{
asherkin@263
    35
}
asherkin@263
    36
asherkin@263
    37
bool
asherkin@263
    38
StringPool::initialize()
asherkin@263
    39
{
asherkin@263
    40
    if (!set_.init(32))
asherkin@263
    41
        return false;
asherkin@263
    42
    return true;
asherkin@263
    43
}
asherkin@263
    44
asherkin@263
    45
String *
asherkin@263
    46
StringPool::add(const char *str, size_t length)
asherkin@263
    47
{
asherkin@263
    48
    StringSet::Insert i = set_.findForAdd(CharsAndLength(str, length));
asherkin@263
    49
    if (i.found())
asherkin@263
    50
        return *i;
asherkin@263
    51
asherkin@263
    52
    Local<String> obj(zone_, String::New(zone_, str, length, Heap::Tenure_Old));
asherkin@263
    53
    if (!obj)
asherkin@263
    54
        return NULL;
asherkin@263
    55
asherkin@263
    56
    set_.add(i, obj);
asherkin@263
    57
    return obj;
asherkin@263
    58
}
asherkin@263
    59
asherkin@263
    60
void
asherkin@263
    61
StringPool::sweep()
asherkin@263
    62
{
asherkin@263
    63
    for (StringSet::iterator i(&set_); !i.empty(); i.next()) {
asherkin@263
    64
        String *obj = *i;
asherkin@263
    65
        MarkBit bits = HeapChunk::GetMarkBit(obj);
asherkin@263
    66
        if (HeapChunk::IsWhite(bits))
asherkin@263
    67
            i.erase();
asherkin@263
    68
        else
asherkin@263
    69
            assert(HeapChunk::IsBlack(bits));
asherkin@263
    70
    }
asherkin@263
    71
    set_.checkDensity();
asherkin@263
    72
}