1 /* 2 * Copyright 2016 gRPC authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef GRPC_CORE_LIB_SLICE_SLICE_WEAK_HASH_TABLE_H 18 #define GRPC_CORE_LIB_SLICE_SLICE_WEAK_HASH_TABLE_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include "src/core/lib/gprpp/memory.h" 23 #include "src/core/lib/gprpp/ref_counted.h" 24 #include "src/core/lib/gprpp/ref_counted_ptr.h" 25 #include "src/core/lib/slice/slice_internal.h" 26 27 /// Weak hash table implementation. 28 /// 29 /// This entries in this table are weak: an entry may be removed at any time due 30 /// to a number of reasons: memory pressure, hash collisions, etc. 31 /// 32 /// The keys are \a grpc_slice objects. The values are of arbitrary type. 33 /// 34 /// This class is thread unsafe. It's the caller's responsibility to ensure 35 /// proper locking when accessing its methods. 36 37 namespace grpc_core { 38 39 template <typename T, size_t Size> 40 class SliceWeakHashTable : public RefCounted<SliceWeakHashTable<T, Size>> { 41 public: 42 /// Creates a new table of at most \a size entries. Create()43 static RefCountedPtr<SliceWeakHashTable> Create() { 44 return MakeRefCounted<SliceWeakHashTable<T, Size>>(); 45 } 46 47 /// Use Create function instead of using this directly. 48 SliceWeakHashTable() = default; 49 ~SliceWeakHashTable() = default; 50 51 /// Add a mapping from \a key to \a value, taking ownership of \a key. This 52 /// operation will always succeed. It may discard older entries. Add(const grpc_slice & key,T value)53 void Add(const grpc_slice& key, T value) { 54 const size_t idx = grpc_slice_hash_internal(key) % Size; 55 entries_[idx].Set(key, std::move(value)); 56 return; 57 } 58 59 /// Returns the value from the table associated with / \a key or null if not 60 /// found. Get(const grpc_slice & key)61 const T* Get(const grpc_slice& key) const { 62 const size_t idx = grpc_slice_hash_internal(key) % Size; 63 const auto& entry = entries_[idx]; 64 return grpc_slice_eq(entry.key(), key) ? entry.value() : nullptr; 65 } 66 67 private: 68 /// The type of the table "rows". 69 class Entry { 70 public: 71 Entry() = default; ~Entry()72 ~Entry() { 73 if (is_set_) grpc_slice_unref_internal(key_); 74 } key()75 const grpc_slice& key() const { return key_; } 76 77 /// Return the entry's value, or null if unset. value()78 const T* value() const { 79 if (!is_set_) return nullptr; 80 return &value_; 81 } 82 83 /// Set the \a key and \a value (which is moved) for the entry. Set(const grpc_slice & key,T && value)84 void Set(const grpc_slice& key, T&& value) { 85 if (is_set_) grpc_slice_unref_internal(key_); 86 key_ = key; 87 value_ = std::move(value); 88 is_set_ = true; 89 } 90 91 private: 92 grpc_slice key_; 93 T value_; 94 bool is_set_ = false; 95 }; 96 97 Entry entries_[Size]; 98 }; 99 100 } // namespace grpc_core 101 102 #endif /* GRPC_CORE_LIB_SLICE_SLICE_WEAK_HASH_TABLE_H */ 103