#ifndef SQUID_BASE_TIDYPOINTER_H
#define SQUID_BASE_TIDYPOINTER_H
-/**
- * A pointer that deletes the object it points to when the pointer's owner or
- * context is gone. Similar to std::unique_ptr but without confusing assignment
- * and with a customizable cleanup method. Prevents memory leaks in
- * the presence of exceptions and processing short cuts.
-*/
-template <typename T, void (*DeAllocator)(T *t)> class TidyPointer
-{
-public:
- /// Delete callback.
- typedef void DCB (T *t);
- TidyPointer(T *t = NULL)
- : raw(t) {}
-public:
- bool operator !() const { return !raw; }
- explicit operator bool() const { return raw; }
- /// Returns raw and possibly NULL pointer
- T *get() const { return raw; }
+#include <memory>
- /// Reset raw pointer - delete last one and save new one.
- void reset(T *t) {
- deletePointer();
- raw = t;
- }
+#define TidyPointer std::unique_ptr
- /// Forget the raw pointer without freeing it. Become a nil pointer.
- T *release() {
- T *ret = raw;
- raw = NULL;
- return ret;
- }
- /// Deallocate raw pointer.
- ~TidyPointer() {
- deletePointer();
- }
-private:
- /// Forbidden copy constructor.
- TidyPointer(TidyPointer<T, DeAllocator> const &);
- /// Forbidden assigment operator.
- TidyPointer <T, DeAllocator> & operator = (TidyPointer<T, DeAllocator> const &);
- /// Deallocate raw pointer. Become a nil pointer.
- void deletePointer() {
- if (raw) {
- DeAllocator(raw);
+// Macro to be used to define the C++ equivalent functor of an extern "C"
+// function. The C++ functor is suffixed with the _cpp extension
+#define CtoCppDtor(function, argument_type) \
+ struct function ## _cpp { \
+ void operator()(argument_type a) { function(a); } \
}
- raw = NULL;
- }
- T *raw; ///< pointer to T object or NULL
-};
-/// DeAllocator for pointers that need free(3) from the std C library
-template<typename T> void tidyFree(T *p)
-{
- xfree(p);
-}
+/// DeAllocator functor for pointers that need free(3) from the std C library
+CtoCppDtor(xfree, char *);
#endif // SQUID_BASE_TIDYPOINTER_H
#endif
/**
- \ingroup SslCrtdSslAPI
- * TidyPointer typedefs for common SSL objects
+ * TidyPointer typedefs for common SSL objects
*/
-sk_free_wrapper(sk_X509, STACK_OF(X509) *, X509_free)
+sk_dtor_wrapper(sk_X509, STACK_OF(X509) *, X509_free);
typedef TidyPointer<STACK_OF(X509), sk_X509_free_wrapper> X509_STACK_Pointer;
CtoCpp1(EVP_PKEY_free, EVP_PKEY *)
typedef Security::LockingPointer<EVP_PKEY, EVP_PKEY_free_cpp, CRYPTO_LOCK_EVP_PKEY> EVP_PKEY_Pointer;
-CtoCpp1(BN_free, BIGNUM *)
+CtoCppDtor(BN_free, BIGNUM *);
typedef TidyPointer<BIGNUM, BN_free_cpp> BIGNUM_Pointer;
-CtoCpp1(BIO_free, BIO *)
+CtoCppDtor(BIO_free, BIO *);
typedef TidyPointer<BIO, BIO_free_cpp> BIO_Pointer;
-CtoCpp1(ASN1_INTEGER_free, ASN1_INTEGER *)
+CtoCppDtor(ASN1_INTEGER_free, ASN1_INTEGER *);
typedef TidyPointer<ASN1_INTEGER, ASN1_INTEGER_free_cpp> ASN1_INT_Pointer;
-CtoCpp1(ASN1_OCTET_STRING_free, ASN1_OCTET_STRING *)
+CtoCppDtor(ASN1_OCTET_STRING_free, ASN1_OCTET_STRING *);
typedef TidyPointer<ASN1_OCTET_STRING, ASN1_OCTET_STRING_free_cpp> ASN1_OCTET_STRING_Pointer;
-CtoCpp1(TXT_DB_free, TXT_DB *)
+CtoCppDtor(TXT_DB_free, TXT_DB *);
typedef TidyPointer<TXT_DB, TXT_DB_free_cpp> TXT_DB_Pointer;
-CtoCpp1(X509_NAME_free, X509_NAME *)
+CtoCppDtor(X509_NAME_free, X509_NAME *);
typedef TidyPointer<X509_NAME, X509_NAME_free_cpp> X509_NAME_Pointer;
-CtoCpp1(RSA_free, RSA *)
+CtoCppDtor(RSA_free, RSA *);
typedef TidyPointer<RSA, RSA_free_cpp> RSA_Pointer;
-CtoCpp1(X509_REQ_free, X509_REQ *)
+CtoCppDtor(X509_REQ_free, X509_REQ *);
typedef TidyPointer<X509_REQ, X509_REQ_free_cpp> X509_REQ_Pointer;
-sk_free_wrapper(sk_X509_NAME, STACK_OF(X509_NAME) *, X509_NAME_free)
+sk_dtor_wrapper(sk_X509_NAME, STACK_OF(X509_NAME) *, X509_NAME_free);
typedef TidyPointer<STACK_OF(X509_NAME), sk_X509_NAME_free_wrapper> X509_NAME_STACK_Pointer;
-CtoCpp1(AUTHORITY_KEYID_free, AUTHORITY_KEYID *)
+CtoCppDtor(AUTHORITY_KEYID_free, AUTHORITY_KEYID *);
typedef TidyPointer<AUTHORITY_KEYID, AUTHORITY_KEYID_free_cpp> AUTHORITY_KEYID_Pointer;
-sk_free_wrapper(sk_GENERAL_NAME, STACK_OF(GENERAL_NAME) *, GENERAL_NAME_free)
+sk_dtor_wrapper(sk_GENERAL_NAME, STACK_OF(GENERAL_NAME) *, GENERAL_NAME_free);
typedef TidyPointer<STACK_OF(GENERAL_NAME), sk_GENERAL_NAME_free_wrapper> GENERAL_NAME_STACK_Pointer;
-CtoCpp1(GENERAL_NAME_free, GENERAL_NAME *)
+CtoCppDtor(GENERAL_NAME_free, GENERAL_NAME *);
typedef TidyPointer<GENERAL_NAME, GENERAL_NAME_free_cpp> GENERAL_NAME_Pointer;
-CtoCpp1(X509_EXTENSION_free, X509_EXTENSION *)
+CtoCppDtor(X509_EXTENSION_free, X509_EXTENSION *);
typedef TidyPointer<X509_EXTENSION, X509_EXTENSION_free_cpp> X509_EXTENSION_Pointer;
/**