RegExpCache.cpp 3.09 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2010 University of Szeged
 * Copyright (C) 2010 Renata Hodovan (hodovan@inf.u-szeged.hu)
4
 * Copyright (C) 2012 Apple Inc. All rights reserved.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "RegExpCache.h"
31 32

#include "Operations.h"
33
#include "RegExpObject.h"
34
#include "StrongInlines.h"
35 36 37

namespace JSC {

38
RegExp* RegExpCache::lookupOrCreate(const String& patternString, RegExpFlags flags)
39
{
40
    RegExpKey key(flags, patternString);
41 42 43
    if (RegExp* regExp = m_weakCache.get(key))
        return regExp;

44
    RegExp* regExp = RegExp::createWithoutCaching(*m_vm, patternString, flags);
45
#if ENABLE(REGEXP_TRACING)
46
    m_vm->addRegExpToTrace(regExp);
47
#endif
48

andersca@apple.com's avatar
andersca@apple.com committed
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 60 61
void RegExpCache::finalize(Handle<Unknown> handle, void*)
{
    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 70
    if (pattern.length() > maxStrongCacheablePatternLength)
        return;
71
    m_strongCache[m_nextEntryInStrongCache].set(*m_vm, regExp);
72 73 74
    m_nextEntryInStrongCache++;
    if (m_nextEntryInStrongCache == maxStrongCacheableEntries)
        m_nextEntryInStrongCache = 0;
75 76
}

77 78
void RegExpCache::invalidateCode()
{
oliver@apple.com's avatar
oliver@apple.com committed
79
    for (int i = 0; i < maxStrongCacheableEntries; i++)
80 81
        m_strongCache[i].clear();
    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 87 88 89
        if (!regExp) // Skip zombies.
            continue;
        regExp->invalidateCode();
    }
90 91
}

92
}