LockingPointer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 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/HardFun.h"
13 
14 #if USE_OPENSSL
15 #include "compat/openssl.h"
16 #if HAVE_OPENSSL_CRYPTO_H
17 #include <openssl/crypto.h>
18 #endif
19 
20 // Macro to be used to define the C++ wrapper function of a sk_*_pop_free
21 // openssl family functions. The C++ function suffixed with the _free_wrapper
22 // extension
23 #define sk_free_wrapper(sk_object, argument, freefunction) \
24  extern "C++" inline void sk_object ## _free_wrapper(argument a) { \
25  sk_object ## _pop_free(a, freefunction); \
26  }
27 
28 #endif /* USE_OPENSSL */
29 
30 // Macro to be used to define the C++ equivalent function of an extern "C"
31 // function. The C++ function suffixed with the _cpp extension
32 #define CtoCpp1(function, argument) \
33  extern "C++" inline void function ## _cpp(argument a) { \
34  function(a); \
35  }
36 
37 namespace Security
38 {
39 
40 inline bool nilFunction(const void *) { return false; }
42 
53 template <typename T, void (*UnLocker)(T *t), class Locker = NilFunctor>
55 {
56 public:
59 
66  explicit LockingPointer(T *t = nullptr): 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 { return raw; }
98 
100  T *get() const { return raw; }
101 
103  void resetWithoutLocking(T *t) {
104  unlock();
105  raw = t;
106  }
107 
108  void resetAndLock(T *t) {
109  if (t != get()) {
111  lock(t);
112  }
113  }
114 
116  void reset() { unlock(); }
117 
119  T *release() {
120  T *ret = raw;
121  raw = nullptr;
122  return ret;
123  }
124 
125 private:
127  void lock(T *t) {
128  if (t) {
129  Locker doLock;
130  doLock(t);
131  }
132  }
133 
136  void unlock() {
137  if (raw) {
138  UnLocker(raw);
139  raw = nullptr;
140  }
141  }
142 
155  T *raw;
156 };
157 
158 } // namespace Security
159 
160 #endif /* SQUID_SRC_SECURITY_LOCKINGPOINTER_H */
161 
T * release()
Forget the raw pointer without unlocking it. Become a nil pointer.
LockingPointer(const SelfType &o)
void resetWithoutLocking(T *t)
Reset raw pointer - unlock any previous one and save new one without locking.
bool operator!=(const SelfType &o) const
const SelfType & operator=(const SelfType &o)
Security::LockingPointer< T, UnLocker, Locker > SelfType
a helper label to simplify this objects API definitions below
LockingPointer(T *t=nullptr)
bool nilFunction(const void *)
void lock(T *t)
The lock() method increments Object&#39;s reference counter.
void reset()
Forget the raw pointer - unlock if any value was set. Become a nil pointer.
HardFun< bool, const void *, nilFunction > NilFunctor
T * get() const
Returns raw and possibly nullptr pointer.
LockingPointer(SelfType &&o)
bool operator==(const SelfType &o) const
~LockingPointer()
use the custom UnLocker to unlock any value still stored.

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors