MySQL  8.0.18
Source Code Documentation
auth_utility.h
Go to the documentation of this file.
1 /* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 /* Internals */
23 
24 #ifndef AUTH_UTILITY_INCLUDED
25 #define AUTH_UTILITY_INCLUDED
26 
27 #include "my_alloc.h"
28 #include "mysql/psi/mysql_rwlock.h"
29 #include "rwlock_scoped_lock.h"
30 
31 #include <map>
32 
33 /**
34  Class to manage MEM_ROOT. It either accepts and initialized MEM_ROOT
35  or initializes a new one and controls its lifespan.
36 */
38  public:
39  explicit Mem_root_base(MEM_ROOT *mem_root);
40  Mem_root_base(const Mem_root_base &) = delete;
41  Mem_root_base &operator=(const Mem_root_base &) = delete;
43  MEM_ROOT *get_mem_root() const;
44 
45  protected:
48 
49  private:
50  Mem_root_base();
51  bool m_inited;
52 };
53 
54 /**
55  Return MEM_ROOT handle.
56 */
57 inline MEM_ROOT *Mem_root_base::get_mem_root() const { return m_mem_root; }
58 
59 /**
60  Map with RWLock protections
61 */
62 template <typename K, typename V>
63 class Map_with_rw_lock {
64  public:
66  Map_with_rw_lock(const Map_with_rw_lock &) = delete;
68  Map_with_rw_lock &operator=(const Map_with_rw_lock &) = delete;
71  m_map.clear();
73  }
74 
75  /**
76  Search an entry
77 
78  @param [in] key Key
79  @param [out] value Value
80 
81  @returns status of search
82  @retval false Entry not found
83  @retval true Entry found. Check value.
84  */
85  bool find(const K &key, V &value) {
86  rwlock_scoped_lock rdlock(&m_lock, false, __FILE__, __LINE__);
87  const auto search_itr = m_map.find(key);
88  if (search_itr != m_map.end()) {
89  value = search_itr->second;
90  return true;
91  }
92  return false;
93  }
94 
95  /**
96  Insert an entry
97 
98  @param [in] key Key
99  @param [in] value Value
100 
101  @returns status of insertion
102  @retval false Error
103  @retval true Success
104  */
105  bool insert(K key, V value) {
106  rwlock_scoped_lock wrlock(&m_lock, true, __FILE__, __LINE__);
107  auto returns = m_map.insert(std::make_pair(key, value));
108  return returns.second;
109  }
110 
111  /**
112  Delete an entry
113 
114  @param [in] key Key to the element to be erased
115  */
116  void erase(K key) {
117  rwlock_scoped_lock wrlock(&m_lock, true, __FILE__, __LINE__);
118  m_map.erase(key);
119  }
120 
121  /**
122  Check limit and clear if needed
123 
124  @param [in] size Limit to check against
125  */
126  void clear_if_greater(size_t size) {
127  rwlock_scoped_lock wrlock(&m_lock, true, __FILE__, __LINE__);
128  if (m_map.size() >= size) m_map.clear();
129  }
130 
131  /** Get the size of the map */
132  size_t size() {
133  rwlock_scoped_lock rdlock(&m_lock, false, __FILE__, __LINE__);
134  return m_map.size();
135  }
136 
137  private:
138  /** Map */
139  std::map<K, V> m_map;
140  /** Lock to protect m_map */
142 };
143 
144 #endif /* AUTH_UTILITY_INCLUDED */
MEM_ROOT * get_mem_root() const
Return MEM_ROOT handle.
Definition: auth_utility.h:57
#define mysql_rwlock_init(K, T)
Definition: mysql_rwlock.h:34
Mem_root_base & operator=(const Mem_root_base &)=delete
Class to manage MEM_ROOT.
Definition: auth_utility.h:37
void erase(K key)
Delete an entry.
Definition: auth_utility.h:116
MEM_ROOT m_internal_mem_root
Definition: auth_utility.h:47
Map_with_rw_lock(PSI_rwlock_key key)
Definition: auth_utility.h:65
Map_with_rw_lock & operator=(const Map_with_rw_lock &)=delete
MEM_ROOT * m_mem_root
Definition: auth_utility.h:46
bool m_inited
Definition: auth_utility.h:51
Instrumentation helpers for rwlock.
bool find(const K &key, V &value)
Search an entry.
Definition: auth_utility.h:85
Map with RWLock protections.
Definition: auth_internal.h:107
~Mem_root_base()
Destructor.
Definition: auth_common.cc:63
#define mysql_rwlock_destroy(T)
Definition: mysql_rwlock.h:44
unsigned int PSI_rwlock_key
Instrumented rwlock key.
Definition: psi_rwlock_bits.h:41
size_t size()
Get the size of the map.
Definition: auth_utility.h:132
void clear_if_greater(size_t size)
Check limit and clear if needed.
Definition: auth_utility.h:126
static const char * key
Definition: suite_stubs.c:14
static MEM_ROOT mem_root
Definition: client_plugin.cc:107
bool insert(K key, V value)
Insert an entry.
Definition: auth_utility.h:105
An instrumented rwlock structure.
Definition: mysql_rwlock_bits.h:50
Locks RW-lock and releases lock on scope exit.
Definition: rwlock_scoped_lock.h:31
Mem_root_base()
Meme_root_base constructor.
Definition: auth_common.cc:56
std::map< K, V > m_map
Map.
Definition: auth_utility.h:139
~Map_with_rw_lock()
Definition: auth_utility.h:70
mysql_rwlock_t m_lock
Lock to protect m_map.
Definition: auth_utility.h:141
const string value("\alue\)
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...