1/*
2 * Copyright (C) 2010 University of Szeged
3 * Copyright (C) 2010 Renata Hodovan ([email protected])
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
36namespace JSC {
37
38RegExp* RegExpCache::lookupOrCreate(const String& patternString, OptionSet<Yarr::Flags> 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
53RegExpCache::RegExpCache(VM* vm)
54 : m_nextEntryInStrongCache(0)
55 , m_vm(vm)
56{
57}
58
59RegExp* RegExpCache::ensureEmptyRegExpSlow(VM& vm)
60{
61 RegExp* regExp = RegExp::create(vm, "", { });
62 m_emptyRegExp.set(vm, regExp);
63 return regExp;
64}
65
66void RegExpCache::finalize(Handle<Unknown> handle, void*)
67{
68 RegExp* regExp = static_cast<RegExp*>(handle.get().asCell());
69 weakRemove(m_weakCache, regExp->key(), regExp);
70}
71
72void RegExpCache::addToStrongCache(RegExp* regExp)
73{
74 String pattern = regExp->pattern();
75 if (pattern.length() > maxStrongCacheablePatternLength)
76 return;
77 m_strongCache[m_nextEntryInStrongCache].set(*m_vm, regExp);
78 m_nextEntryInStrongCache++;
79 if (m_nextEntryInStrongCache == maxStrongCacheableEntries)
80 m_nextEntryInStrongCache = 0;
81}
82
83void RegExpCache::deleteAllCode()
84{
85 for (int i = 0; i < maxStrongCacheableEntries; i++)
86 m_strongCache[i].clear();
87 m_nextEntryInStrongCache = 0;
88
89 RegExpCacheMap::iterator end = m_weakCache.end();
90 for (RegExpCacheMap::iterator it = m_weakCache.begin(); it != end; ++it) {
91 RegExp* regExp = it->value.get();
92 if (!regExp) // Skip zombies.
93 continue;
94 regExp->deleteCode();
95 }
96}
97
98}
99