|
Lines 1-5
WebCore/platform/text/cf/StringImplCF.cpp_sec1
|
| 1 |
/** |
1 |
/* |
| 2 |
* Copyright (C) 2006 Apple Computer, Inc. |
2 |
* Copyright (C) 2006, 2009 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 |
|
Lines 24-37
WebCore/platform/text/cf/StringImplCF.cpp_sec2
|
| 24 |
#if PLATFORM(CF) || (PLATFORM(QT) && PLATFORM(DARWIN)) |
24 |
#if PLATFORM(CF) || (PLATFORM(QT) && PLATFORM(DARWIN)) |
| 25 |
|
25 |
|
| 26 |
#include <CoreFoundation/CoreFoundation.h> |
26 |
#include <CoreFoundation/CoreFoundation.h> |
|
|
27 |
#include <wtf/PassRefPtr.h> |
| 28 |
|
| 29 |
#if PLATFORM(MAC) || !defined(BUILDING_ON_TIGER) |
| 30 |
#include <objc/objc-auto.h> |
| 31 |
#endif |
| 27 |
|
32 |
|
| 28 |
namespace WebCore { |
33 |
namespace WebCore { |
| 29 |
|
34 |
|
|
|
35 |
namespace StringWrapperCFAllocator { |
| 36 |
|
| 37 |
static CFAllocatorRef create(); |
| 38 |
|
| 39 |
static StringImpl* currentString; |
| 40 |
|
| 41 |
static CFAllocatorRef allocator() |
| 42 |
{ |
| 43 |
static CFAllocatorRef allocator = create(); |
| 44 |
return allocator; |
| 45 |
} |
| 46 |
|
| 47 |
static const void* retain(const void* info) |
| 48 |
{ |
| 49 |
return info; |
| 50 |
} |
| 51 |
|
| 52 |
static void release(const void*) |
| 53 |
{ |
| 54 |
ASSERT_NOT_REACHED(); |
| 55 |
} |
| 56 |
|
| 57 |
static CFStringRef copyDescription(const void*) |
| 58 |
{ |
| 59 |
return CFSTR("WebCore::String-based allocator"); |
| 60 |
} |
| 61 |
|
| 62 |
static void* allocate(CFIndex size, CFOptionFlags, void*) |
| 63 |
{ |
| 64 |
StringImpl** header = static_cast<StringImpl**>(fastMalloc(sizeof(StringImpl*) + size)); |
| 65 |
if (currentString) |
| 66 |
currentString->ref(); // Balanced by call to deref in StringWrapperCFAllocator::deallocate. |
| 67 |
*header = currentString; |
| 68 |
currentString = 0; |
| 69 |
return header + 1; |
| 70 |
} |
| 71 |
|
| 72 |
static void* reallocate(void* pointer, CFIndex newSize, CFOptionFlags, void*) |
| 73 |
{ |
| 74 |
StringImpl** header = static_cast<StringImpl**>(pointer) - 1; |
| 75 |
header = static_cast<StringImpl**>(fastRealloc(header, sizeof(StringImpl*) + newSize)); |
| 76 |
return header + 1; |
| 77 |
} |
| 78 |
|
| 79 |
static void deallocate(void* pointer, void*) |
| 80 |
{ |
| 81 |
StringImpl** header = static_cast<StringImpl**>(pointer) - 1; |
| 82 |
if (StringImpl* string = *header) |
| 83 |
string->deref(); // Balanced by call to ref in StringWrapperCFAllocator::allocate. |
| 84 |
fastFree(header); |
| 85 |
} |
| 86 |
|
| 87 |
CFIndex preferredSize(CFIndex size, CFOptionFlags, void*) |
| 88 |
{ |
| 89 |
// FIXME: If FastMalloc provided a "good size" callback, we'd want to use it here. |
| 90 |
// Note that this optimization would help performance for strings created with the |
| 91 |
// allocator that are mutable, and those typically are only created by callers who |
| 92 |
// make a new string using the old string's allocator, such as some of the call |
| 93 |
// sites in CFURL. |
| 94 |
return size; |
| 95 |
} |
| 96 |
|
| 97 |
static CFAllocatorRef create() |
| 98 |
{ |
| 99 |
#if PLATFORM(MAC) || !defined(BUILDING_ON_TIGER) |
| 100 |
if (objc_collectingEnabled()) |
| 101 |
return 0; |
| 102 |
#endif |
| 103 |
CFAllocatorContext context = { 0, 0, retain, release, copyDescription, allocate, reallocate, deallocate, preferredSize }; |
| 104 |
return CFAllocatorCreate(0, &context); |
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
|
| 30 |
CFStringRef StringImpl::createCFString() |
109 |
CFStringRef StringImpl::createCFString() |
| 31 |
{ |
110 |
{ |
| 32 |
return CFStringCreateWithCharacters(NULL, reinterpret_cast<const UniChar*>(m_data), m_length); |
111 |
CFAllocatorRef allocator = StringWrapperCFAllocator::allocator(); |
|
|
112 |
|
| 113 |
#if PLATFORM(MAC) || !defined(BUILDING_ON_TIGER) |
| 114 |
// Since garbage collection isn't compatible with custom allocators, use this code path when it's active. |
| 115 |
if (!allocator) |
| 116 |
return CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar*>(m_data), m_length); |
| 117 |
#endif |
| 118 |
|
| 119 |
// Put pointer to the StringImpl in a global so the allocator can store it with the CFString. |
| 120 |
ASSERT(!StringWrapperCFAllocator::currentString); |
| 121 |
StringWrapperCFAllocator::currentString = this; |
| 122 |
|
| 123 |
CFStringRef string = CFStringCreateWithCharactersNoCopy(allocator, reinterpret_cast<const UniChar*>(m_data), m_length, kCFAllocatorNull); |
| 124 |
|
| 125 |
// The allocator cleared the global when it read it, but also clear it here just in case. |
| 126 |
ASSERT(!StringWrapperCFAllocator::currentString); |
| 127 |
StringWrapperCFAllocator::currentString = 0; |
| 128 |
|
| 129 |
return string; |
| 33 |
} |
130 |
} |
| 34 |
|
131 |
|
|
|
132 |
// On StringImpl creation we could check if the allocator is the StringWrapperCFAllocator. |
| 133 |
// If it is, then we could find the original StringImpl and just return that. But to |
| 134 |
// do that we'd have to compute the offset from CFStringRef to the allocated block; |
| 135 |
// the CFStringRef is *not* at the start of an allocated block. Testing shows 1000x |
| 136 |
// more calls to createCFString than calls to the create functions with the appropriate |
| 137 |
// allocator, so it's probably not urgent optimize that case. |
| 138 |
|
| 35 |
} |
139 |
} |
| 36 |
|
140 |
|
| 37 |
#endif // PLATFORM(CF) || (PLATFORM(QT) && PLATFORM(DARWIN)) |
141 |
#endif // PLATFORM(CF) || (PLATFORM(QT) && PLATFORM(DARWIN)) |