asherkin@263: /* asherkin@263: * Copyright (C) 2012 David Anderson asherkin@263: * asherkin@263: * This file is part of JITCraft. asherkin@263: * asherkin@263: * JITCraft is free software: you can redistribute it and/or modify it under asherkin@263: * the terms of the GNU General Public License as published by the Free asherkin@263: * Software Foundation, either version 3 of the License, or (at your option) asherkin@263: * any later version. asherkin@263: * asherkin@263: * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY asherkin@263: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS asherkin@263: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. asherkin@263: * asherkin@263: * You should have received a copy of the GNU General Public License along with asherkin@263: * JITCraft. If not, see http://www.gnu.org/licenses/. asherkin@263: */ asherkin@263: #include "Zone.h" asherkin@263: #include "StringPool.h" asherkin@263: #include "Strings.h" asherkin@263: #include "Spaces.h" asherkin@263: #include "Spaces-inl.h" asherkin@263: #include "Heap-inl.h" asherkin@263: asherkin@263: using namespace ke; asherkin@263: asherkin@263: StringPool::StringPool(Zone *zone) asherkin@263: : zone_(zone), asherkin@263: set_(ZoneAllocatorPolicy()) asherkin@263: { asherkin@263: } asherkin@263: asherkin@263: StringPool::~StringPool() asherkin@263: { asherkin@263: } asherkin@263: asherkin@263: bool asherkin@263: StringPool::initialize() asherkin@263: { asherkin@263: if (!set_.init(32)) asherkin@263: return false; asherkin@263: return true; asherkin@263: } asherkin@263: asherkin@263: String * asherkin@263: StringPool::add(const char *str, size_t length) asherkin@263: { asherkin@263: StringSet::Insert i = set_.findForAdd(CharsAndLength(str, length)); asherkin@263: if (i.found()) asherkin@263: return *i; asherkin@263: asherkin@263: Local obj(zone_, String::New(zone_, str, length, Heap::Tenure_Old)); asherkin@263: if (!obj) asherkin@263: return NULL; asherkin@263: asherkin@263: set_.add(i, obj); asherkin@263: return obj; asherkin@263: } asherkin@263: asherkin@263: void asherkin@263: StringPool::sweep() asherkin@263: { asherkin@263: for (StringSet::iterator i(&set_); !i.empty(); i.next()) { asherkin@263: String *obj = *i; asherkin@263: MarkBit bits = HeapChunk::GetMarkBit(obj); asherkin@263: if (HeapChunk::IsWhite(bits)) asherkin@263: i.erase(); asherkin@263: else asherkin@263: assert(HeapChunk::IsBlack(bits)); asherkin@263: } asherkin@263: set_.checkDensity(); asherkin@263: }