18e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project/* 206ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen * Copyright (C) 2010 University of Szeged 306ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen * Copyright (C) 2010 Renata Hodovan (hodovan@inf.u-szeged.hu) 406ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen * All rights reserved. 58e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 68e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Redistribution and use in source and binary forms, with or without 78e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * modification, are permitted provided that the following conditions 88e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * are met: 98e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 1. Redistributions of source code must retain the above copyright 108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * notice, this list of conditions and the following disclaimer. 118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright 128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * notice, this list of conditions and the following disclaimer in the 138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * documentation and/or other materials provided with the distribution. 148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 1506ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY 168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1806ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR 198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2506ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project */ 278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 2806ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen#include "RegExp.h" 2906ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen#include "RegExpKey.h" 3006ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen#include "UString.h" 31f486d19d62f1bc33246748b14b14a9dfa617b57fIain Merrick#include <wtf/FixedArray.h> 32f486d19d62f1bc33246748b14b14a9dfa617b57fIain Merrick#include <wtf/HashMap.h> 338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 3406ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen#ifndef RegExpCache_h 3506ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen#define RegExpCache_h 368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 3706ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsennamespace JSC { 388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 3906ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsenclass RegExpCache { 40ca9cb53ed1119a3fd98fafa0972ffeb56dee1c24Steve Block 41ca9cb53ed1119a3fd98fafa0972ffeb56dee1c24Steve Blocktypedef HashMap<RegExpKey, RefPtr<RegExp> > RegExpCacheMap; 42ca9cb53ed1119a3fd98fafa0972ffeb56dee1c24Steve Block 438f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qianpublic: 442bde8e466a4451c7319e3a072d118917957d6554Steve Block PassRefPtr<RegExp> lookupOrCreate(const UString& patternString, RegExpFlags); 452bde8e466a4451c7319e3a072d118917957d6554Steve Block PassRefPtr<RegExp> create(const UString& patternString, RegExpFlags, RegExpCacheMap::iterator); 4606ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen RegExpCache(JSGlobalData* globalData); 478e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 488f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qianprivate: 4906ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen static const unsigned maxCacheablePatternLength = 256; 50f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch 51f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch#if PLATFORM(IOS) 52f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch // The RegExpCache can currently hold onto multiple Mb of memory; 53f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch // as a short-term fix some embedded platforms may wish to reduce the cache size. 54f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch static const int maxCacheableEntries = 32; 55f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch#else 5606ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen static const int maxCacheableEntries = 256; 57f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch#endif 5806ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen 59ca9cb53ed1119a3fd98fafa0972ffeb56dee1c24Steve Block FixedArray<RegExpKey, maxCacheableEntries> patternKeyArray; 6006ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen RegExpCacheMap m_cacheMap; 6106ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen JSGlobalData* m_globalData; 6206ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen int m_nextKeyToEvict; 6306ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen bool m_isFull; 648f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian}; 658f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian 6606ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen} // namespace JSC 678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 6806ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen#endif // RegExpCache_h 69