Changeset 24059 in webkit for trunk/JavaScriptCore/wtf/HashMap.h


Ignore:
Timestamp:
Jul 6, 2007, 5:09:08 AM (18 years ago)
Author:
mjs
Message:

JavaScriptCore:

Reviewed by Antti.

  • <rdar://problem/5311093> JavaScriptCore fails to build with strict-aliasing warnings


  • Configurations/Base.xcconfig: Re-enable -Wstrict-aliasing
  • bindings/jni/jni_utility.cpp: (KJS::Bindings::getJNIEnv): Type-pun via a union instead of a pointer cast.
  • wtf/HashMap.h: (WTF::): Instead of doing type-punned assignments via pointer cast, do one of three things: (1) assign directly w/o cast if storage type matches real type; (2) assign using cast via union if type does not need reffing; (3) copy with memcpy and ref/deref manually if type needs reffing. This is ok peref-wise because memcpy of a constant length gets optomized. HashTraits are now expected to make ref()/deref() take the storage type, not the true type.
  • wtf/HashSet.h: (WTF::): Same basic idea.
  • wtf/HashTable.h: (WTF::): Added Assigner template for use by HashMap/HashSet. Change RefCounter to call ref() and deref() via storage type, avoiding the need to type-pun. (WTF::RefCounter::ref): ditto (WTF::RefCounter::deref): ditto
  • wtf/HashTraits.h: (WTF::): Change ref() and deref() for RefPtr HashTraits to take the storage type; cast via union to pointer type.
  • wtf/FastMalloc.cpp: (WTF::TCMalloc_PageHeap::init): Changed from constructor to init function so this can go in a union. (WTF::): redefine pageheap macro in terms of getPageHeap(). (WTF::getPageHeap): new inline function, helper for pageheap macro. This hides the cast in a union. (WTF::TCMalloc_ThreadCache::InitModule): Call init() instead of using placement new to initialize page heap.
  • wtf/TCPageMap.h: (TCMalloc_PageMap1::init): Changed from constructor to init function. (TCMalloc_PageMap2::init): ditto (TCMalloc_PageMap3::init): ditto

WebCore:

Reviewed by Antti.

  • <rdar://problem/5311093> JavaScriptCore fails to build with strict-aliasing warnings
  • platform/StringHash.h: (WTF::): Adapt to newer way to do storage types.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/HashMap.h

    r21468 r24059  
    112112    };
    113113
    114     template<bool canReplaceDeletedKey, typename ValueType, typename ValueStorageTraits, typename HashFunctions>
     114    template<bool canReplaceDeletedKey, typename ValueType, typename ValueTraits, typename ValueStorageTraits, typename HashFunctions>
    115115    struct HashMapTranslator;
    116116
    117     template<typename ValueType, typename ValueStorageTraits, typename HashFunctions>
    118     struct HashMapTranslator<true, ValueType, ValueStorageTraits, HashFunctions> {
     117    template<typename ValueType, typename ValueTraits, typename ValueStorageTraits, typename HashFunctions>
     118    struct HashMapTranslator<true, ValueType, ValueTraits, ValueStorageTraits, HashFunctions> {
    119119        typedef typename ValueType::first_type KeyType;
    120120        typedef typename ValueType::second_type MappedType;
     
    122122        typedef typename ValueStorageTraits::FirstTraits KeyStorageTraits;
    123123        typedef typename KeyStorageTraits::TraitType KeyStorageType;
     124        typedef typename ValueStorageTraits::SecondTraits MappedStorageTraits;
     125        typedef typename MappedStorageTraits::TraitType MappedStorageType;
     126        typedef typename ValueTraits::FirstTraits KeyTraits;
     127        typedef typename ValueTraits::SecondTraits MappedTraits;
    124128
    125129        static unsigned hash(const KeyType& key) { return HashFunctions::hash(key); }
     
    127131        static void translate(ValueStorageType& location, const KeyType& key, const MappedType& mapped, unsigned)
    128132        {
    129             *(KeyType*)&location.first = key;
    130             *(MappedType*)&location.second = mapped;
     133            Assigner<KeyTraits::needsRef, KeyType, KeyStorageType, KeyTraits>::assign(key, location.first);
     134            Assigner<MappedTraits::needsRef, MappedType, MappedStorageType, MappedTraits>::assign(mapped, location.second);
    131135        }
    132136    };
    133137
    134     template<typename ValueType, typename ValueStorageTraits, typename HashFunctions>
    135     struct HashMapTranslator<false, ValueType, ValueStorageTraits, HashFunctions> {
     138    template<typename ValueType, typename ValueTraits, typename ValueStorageTraits, typename HashFunctions>
     139    struct HashMapTranslator<false, ValueType, ValueTraits, ValueStorageTraits, HashFunctions> {
    136140        typedef typename ValueType::first_type KeyType;
    137141        typedef typename ValueType::second_type MappedType;
     
    139143        typedef typename ValueStorageTraits::FirstTraits KeyStorageTraits;
    140144        typedef typename KeyStorageTraits::TraitType KeyStorageType;
    141 
     145        typedef typename ValueStorageTraits::SecondTraits MappedStorageTraits;
     146        typedef typename MappedStorageTraits::TraitType MappedStorageType;
     147        typedef typename ValueTraits::FirstTraits KeyTraits;
     148        typedef typename ValueTraits::SecondTraits MappedTraits;
     149       
    142150        static unsigned hash(const KeyType& key) { return HashFunctions::hash(key); }
    143151        static bool equal(const KeyStorageType& a, const KeyType& b) { return HashFunctions::equal(*(KeyType*)&a, b); }
     
    146154            if (location.first == KeyStorageTraits::deletedValue())
    147155                location.first = KeyStorageTraits::emptyValue();
    148             *(KeyType*)&location.first = key;
    149             *(MappedType*)&location.second = mapped;
     156            Assigner<KeyTraits::needsRef, KeyType, KeyStorageType, KeyTraits>::assign(key, location.first);
     157            Assigner<MappedTraits::needsRef, MappedType, MappedStorageType, MappedTraits>::assign(mapped, location.second);
    150158        }
    151159    };
     
    260268    {
    261269        const bool canReplaceDeletedKey = !KeyTraits::needsDestruction || KeyStorageTraits::needsDestruction;
    262         typedef HashMapTranslator<canReplaceDeletedKey, ValueType, ValueStorageTraits, HashFunctions> TranslatorType;
     270        typedef HashMapTranslator<canReplaceDeletedKey, ValueType, ValueTraits, ValueStorageTraits, HashFunctions> TranslatorType;
    263271        return m_impl.template add<KeyType, MappedType, TranslatorType>(key, mapped);
    264272    }
Note: See TracChangeset for help on using the changeset viewer.