src/StringPool.cpp
author David Anderson <dvander@alliedmods.net>
Mon Jul 30 12:13:22 2012 -0700 (2012-07-30)
changeset 142 a000bafece09
parent 92 e3974e7360e9
child 263 ba85a47ee414
permissions -rw-r--r--
Implement re-tag unary expression.
     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 "Zone.h"
    19 #include "StringPool.h"
    20 #include "Strings.h"
    21 #include "Spaces.h"
    22 #include "Spaces-inl.h"
    23 #include "Heap-inl.h"
    24 
    25 using namespace ke;
    26 
    27 StringPool::StringPool(Zone *zone)
    28   : zone_(zone),
    29     set_(ZoneAllocatorPolicy())
    30 {
    31 }
    32 
    33 StringPool::~StringPool()
    34 {
    35 }
    36 
    37 bool
    38 StringPool::initialize()
    39 {
    40     if (!set_.init(32))
    41         return false;
    42     return true;
    43 }
    44 
    45 String *
    46 StringPool::add(const char *str, size_t length)
    47 {
    48     StringSet::Insert i = set_.findForAdd(CharsAndLength(str, length));
    49     if (i.found())
    50         return *i;
    51 
    52     Local<String> obj(zone_, String::New(zone_, str, length, Heap::Tenure_Old));
    53     if (!obj)
    54         return NULL;
    55 
    56     set_.add(i, obj);
    57     return obj;
    58 }
    59 
    60 void
    61 StringPool::sweep()
    62 {
    63     for (StringSet::iterator i(&set_); !i.empty(); i.next()) {
    64         String *obj = *i;
    65         MarkBit bits = HeapChunk::GetMarkBit(obj);
    66         if (HeapChunk::IsWhite(bits))
    67             i.erase();
    68         else
    69             assert(HeapChunk::IsBlack(bits));
    70     }
    71     set_.checkDensity();
    72 }