-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathauth_utility.h
118 lines (97 loc) · 3.35 KB
/
auth_utility.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Copyright (c) 2018, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* Internals */
#ifndef AUTH_UTILITY_INCLUDED
#define AUTH_UTILITY_INCLUDED
#include "mysql/psi/mysql_rwlock.h"
#include "rwlock_scoped_lock.h"
#include <map>
/**
Map with RWLock protections
*/
template <typename K, typename V>
class Map_with_rw_lock {
public:
Map_with_rw_lock(PSI_rwlock_key key) { mysql_rwlock_init(key, &m_lock); }
Map_with_rw_lock(const Map_with_rw_lock &) = delete;
Map_with_rw_lock(Map_with_rw_lock &&) = delete;
Map_with_rw_lock &operator=(const Map_with_rw_lock &) = delete;
Map_with_rw_lock &operator=(Map_with_rw_lock &&) = delete;
~Map_with_rw_lock() {
m_map.clear();
mysql_rwlock_destroy(&m_lock);
}
/**
Search an entry
@param [in] key Key
@param [out] value Value
@returns status of search
@retval false Entry not found
@retval true Entry found. Check value.
*/
bool find(const K &key, V &value) {
rwlock_scoped_lock rdlock(&m_lock, false, __FILE__, __LINE__);
const auto search_itr = m_map.find(key);
if (search_itr != m_map.end()) {
value = search_itr->second;
return true;
}
return false;
}
/**
Insert an entry
@param [in] key Key
@param [in] value Value
@returns status of insertion
@retval false Error
@retval true Success
*/
bool insert(K key, V value) {
rwlock_scoped_lock wrlock(&m_lock, true, __FILE__, __LINE__);
auto returns = m_map.insert(std::make_pair(key, value));
return returns.second;
}
/**
Delete an entry
@param [in] key Key to the element to be erased
*/
void erase(K key) {
rwlock_scoped_lock wrlock(&m_lock, true, __FILE__, __LINE__);
m_map.erase(key);
}
/**
Check limit and clear if needed
@param [in] size Limit to check against
*/
void clear_if_greater(size_t size) {
rwlock_scoped_lock wrlock(&m_lock, true, __FILE__, __LINE__);
if (m_map.size() >= size) m_map.clear();
}
/** Get the size of the map */
size_t size() {
rwlock_scoped_lock rdlock(&m_lock, false, __FILE__, __LINE__);
return m_map.size();
}
private:
/** Map */
std::map<K, V> m_map;
/** Lock to protect m_map */
mysql_rwlock_t m_lock;
};
#endif /* AUTH_UTILITY_INCLUDED */