| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_STRING16_H_ | 5 #ifndef BASE_STRING16_H_ |
| 6 #define BASE_STRING16_H_ | 6 #define BASE_STRING16_H_ |
| 7 | 7 |
| 8 // WHAT: | 8 // WHAT: |
| 9 // A version of std::basic_string that provides 2-byte characters even when | 9 // A version of std::basic_string that provides 2-byte characters even when |
| 10 // wchar_t is not implemented as a 2-byte type. You can access this class as | 10 // wchar_t is not implemented as a 2-byte type. You can access this class as |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 return c1 == c2; | 115 return c1 == c2; |
| 116 } | 116 } |
| 117 | 117 |
| 118 static int_type eof() { | 118 static int_type eof() { |
| 119 return static_cast<int_type>(EOF); | 119 return static_cast<int_type>(EOF); |
| 120 } | 120 } |
| 121 }; | 121 }; |
| 122 | 122 |
| 123 } // namespace base | 123 } // namespace base |
| 124 | 124 |
| 125 // The string class will be explicitly instantiated only once, in string16.cc. |
| 126 // |
| 127 // std::basic_string<> in GNU libstdc++ contains a static data member, |
| 128 // _S_empty_rep_storage, to represent empty strings. When an operation such |
| 129 // as assignment or destruction is performed on a string, causing its existing |
| 130 // data member to be invalidated, it must not be freed if this static data |
| 131 // member is being used. Otherwise, it counts as an attempt to free static |
| 132 // (and not allocated) data, which is a memory error. |
| 133 // |
| 134 // Generally, due to C++ template magic, _S_empty_rep_storage will be marked |
| 135 // as a coalesced symbol, meaning that the linker will combine multiple |
| 136 // instances into a single one when generating output. |
| 137 // |
| 138 // If a string class is used by multiple shared libraries, a problem occurs. |
| 139 // Each library will get its own copy of _S_empty_rep_storage. When strings |
| 140 // are passed across a library boundary for alteration or destruction, memory |
| 141 // errors will result. GNU libstdc++ contains a configuration option, |
| 142 // --enable-fully-dynamic-string (_GLIBCXX_FULLY_DYNAMIC_STRING), which |
| 143 // disables the static data member optimization, but it's a good optimization |
| 144 // and non-STL code is generally at the mercy of the system's STL |
| 145 // configuration. Fully-dynamic strings are not the default for GNU libstdc++ |
| 146 // libstdc++ itself or for the libstdc++ installations on the systems we care |
| 147 // about, such as Mac OS X and relevant flavors of Linux. |
| 148 // |
| 149 // See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24196 . |
| 150 // |
| 151 // To avoid problems, string classes need to be explicitly instantiated only |
| 152 // once, in exactly one library. All other string users see it via an "extern" |
| 153 // declaration. This is precisely how GNU libstdc++ handles |
| 154 // std::basic_string<char> (string) and std::basic_string<wchar_t> (wstring). |
| 155 // |
| 156 // This also works around a Mac OS X linker bug in ld64-85.2.1 (Xcode 3.1.2), |
| 157 // in which the linker does not fully coalesce symbols when dead code |
| 158 // stripping is enabled. This bug causes the memory errors described above |
| 159 // to occur even when a std::basic_string<> does not cross shared library |
| 160 // boundaries, such as in statically-linked executables. |
| 161 // |
| 162 // TODO(mark): File this bug with Apple and update this note with a bug number. |
| 163 |
| 164 extern template class std::basic_string<char16, base::string16_char_traits>; |
| 165 |
| 125 typedef std::basic_string<char16, base::string16_char_traits> string16; | 166 typedef std::basic_string<char16, base::string16_char_traits> string16; |
| 126 | 167 |
| 127 extern std::ostream& operator<<(std::ostream& out, const string16& str); | 168 extern std::ostream& operator<<(std::ostream& out, const string16& str); |
| 128 | 169 |
| 129 #endif // WCHAR_T_IS_UTF32 | 170 #endif // WCHAR_T_IS_UTF32 |
| 130 | 171 |
| 131 #endif // BASE_STRING16_H_ | 172 #endif // BASE_STRING16_H_ |
| OLD | NEW |