Changeset 55878 in webkit for trunk/JavaScriptCore/runtime/UStringImpl.cpp
- Timestamp:
- Mar 11, 2010, 7:14:17 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/runtime/UStringImpl.cpp
r55825 r55878 37 37 namespace JSC { 38 38 39 static const unsigned minLengthToShare = 20; 40 41 UStringImpl::~UStringImpl() 42 { 43 ASSERT(!isStatic()); 44 checkConsistency(); 45 46 if (isIdentifier()) 47 Identifier::remove(this); 48 49 BufferOwnership ownership = bufferOwnership(); 50 if (ownership != BufferInternal) { 51 if (ownership == BufferOwned) { 52 ASSERT(!m_sharedBuffer); 53 ASSERT(m_data); 54 fastFree(const_cast<UChar*>(m_data)); 55 } else if (ownership == BufferSubstring) { 56 ASSERT(m_substringBuffer); 57 m_substringBuffer->deref(); 58 } else { 59 ASSERT(ownership == BufferShared); 60 ASSERT(m_sharedBuffer); 61 m_sharedBuffer->deref(); 62 } 63 } 64 } 65 39 66 UStringImpl* UStringImpl::empty() 40 67 { … … 44 71 DEFINE_STATIC_LOCAL(UStringImpl, emptyString, (invalidNonNullUCharPtr, 0, ConstructStaticString)); 45 72 return &emptyString; 73 } 74 75 PassRefPtr<UStringImpl> UStringImpl::createUninitialized(unsigned length, UChar*& data) 76 { 77 if (!length) { 78 data = 0; 79 return empty(); 80 } 81 82 // Allocate a single buffer large enough to contain the StringImpl 83 // struct as well as the data which it contains. This removes one 84 // heap allocation from this call. 85 if (length > ((std::numeric_limits<size_t>::max() - sizeof(UStringImpl)) / sizeof(UChar))) 86 CRASH(); 87 size_t size = sizeof(UStringImpl) + length * sizeof(UChar); 88 UStringImpl* string = static_cast<UStringImpl*>(fastMalloc(size)); 89 90 data = reinterpret_cast<UChar*>(string + 1); 91 return adoptRef(new (string) UStringImpl(length)); 46 92 } 47 93 … … 78 124 } 79 125 80 SharedUChar* UStringImpl::baseSharedBuffer()126 PassRefPtr<UStringImpl> UStringImpl::create(PassRefPtr<SharedUChar> sharedBuffer, const UChar* buffer, unsigned length) 81 127 { 82 ASSERT((bufferOwnership() == BufferShared) 83 || ((bufferOwnership() == BufferOwned) && !m_buffer)); 84 85 if (bufferOwnership() != BufferShared) { 86 m_refCountAndFlags = (m_refCountAndFlags & ~s_refCountMaskBufferOwnership) | BufferShared; 87 m_bufferShared = SharedUChar::create(new SharableUChar(m_data)).releaseRef(); 88 } 89 90 return m_bufferShared; 128 if (!length) 129 return empty(); 130 return adoptRef(new UStringImpl(buffer, length, sharedBuffer)); 91 131 } 92 132 93 133 SharedUChar* UStringImpl::sharedBuffer() 94 134 { 95 if (m_length < s_minLengthToShare)135 if (m_length < minLengthToShare) 96 136 return 0; 137 // All static strings are smaller that the minimim length to share. 97 138 ASSERT(!isStatic()); 98 139 99 UStringImpl* owner = bufferOwnerString(); 100 if (owner->bufferOwnership() == BufferInternal) 140 BufferOwnership ownership = bufferOwnership(); 141 142 if (ownership == BufferInternal) 101 143 return 0; 144 if (ownership == BufferSubstring) 145 return m_substringBuffer->sharedBuffer(); 146 if (ownership == BufferOwned) { 147 ASSERT(!m_sharedBuffer); 148 m_sharedBuffer = SharedUChar::create(new SharableUChar(m_data)).releaseRef(); 149 m_refCountAndFlags = (m_refCountAndFlags & ~s_refCountMaskBufferOwnership) | BufferShared; 150 } 102 151 103 return owner->baseSharedBuffer(); 104 } 105 106 UStringImpl::~UStringImpl() 107 { 108 ASSERT(!isStatic()); 109 checkConsistency(); 110 111 if (isIdentifier()) 112 Identifier::remove(this); 113 114 if (bufferOwnership() != BufferInternal) { 115 if (bufferOwnership() == BufferOwned) 116 fastFree(const_cast<UChar*>(m_data)); 117 else if (bufferOwnership() == BufferSubstring) 118 m_bufferSubstring->deref(); 119 else { 120 ASSERT(bufferOwnership() == BufferShared); 121 m_bufferShared->deref(); 122 } 123 } 152 ASSERT(bufferOwnership() == BufferShared); 153 ASSERT(m_sharedBuffer); 154 return m_sharedBuffer; 124 155 } 125 156
Note:
See TracChangeset
for help on using the changeset viewer.