]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/RegExpCache.cpp
JavaScriptCore-7600.1.4.9.tar.gz
[apple/javascriptcore.git] / runtime / RegExpCache.cpp
1 /*
2 * Copyright (C) 2010 University of Szeged
3 * Copyright (C) 2010 Renata Hodovan (hodovan@inf.u-szeged.hu)
4 * Copyright (C) 2012 Apple Inc. All rights reserved.
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"
30 #include "RegExpCache.h"
31
32 #include "JSCInlines.h"
33 #include "RegExpObject.h"
34 #include "StrongInlines.h"
35
36 namespace JSC {
37
38 RegExp* RegExpCache::lookupOrCreate(const String& patternString, RegExpFlags flags)
39 {
40 RegExpKey key(flags, patternString);
41 if (RegExp* regExp = m_weakCache.get(key))
42 return regExp;
43
44 RegExp* regExp = RegExp::createWithoutCaching(*m_vm, patternString, flags);
45 #if ENABLE(REGEXP_TRACING)
46 m_vm->addRegExpToTrace(regExp);
47 #endif
48
49 weakAdd(m_weakCache, key, Weak<RegExp>(regExp, this));
50 return regExp;
51 }
52
53 RegExpCache::RegExpCache(VM* vm)
54 : m_nextEntryInStrongCache(0)
55 , m_vm(vm)
56 {
57 }
58
59 void RegExpCache::finalize(Handle<Unknown> handle, void*)
60 {
61 RegExp* regExp = static_cast<RegExp*>(handle.get().asCell());
62 weakRemove(m_weakCache, regExp->key(), regExp);
63 regExp->invalidateCode();
64 }
65
66 void RegExpCache::addToStrongCache(RegExp* regExp)
67 {
68 String pattern = regExp->pattern();
69 if (pattern.length() > maxStrongCacheablePatternLength)
70 return;
71 m_strongCache[m_nextEntryInStrongCache].set(*m_vm, regExp);
72 m_nextEntryInStrongCache++;
73 if (m_nextEntryInStrongCache == maxStrongCacheableEntries)
74 m_nextEntryInStrongCache = 0;
75 }
76
77 void RegExpCache::invalidateCode()
78 {
79 for (int i = 0; i < maxStrongCacheableEntries; i++)
80 m_strongCache[i].clear();
81 m_nextEntryInStrongCache = 0;
82
83 RegExpCacheMap::iterator end = m_weakCache.end();
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 }
90 }
91
92 }