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
31// Macro to be used to define the C++ equivalent function of an extern "C"
32// function. The C++ function suffixed with the _cpp extension
33#define CtoCpp1(function, argument) \
34 extern "C++" inline void function ## _cpp(argument a) { \
35 function(a); \
36 }
37
38namespace Security
39{
40
41inline bool nilFunction(const void *) { return false; }
43
54template <typename T, void (*UnLocker)(T *t), class Locker = NilFunctor>
56{
57public:
60
62 constexpr LockingPointer(): raw(nullptr) {}
63
65 constexpr LockingPointer(std::nullptr_t): raw(nullptr) {}
66
73 explicit LockingPointer(T *t): raw(nullptr) {
74 // de-optimized for clarity about non-locking
76 }
77
80
81 // copy semantics are okay only when adding a lock reference
82 LockingPointer(const SelfType &o) : raw(nullptr) {
83 resetAndLock(o.get());
84 }
85 const SelfType &operator =(const SelfType &o) {
86 resetAndLock(o.get());
87 return *this;
88 }
89
90 LockingPointer(SelfType &&o) : raw(nullptr) {
91 resetWithoutLocking(o.release());
92 }
94 if (o.get() != raw)
95 resetWithoutLocking(o.release());
96 return *this;
97 }
98
99 bool operator !() const { return !raw; }
100 explicit operator bool() const { return raw; }
101 bool operator ==(const SelfType &o) const { return (o.get() == raw); }
102 bool operator !=(const SelfType &o) const { return (o.get() != raw); }
103
104 T &operator *() const { Assure(raw); return *raw; }
105 T *operator ->() const { return raw; }
106
108 T *get() const { return raw; }
109
112 unlock();
113 raw = t;
114 }
115
116 void resetAndLock(T *t) {
117 if (t != get()) {
119 lock(t);
120 }
121 }
122
124 void reset() { unlock(); }
125
127 T *release() {
128 T *ret = raw;
129 raw = nullptr;
130 return ret;
131 }
132
133private:
135 void lock(T *t) {
136 if (t) {
137 Locker doLock;
138 doLock(t);
139 }
140 }
141
144 void unlock() {
145 if (raw) {
146 UnLocker(raw);
147 raw = nullptr;
148 }
149 }
150
163 T *raw;
164};
165
166} // namespace Security
167
168#endif /* SQUID_SRC_SECURITY_LOCKINGPOINTER_H */
169
#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