Ignore:
Timestamp:
Apr 5, 2006, 2:19:57 PM (19 years ago)
Author:
darin
Message:

JavaScriptCore:

Reviewed by Maciej.

Change HashMap and HashSet implementation so they fold various types together.
This allows us to implement maps and sets that use RefPtr<WebCore::StringImpl>
and WebCore::String in terms of the underlying raw pointer type, and hence use
-1 for the deleted value.

  • kxmlcore/HashTraits.h: Added a new type to HashTraits, StorageTraits, which is a type to be used when storing a value that has the same layout as the type itself. This is used only for non-key cases. In the case of keys, the hash function must also be considered. Moved emptyValue out of GenericHashTraitsBase into GenericHashTraits. Added a new bool to HashTraits, needsRef, which indicates whether the type needs explicit reference counting. If the type itself has needsRef true, but the storage type has needsRef false, then the HashSet or HashMap has to handle the reference counting explicitly. Added hash trait specializations for all signed integer values that give -1 as the deleted value. Gave all integers StorageTraits of the canonical integer type of the same size so int and long will share code. Gave all pointers and RefPtrs StorageTraits of the appropriately sized integer type. Removed redundant TraitType and emptyValue definitions in the pointer specialization for HashTraits. Added PairBaseHashTraits, which doesn't try to set up needsDestruction and deletedValue. Useful for types where we don't want to force the existence of deletedValue, such as the type of a pair in a HashMap which is not the actual storage type. Removed an unneeded parameter from the DeletedValueAssigner template. Added HashKeyStorageTraits template, which determines what type can be used to store a given hash key type with a given hash function, and specialized it for pointers and RefPtr so that pointer hash tables share an underlying HashTable that uses IntHash.
  • kxmlcore/HashTable.h: Added HashTableConstIteratorAdapter, HashTableIteratorAdapter, NeedsRef, RefCountManagerBase, RefCountManager, HashTableRefCountManagerBase, and HashTableRefCountManager. All are used by both HashSet and HashMap to handle hash tables where the type stored is not the same as the real value type.


  • kxmlcore/HashFunctions.h: Added a new struct named IntTypes that finds an integer type given a sizeof value. Renamed pointerHash to intHash and made it use overloading and take integer parameters. Added an IntHash struct which is a hash function that works for integers. Changed PtrHash to call IntHash with an appropriately sized integer. Made IntHash the default hash function for many integer types. Made PtrHash the default hash function for RefPtr as well as for raw pointers.
  • kxmlcore/HashSet.h: Changed implementation to use a separate "storage type" derived from the new traits. The HashTable will use the storage type and all necessary translation and ref/deref is done at the HashSet level. Also reorganized the file so that the HashSet is at the top and has no inline implementation inside it so it's easy to read the interface to HashSet.
  • kxmlcore/HashMap.h: Changed implementation to use a separate "storage type" derived from the new traits. The HashTable will use the storage type and all necessary translation and ref/deref is done at the HashMap level. Also reorganized the file so that the HashMap is at the top and has no inline implementation inside it so it's easy to read the interface to HashMap.
  • kxmlcore/HashMapPtrSpec.h: Removed. Superceded by optimizations in HashMap itself.
  • JavaScriptCore.xcodeproj/project.pbxproj: Remove HashMapPtrSpec.h, resort files, and also remove some unnecessary build settings from the aggregate target that generates derived sources.
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj: Ditto.

WebCore:

Reviewed by Maciej.

  • platform/StringHash.h: Added. Moved hash functions and such for WebCore::String and friends into this file so we don't have to include the hash traits header everywhere. Changed hashing for WebCore::StringImpl and WebCore::String so that they use a raw pointer for the underlying storage type, taking advantage of the new feature added in JavaScriptCore.
  • platform/AtomicString.h: Moved StrHash specialization to StringHash.h.
  • platform/PlatformString.h: Moved StrHash specialization to StringHash.h.
  • platform/StringImpl.h: Moved StrHash, CaseInsensitiveHash, and HashTraits to StringHash.h. Left DefaultHash behind so that you can't get the wrong hash function by accident if you forget to include "StringHash.h".
  • platform/StringImpl.cpp: Added include of StringHash.h and removed RefPtr<StringImpl> HashTraits<RefPtr<StringImpl> >::_deleted, which is the object with a global initializer causing all the trouble!
  • kwq/AccessibilityObjectCache.h: Changed hash function to be IntHash instead of PtrHash.
  • dom/StyledElement.cpp: Changed MappedAttributeKeyTraits to inherit from the generic traits in KXMLCore so we get a StorageType. Also cleaned up a tiny bit by adding default values to the MappedAttributeKey constructor.
  • platform/CharsetNames.cpp: Changed hash traits here to be a new TextEncodingIDHashTraits struct rather than defining new default traits for the integer type since more integer types have default traits in HashTraits.h now. Also added a specialization so this class will share the underlying implementation (since InvalidEncoding happens to be -1).
  • bridge/mac/FrameMac.h:
  • dom/Document.h:
  • dom/xml_tokenizer.h:
  • khtml/xsl/XSLTProcessor.h:
  • kwq/JavaAppletWidget.h:
  • page/FramePrivate.h:
  • page/Page.cpp:
  • platform/AtomicString.cpp:
  • platform/TransferJob.h:
  • rendering/render_applet.h: Added include of StringHash.h.
  • WebCore.xcodeproj/project.pbxproj: Added StringHash.h. Remove unneeded CREATE_HASH_TABLE variable in build settings. Re-sorted some file lists. Added quotes to the CREATE_HASH_TABLE initialization in the rule that builds generated files. Removed various unneeded build settings for that target as well.
  • ForwardingHeaders/kxmlcore/HashTraits.h: Added.
  • other minor cleanup
  • bridge/mac/FrameMac.mm: Sorted includes.
  • dom/Node.cpp: Removed bogus symbol after #endif.
  • khtml/xsl/XSLTProcessor.cpp: Sorted includes. Removed redundant using namespace WebCore.
  • loader/Cache.cpp: Ditto.

