/*
* Copyright (C) 2010 University of Szeged
* Copyright (C) 2010 Renata Hodovan (hodovan@inf.u-szeged.hu)
+ * Copyright (C) 2012 Apple Inc. All rights reserved.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
*/
#include "config.h"
-
#include "RegExpCache.h"
+
+#include "Operations.h"
#include "RegExpObject.h"
+#include "StrongInlines.h"
namespace JSC {
-RegExp* RegExpCache::lookupOrCreate(const UString& patternString, RegExpFlags flags)
+RegExp* RegExpCache::lookupOrCreate(const String& patternString, RegExpFlags flags)
{
RegExpKey key(flags, patternString);
- RegExpCacheMap::iterator result = m_weakCache.find(key);
- if (result != m_weakCache.end())
- return result->second.get();
- RegExp* regExp = new (m_globalData) RegExp(m_globalData, patternString, flags);
+ if (RegExp* regExp = m_weakCache.get(key))
+ return regExp;
+
+ RegExp* regExp = RegExp::createWithoutCaching(*m_vm, patternString, flags);
#if ENABLE(REGEXP_TRACING)
- m_globalData->addRegExpToTrace(regExp);
+ m_vm->addRegExpToTrace(regExp);
#endif
- // We need to do a second lookup to add the RegExp as
- // allocating it may have caused a gc cycle, which in
- // turn may have removed items from the cache.
- m_weakCache.add(key, Weak<RegExp>(*m_globalData, regExp, this));
+
+ weakAdd(m_weakCache, key, PassWeak<RegExp>(regExp, this));
return regExp;
}
-RegExpCache::RegExpCache(JSGlobalData* globalData)
+RegExpCache::RegExpCache(VM* vm)
: m_nextEntryInStrongCache(0)
- , m_globalData(globalData)
+ , m_vm(vm)
{
}
void RegExpCache::finalize(Handle<Unknown> handle, void*)
{
RegExp* regExp = static_cast<RegExp*>(handle.get().asCell());
- m_weakCache.remove(regExp->key());
+ weakRemove(m_weakCache, regExp->key(), regExp);
regExp->invalidateCode();
}
void RegExpCache::addToStrongCache(RegExp* regExp)
{
- UString pattern = regExp->pattern();
+ String pattern = regExp->pattern();
if (pattern.length() > maxStrongCacheablePatternLength)
return;
- m_strongCache[m_nextEntryInStrongCache].set(*m_globalData, regExp);
+ m_strongCache[m_nextEntryInStrongCache].set(*m_vm, regExp);
m_nextEntryInStrongCache++;
if (m_nextEntryInStrongCache == maxStrongCacheableEntries)
m_nextEntryInStrongCache = 0;
for (int i = 0; i < maxStrongCacheableEntries; i++)
m_strongCache[i].clear();
m_nextEntryInStrongCache = 0;
+
RegExpCacheMap::iterator end = m_weakCache.end();
- for (RegExpCacheMap::iterator ptr = m_weakCache.begin(); ptr != end; ++ptr)
- ptr->second->invalidateCode();
+ for (RegExpCacheMap::iterator it = m_weakCache.begin(); it != end; ++it) {
+ RegExp* regExp = it->value.get();
+ if (!regExp) // Skip zombies.
+ continue;
+ regExp->invalidateCode();
+ }
}
}