]> git.ipfire.org Git - thirdparty/squid.git/blame - src/security/LockingPointer.h
Sync with trunk rev.14719
[thirdparty/squid.git] / src / security / LockingPointer.h
CommitLineData
89deb186 1/*
ef57eb7b 2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
89deb186
AJ
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
f97700a0
AJ
9#ifndef SQUID_SRC_SECURITY_LOCKINGPOINTER_H
10#define SQUID_SRC_SECURITY_LOCKINGPOINTER_H
11
12#include "base/TidyPointer.h"
13
eacc1666
AJ
14#if USE_OPENSSL
15#if HAVE_OPENSSL_CRYPTO_H
16#include <openssl/crypto.h>
17#endif
18
19// Macro to be used to define the C++ wrapper function of a sk_*_pop_free
20// openssl family functions. The C++ function suffixed with the _free_wrapper
21// extension
22#define sk_free_wrapper(sk_object, argument, freefunction) \
23 extern "C++" inline void sk_object ## _free_wrapper(argument a) { \
24 sk_object ## _pop_free(a, freefunction); \
25 }
26
27#endif
28
29// Macro to be used to define the C++ equivalent function of an extern "C"
30// function. The C++ function suffixed with the _cpp extension
31#define CtoCpp1(function, argument) \
32 extern "C++" inline void function ## _cpp(argument a) { \
33 function(a); \
34 }
35
f97700a0
AJ
36namespace Security
37{
38
39/**
89deb186
AJ
40 * Add SSL locking (a.k.a. reference counting) and assignment to TidyPointer
41 */
f97700a0
AJ
42template <typename T, void (*DeAllocator)(T *t), int lock>
43class LockingPointer: public TidyPointer<T, DeAllocator>
44{
45public:
46 typedef TidyPointer<T, DeAllocator> Parent;
89deb186
AJ
47 typedef LockingPointer<T, DeAllocator, lock> SelfType;
48
014a9017 49 explicit LockingPointer(T *t = nullptr): Parent(t) {}
89deb186
AJ
50
51 explicit LockingPointer(const SelfType &o): Parent() {
014a9017 52 resetAndLock(o.get());
89deb186
AJ
53 }
54
55 SelfType &operator =(const SelfType & o) {
014a9017 56 resetAndLock(o.get());
89deb186
AJ
57 return *this;
58 }
f97700a0 59
014a9017
AJ
60#if __cplusplus >= 201103L
61 explicit LockingPointer(LockingPointer<T, DeAllocator, lock> &&o): Parent(o.release()) {
62 }
89deb186
AJ
63
64 LockingPointer<T, DeAllocator, lock> &operator =(LockingPointer<T, DeAllocator, lock> &&o) {
01b50eb1
AR
65 if (o.get() != this->get())
66 this->reset(o.release());
89deb186
AJ
67 return *this;
68 }
69#endif
f97700a0 70
014a9017
AJ
71 void resetAndLock(T *t) {
72 if (t != this->get()) {
73 this->reset(t);
f97700a0 74#if USE_OPENSSL
014a9017
AJ
75 if (t)
76 CRYPTO_add(&t->references, 1, lock);
77#elif USE_GNUTLS
78 // XXX: GnuTLS does not provide locking ?
f97700a0 79#else
014a9017 80 assert(false);
f97700a0
AJ
81#endif
82 }
83 }
84};
85
86} // namespace Security
87
88#endif /* SQUID_SRC_SECURITY_LOCKINGPOINTER_H */
63b8c4d7 89