Changeset 39747 in webkit for trunk/JavaScriptCore/runtime


Ignore:
Timestamp:
Jan 9, 2009, 9:53:36 AM (16 years ago)
Author:
[email protected]
Message:

2009-01-09 David Levin <[email protected]>

Reviewed by Oliver Hunt.

https://bugs.webkit.org/show_bug.cgi?id=23175

Added a template to make the pointer and flags combination
in UString more readable and less error prone.

Location:
trunk/JavaScriptCore/runtime
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/Identifier.cpp

    r39350 r39747  
    125125{
    126126    if (!c) {
    127         UString::Rep::null.hash();
    128         return &UString::Rep::null;
     127        UString::Rep::null().hash();
     128        return &UString::Rep::null();
    129129    }
    130130    if (!c[0]) {
    131         UString::Rep::empty.hash();
    132         return &UString::Rep::empty;
     131        UString::Rep::empty().hash();
     132        return &UString::Rep::empty();
    133133    }
    134134    if (!c[1])
     
    195195    }
    196196    if (!length) {
    197         UString::Rep::empty.hash();
    198         return &UString::Rep::empty;
     197        UString::Rep::empty().hash();
     198        return &UString::Rep::empty();
    199199    }
    200200    UCharBuffer buf = {s, length};
     
    226226    }
    227227    if (!r->len) {
    228         UString::Rep::empty.hash();
    229         return &UString::Rep::empty;
     228        UString::Rep::empty().hash();
     229        return &UString::Rep::empty();
    230230    }
    231231    return *globalData->identifierTable->add(r).first;
  • trunk/JavaScriptCore/runtime/InitializeThreading.cpp

    r39670 r39747  
    5050{
    5151    WTF::initializeThreading();
     52    initializeUString();
    5253#if ENABLE(JSC_MULTIPLE_THREADS)
    5354    s_dtoaP5Mutex = new Mutex;
    54     UString::null();
    5555    initDateMath();
    5656#endif
  • trunk/JavaScriptCore/runtime/JSGlobalData.cpp

    r38622 r39747  
    3131
    3232#include "ArgList.h"
     33#include "Collector.h"
    3334#include "CommonIdentifiers.h"
     35#include "InitializeThreading.h"
     36#include "Interpreter.h"
    3437#include "JSActivation.h"
    3538#include "JSClassRef.h"
     
    3740#include "JSNotAnObject.h"
    3841#include "JSStaticScopeObject.h"
    39 #include "Interpreter.h"
    4042#include "Parser.h"
    41 #include "Collector.h"
    4243#include "Lexer.h"
    4344#include "Lookup.h"
     
    143144PassRefPtr<JSGlobalData> JSGlobalData::create()
    144145{
     146    initializeThreading();
    145147    return adoptRef(new JSGlobalData);
    146148}
  • trunk/JavaScriptCore/runtime/PropertyNameArray.cpp

    r38087 r39747  
    2828void PropertyNameArray::add(UString::Rep* identifier)
    2929{
    30     ASSERT(identifier == &UString::Rep::null || identifier == &UString::Rep::empty || identifier->identifierTable());
     30    ASSERT(identifier == &UString::Rep::null() || identifier == &UString::Rep::empty() || identifier->identifierTable());
    3131
    3232    size_t size = m_data->propertyNameVector().size();
  • trunk/JavaScriptCore/runtime/UString.cpp

    r38390 r39747  
    33 *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
    44 *  Copyright (C) 2007 Cameron Zwarich ([email protected])
     5 *  Copyright (c) 2009, Google Inc. All rights reserved.
    56 *
    67 *  This library is free software; you can redistribute it and/or
     
    187188// reduce the possibility of it becoming zero due to ref/deref not being thread-safe.
    188189static UChar sharedEmptyChar;
    189 UString::Rep UString::Rep::null = { 0, 0, INT_MAX / 2, 0, 1, &UString::Rep::null, 0, 0, 0, 0, 0, 0 };
    190 UString::Rep UString::Rep::empty = { 0, 0, INT_MAX / 2, 0, 1, &UString::Rep::empty, 0, &sharedEmptyChar, 0, 0, 0, 0 };
     190UString::Rep* UString::Rep::nullBaseString;
     191UString::Rep* UString::Rep::emptyBaseString;
     192UString* UString::nullUString;
     193
     194void initializeStaticBaseString(int len, UChar* buf, UString::Rep& base)
     195{
     196    base.offset = 0;
     197    base.len = len;
     198    base.rc = INT_MAX / 2;
     199    base._hash = 0;
     200    base.m_identifierTableAndFlags.setFlag(UString::Rep::StaticFlag);
     201    base.baseString = &base;
     202    base.buf = buf;
     203    base.preCapacity = 0;
     204    base.usedPreCapacity = 0;
     205    base.capacity = 0;
     206    base.usedCapacity = 0;
     207    base.reportedCost = 0;
     208}
     209
     210void initializeUString()
     211{
     212    UString::Rep::nullBaseString = new UString::Rep;
     213    initializeStaticBaseString(0, 0, *UString::Rep::nullBaseString);
     214
     215    UString::Rep::emptyBaseString = new UString::Rep;
     216    initializeStaticBaseString(0, &sharedEmptyChar, *UString::Rep::emptyBaseString);
     217
     218    UString::nullUString = new UString;
     219}
    191220
    192221static char* statBuffer = 0; // Only used for debugging via UString::ascii().
     
    206235    r->rc = 1;
    207236    r->_hash = 0;
    208     r->m_identifierTable = 0;
    209237    r->baseString = r;
    210238    r->reportedCost = 0;
     
    238266    r->rc = 1;
    239267    r->_hash = 0;
    240     r->m_identifierTable = 0;
    241268    r->baseString = base.releaseRef();
    242269    r->reportedCost = 0;
     
    256283{
    257284    if (!string)
    258         return &UString::Rep::null;
     285        return &UString::Rep::null();
    259286
    260287    size_t length = strlen(string);
     
    262289    UChar* p = buffer.data();
    263290    if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
    264         return &UString::Rep::null;
     291        return &UString::Rep::null();
    265292
    266293    return UString::Rep::createCopying(buffer.data(), p - buffer.data());
     
    495522{
    496523    if (!c)
    497         return &UString::Rep::null;
     524        return &UString::Rep::null();
    498525
    499526    if (!c[0])
    500         return &UString::Rep::empty;
     527        return &UString::Rep::empty();
    501528
    502529    size_t length = strlen(c);
    503530    UChar* d = allocChars(length);
    504531    if (!d)
    505         return &UString::Rep::null;
     532        return &UString::Rep::null();
    506533    else {
    507534        for (size_t i = 0; i < length; i++)
     
    520547{
    521548    if (length == 0)
    522         m_rep = &Rep::empty;
     549        m_rep = &Rep::empty();
    523550    else
    524551        m_rep = Rep::createCopying(c, length);
     
    528555{
    529556    if (length == 0)
    530         m_rep = &Rep::empty;
     557        m_rep = &Rep::empty();
    531558    else if (copy)
    532559        m_rep = Rep::createCopying(c, length);
     
    538565{
    539566    if (!buffer.size())
    540         m_rep = &Rep::empty;
     567        m_rep = &Rep::empty();
    541568    else
    542569        m_rep = Rep::createCopying(buffer.data(), buffer.size());
     
    562589        // this is direct and has refcount of 1 (so we can just alter it directly)
    563590        if (!expandCapacity(rep.get(), thisOffset + length))
    564             rep = &UString::Rep::null;
     591            rep = &UString::Rep::null();
    565592        if (rep->data()) {
    566593            copyChars(rep->data() + thisSize, tData, tSize);
     
    571598        // this reaches the end of the buffer - extend it if it's long enough to append to
    572599        if (!expandCapacity(rep.get(), thisOffset + length))
    573             rep = &UString::Rep::null;
     600            rep = &UString::Rep::null();
    574601        if (rep->data()) {
    575602            copyChars(rep->data() + thisSize, tData, tSize);
     
    581608        UChar* d = allocChars(newCapacity);
    582609        if (!d)
    583             rep = &UString::Rep::null;
     610            rep = &UString::Rep::null();
    584611        else {
    585612            copyChars(d, rep->data(), thisSize);
     
    636663        UChar* d = allocChars(newCapacity);
    637664        if (!d)
    638             rep = &UString::Rep::null;
     665            rep = &UString::Rep::null();
    639666        else {
    640667            copyChars(d, rep->data(), thisSize);
     
    832859}
    833860
    834 const UString& UString::null()
    835 {
    836     static UString* n = new UString; // Should be called from main thread at least once to be safely initialized.
    837     return *n;
    838 }
    839 
    840861UString UString::from(int i)
    841862{
     
    11861207{
    11871208    if (!c) {
    1188         m_rep = &Rep::null;
     1209        m_rep = &Rep::null();
    11891210        return *this;
    11901211    }
    11911212
    11921213    if (!c[0]) {
    1193         m_rep = &Rep::empty;
     1214        m_rep = &Rep::empty();
    11941215        return *this;
    11951216    }
     
    16271648NEVER_INLINE void UString::makeNull()
    16281649{
    1629     m_rep = &Rep::null;
     1650    m_rep = &Rep::null();
    16301651}
    16311652
     
    16331654NEVER_INLINE UString::Rep* UString::nullRep()
    16341655{
    1635     return &Rep::null;
     1656    return &Rep::null();
    16361657}
    16371658
  • trunk/JavaScriptCore/runtime/UString.h

    r39554 r39747  
    22 *  Copyright (C) 1999-2000 Harri Porten ([email protected])
    33 *  Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
     4 *  Copyright (c) 2009, Google Inc. All rights reserved.
    45 *
    56 *  This library is free software; you can redistribute it and/or
     
    2930#include <wtf/FastMalloc.h>
    3031#include <wtf/PassRefPtr.h>
     32#include <wtf/PtrAndFlags.h>
    3133#include <wtf/RefPtr.h>
    3234#include <wtf/Vector.h>
     
    99101            static unsigned computeHash(const char* s) { return computeHash(s, strlen(s)); }
    100102
    101             IdentifierTable* identifierTable() const { return reinterpret_cast<IdentifierTable*>(m_identifierTable & ~static_cast<uintptr_t>(1)); }
    102             void setIdentifierTable(IdentifierTable* table) { ASSERT(!isStatic()); m_identifierTable = reinterpret_cast<intptr_t>(table); }
    103 
    104             bool isStatic() const { return m_identifierTable & 1; }
    105             void setStatic(bool v) { ASSERT(!identifierTable()); m_identifierTable = v; }
     103            IdentifierTable* identifierTable() const { return m_identifierTableAndFlags.get(); }
     104            void setIdentifierTable(IdentifierTable* table) { ASSERT(!isStatic()); m_identifierTableAndFlags.set(table); }
     105
     106            bool isStatic() const { return m_identifierTableAndFlags.isFlagSet(StaticFlag); }
     107            void setStatic(bool v) { ASSERT(!identifierTable()); if (v) m_identifierTableAndFlags.setFlag(StaticFlag); else m_identifierTableAndFlags.clearFlag(StaticFlag); }
    106108
    107109            Rep* ref() { ++rc; return this; }
     
    109111
    110112            void checkConsistency() const;
     113            enum UStringFlags {
     114                StaticFlag
     115            };
    111116
    112117            // unshared data
     
    115120            int rc; // For null and empty static strings, this field does not reflect a correct count, because ref/deref are not thread-safe. A special case in destroy() guarantees that these do not get deleted.
    116121            mutable unsigned _hash;
    117             intptr_t m_identifierTable; // A pointer to identifier table. The lowest bit is used to indicate whether the string is static (null or empty).
     122            PtrAndFlags<IdentifierTable, UStringFlags> m_identifierTableAndFlags;
    118123            UString::Rep* baseString;
    119124            size_t reportedCost;
     
    126131            int preCapacity;
    127132
    128             static Rep null;
    129             static Rep empty;
     133            static Rep& null() { return *nullBaseString; }
     134            static Rep& empty() { return *emptyBaseString; }
     135        private:
     136            friend void initializeUString();
     137            static Rep* nullBaseString;
     138            static Rep* emptyBaseString;
    130139        };
    131140
     
    205214        const UChar* data() const { return m_rep->data(); }
    206215
    207         bool isNull() const { return (m_rep == &Rep::null); }
     216        bool isNull() const { return (m_rep == &Rep::null()); }
    208217        bool isEmpty() const { return (!m_rep->len); }
    209218
     
    231240        UString substr(int pos = 0, int len = -1) const;
    232241
    233         static const UString& null();
     242        static const UString& null() { return *nullUString; }
    234243
    235244        Rep* rep() const { return m_rep.get(); }
     
    252261
    253262        RefPtr<Rep> m_rep;
    254 
     263        static UString* nullUString;
     264
     265        friend void initializeUString();
    255266        friend bool operator==(const UString&, const UString&);
    256267        friend PassRefPtr<Rep> concatenate(Rep*, Rep*); // returns 0 if out of memory
     
    306317
    307318    inline UString::UString()
    308         : m_rep(&Rep::null)
     319        : m_rep(&Rep::null())
    309320    {
    310321    }
     
    348359    };
    349360
     361    void initializeUString();
    350362} // namespace JSC
    351363
Note: See TracChangeset for help on using the changeset viewer.