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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include "RegExpCache.h"
32 #include "JSCInlines.h"
33 #include "RegExpObject.h"
34 #include "StrongInlines.h"
38 RegExp
* RegExpCache::lookupOrCreate(const String
& patternString
, RegExpFlags flags
)
40 RegExpKey
key(flags
, patternString
);
41 if (RegExp
* regExp
= m_weakCache
.get(key
))
44 RegExp
* regExp
= RegExp::createWithoutCaching(*m_vm
, patternString
, flags
);
45 #if ENABLE(REGEXP_TRACING)
46 m_vm
->addRegExpToTrace(regExp
);
49 weakAdd(m_weakCache
, key
, Weak
<RegExp
>(regExp
, this));
53 RegExpCache::RegExpCache(VM
* vm
)
54 : m_nextEntryInStrongCache(0)
59 void RegExpCache::finalize(Handle
<Unknown
> handle
, void*)
61 RegExp
* regExp
= static_cast<RegExp
*>(handle
.get().asCell());
62 weakRemove(m_weakCache
, regExp
->key(), regExp
);
63 regExp
->invalidateCode();
66 void RegExpCache::addToStrongCache(RegExp
* regExp
)
68 String pattern
= regExp
->pattern();
69 if (pattern
.length() > maxStrongCacheablePatternLength
)
71 m_strongCache
[m_nextEntryInStrongCache
].set(*m_vm
, regExp
);
72 m_nextEntryInStrongCache
++;
73 if (m_nextEntryInStrongCache
== maxStrongCacheableEntries
)
74 m_nextEntryInStrongCache
= 0;
77 void RegExpCache::invalidateCode()
79 for (int i
= 0; i
< maxStrongCacheableEntries
; i
++)
80 m_strongCache
[i
].clear();
81 m_nextEntryInStrongCache
= 0;
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.
88 regExp
->invalidateCode();