]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/security/LockingPointer.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / security / LockingPointer.h
index 60244512400370e22accc758bebc40e52a77d32a..e0395f412f5f7dd680ed5551f674e1957aee2b24 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
+ * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
  *
  * Squid software is distributed under GPLv2+ license and includes
  * contributions from numerous individuals and organizations.
@@ -9,6 +9,8 @@
 #ifndef SQUID_SRC_SECURITY_LOCKINGPOINTER_H
 #define SQUID_SRC_SECURITY_LOCKINGPOINTER_H
 
+#include "base/HardFun.h"
+
 #if USE_OPENSSL
 #if HAVE_OPENSSL_CRYPTO_H
 #include <openssl/crypto.h>
@@ -34,6 +36,9 @@
 namespace Security
 {
 
+inline bool nilFunction(const void *) { return false; }
+typedef HardFun<bool, const void *, nilFunction> NilFunctor;
+
 /**
  * A shared pointer to a reference-counting Object with library-specific
  * absorption, locking, and unlocking implementations. The API largely
@@ -44,12 +49,12 @@ namespace Security
  * pre-lock objects before they are fed to LockingPointer, necessitating
  * this resetWithoutLocking() customization hook.
  */
-template <typename T, void (*UnLocker)(T *t), int lockId>
+template <typename T, void (*UnLocker)(T *t), class Locker = NilFunctor>
 class LockingPointer
 {
 public:
     /// a helper label to simplify this objects API definitions below
-    typedef Security::LockingPointer<T, UnLocker, lockId> SelfType;
+    typedef Security::LockingPointer<T, UnLocker, Locker> SelfType;
 
     /**
      * Construct directly from a raw pointer.
@@ -66,7 +71,7 @@ public:
     ~LockingPointer() { unlock(); }
 
     // copy semantics are okay only when adding a lock reference
-    explicit LockingPointer(const SelfType &o) : raw(nullptr) {
+    LockingPointer(const SelfType &o) : raw(nullptr) {
         resetAndLock(o.get());
     }
     const SelfType &operator =(const SelfType &o) {
@@ -85,6 +90,10 @@ public:
 
     bool operator !() const { return !raw; }
     explicit operator bool() const { return raw; }
+    bool operator ==(const SelfType &o) const { return (o.get() == raw); }
+    bool operator !=(const SelfType &o) const { return (o.get() != raw); }
+
+    T *operator ->() const { return raw; }
 
     /// Returns raw and possibly nullptr pointer
     T *get() const { return raw; }
@@ -115,14 +124,10 @@ public:
 private:
     /// The lock() method increments Object's reference counter.
     void lock(T *t) {
-#if USE_OPENSSL
-        if (t)
-            CRYPTO_add(&t->references, 1, lockId);
-#elif USE_GNUTLS
-        // XXX: GnuTLS does not provide locking ?
-#else
-        assert(false);
-#endif
+        if (t) {
+            Locker doLock;
+            doLock(t);
+        }
     }
 
     /// Become a nil pointer. Decrements any pointed-to Object's reference counter