Index: src/base/hashmap.h |
diff --git a/src/hashmap.h b/src/base/hashmap.h |
similarity index 84% |
rename from src/hashmap.h |
rename to src/base/hashmap.h |
index f94def7c3c7a04cb4c0e534074c4f94b092a0f81..470bf616fdba962cc65b9abc66099107e0a543f8 100644 |
--- a/src/hashmap.h |
+++ b/src/base/hashmap.h |
@@ -2,21 +2,29 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#ifndef V8_HASHMAP_H_ |
-#define V8_HASHMAP_H_ |
+// The reason we write our own hash map instead of using unordered_map in STL, |
+// is that STL containers use a mutex pool on debug build, which will lead to |
+// deadlock when we are using async signal handler. |
+ |
+#ifndef V8_BASE_HASHMAP_H_ |
+#define V8_BASE_HASHMAP_H_ |
-#include "src/allocation.h" |
#include "src/base/bits.h" |
#include "src/base/logging.h" |
-#include "src/utils.h" |
namespace v8 { |
-namespace internal { |
+namespace base { |
+ |
+class DefaultAllocationPolicy { |
+ public: |
+ V8_INLINE void* New(size_t size) { return malloc(size); } |
+ V8_INLINE static void Delete(void* p) { free(p); } |
+}; |
-template<class AllocationPolicy> |
+template <class AllocationPolicy> |
class TemplateHashMapImpl { |
public: |
- typedef bool (*MatchFun) (void* key1, void* key2); |
+ typedef bool (*MatchFun)(void* key1, void* key2); |
// The default capacity. This is used by the call sites which want |
// to pass in a non-default AllocationPolicy but want to use the |
@@ -38,7 +46,7 @@ class TemplateHashMapImpl { |
void* key; |
void* value; |
uint32_t hash; // The full hash value for key |
- int order; // If you never remove entries this is the insertion order. |
+ int order; // If you never remove entries this is the insertion order. |
}; |
// If an entry with matching key is found, returns that entry. |
@@ -79,9 +87,7 @@ class TemplateHashMapImpl { |
Entry* Next(Entry* p) const; |
// Some match functions defined for convenience. |
- static bool PointersMatch(void* key1, void* key2) { |
- return key1 == key2; |
- } |
+ static bool PointersMatch(void* key1, void* key2) { return key1 == key2; } |
private: |
MatchFun match_; |
@@ -95,22 +101,20 @@ class TemplateHashMapImpl { |
void Resize(AllocationPolicy allocator); |
}; |
-typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap; |
+typedef TemplateHashMapImpl<DefaultAllocationPolicy> HashMap; |
-template<class AllocationPolicy> |
+template <class AllocationPolicy> |
TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl( |
MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) { |
match_ = match; |
Initialize(initial_capacity, allocator); |
} |
- |
-template<class AllocationPolicy> |
+template <class AllocationPolicy> |
TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() { |
AllocationPolicy::Delete(map_); |
} |
- |
template <class AllocationPolicy> |
typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
TemplateHashMapImpl<AllocationPolicy>::Lookup(void* key, uint32_t hash) const { |
@@ -118,7 +122,6 @@ TemplateHashMapImpl<AllocationPolicy>::Lookup(void* key, uint32_t hash) const { |
return p->key != NULL ? p : NULL; |
} |
- |
template <class AllocationPolicy> |
typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert( |
@@ -145,8 +148,7 @@ TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert( |
return p; |
} |
- |
-template<class AllocationPolicy> |
+template <class AllocationPolicy> |
void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) { |
// Lookup the entry for the key to remove. |
Entry* p = Probe(key, hash); |
@@ -194,8 +196,7 @@ void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) { |
// If the entry at position q has its initial position outside the range |
// between p and q it can be moved forward to position p and will still be |
// found. There is now a new candidate entry for clearing. |
- if ((q > p && (r <= p || r > q)) || |
- (q < p && (r <= p && r > q))) { |
+ if ((q > p && (r <= p || r > q)) || (q < p && (r <= p && r > q))) { |
*p = *q; |
p = q; |
} |
@@ -207,8 +208,7 @@ void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) { |
return value; |
} |
- |
-template<class AllocationPolicy> |
+template <class AllocationPolicy> |
void TemplateHashMapImpl<AllocationPolicy>::Clear() { |
// Mark all entries as empty. |
const Entry* end = map_end(); |
@@ -218,17 +218,15 @@ void TemplateHashMapImpl<AllocationPolicy>::Clear() { |
occupancy_ = 0; |
} |
- |
-template<class AllocationPolicy> |
+template <class AllocationPolicy> |
typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
- TemplateHashMapImpl<AllocationPolicy>::Start() const { |
+TemplateHashMapImpl<AllocationPolicy>::Start() const { |
return Next(map_ - 1); |
} |
- |
-template<class AllocationPolicy> |
+template <class AllocationPolicy> |
typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
- TemplateHashMapImpl<AllocationPolicy>::Next(Entry* p) const { |
+TemplateHashMapImpl<AllocationPolicy>::Next(Entry* p) const { |
const Entry* end = map_end(); |
DCHECK(map_ - 1 <= p && p < end); |
for (p++; p < end; p++) { |
@@ -239,7 +237,6 @@ typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
return NULL; |
} |
- |
template <class AllocationPolicy> |
typename TemplateHashMapImpl<AllocationPolicy>::Entry* |
TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) const { |
@@ -261,22 +258,20 @@ TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) const { |
return p; |
} |
- |
-template<class AllocationPolicy> |
+template <class AllocationPolicy> |
void TemplateHashMapImpl<AllocationPolicy>::Initialize( |
uint32_t capacity, AllocationPolicy allocator) { |
DCHECK(base::bits::IsPowerOfTwo32(capacity)); |
map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry))); |
if (map_ == NULL) { |
- v8::internal::FatalProcessOutOfMemory("HashMap::Initialize"); |
+ FATAL("Out of memory: HashMap::Initialize"); |
return; |
} |
capacity_ = capacity; |
Clear(); |
} |
- |
-template<class AllocationPolicy> |
+template <class AllocationPolicy> |
void TemplateHashMapImpl<AllocationPolicy>::Resize(AllocationPolicy allocator) { |
Entry* map = map_; |
uint32_t n = occupancy_; |
@@ -298,12 +293,11 @@ void TemplateHashMapImpl<AllocationPolicy>::Resize(AllocationPolicy allocator) { |
AllocationPolicy::Delete(map); |
} |
- |
// A hash map for pointer keys and values with an STL-like interface. |
-template<class Key, class Value, class AllocationPolicy> |
-class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> { |
+template <class Key, class Value, class AllocationPolicy> |
+class TemplateHashMap : private TemplateHashMapImpl<AllocationPolicy> { |
public: |
- STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT |
+ STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT |
STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT |
struct value_type { |
Key* first; |
@@ -318,12 +312,12 @@ class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> { |
} |
value_type* operator->() { return reinterpret_cast<value_type*>(entry_); } |
- bool operator!=(const Iterator& other) { return entry_ != other.entry_; } |
+ bool operator!=(const Iterator& other) { return entry_ != other.entry_; } |
private: |
Iterator(const TemplateHashMapImpl<AllocationPolicy>* map, |
- typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry) : |
- map_(map), entry_(entry) { } |
+ typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry) |
+ : map_(map), entry_(entry) {} |
const TemplateHashMapImpl<AllocationPolicy>* map_; |
typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry_; |
@@ -334,10 +328,10 @@ class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> { |
TemplateHashMap( |
typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match, |
AllocationPolicy allocator = AllocationPolicy()) |
- : TemplateHashMapImpl<AllocationPolicy>( |
+ : TemplateHashMapImpl<AllocationPolicy>( |
match, |
TemplateHashMapImpl<AllocationPolicy>::kDefaultHashMapCapacity, |
- allocator) { } |
+ allocator) {} |
Iterator begin() const { return Iterator(this, this->Start()); } |
Iterator end() const { return Iterator(this, NULL); } |
@@ -350,7 +344,7 @@ class TemplateHashMap: private TemplateHashMapImpl<AllocationPolicy> { |
} |
}; |
-} // namespace internal |
+} // namespace base |
} // namespace v8 |
-#endif // V8_HASHMAP_H_ |
+#endif // V8_BASE_HASHMAP_H_ |