Squid Web Cache master
Loading...
Searching...
No Matches
LockingPointer.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#ifndef SQUID_SRC_SECURITY_LOCKINGPOINTER_H
10#define SQUID_SRC_SECURITY_LOCKINGPOINTER_H
11
12#include "base/Assure.h"
13#include "base/HardFun.h"
14
15#include <cstddef>
16
17#if USE_OPENSSL
18#include "compat/openssl.h"
19#if HAVE_OPENSSL_CRYPTO_H
20#include <openssl/crypto.h>
21#endif
22
23// Macro to be used to define the C++ wrapper function of a sk_*_pop_free
24// openssl family functions. The C++ function suffixed with the _free_wrapper
25// extension
26#define sk_free_wrapper(sk_object, argument, freefunction) \
27 extern "C++" inline void sk_object ## _free_wrapper(argument a) { \
28 sk_object ## _pop_free(a, freefunction); \
29 }
30
31#endif /* USE_OPENSSL */
32
33namespace Security
34{
35
36inline bool nilFunction(const void *) { return false; }
38
49template <typename T, void (*UnLocker)(T *t), class Locker = NilFunctor>
51{
52public:
55
57 constexpr LockingPointer(): raw(nullptr) {}
58
60 constexpr LockingPointer(std::nullptr_t): raw(nullptr) {}
61
68 explicit LockingPointer(T *t): raw(nullptr) {
69 // de-optimized for clarity about non-locking
71 }
72
75
76 // copy semantics are okay only when adding a lock reference
77 LockingPointer(const SelfType &o) : raw(nullptr) {
78 resetAndLock(o.get());
79 }
80 const SelfType &operator =(const SelfType &o) {
81 resetAndLock(o.get());
82 return *this;
83 }
84
85 LockingPointer(SelfType &&o) : raw(nullptr) {
86 resetWithoutLocking(o.release());
87 }
89 if (o.get() != raw)
90 resetWithoutLocking(o.release());
91 return *this;
92 }
93
94 bool operator !() const { return !raw; }
95 explicit operator bool() const { return raw; }
96 bool operator ==(const SelfType &o) const { return (o.get() == raw); }
97 bool operator !=(const SelfType &o) const { return (o.get() != raw); }
98
99 T &operator *() const { Assure(raw); return *raw; }
100 T *operator ->() const { return raw; }
101
103 T *get() const { return raw; }
104
107 unlock();
108 raw = t;
109 }
110
111 void resetAndLock(T *t) {
112 if (t != get()) {
114 lock(t);
115 }
116 }
117
119 void reset() { unlock(); }
120
122 T *release() {
123 T *ret = raw;
124 raw = nullptr;
125 return ret;
126 }
127
128private:
130 void lock(T *t) {
131 if (t) {
132 Locker doLock;
133 doLock(t);
134 }
135 }
136
139 void unlock() {
140 if (raw) {
141 UnLocker(raw);
142 raw = nullptr;
143 }
144 }
145
158 T *raw;
159};
160
161} // namespace Security
162
163#endif /* SQUID_SRC_SECURITY_LOCKINGPOINTER_H */
164
#define Assure(condition)
Definition Assure.h:35
void reset()
Forget the raw pointer - unlock if any value was set. Become a nil pointer.
bool operator!=(const SelfType &o) const
const SelfType & operator=(const SelfType &o)
constexpr LockingPointer(std::nullptr_t)
constructs a nil smart pointer from nullptr
void resetWithoutLocking(T *t)
Reset raw pointer - unlock any previous one and save new one without locking.
~LockingPointer()
use the custom UnLocker to unlock any value still stored.
bool operator==(const SelfType &o) const
T * release()
Forget the raw pointer without unlocking it. Become a nil pointer.
void lock(T *t)
The lock() method increments Object's reference counter.
T * get() const
Returns raw and possibly nullptr pointer.
Security::LockingPointer< T, UnLocker, Locker > SelfType
a helper label to simplify this objects API definitions below
LockingPointer(const SelfType &o)
constexpr LockingPointer()
constructs a nil smart pointer
Network/connection security abstraction layer.
Definition Connection.h:34
bool nilFunction(const void *)
HardFun< bool, const void *, nilFunction > NilFunctor