Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1062)

Unified Diff: src/libsampler/hashmap.h

Issue 1922303002: Create libsampler as V8 sampler library. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix TSAN failure Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/libsampler/DEPS ('k') | src/libsampler/utils.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/libsampler/hashmap.h
diff --git a/src/hashmap.h b/src/libsampler/hashmap.h
similarity index 56%
copy from src/hashmap.h
copy to src/libsampler/hashmap.h
index f94def7c3c7a04cb4c0e534074c4f94b092a0f81..e4b3cc6009b977756f173d01fca426386358b499 100644
--- a/src/hashmap.h
+++ b/src/libsampler/hashmap.h
@@ -2,34 +2,31 @@
// 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_
+// This file is ported from src/hashmap.h
+
+#ifndef V8_LIBSAMPLER_HASHMAP_H_
+#define V8_LIBSAMPLER_HASHMAP_H_
-#include "src/allocation.h"
#include "src/base/bits.h"
#include "src/base/logging.h"
-#include "src/utils.h"
+#include "src/libsampler/utils.h"
namespace v8 {
-namespace internal {
+namespace sampler {
-template<class AllocationPolicy>
-class TemplateHashMapImpl {
+class HashMapImpl {
public:
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
- // default value of capacity specified by the implementation.
+ // The default capacity.
static const uint32_t kDefaultHashMapCapacity = 8;
// initial_capacity is the size of the initial hash map;
// it must be a power of 2 (and thus must not be 0).
- TemplateHashMapImpl(MatchFun match,
- uint32_t capacity = kDefaultHashMapCapacity,
- AllocationPolicy allocator = AllocationPolicy());
+ HashMapImpl(MatchFun match,
+ uint32_t capacity = kDefaultHashMapCapacity);
- ~TemplateHashMapImpl();
+ ~HashMapImpl();
// HashMap entries are (key, value, hash) triplets.
// Some clients may not need to use the value slot
@@ -48,8 +45,7 @@ class TemplateHashMapImpl {
// If an entry with matching key is found, returns that entry.
// If no matching entry is found, a new entry is inserted with
// corresponding key, key hash, and NULL value.
- Entry* LookupOrInsert(void* key, uint32_t hash,
- AllocationPolicy allocator = AllocationPolicy());
+ Entry* LookupOrInsert(void* key, uint32_t hash);
// Removes the entry with matching key.
// It returns the value of the deleted entry
@@ -91,38 +87,30 @@ class TemplateHashMapImpl {
Entry* map_end() const { return map_ + capacity_; }
Entry* Probe(void* key, uint32_t hash) const;
- void Initialize(uint32_t capacity, AllocationPolicy allocator);
- void Resize(AllocationPolicy allocator);
+ void Initialize(uint32_t capacity);
+ void Resize();
};
-typedef TemplateHashMapImpl<FreeStoreAllocationPolicy> HashMap;
+typedef HashMapImpl HashMap;
-template<class AllocationPolicy>
-TemplateHashMapImpl<AllocationPolicy>::TemplateHashMapImpl(
- MatchFun match, uint32_t initial_capacity, AllocationPolicy allocator) {
+HashMapImpl::HashMapImpl(MatchFun match, uint32_t initial_capacity) {
match_ = match;
- Initialize(initial_capacity, allocator);
+ Initialize(initial_capacity);
}
-template<class AllocationPolicy>
-TemplateHashMapImpl<AllocationPolicy>::~TemplateHashMapImpl() {
- AllocationPolicy::Delete(map_);
+HashMapImpl::~HashMapImpl() {
+ Malloced::Delete(map_);
}
-template <class AllocationPolicy>
-typename TemplateHashMapImpl<AllocationPolicy>::Entry*
-TemplateHashMapImpl<AllocationPolicy>::Lookup(void* key, uint32_t hash) const {
+HashMapImpl::Entry* HashMapImpl::Lookup(void* key, uint32_t hash) const {
Entry* p = Probe(key, hash);
return p->key != NULL ? p : NULL;
}
-template <class AllocationPolicy>
-typename TemplateHashMapImpl<AllocationPolicy>::Entry*
-TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert(
- void* key, uint32_t hash, AllocationPolicy allocator) {
+HashMapImpl::Entry* HashMapImpl::LookupOrInsert(void* key, uint32_t hash) {
// Find a matching entry.
Entry* p = Probe(key, hash);
if (p->key != NULL) {
@@ -138,7 +126,7 @@ TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert(
// Grow the map if we reached >= 80% occupancy.
if (occupancy_ + occupancy_ / 4 >= capacity_) {
- Resize(allocator);
+ Resize();
p = Probe(key, hash);
}
@@ -146,8 +134,7 @@ TemplateHashMapImpl<AllocationPolicy>::LookupOrInsert(
}
-template<class AllocationPolicy>
-void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) {
+void* HashMapImpl::Remove(void* key, uint32_t hash) {
// Lookup the entry for the key to remove.
Entry* p = Probe(key, hash);
if (p->key == NULL) {
@@ -208,8 +195,7 @@ void* TemplateHashMapImpl<AllocationPolicy>::Remove(void* key, uint32_t hash) {
}
-template<class AllocationPolicy>
-void TemplateHashMapImpl<AllocationPolicy>::Clear() {
+void HashMapImpl::Clear() {
// Mark all entries as empty.
const Entry* end = map_end();
for (Entry* p = map_; p < end; p++) {
@@ -219,16 +205,12 @@ void TemplateHashMapImpl<AllocationPolicy>::Clear() {
}
-template<class AllocationPolicy>
-typename TemplateHashMapImpl<AllocationPolicy>::Entry*
- TemplateHashMapImpl<AllocationPolicy>::Start() const {
+HashMapImpl::Entry* HashMapImpl::Start() const {
return Next(map_ - 1);
}
-template<class AllocationPolicy>
-typename TemplateHashMapImpl<AllocationPolicy>::Entry*
- TemplateHashMapImpl<AllocationPolicy>::Next(Entry* p) const {
+HashMapImpl::Entry* HashMapImpl::Next(Entry* p) const {
const Entry* end = map_end();
DCHECK(map_ - 1 <= p && p < end);
for (p++; p < end; p++) {
@@ -240,9 +222,7 @@ typename TemplateHashMapImpl<AllocationPolicy>::Entry*
}
-template <class AllocationPolicy>
-typename TemplateHashMapImpl<AllocationPolicy>::Entry*
-TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) const {
+HashMapImpl::Entry* HashMapImpl::Probe(void* key, uint32_t hash) const {
DCHECK(key != NULL);
DCHECK(base::bits::IsPowerOfTwo32(capacity_));
@@ -262,32 +242,26 @@ TemplateHashMapImpl<AllocationPolicy>::Probe(void* key, uint32_t hash) const {
}
-template<class AllocationPolicy>
-void TemplateHashMapImpl<AllocationPolicy>::Initialize(
- uint32_t capacity, AllocationPolicy allocator) {
+void HashMapImpl::Initialize(uint32_t capacity) {
DCHECK(base::bits::IsPowerOfTwo32(capacity));
- map_ = reinterpret_cast<Entry*>(allocator.New(capacity * sizeof(Entry)));
- if (map_ == NULL) {
- v8::internal::FatalProcessOutOfMemory("HashMap::Initialize");
- return;
- }
+ map_ = reinterpret_cast<Entry*>(Malloced::New(capacity * sizeof(Entry)));
+ CHECK(map_ != NULL);
capacity_ = capacity;
Clear();
}
-template<class AllocationPolicy>
-void TemplateHashMapImpl<AllocationPolicy>::Resize(AllocationPolicy allocator) {
+void HashMapImpl::Resize() {
Entry* map = map_;
uint32_t n = occupancy_;
// Allocate larger map.
- Initialize(capacity_ * 2, allocator);
+ Initialize(capacity_ * 2);
// Rehash all current entries.
for (Entry* p = map; n > 0; p++) {
if (p->key != NULL) {
- Entry* entry = LookupOrInsert(p->key, p->hash, allocator);
+ Entry* entry = LookupOrInsert(p->key, p->hash);
entry->value = p->value;
entry->order = p->order;
n--;
@@ -295,62 +269,10 @@ void TemplateHashMapImpl<AllocationPolicy>::Resize(AllocationPolicy allocator) {
}
// Delete old map.
- AllocationPolicy::Delete(map);
+ Malloced::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> {
- public:
- STATIC_ASSERT(sizeof(Key*) == sizeof(void*)); // NOLINT
- STATIC_ASSERT(sizeof(Value*) == sizeof(void*)); // NOLINT
- struct value_type {
- Key* first;
- Value* second;
- };
-
- class Iterator {
- public:
- Iterator& operator++() {
- entry_ = map_->Next(entry_);
- return *this;
- }
-
- value_type* operator->() { return reinterpret_cast<value_type*>(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) { }
-
- const TemplateHashMapImpl<AllocationPolicy>* map_;
- typename TemplateHashMapImpl<AllocationPolicy>::Entry* entry_;
-
- friend class TemplateHashMap;
- };
-
- TemplateHashMap(
- typename TemplateHashMapImpl<AllocationPolicy>::MatchFun match,
- AllocationPolicy allocator = AllocationPolicy())
- : TemplateHashMapImpl<AllocationPolicy>(
- match,
- TemplateHashMapImpl<AllocationPolicy>::kDefaultHashMapCapacity,
- allocator) { }
-
- Iterator begin() const { return Iterator(this, this->Start()); }
- Iterator end() const { return Iterator(this, NULL); }
- Iterator find(Key* key, bool insert = false,
- AllocationPolicy allocator = AllocationPolicy()) {
- if (insert) {
- return Iterator(this, this->LookupOrInsert(key, key->Hash(), allocator));
- }
- return Iterator(this, this->Lookup(key, key->Hash()));
- }
-};
-
-} // namespace internal
+} // namespace sampler
} // namespace v8
-#endif // V8_HASHMAP_H_
+#endif // V8_LIBSAMPLER_HASHMAP_H_
« no previous file with comments | « src/libsampler/DEPS ('k') | src/libsampler/utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698