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/HashTable.h

    r19302 r24059  
    851851    };
    852852
    853     template<bool needsRef, typename ValueTraits> struct RefCounterBase;
    854 
    855     template<typename ValueTraits>
    856     struct RefCounterBase<false, ValueTraits> {
    857         typedef typename ValueTraits::TraitType ValueType;
    858         static void ref(const ValueType&) { }
    859         static void deref(const ValueType&) { }
    860     };
    861 
    862     template<typename ValueTraits>
    863     struct RefCounterBase<true, ValueTraits> {
    864         typedef typename ValueTraits::TraitType ValueType;
    865         static void ref(const ValueType& v) { ValueTraits::ref(*(const ValueType*)&v); }
    866         static void deref(const ValueType& v) { ValueTraits::deref(*(const ValueType*)&v); }
     853    template<bool needsRef, typename ValueTraits, typename ValueStorageTraits> struct RefCounterBase;
     854
     855    template<typename ValueTraits, typename ValueStorageTraits>
     856    struct RefCounterBase<false, ValueTraits, ValueStorageTraits> {
     857        typedef typename ValueStorageTraits::TraitType ValueStorageType;
     858        static void ref(const ValueStorageType&) { }
     859        static void deref(const ValueStorageType&) { }
     860    };
     861
     862    template<typename ValueTraits, typename ValueStorageTraits>
     863    struct RefCounterBase<true, ValueTraits, ValueStorageTraits> {
     864        typedef typename ValueStorageTraits::TraitType ValueStorageType;
     865        static void ref(const ValueStorageType& v) { ValueTraits::ref(v); }
     866        static void deref(const ValueStorageType& v) { ValueTraits::deref(v); }
    867867    };
    868868
     
    871871        typedef typename ValueStorageTraits::TraitType ValueStorageType;
    872872        static const bool needsRef = NeedsRef<ValueTraits, ValueStorageTraits>::value;
    873         typedef RefCounterBase<needsRef, ValueTraits> Base;
    874         static void ref(const ValueStorageType& v) { Base::ref(*(const ValueType*)&v); }
    875         static void deref(const ValueStorageType& v) { Base::deref(*(const ValueType*)&v); }
     873        typedef RefCounterBase<needsRef, ValueTraits, ValueStorageTraits> Base;
     874        static void ref(const ValueStorageType& v) { Base::ref(v); }
     875        static void deref(const ValueStorageType& v) { Base::deref(v); }
    876876    };
    877877
     
    885885        static const bool firstNeedsRef = NeedsRef<FirstTraits, FirstStorageTraits>::value;
    886886        static const bool secondNeedsRef = NeedsRef<SecondTraits, SecondStorageTraits>::value;
    887         typedef RefCounterBase<firstNeedsRef, FirstTraits> FirstBase;
    888         typedef RefCounterBase<secondNeedsRef, SecondTraits> SecondBase;
     887        typedef RefCounterBase<firstNeedsRef, FirstTraits, FirstStorageTraits> FirstBase;
     888        typedef RefCounterBase<secondNeedsRef, SecondTraits, SecondStorageTraits> SecondBase;
    889889        static void ref(const ValueStorageType& v) {
    890             FirstBase::ref(*(const FirstType*)&v.first);
    891             SecondBase::ref(*(const SecondType*)&v.second);
     890            FirstBase::ref(v.first);
     891            SecondBase::ref(v.second);
    892892        }
    893893        static void deref(const ValueStorageType& v) {
    894             FirstBase::deref(*(const FirstType*)&v.first);
    895             SecondBase::deref(*(const SecondType*)&v.second);
     894            FirstBase::deref(v.first);
     895            SecondBase::deref(v.second);
    896896        }
    897897    };
     
    938938    };
    939939
     940    // helper template for HashMap and HashSet.
     941    template<bool needsRef, typename FromType, typename ToType, typename FromTraits> struct Assigner;
     942   
     943    template<typename FromType, typename ToType, typename FromTraits> struct Assigner<false, FromType, ToType, FromTraits> {
     944        typedef union {
     945            FromType m_from;
     946            ToType m_to;
     947        } UnionType;
     948
     949        static void assign(const FromType& from, ToType& to) { reinterpret_cast<UnionType*>(&to)->m_from = from; }
     950    };
     951   
     952    template<typename FromType, typename ToType, typename FromTraits> struct Assigner<true, FromType, ToType, FromTraits> {
     953        static void assign(const FromType& from, ToType& to)
     954        {
     955            ToType oldTo = to;
     956            memcpy(&to, &from, sizeof(FromType));
     957            FromTraits::ref(to);
     958            FromTraits::deref(oldTo);
     959        }
     960    };
     961   
     962    template<typename FromType, typename FromTraits> struct Assigner<false, FromType, FromType, FromTraits> {
     963        static void assign(const FromType& from, FromType& to) { to = from; }
     964    };   
     965   
     966    template<typename FromType, typename FromTraits> struct Assigner<true, FromType, FromType, FromTraits> {
     967        static void assign(const FromType& from, FromType& to) { to = from; }
     968    };   
     969
    940970} // namespace WTF
    941971
Note: See TracChangeset for help on using the changeset viewer.