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