| 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 18 matching lines...) Expand all Loading... |
| 29 #include "wtf/RetainPtr.h" | 29 #include "wtf/RetainPtr.h" |
| 30 #include "wtf/Threading.h" | 30 #include "wtf/Threading.h" |
| 31 #include <CoreFoundation/CoreFoundation.h> | 31 #include <CoreFoundation/CoreFoundation.h> |
| 32 | 32 |
| 33 namespace WTF { | 33 namespace WTF { |
| 34 | 34 |
| 35 namespace StringWrapperCFAllocator { | 35 namespace StringWrapperCFAllocator { |
| 36 | 36 |
| 37 static StringImpl* currentString; | 37 static StringImpl* currentString; |
| 38 | 38 |
| 39 static const void* retain(const void* info) | 39 static const void* retain(const void* info) { |
| 40 { | 40 return info; |
| 41 return info; | |
| 42 } | 41 } |
| 43 | 42 |
| 44 NO_RETURN_DUE_TO_ASSERT | 43 NO_RETURN_DUE_TO_ASSERT |
| 45 static void release(const void*) | 44 static void release(const void*) { |
| 46 { | 45 ASSERT_NOT_REACHED(); |
| 47 ASSERT_NOT_REACHED(); | |
| 48 } | 46 } |
| 49 | 47 |
| 50 static CFStringRef copyDescription(const void*) | 48 static CFStringRef copyDescription(const void*) { |
| 51 { | 49 return CFSTR("WTF::String-based allocator"); |
| 52 return CFSTR("WTF::String-based allocator"); | |
| 53 } | 50 } |
| 54 | 51 |
| 55 static void* allocate(CFIndex size, CFOptionFlags, void*) | 52 static void* allocate(CFIndex size, CFOptionFlags, void*) { |
| 56 { | 53 StringImpl* underlyingString = 0; |
| 57 StringImpl* underlyingString = 0; | 54 if (isMainThread()) { |
| 58 if (isMainThread()) { | 55 underlyingString = currentString; |
| 59 underlyingString = currentString; | 56 if (underlyingString) { |
| 60 if (underlyingString) { | 57 currentString = 0; |
| 61 currentString = 0; | 58 underlyingString->ref(); // Balanced by call to deref in deallocate below
. |
| 62 underlyingString->ref(); // Balanced by call to deref in deallocate
below. | |
| 63 } | |
| 64 } | 59 } |
| 65 StringImpl** header = static_cast<StringImpl**>(WTF::Partitions::fastMalloc(
sizeof(StringImpl*) + size)); | 60 } |
| 66 *header = underlyingString; | 61 StringImpl** header = static_cast<StringImpl**>(WTF::Partitions::fastMalloc(si
zeof(StringImpl*) + size)); |
| 67 return header + 1; | 62 *header = underlyingString; |
| 63 return header + 1; |
| 68 } | 64 } |
| 69 | 65 |
| 70 static void* reallocate(void* pointer, CFIndex newSize, CFOptionFlags, void*) | 66 static void* reallocate(void* pointer, CFIndex newSize, CFOptionFlags, void*) { |
| 71 { | 67 size_t newAllocationSize = sizeof(StringImpl*) + newSize; |
| 72 size_t newAllocationSize = sizeof(StringImpl*) + newSize; | 68 StringImpl** header = static_cast<StringImpl**>(pointer) - 1; |
| 73 StringImpl** header = static_cast<StringImpl**>(pointer) - 1; | 69 ASSERT(!*header); |
| 74 ASSERT(!*header); | 70 header = static_cast<StringImpl**>(WTF::Partitions::fastRealloc(header, newAll
ocationSize)); |
| 75 header = static_cast<StringImpl**>(WTF::Partitions::fastRealloc(header, newA
llocationSize)); | 71 return header + 1; |
| 76 return header + 1; | |
| 77 } | 72 } |
| 78 | 73 |
| 79 static void deallocateOnMainThread(void* headerPointer) | 74 static void deallocateOnMainThread(void* headerPointer) { |
| 80 { | 75 StringImpl** header = static_cast<StringImpl**>(headerPointer); |
| 81 StringImpl** header = static_cast<StringImpl**>(headerPointer); | 76 StringImpl* underlyingString = *header; |
| 82 StringImpl* underlyingString = *header; | 77 ASSERT(underlyingString); |
| 83 ASSERT(underlyingString); | 78 underlyingString->deref(); // Balanced by call to ref in allocate above. |
| 84 underlyingString->deref(); // Balanced by call to ref in allocate above. | 79 WTF::Partitions::fastFree(header); |
| 85 WTF::Partitions::fastFree(header); | |
| 86 } | 80 } |
| 87 | 81 |
| 88 static void deallocate(void* pointer, void*) | 82 static void deallocate(void* pointer, void*) { |
| 89 { | 83 StringImpl** header = static_cast<StringImpl**>(pointer) - 1; |
| 90 StringImpl** header = static_cast<StringImpl**>(pointer) - 1; | 84 StringImpl* underlyingString = *header; |
| 91 StringImpl* underlyingString = *header; | 85 if (!underlyingString) { |
| 92 if (!underlyingString) { | 86 WTF::Partitions::fastFree(header); |
| 93 WTF::Partitions::fastFree(header); | 87 } else { |
| 88 if (!isMainThread()) { |
| 89 internal::callOnMainThread(&deallocateOnMainThread, header); |
| 94 } else { | 90 } else { |
| 95 if (!isMainThread()) { | 91 underlyingString->deref(); // Balanced by call to ref in allocate above. |
| 96 internal::callOnMainThread(&deallocateOnMainThread, header); | 92 WTF::Partitions::fastFree(header); |
| 97 } else { | |
| 98 underlyingString->deref(); // Balanced by call to ref in allocate ab
ove. | |
| 99 WTF::Partitions::fastFree(header); | |
| 100 } | |
| 101 } | 93 } |
| 94 } |
| 102 } | 95 } |
| 103 | 96 |
| 104 static CFIndex preferredSize(CFIndex size, CFOptionFlags, void*) | 97 static CFIndex preferredSize(CFIndex size, CFOptionFlags, void*) { |
| 105 { | 98 // FIXME: If FastMalloc provided a "good size" callback, we'd want to use it h
ere. |
| 106 // FIXME: If FastMalloc provided a "good size" callback, we'd want to use it
here. | 99 // Note that this optimization would help performance for strings created with
the |
| 107 // Note that this optimization would help performance for strings created wi
th the | 100 // allocator that are mutable, and those typically are only created by callers
who |
| 108 // allocator that are mutable, and those typically are only created by calle
rs who | 101 // make a new string using the old string's allocator, such as some of the cal
l |
| 109 // make a new string using the old string's allocator, such as some of the c
all | 102 // sites in CFURL. |
| 110 // sites in CFURL. | 103 return size; |
| 111 return size; | |
| 112 } | 104 } |
| 113 | 105 |
| 114 static CFAllocatorRef create() | 106 static CFAllocatorRef create() { |
| 115 { | 107 CFAllocatorContext context = {0, 0, retain, release, copyDescription, allocate
, reallocate, deallocate, preferredSize}; |
| 116 CFAllocatorContext context = { 0, 0, retain, release, copyDescription, alloc
ate, reallocate, deallocate, preferredSize }; | 108 return CFAllocatorCreate(0, &context); |
| 117 return CFAllocatorCreate(0, &context); | |
| 118 } | 109 } |
| 119 | 110 |
| 120 static CFAllocatorRef allocator() | 111 static CFAllocatorRef allocator() { |
| 121 { | 112 static CFAllocatorRef allocator = create(); |
| 122 static CFAllocatorRef allocator = create(); | 113 return allocator; |
| 123 return allocator; | 114 } |
| 124 } | 115 } |
| 125 | 116 |
| 126 } | 117 RetainPtr<CFStringRef> StringImpl::createCFString() { |
| 118 // Since garbage collection isn't compatible with custom allocators, we |
| 119 // can't use the NoCopy variants of CFStringCreate*() when GC is enabled. |
| 120 if (!m_length || !isMainThread()) { |
| 121 if (is8Bit()) |
| 122 return adoptCF(CFStringCreateWithBytes(0, reinterpret_cast<const UInt8*>(c
haracters8()), m_length, kCFStringEncodingISOLatin1, false)); |
| 123 return adoptCF(CFStringCreateWithCharacters(0, reinterpret_cast<const UniCha
r*>(characters16()), m_length)); |
| 124 } |
| 125 CFAllocatorRef allocator = StringWrapperCFAllocator::allocator(); |
| 127 | 126 |
| 128 RetainPtr<CFStringRef> StringImpl::createCFString() | 127 // Put pointer to the StringImpl in a global so the allocator can store it wit
h the CFString. |
| 129 { | 128 ASSERT(!StringWrapperCFAllocator::currentString); |
| 130 // Since garbage collection isn't compatible with custom allocators, we | 129 StringWrapperCFAllocator::currentString = this; |
| 131 // can't use the NoCopy variants of CFStringCreate*() when GC is enabled. | |
| 132 if (!m_length || !isMainThread()) { | |
| 133 if (is8Bit()) | |
| 134 return adoptCF(CFStringCreateWithBytes(0, reinterpret_cast<const UIn
t8*>(characters8()), m_length, kCFStringEncodingISOLatin1, false)); | |
| 135 return adoptCF(CFStringCreateWithCharacters(0, reinterpret_cast<const Un
iChar*>(characters16()), m_length)); | |
| 136 } | |
| 137 CFAllocatorRef allocator = StringWrapperCFAllocator::allocator(); | |
| 138 | 130 |
| 139 // Put pointer to the StringImpl in a global so the allocator can store it w
ith the CFString. | 131 CFStringRef string; |
| 140 ASSERT(!StringWrapperCFAllocator::currentString); | 132 if (is8Bit()) |
| 141 StringWrapperCFAllocator::currentString = this; | 133 string = CFStringCreateWithBytesNoCopy(allocator, reinterpret_cast<const UIn
t8*>(characters8()), m_length, kCFStringEncodingISOLatin1, false, kCFAllocatorNu
ll); |
| 134 else |
| 135 string = CFStringCreateWithCharactersNoCopy(allocator, reinterpret_cast<cons
t UniChar*>(characters16()), m_length, kCFAllocatorNull); |
| 136 // CoreFoundation might not have to allocate anything, we clear currentString
in case we did not execute allocate(). |
| 137 StringWrapperCFAllocator::currentString = 0; |
| 142 | 138 |
| 143 CFStringRef string; | 139 return adoptCF(string); |
| 144 if (is8Bit()) | |
| 145 string = CFStringCreateWithBytesNoCopy(allocator, reinterpret_cast<const
UInt8*>(characters8()), m_length, kCFStringEncodingISOLatin1, false, kCFAllocat
orNull); | |
| 146 else | |
| 147 string = CFStringCreateWithCharactersNoCopy(allocator, reinterpret_cast<
const UniChar*>(characters16()), m_length, kCFAllocatorNull); | |
| 148 // CoreFoundation might not have to allocate anything, we clear currentStrin
g in case we did not execute allocate(). | |
| 149 StringWrapperCFAllocator::currentString = 0; | |
| 150 | |
| 151 return adoptCF(string); | |
| 152 } | 140 } |
| 153 | 141 |
| 154 // On StringImpl creation we could check if the allocator is the StringWrapperCF
Allocator. | 142 // On StringImpl creation we could check if the allocator is the StringWrapperCF
Allocator. |
| 155 // If it is, then we could find the original StringImpl and just return that. Bu
t to | 143 // If it is, then we could find the original StringImpl and just return that. Bu
t to |
| 156 // do that we'd have to compute the offset from CFStringRef to the allocated blo
ck; | 144 // do that we'd have to compute the offset from CFStringRef to the allocated blo
ck; |
| 157 // the CFStringRef is *not* at the start of an allocated block. Testing shows 10
00x | 145 // the CFStringRef is *not* at the start of an allocated block. Testing shows 10
00x |
| 158 // more calls to createCFString than calls to the create functions with the appr
opriate | 146 // more calls to createCFString than calls to the create functions with the appr
opriate |
| 159 // allocator, so it's probably not urgent optimize that case. | 147 // allocator, so it's probably not urgent optimize that case. |
| 160 | |
| 161 } | 148 } |
| 162 | 149 |
| 163 #endif // OS(MACOSX) | 150 #endif // OS(MACOSX) |
| OLD | NEW |