Changeset 31677 in webkit for trunk/JavaScriptCore/kjs/ustring.cpp
- Timestamp:
- Apr 6, 2008, 11:33:20 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/ustring.cpp
r30942 r31677 111 111 } 112 112 113 CString CString::adopt(char* c, size_t len) 114 { 115 CString s; 116 s.data = c; 117 s.length = len; 118 119 return s; 120 } 121 113 122 CString &CString::append(const CString &t) 114 123 { … … 163 172 } 164 173 165 // Hack here to avoid a global with a constructor; point to an unsigned short instead of a UChar.174 // These static strings are immutable, except for rc, whose initial value is chosen to reduce the possibility of it becoming zero due to ref/deref not being thread-safe. 166 175 static UChar sharedEmptyChar; 167 UString::Rep UString::Rep::null = { 0, 0, 1, 0, 0, &UString::Rep::null, 0, 0, 0, 0, 0, 0 }; 168 UString::Rep UString::Rep::empty = { 0, 0, 1, 0, 0, &UString::Rep::empty, 0, &sharedEmptyChar, 0, 0, 0, 0 }; 169 const int normalStatBufferSize = 4096; 170 static char *statBuffer = 0; // FIXME: This buffer is never deallocated. 171 static int statBufferSize = 0; 176 UString::Rep UString::Rep::null = { 0, 0, INT_MAX / 2, 0, false, true, &UString::Rep::null, 0, 0, 0, 0, 0, 0 }; 177 UString::Rep UString::Rep::empty = { 0, 0, INT_MAX / 2, 0, false, true, &UString::Rep::empty, 0, &sharedEmptyChar, 0, 0, 0, 0 }; 178 179 static char* statBuffer = 0; // Only used for debugging via UString::ascii(). 172 180 173 181 PassRefPtr<UString::Rep> UString::Rep::createCopying(const UChar *d, int l) 174 182 { 175 ASSERT(JSLock::lockCount() > 0);176 177 183 int sizeInBytes = l * sizeof(UChar); 178 184 UChar *copyD = static_cast<UChar *>(fastMalloc(sizeInBytes)); … … 184 190 PassRefPtr<UString::Rep> UString::Rep::create(UChar *d, int l) 185 191 { 186 ASSERT(JSLock::lockCount() > 0);187 188 192 Rep* r = new Rep; 189 193 r->offset = 0; … … 191 195 r->rc = 1; 192 196 r->_hash = 0; 193 r->isIdentifier = 0; 197 r->isIdentifier = false; 198 r->isStatic = false; 194 199 r->baseString = r; 195 200 r->reportedCost = 0; … … 206 211 PassRefPtr<UString::Rep> UString::Rep::create(PassRefPtr<Rep> base, int offset, int length) 207 212 { 208 ASSERT(JSLock::lockCount() > 0);209 213 ASSERT(base); 210 214 … … 221 225 r->rc = 1; 222 226 r->_hash = 0; 223 r->isIdentifier = 0; 227 r->isIdentifier = false; 228 r->isStatic = false; 224 229 r->baseString = base.releaseRef(); 225 230 r->reportedCost = 0; … … 236 241 void UString::Rep::destroy() 237 242 { 238 ASSERT(JSLock::lockCount() > 0); 239 240 if (isIdentifier) 241 Identifier::remove(this); 242 if (baseString != this) { 243 baseString->deref(); 244 } else { 245 fastFree(buf); 246 } 247 delete this; 243 // Static null and empty strings can never be destroyed, but we cannot rely on reference counting, because ref/deref are not thread-safe. 244 if (!isStatic) { 245 if (isIdentifier) 246 Identifier::remove(this); 247 if (baseString == this) 248 fastFree(buf); 249 else 250 baseString->deref(); 251 252 delete this; 253 } 248 254 } 249 255 … … 519 525 const UString& UString::null() 520 526 { 521 static UString* n = new UString; 527 static UString* n = new UString; // Should be called from main thread at least once to be safely initialized. 522 528 return *n; 523 529 } … … 858 864 CString UString::cstring() const 859 865 { 860 return ascii();861 }862 863 char *UString::ascii() const864 {865 // Never make the buffer smaller than normalStatBufferSize.866 // Thus we almost never need to reallocate.867 866 int length = size(); 868 867 int neededSize = length + 1; 869 if (neededSize < normalStatBufferSize) { 870 neededSize = normalStatBufferSize; 871 } 872 if (neededSize != statBufferSize) { 873 delete [] statBuffer; 874 statBuffer = new char [neededSize]; 875 statBufferSize = neededSize; 876 } 868 char* buf = new char[neededSize]; 869 870 const UChar* p = data(); 871 char* q = buf; 872 const UChar* limit = p + length; 873 while (p != limit) { 874 *q = static_cast<char>(p[0]); 875 ++p; 876 ++q; 877 } 878 *q = '\0'; 879 880 return CString::adopt(buf, length); 881 } 882 883 char *UString::ascii() const 884 { 885 int length = size(); 886 int neededSize = length + 1; 887 delete[] statBuffer; 888 statBuffer = new char[neededSize]; 877 889 878 890 const UChar *p = data(); … … 950 962 return NaN; 951 963 952 const char *c = ascii(); 964 CString s = cstring(); 965 const char* c = s.c_str(); 953 966 954 967 // skip leading white space
Note:
See TracChangeset
for help on using the changeset viewer.