1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/StringPool.cpp Tue Jul 24 23:53:34 2012 -0700
1.3 @@ -0,0 +1,69 @@
1.4 +/*
1.5 + * Copyright (C) 2012 David Anderson
1.6 + *
1.7 + * This file is part of JITCraft.
1.8 + *
1.9 + * JITCraft is free software: you can redistribute it and/or modify it under
1.10 + * the terms of the GNU General Public License as published by the Free
1.11 + * Software Foundation, either version 3 of the License, or (at your option)
1.12 + * any later version.
1.13 + *
1.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License along with
1.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
1.20 + */
1.21 +#include "StringPool.h"
1.22 +#include "Strings.h"
1.23 +#include "Spaces.h"
1.24 +
1.25 +using namespace ke;
1.26 +
1.27 +StringPool::StringPool(Zone *zone)
1.28 + : zone_(zone),
1.29 + set_(ZoneAllocatorPolicy())
1.30 +{
1.31 +}
1.32 +
1.33 +StringPool::~StringPool()
1.34 +{
1.35 +}
1.36 +
1.37 +bool
1.38 +StringPool::initialize()
1.39 +{
1.40 + if (!set_.init(32))
1.41 + return false;
1.42 + return true;
1.43 +}
1.44 +
1.45 +String *
1.46 +StringPool::add(const char *str, size_t length)
1.47 +{
1.48 + StringSet::Insert i = set_.findForAdd(CharsAndLength(str, length));
1.49 + if (i.found())
1.50 + return *i;
1.51 +
1.52 + Local<String> obj(zone_, String::New(zone_, str, length, Heap::Tenure_Old));
1.53 + if (!obj)
1.54 + return NULL;
1.55 +
1.56 + set_.add(i, obj);
1.57 + return obj;
1.58 +}
1.59 +
1.60 +void
1.61 +StringPool::sweep()
1.62 +{
1.63 + for (StringSet::iterator i(&set_); !i.empty(); i.next()) {
1.64 + String *obj = *i;
1.65 + MarkBit bits = HeapChunk::GetMarkBit(obj);
1.66 + if (HeapChunk::IsWhite(bits))
1.67 + i.erase();
1.68 + else
1.69 + assert(HeapChunk::IsBlack(bits));
1.70 + }
1.71 + set_.checkDensity();
1.72 +}