src/StringPool.cpp
author David Anderson <danderson@mozilla.com>
Wed Jul 25 00:48:08 2012 -0700 (2012-07-25)
changeset 91 5f4402180204
parent 86 32ad0523b309
child 92 e3974e7360e9
permissions -rw-r--r--
Added clang support. A lot of the motion here is introducing a new offsetof because clang's seems broken?
     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 "StringPool.h"
    19 #include "Strings.h"
    20 #include "Spaces.h"
    21 
    22 using namespace ke;
    23 
    24 StringPool::StringPool(Zone *zone)
    25   : zone_(zone),
    26     set_(ZoneAllocatorPolicy())
    27 {
    28 }
    29 
    30 StringPool::~StringPool()
    31 {
    32 }
    33 
    34 bool
    35 StringPool::initialize()
    36 {
    37     if (!set_.init(32))
    38         return false;
    39     return true;
    40 }
    41 
    42 String *
    43 StringPool::add(const char *str, size_t length)
    44 {
    45     StringSet::Insert i = set_.findForAdd(CharsAndLength(str, length));
    46     if (i.found())
    47         return *i;
    48 
    49     Local<String> obj(zone_, String::New(zone_, str, length, Heap::Tenure_Old));
    50     if (!obj)
    51         return NULL;
    52 
    53     set_.add(i, obj);
    54     return obj;
    55 }
    56 
    57 void
    58 StringPool::sweep()
    59 {
    60     for (StringSet::iterator i(&set_); !i.empty(); i.next()) {
    61         String *obj = *i;
    62         MarkBit bits = HeapChunk::GetMarkBit(obj);
    63         if (HeapChunk::IsWhite(bits))
    64             i.erase();
    65         else
    66             assert(HeapChunk::IsBlack(bits));
    67     }
    68     set_.checkDensity();
    69 }