X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/14957cd040308e3eeec43d26bae5d76da13fcd85..refs/heads/master:/runtime/RegExpCache.cpp diff --git a/runtime/RegExpCache.cpp b/runtime/RegExpCache.cpp index 2dce197..dbe647f 100644 --- a/runtime/RegExpCache.cpp +++ b/runtime/RegExpCache.cpp @@ -1,6 +1,7 @@ /* * 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 @@ -26,48 +27,48 @@ */ #include "config.h" - #include "RegExpCache.h" + +#include "JSCInlines.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(*m_globalData, regExp, this)); + + weakAdd(m_weakCache, key, Weak(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 handle, void*) { RegExp* regExp = static_cast(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; @@ -78,9 +79,14 @@ void RegExpCache::invalidateCode() 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(); + } } }