OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) | 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) |
4 * (C) 2001 Dirk Mueller ( mueller@kde.org ) | 4 * (C) 2001 Dirk Mueller ( mueller@kde.org ) |
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All r
ights reserved. | 5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All r
ights reserved. |
6 * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net) | 6 * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net) |
7 * | 7 * |
8 * This library is free software; you can redistribute it and/or | 8 * This library is free software; you can redistribute it and/or |
9 * modify it under the terms of the GNU Library General Public | 9 * modify it under the terms of the GNU Library General Public |
10 * License as published by the Free Software Foundation; either | 10 * License as published by the Free Software Foundation; either |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 // Same as createUninitialized() except here we use realloc. | 351 // Same as createUninitialized() except here we use realloc. |
352 RELEASE_ASSERT(length <= ((std::numeric_limits<unsigned>::max() - sizeof(Str
ingImpl)) / sizeof(UChar))); | 352 RELEASE_ASSERT(length <= ((std::numeric_limits<unsigned>::max() - sizeof(Str
ingImpl)) / sizeof(UChar))); |
353 size_t size = sizeof(StringImpl) + length * sizeof(UChar); | 353 size_t size = sizeof(StringImpl) + length * sizeof(UChar); |
354 originalString->~StringImpl(); | 354 originalString->~StringImpl(); |
355 StringImpl* string = static_cast<StringImpl*>(partitionReallocGeneric(Partit
ions::getBufferPartition(), originalString.leakRef(), size)); | 355 StringImpl* string = static_cast<StringImpl*>(partitionReallocGeneric(Partit
ions::getBufferPartition(), originalString.leakRef(), size)); |
356 | 356 |
357 data = reinterpret_cast<UChar*>(string + 1); | 357 data = reinterpret_cast<UChar*>(string + 1); |
358 return adoptRef(new (string) StringImpl(length)); | 358 return adoptRef(new (string) StringImpl(length)); |
359 } | 359 } |
360 | 360 |
361 static Vector<StringImpl*>& staticStrings() | 361 static StaticStringsTable& staticStrings() |
362 { | 362 { |
363 DEFINE_STATIC_LOCAL(Vector<StringImpl*>, staticStrings, ()); | 363 DEFINE_STATIC_LOCAL(StaticStringsTable, staticStrings, ()); |
364 return staticStrings; | 364 return staticStrings; |
365 } | 365 } |
366 | 366 |
367 #ifndef NDEBUG | 367 #ifndef NDEBUG |
368 static bool s_allowCreationOfStaticStrings = true; | 368 static bool s_allowCreationOfStaticStrings = true; |
369 #endif | 369 #endif |
370 | 370 |
371 const Vector<StringImpl*>& StringImpl::allStaticStrings() | 371 const StaticStringsTable& StringImpl::allStaticStrings() |
372 { | 372 { |
373 return staticStrings(); | 373 return staticStrings(); |
374 } | 374 } |
375 | 375 |
376 void StringImpl::freezeStaticStrings() | 376 void StringImpl::freezeStaticStrings() |
377 { | 377 { |
378 ASSERT(isMainThread()); | 378 ASSERT(isMainThread()); |
379 | 379 |
380 #ifndef NDEBUG | 380 #ifndef NDEBUG |
381 s_allowCreationOfStaticStrings = false; | 381 s_allowCreationOfStaticStrings = false; |
382 #endif | 382 #endif |
| 383 } |
383 | 384 |
384 staticStrings().shrinkToFit(); | 385 unsigned StringImpl::m_highestStaticStringLength = 0; |
385 } | |
386 | 386 |
387 StringImpl* StringImpl::createStatic(const char* string, unsigned length, unsign
ed hash) | 387 StringImpl* StringImpl::createStatic(const char* string, unsigned length, unsign
ed hash) |
388 { | 388 { |
389 ASSERT(s_allowCreationOfStaticStrings); | 389 ASSERT(s_allowCreationOfStaticStrings); |
390 ASSERT(string); | 390 ASSERT(string); |
391 ASSERT(length); | 391 ASSERT(length); |
392 | 392 |
| 393 StaticStringsTable::const_iterator it = staticStrings().find(hash); |
| 394 if (it != staticStrings().end()) { |
| 395 ASSERT(!memcmp(string, it->value + 1, length * sizeof(LChar))); |
| 396 return it->value; |
| 397 } |
| 398 |
393 // Allocate a single buffer large enough to contain the StringImpl | 399 // Allocate a single buffer large enough to contain the StringImpl |
394 // struct as well as the data which it contains. This removes one | 400 // struct as well as the data which it contains. This removes one |
395 // heap allocation from this call. | 401 // heap allocation from this call. |
396 RELEASE_ASSERT(length <= ((std::numeric_limits<unsigned>::max() - sizeof(Str
ingImpl)) / sizeof(LChar))); | 402 RELEASE_ASSERT(length <= ((std::numeric_limits<unsigned>::max() - sizeof(Str
ingImpl)) / sizeof(LChar))); |
397 size_t size = sizeof(StringImpl) + length * sizeof(LChar); | 403 size_t size = sizeof(StringImpl) + length * sizeof(LChar); |
| 404 |
398 WTF_ANNOTATE_SCOPED_MEMORY_LEAK; | 405 WTF_ANNOTATE_SCOPED_MEMORY_LEAK; |
399 StringImpl* impl = static_cast<StringImpl*>(partitionAllocGeneric(Partitions
::getBufferPartition(), size)); | 406 StringImpl* impl = static_cast<StringImpl*>(partitionAllocGeneric(Partitions
::getBufferPartition(), size)); |
400 | 407 |
401 LChar* data = reinterpret_cast<LChar*>(impl + 1); | 408 LChar* data = reinterpret_cast<LChar*>(impl + 1); |
402 impl = new (impl) StringImpl(length, hash, StaticString); | 409 impl = new (impl) StringImpl(length, hash, StaticString); |
403 memcpy(data, string, length * sizeof(LChar)); | 410 memcpy(data, string, length * sizeof(LChar)); |
404 #ifndef NDEBUG | 411 #ifndef NDEBUG |
405 impl->assertHashIsCorrect(); | 412 impl->assertHashIsCorrect(); |
406 #endif | 413 #endif |
407 | 414 |
408 ASSERT(isMainThread()); | 415 ASSERT(isMainThread()); |
409 staticStrings().append(impl); | 416 m_highestStaticStringLength = std::max(m_highestStaticStringLength, length); |
| 417 staticStrings().add(hash, impl); |
410 WTF_ANNOTATE_BENIGN_RACE(impl, | 418 WTF_ANNOTATE_BENIGN_RACE(impl, |
411 "Benign race on the reference counter of a static string created by Stri
ngImpl::createStatic"); | 419 "Benign race on the reference counter of a static string created by Stri
ngImpl::createStatic"); |
412 | 420 |
413 return impl; | 421 return impl; |
414 } | 422 } |
415 | 423 |
416 PassRefPtr<StringImpl> StringImpl::create(const UChar* characters, unsigned leng
th) | 424 PassRefPtr<StringImpl> StringImpl::create(const UChar* characters, unsigned leng
th) |
417 { | 425 { |
418 if (!characters || !length) | 426 if (!characters || !length) |
419 return empty(); | 427 return empty(); |
(...skipping 1663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2083 | 2091 |
2084 size_t StringImpl::sizeInBytes() const | 2092 size_t StringImpl::sizeInBytes() const |
2085 { | 2093 { |
2086 size_t size = length(); | 2094 size_t size = length(); |
2087 if (!is8Bit()) | 2095 if (!is8Bit()) |
2088 size *= 2; | 2096 size *= 2; |
2089 return size + sizeof(*this); | 2097 return size + sizeof(*this); |
2090 } | 2098 } |
2091 | 2099 |
2092 } // namespace WTF | 2100 } // namespace WTF |
OLD | NEW |