• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   /// Add a mapping from \a key to \a value, taking ownership of \a key. This
48   /// operation will always succeed. It may discard older entries.
Add(grpc_slice key,T value)49   void Add(grpc_slice key, T value) {
50     const size_t idx = grpc_slice_hash(key) % Size;
51     entries_[idx].Set(key, std::move(value));
52     return;
53   }
54 
55   /// Returns the value from the table associated with / \a key or null if not
56   /// found.
Get(const grpc_slice key)57   const T* Get(const grpc_slice key) const {
58     const size_t idx = grpc_slice_hash(key) % Size;
59     const auto& entry = entries_[idx];
60     return grpc_slice_eq(entry.key(), key) ? entry.value() : nullptr;
61   }
62 
63  private:
64   // So New() can call our private ctor.
65   template <typename T2, typename... Args>
66   friend T2* New(Args&&... args);
67 
68   // So Delete() can call our private dtor.
69   template <typename T2>
70   friend void Delete(T2*);
71 
72   SliceWeakHashTable() = default;
73   ~SliceWeakHashTable() = default;
74 
75   /// The type of the table "rows".
76   class Entry {
77    public:
78     Entry() = default;
~Entry()79     ~Entry() {
80       if (is_set_) grpc_slice_unref_internal(key_);
81     }
key()82     grpc_slice key() const { return key_; }
83 
84     /// Return the entry's value, or null if unset.
value()85     const T* value() const {
86       if (!is_set_) return nullptr;
87       return &value_;
88     }
89 
90     /// Set the \a key and \a value (which is moved) for the entry.
Set(grpc_slice key,T && value)91     void Set(grpc_slice key, T&& value) {
92       if (is_set_) grpc_slice_unref_internal(key_);
93       key_ = key;
94       value_ = std::move(value);
95       is_set_ = true;
96     }
97 
98    private:
99     grpc_slice key_;
100     T value_;
101     bool is_set_ = false;
102   };
103 
104   Entry entries_[Size];
105 };
106 
107 }  // namespace grpc_core
108 
109 #endif /* GRPC_CORE_LIB_SLICE_SLICE_WEAK_HASH_TABLE_H */
110