RegExpCache.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "config.h"
  29. #include "RegExpCache.h"
  30. #include "Operations.h"
  31. #include "RegExpObject.h"
  32. #include "StrongInlines.h"
  33. namespace JSC {
  34. RegExp* RegExpCache::lookupOrCreate(const String& patternString, RegExpFlags flags)
  35. {
  36. RegExpKey key(flags, patternString);
  37. if (RegExp* regExp = m_weakCache.get(key))
  38. return regExp;
  39. RegExp* regExp = RegExp::createWithoutCaching(*m_vm, patternString, flags);
  40. #if ENABLE(REGEXP_TRACING)
  41. m_vm->addRegExpToTrace(regExp);
  42. #endif
  43. weakAdd(m_weakCache, key, PassWeak<RegExp>(regExp, this));
  44. return regExp;
  45. }
  46. RegExpCache::RegExpCache(VM* vm)
  47. : m_nextEntryInStrongCache(0)
  48. , m_vm(vm)
  49. {
  50. }
  51. void RegExpCache::finalize(Handle<Unknown> handle, void*)
  52. {
  53. RegExp* regExp = static_cast<RegExp*>(handle.get().asCell());
  54. weakRemove(m_weakCache, regExp->key(), regExp);
  55. regExp->invalidateCode();
  56. }
  57. void RegExpCache::addToStrongCache(RegExp* regExp)
  58. {
  59. String pattern = regExp->pattern();
  60. if (pattern.length() > maxStrongCacheablePatternLength)
  61. return;
  62. m_strongCache[m_nextEntryInStrongCache].set(*m_vm, regExp);
  63. m_nextEntryInStrongCache++;
  64. if (m_nextEntryInStrongCache == maxStrongCacheableEntries)
  65. m_nextEntryInStrongCache = 0;
  66. }
  67. void RegExpCache::invalidateCode()
  68. {
  69. for (int i = 0; i < maxStrongCacheableEntries; i++)
  70. m_strongCache[i].clear();
  71. m_nextEntryInStrongCache = 0;
  72. RegExpCacheMap::iterator end = m_weakCache.end();
  73. for (RegExpCacheMap::iterator it = m_weakCache.begin(); it != end; ++it) {
  74. RegExp* regExp = it->value.get();
  75. if (!regExp) // Skip zombies.
  76. continue;
  77. regExp->invalidateCode();
  78. }
  79. }
  80. }