| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2009, 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2009, 2012 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
| 6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
| 7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
| 8 * | 8 * |
| 9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 namespace StringWrapperCFAllocator { | 33 namespace StringWrapperCFAllocator { |
| 34 | 34 |
| 35 static StringImpl* currentString; | 35 static StringImpl* currentString; |
| 36 | 36 |
| 37 static const void* retain(const void* info) { | 37 static const void* retain(const void* info) { |
| 38 return info; | 38 return info; |
| 39 } | 39 } |
| 40 | 40 |
| 41 static void release(const void*) { | 41 static void release(const void*) { |
| 42 ASSERT_NOT_REACHED(); | 42 NOTREACHED(); |
| 43 } | 43 } |
| 44 | 44 |
| 45 static CFStringRef copyDescription(const void*) { | 45 static CFStringRef copyDescription(const void*) { |
| 46 return CFSTR("WTF::String-based allocator"); | 46 return CFSTR("WTF::String-based allocator"); |
| 47 } | 47 } |
| 48 | 48 |
| 49 static void* allocate(CFIndex size, CFOptionFlags, void*) { | 49 static void* allocate(CFIndex size, CFOptionFlags, void*) { |
| 50 StringImpl* underlyingString = 0; | 50 StringImpl* underlyingString = 0; |
| 51 if (isMainThread()) { | 51 if (isMainThread()) { |
| 52 underlyingString = currentString; | 52 underlyingString = currentString; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 67 StringImpl** header = static_cast<StringImpl**>(pointer) - 1; | 67 StringImpl** header = static_cast<StringImpl**>(pointer) - 1; |
| 68 ASSERT(!*header); | 68 ASSERT(!*header); |
| 69 header = static_cast<StringImpl**>(WTF::Partitions::fastRealloc( | 69 header = static_cast<StringImpl**>(WTF::Partitions::fastRealloc( |
| 70 header, newAllocationSize, WTF_HEAP_PROFILER_TYPE_NAME(StringImpl*))); | 70 header, newAllocationSize, WTF_HEAP_PROFILER_TYPE_NAME(StringImpl*))); |
| 71 return header + 1; | 71 return header + 1; |
| 72 } | 72 } |
| 73 | 73 |
| 74 static void deallocateOnMainThread(void* headerPointer) { | 74 static void deallocateOnMainThread(void* headerPointer) { |
| 75 StringImpl** header = static_cast<StringImpl**>(headerPointer); | 75 StringImpl** header = static_cast<StringImpl**>(headerPointer); |
| 76 StringImpl* underlyingString = *header; | 76 StringImpl* underlyingString = *header; |
| 77 ASSERT(underlyingString); | 77 DCHECK(underlyingString); |
| 78 underlyingString->deref(); // Balanced by call to ref in allocate above. | 78 underlyingString->deref(); // Balanced by call to ref in allocate above. |
| 79 WTF::Partitions::fastFree(header); | 79 WTF::Partitions::fastFree(header); |
| 80 } | 80 } |
| 81 | 81 |
| 82 static void deallocate(void* pointer, void*) { | 82 static void deallocate(void* pointer, void*) { |
| 83 StringImpl** header = static_cast<StringImpl**>(pointer) - 1; | 83 StringImpl** header = static_cast<StringImpl**>(pointer) - 1; |
| 84 StringImpl* underlyingString = *header; | 84 StringImpl* underlyingString = *header; |
| 85 if (!underlyingString) { | 85 if (!underlyingString) { |
| 86 WTF::Partitions::fastFree(header); | 86 WTF::Partitions::fastFree(header); |
| 87 } else { | 87 } else { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 return adoptCF(CFStringCreateWithBytes( | 125 return adoptCF(CFStringCreateWithBytes( |
| 126 0, reinterpret_cast<const UInt8*>(characters8()), m_length, | 126 0, reinterpret_cast<const UInt8*>(characters8()), m_length, |
| 127 kCFStringEncodingISOLatin1, false)); | 127 kCFStringEncodingISOLatin1, false)); |
| 128 return adoptCF(CFStringCreateWithCharacters( | 128 return adoptCF(CFStringCreateWithCharacters( |
| 129 0, reinterpret_cast<const UniChar*>(characters16()), m_length)); | 129 0, reinterpret_cast<const UniChar*>(characters16()), m_length)); |
| 130 } | 130 } |
| 131 CFAllocatorRef allocator = StringWrapperCFAllocator::allocator(); | 131 CFAllocatorRef allocator = StringWrapperCFAllocator::allocator(); |
| 132 | 132 |
| 133 // Put pointer to the StringImpl in a global so the allocator can store it | 133 // Put pointer to the StringImpl in a global so the allocator can store it |
| 134 // with the CFString. | 134 // with the CFString. |
| 135 ASSERT(!StringWrapperCFAllocator::currentString); | 135 DCHECK(!StringWrapperCFAllocator::currentString); |
| 136 StringWrapperCFAllocator::currentString = this; | 136 StringWrapperCFAllocator::currentString = this; |
| 137 | 137 |
| 138 CFStringRef string; | 138 CFStringRef string; |
| 139 if (is8Bit()) | 139 if (is8Bit()) |
| 140 string = CFStringCreateWithBytesNoCopy( | 140 string = CFStringCreateWithBytesNoCopy( |
| 141 allocator, reinterpret_cast<const UInt8*>(characters8()), m_length, | 141 allocator, reinterpret_cast<const UInt8*>(characters8()), m_length, |
| 142 kCFStringEncodingISOLatin1, false, kCFAllocatorNull); | 142 kCFStringEncodingISOLatin1, false, kCFAllocatorNull); |
| 143 else | 143 else |
| 144 string = CFStringCreateWithCharactersNoCopy( | 144 string = CFStringCreateWithCharactersNoCopy( |
| 145 allocator, reinterpret_cast<const UniChar*>(characters16()), m_length, | 145 allocator, reinterpret_cast<const UniChar*>(characters16()), m_length, |
| 146 kCFAllocatorNull); | 146 kCFAllocatorNull); |
| 147 // CoreFoundation might not have to allocate anything, we clear currentString | 147 // CoreFoundation might not have to allocate anything, we clear currentString |
| 148 // in case we did not execute allocate(). | 148 // in case we did not execute allocate(). |
| 149 StringWrapperCFAllocator::currentString = 0; | 149 StringWrapperCFAllocator::currentString = 0; |
| 150 | 150 |
| 151 return adoptCF(string); | 151 return adoptCF(string); |
| 152 } | 152 } |
| 153 | 153 |
| 154 // On StringImpl creation we could check if the allocator is the | 154 // On StringImpl creation we could check if the allocator is the |
| 155 // StringWrapperCFAllocator. If it is, then we could find the original | 155 // StringWrapperCFAllocator. If it is, then we could find the original |
| 156 // StringImpl and just return that. But to do that we'd have to compute the | 156 // StringImpl and just return that. But to do that we'd have to compute the |
| 157 // offset from CFStringRef to the allocated block; the CFStringRef is *not* at | 157 // offset from CFStringRef to the allocated block; the CFStringRef is *not* at |
| 158 // the start of an allocated block. Testing shows 1000x more calls to | 158 // the start of an allocated block. Testing shows 1000x more calls to |
| 159 // createCFString than calls to the create functions with the appropriate | 159 // createCFString than calls to the create functions with the appropriate |
| 160 // allocator, so it's probably not urgent optimize that case. | 160 // allocator, so it's probably not urgent optimize that case. |
| 161 | 161 |
| 162 } // namespace WTF | 162 } // namespace WTF |
| 163 | 163 |
| 164 #endif // OS(MACOSX) | 164 #endif // OS(MACOSX) |
| OLD | NEW |