]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/RegExpCache.cpp
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / runtime / RegExpCache.cpp
CommitLineData
4e4e5a6f
A
1/*
2 * Copyright (C) 2010 University of Szeged
3 * Copyright (C) 2010 Renata Hodovan (hodovan@inf.u-szeged.hu)
93a37866 4 * Copyright (C) 2012 Apple Inc. All rights reserved.
4e4e5a6f
A
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
4e4e5a6f 30#include "RegExpCache.h"
93a37866 31
81345200 32#include "JSCInlines.h"
14957cd0 33#include "RegExpObject.h"
6fe7ccc8 34#include "StrongInlines.h"
4e4e5a6f
A
35
36namespace JSC {
37
93a37866 38RegExp* RegExpCache::lookupOrCreate(const String& patternString, RegExpFlags flags)
4e4e5a6f 39{
14957cd0 40 RegExpKey key(flags, patternString);
93a37866
A
41 if (RegExp* regExp = m_weakCache.get(key))
42 return regExp;
43
44 RegExp* regExp = RegExp::createWithoutCaching(*m_vm, patternString, flags);
14957cd0 45#if ENABLE(REGEXP_TRACING)
93a37866 46 m_vm->addRegExpToTrace(regExp);
14957cd0 47#endif
93a37866 48
81345200 49 weakAdd(m_weakCache, key, Weak<RegExp>(regExp, this));
14957cd0 50 return regExp;
4e4e5a6f
A
51}
52
93a37866 53RegExpCache::RegExpCache(VM* vm)
14957cd0 54 : m_nextEntryInStrongCache(0)
93a37866 55 , m_vm(vm)
4e4e5a6f 56{
14957cd0 57}
4e4e5a6f 58
14957cd0
A
59void RegExpCache::finalize(Handle<Unknown> handle, void*)
60{
61 RegExp* regExp = static_cast<RegExp*>(handle.get().asCell());
93a37866 62 weakRemove(m_weakCache, regExp->key(), regExp);
14957cd0
A
63 regExp->invalidateCode();
64}
4e4e5a6f 65
14957cd0
A
66void RegExpCache::addToStrongCache(RegExp* regExp)
67{
93a37866 68 String pattern = regExp->pattern();
14957cd0
A
69 if (pattern.length() > maxStrongCacheablePatternLength)
70 return;
93a37866 71 m_strongCache[m_nextEntryInStrongCache].set(*m_vm, regExp);
14957cd0
A
72 m_nextEntryInStrongCache++;
73 if (m_nextEntryInStrongCache == maxStrongCacheableEntries)
74 m_nextEntryInStrongCache = 0;
4e4e5a6f
A
75}
76
14957cd0 77void RegExpCache::invalidateCode()
4e4e5a6f 78{
14957cd0
A
79 for (int i = 0; i < maxStrongCacheableEntries; i++)
80 m_strongCache[i].clear();
81 m_nextEntryInStrongCache = 0;
93a37866 82
14957cd0 83 RegExpCacheMap::iterator end = m_weakCache.end();
93a37866
A
84 for (RegExpCacheMap::iterator it = m_weakCache.begin(); it != end; ++it) {
85 RegExp* regExp = it->value.get();
86 if (!regExp) // Skip zombies.
87 continue;
88 regExp->invalidateCode();
89 }
4e4e5a6f
A
90}
91
92}