/src/3rdparty/webkit/Source/JavaScriptCore/wtf/text/StringImplBase.h

https://bitbucket.org/ultra_iter/qt-vtl · C Header · 100 lines · 53 code · 14 blank · 33 comment · 1 complexity · 9187bbb6cb5fecb5853fa06eb29b90ad MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef StringImplBase_h
  26. #define StringImplBase_h
  27. #include <wtf/unicode/Unicode.h>
  28. namespace WTF {
  29. class StringImplBase {
  30. WTF_MAKE_NONCOPYABLE(StringImplBase); WTF_MAKE_FAST_ALLOCATED;
  31. public:
  32. bool isStringImpl() { return (m_refCountAndFlags & s_refCountInvalidForStringImpl) != s_refCountInvalidForStringImpl; }
  33. unsigned length() const { return m_length; }
  34. void ref() { m_refCountAndFlags += s_refCountIncrement; }
  35. protected:
  36. enum BufferOwnership {
  37. BufferInternal,
  38. BufferOwned,
  39. BufferSubstring,
  40. BufferShared,
  41. };
  42. // For SmallStringStorage, which allocates an array and uses an in-place new.
  43. StringImplBase() { }
  44. StringImplBase(unsigned length, BufferOwnership ownership)
  45. : m_refCountAndFlags(s_refCountIncrement | s_refCountFlagShouldReportedCost | ownership)
  46. , m_length(length)
  47. {
  48. ASSERT(isStringImpl());
  49. }
  50. enum StaticStringConstructType { ConstructStaticString };
  51. StringImplBase(unsigned length, StaticStringConstructType)
  52. : m_refCountAndFlags(s_refCountFlagStatic | s_refCountFlagIsIdentifier | BufferOwned)
  53. , m_length(length)
  54. {
  55. ASSERT(isStringImpl());
  56. }
  57. // This constructor is not used when creating StringImpl objects,
  58. // and sets the flags into a state marking the object as such.
  59. enum NonStringImplConstructType { ConstructNonStringImpl };
  60. StringImplBase(NonStringImplConstructType)
  61. : m_refCountAndFlags(s_refCountIncrement | s_refCountInvalidForStringImpl)
  62. , m_length(0)
  63. {
  64. ASSERT(!isStringImpl());
  65. }
  66. // The bottom 7 bits hold flags, the top 25 bits hold the ref count.
  67. // When dereferencing StringImpls we check for the ref count AND the
  68. // static bit both being zero - static strings are never deleted.
  69. static const unsigned s_refCountMask = 0xFFFFFF80;
  70. static const unsigned s_refCountIncrement = 0x80;
  71. static const unsigned s_refCountFlagStatic = 0x40;
  72. static const unsigned s_refCountFlagHasTerminatingNullCharacter = 0x20;
  73. static const unsigned s_refCountFlagIsAtomic = 0x10;
  74. static const unsigned s_refCountFlagShouldReportedCost = 0x8;
  75. static const unsigned s_refCountFlagIsIdentifier = 0x4;
  76. static const unsigned s_refCountMaskBufferOwnership = 0x3;
  77. // An invalid permutation of flags (static & shouldReportedCost - static strings do not
  78. // set shouldReportedCost in the constructor, and this bit is only ever cleared, not set).
  79. // Used by "ConstructNonStringImpl" constructor, above.
  80. static const unsigned s_refCountInvalidForStringImpl = s_refCountFlagStatic | s_refCountFlagShouldReportedCost;
  81. unsigned m_refCountAndFlags;
  82. unsigned m_length;
  83. };
  84. } // namespace WTF
  85. using WTF::StringImplBase;
  86. #endif