WebKitTools:

Reviewed by Maciej.

  • Scripts/check-for-global-initializers: Remove StringImpl from the list of files that are allowed to have global initializers.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kxmlcore/HashFunctions.h

    r12366 r13703  
    22/*
    33 * This file is part of the KDE libraries
    4  * Copyright (C) 2005 Apple Computer, Inc.
     4 * Copyright (C) 2005, 2006 Apple Computer, Inc.
    55 *
    66 * This library is free software; you can redistribute it and/or
     
    2424#define KXMLCORE_HASH_FUNCTIONS_H
    2525
     26#include "RefPtr.h"
    2627#include <stdint.h>
    27 
    28 #include "RefPtr.h"
    2928
    3029namespace KXMLCore {
    3130
    32     template<typename T> class DefaultHash;
     31    template<size_t size> struct IntTypes;
     32    template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t UnsignedType; };
     33    template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; };
     34    template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; };
     35    template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; };
    3336
    34     template<int size> unsigned pointerHash(void *pointer);
     37    // integer hash function
    3538
    36     // Thomas Wang's 32 bit mix
    37     // http://www.cris.com/~Ttwang/tech/inthash.htm
    38     template<> inline unsigned pointerHash<4>(void *pointer)
     39    // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
     40    inline unsigned intHash(uint32_t key)
    3941    {
    40         uint32_t key = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(pointer));
    4142        key += ~(key << 15);
    4243        key ^= (key >> 10);
     
    4849    }
    4950   
    50     // Thomas Wang's 64 bit mix
    51     // http://www.cris.com/~Ttwang/tech/inthash.htm
    52     template<> inline unsigned pointerHash<8>(void *pointer)
     51    // Thomas Wang's 64 bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
     52    inline unsigned intHash(uint64_t key)
    5353    {
    54         uint64_t key = reinterpret_cast<uint64_t>(pointer);
    5554        key += ~(key << 32);
    5655        key ^= (key >> 22);
     
    6463    }
    6564
    66     // pointer identity hash - default for pointer types that don't
    67     // explicitly specialize otherwise; also should work for integer
    68     // types
    69     template<typename T> struct PtrHash {
    70         static unsigned hash(T key) { return pointerHash<sizeof(void *)>((void *)key); }
     65    template<typename T> struct IntHash {
     66        static unsigned hash(T key) { return intHash(static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key)); }
    7167        static bool equal(T a, T b) { return a == b; }
    7268    };
    7369
     70    // pointer identity hash function
     71
     72    template<typename T> struct PtrHash {
     73        static unsigned hash(T key) { return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key)); }
     74        static bool equal(T a, T b) { return a == b; }
     75    };
    7476    template<typename P> struct PtrHash<RefPtr<P> > {
    75         static unsigned hash(const RefPtr<P>& key) { return  pointerHash<sizeof(void *)>((void *)key.get()); }
     77        static unsigned hash(const RefPtr<P>& key) { return PtrHash<P*>::hash(key.get()); }
    7678        static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; }
    7779    };
    7880
    79     template<typename P> struct DefaultHash<P *> {
    80         typedef PtrHash<P *> Hash;
    81     };
     81    // default hash function for each type
    8282
    83     template<> struct DefaultHash<int> {
    84         typedef PtrHash<int> Hash;
    85     };
    86    
     83    template<typename T> struct DefaultHash;
     84
     85    // make IntHash the default hash function for many integer types
     86
     87    template<> struct DefaultHash<int> { typedef IntHash<unsigned> Hash; };
     88    template<> struct DefaultHash<unsigned> { typedef IntHash<unsigned> Hash; };
     89    template<> struct DefaultHash<long> { typedef IntHash<unsigned long> Hash; };
     90    template<> struct DefaultHash<unsigned long> { typedef IntHash<unsigned long> Hash; };
     91    template<> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; };
     92    template<> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned long long> Hash; };
     93
     94    // make PtrHash the default hash function for pointer types that don't specialize
     95
     96    template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; };
     97    template<typename P> struct DefaultHash<RefPtr<P> > { typedef PtrHash<RefPtr<P> > Hash; };
     98
    8799} // namespace KXMLCore
    88100
    89101using KXMLCore::DefaultHash;
     102using KXMLCore::IntHash;
    90103using KXMLCore::PtrHash;
    91104
Note: See TracChangeset for help on using the changeset viewer.