LockingPointer.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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#if USE_OPENSSL
16#include "compat/openssl.h"
17#if HAVE_OPENSSL_CRYPTO_H
18#include <openssl/crypto.h>
19#endif
20
21// Macro to be used to define the C++ wrapper function of a sk_*_pop_free
22// openssl family functions. The C++ function suffixed with the _free_wrapper
23// extension
24#define sk_free_wrapper(sk_object, argument, freefunction) \
25 extern "C++" inline void sk_object ## _free_wrapper(argument a) { \
26 sk_object ## _pop_free(a, freefunction); \
27 }
28
29#endif /* USE_OPENSSL */
30
31namespace Security
32{
33
34inline bool nilFunction(const void *) { return false; }
36
47template <typename T, void (*UnLocker)(T *t), class Locker = NilFunctor>
49{
50public:
53
55 constexpr LockingPointer(): raw(nullptr) {}
56
58 constexpr LockingPointer(std::nullptr_t): raw(nullptr) {}
59
66 explicit LockingPointer(T *t): raw(nullptr) {
67 // de-optimized for clarity about non-locking
69 }
70
73
74 // copy semantics are okay only when adding a lock reference
75 LockingPointer(const SelfType &o) : raw(nullptr) {
76 resetAndLock(o.get());
77 }
78 const SelfType &operator =(const SelfType &o) {
79 resetAndLock(o.get());
80 return *this;
81 }
82
83 LockingPointer(SelfType &&o) : raw(nullptr) {
84 resetWithoutLocking(o.release());
85 }
87 if (o.get() != raw)
88 resetWithoutLocking(o.release());
89 return *this;
90 }
91
92 bool operator !() const { return !raw; }
93 explicit operator bool() const { return raw; }
94 bool operator ==(const SelfType &o) const { return (o.get() == raw); }
95 bool operator !=(const SelfType &o) const { return (o.get() != raw); }
96
97 T &operator *() const { Assure(raw); return *raw; }
98 T *operator ->() const { return raw; }
99
101 T *get() const { return raw; }
102
105 unlock();
106 raw = t;
107 }
108
109 void resetAndLock(T *t) {
110 if (t != get()) {
112 lock(t);
113 }
114 }
115
117 void reset() { unlock(); }
118
120 T *release() {
121 T *ret = raw;
122 raw = nullptr;
123 return ret;
124 }
125
126private:
128 void lock(T *t) {
129 if (t) {
130 Locker doLock;
131 doLock(t);
132 }
133 }
134
137 void unlock() {
138 if (raw) {
139 UnLocker(raw);
140 raw = nullptr;
141 }
142 }
143
156 T *raw;
157};
158
159} // namespace Security
160
161#endif /* SQUID_SRC_SECURITY_LOCKINGPOINTER_H */
162
#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.
LockingPointer(SelfType &&o)
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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors