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 26 #ifndef StringImplBase_h 27 #define StringImplBase_h 28 29 #include <wtf/unicode/Unicode.h> 30 31 namespace WTF { 32 33 class StringImplBase { 34 WTF_MAKE_NONCOPYABLE(StringImplBase); WTF_MAKE_FAST_ALLOCATED; 35 public: isStringImpl()36 bool isStringImpl() { return (m_refCountAndFlags & s_refCountInvalidForStringImpl) != s_refCountInvalidForStringImpl; } length()37 unsigned length() const { return m_length; } ref()38 void ref() { m_refCountAndFlags += s_refCountIncrement; } 39 40 protected: 41 enum BufferOwnership { 42 BufferInternal, 43 BufferOwned, 44 BufferSubstring, 45 BufferShared, 46 }; 47 48 // For SmallStringStorage, which allocates an array and uses an in-place new. StringImplBase()49 StringImplBase() { } 50 StringImplBase(unsigned length,BufferOwnership ownership)51 StringImplBase(unsigned length, BufferOwnership ownership) 52 : m_refCountAndFlags(s_refCountIncrement | s_refCountFlagShouldReportedCost | ownership) 53 , m_length(length) 54 { 55 ASSERT(isStringImpl()); 56 } 57 58 enum StaticStringConstructType { ConstructStaticString }; StringImplBase(unsigned length,StaticStringConstructType)59 StringImplBase(unsigned length, StaticStringConstructType) 60 : m_refCountAndFlags(s_refCountFlagStatic | s_refCountFlagIsIdentifier | BufferOwned) 61 , m_length(length) 62 { 63 ASSERT(isStringImpl()); 64 } 65 66 // This constructor is not used when creating StringImpl objects, 67 // and sets the flags into a state marking the object as such. 68 enum NonStringImplConstructType { ConstructNonStringImpl }; StringImplBase(NonStringImplConstructType)69 StringImplBase(NonStringImplConstructType) 70 : m_refCountAndFlags(s_refCountIncrement | s_refCountInvalidForStringImpl) 71 , m_length(0) 72 { 73 ASSERT(!isStringImpl()); 74 } 75 76 // The bottom 7 bits hold flags, the top 25 bits hold the ref count. 77 // When dereferencing StringImpls we check for the ref count AND the 78 // static bit both being zero - static strings are never deleted. 79 static const unsigned s_refCountMask = 0xFFFFFF80; 80 static const unsigned s_refCountIncrement = 0x80; 81 static const unsigned s_refCountFlagStatic = 0x40; 82 static const unsigned s_refCountFlagHasTerminatingNullCharacter = 0x20; 83 static const unsigned s_refCountFlagIsAtomic = 0x10; 84 static const unsigned s_refCountFlagShouldReportedCost = 0x8; 85 static const unsigned s_refCountFlagIsIdentifier = 0x4; 86 static const unsigned s_refCountMaskBufferOwnership = 0x3; 87 // An invalid permutation of flags (static & shouldReportedCost - static strings do not 88 // set shouldReportedCost in the constructor, and this bit is only ever cleared, not set). 89 // Used by "ConstructNonStringImpl" constructor, above. 90 static const unsigned s_refCountInvalidForStringImpl = s_refCountFlagStatic | s_refCountFlagShouldReportedCost; 91 92 unsigned m_refCountAndFlags; 93 unsigned m_length; 94 }; 95 96 } // namespace WTF 97 98 using WTF::StringImplBase; 99 100 #endif 101