2014-07-21 05:22:06 +02:00
|
|
|
// OpenVPN -- An application to securely tunnel IP networks
|
|
|
|
// over a single port, with support for SSL/TLS-based
|
|
|
|
// session authentication and key exchange,
|
|
|
|
// packet encryption, packet authentication, and
|
|
|
|
// packet compression.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2022-09-29 11:41:13 +02:00
|
|
|
// Copyright (C) 2012-2022 OpenVPN Inc.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2017-12-21 21:42:20 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License Version 3
|
2014-07-21 05:22:06 +02:00
|
|
|
// as published by the Free Software Foundation.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-12-21 21:42:20 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2014-07-21 05:22:06 +02:00
|
|
|
//
|
2017-12-21 21:42:20 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2014-07-21 05:22:06 +02:00
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
2012-08-24 23:13:42 +02:00
|
|
|
|
2015-05-17 09:41:16 +02:00
|
|
|
#ifndef OPENVPN_COMMON_UNIQUEPTR_H
|
|
|
|
#define OPENVPN_COMMON_UNIQUEPTR_H
|
2012-11-23 07:18:43 +01:00
|
|
|
|
2015-05-17 09:41:16 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
2021-10-20 19:06:31 +02:00
|
|
|
#include <type_traits>
|
2012-03-08 11:30:43 +01:00
|
|
|
|
|
|
|
namespace openvpn {
|
2015-05-17 09:41:16 +02:00
|
|
|
template <typename T>
|
|
|
|
using unique_ptr_del = std::unique_ptr<T, std::function<void(T *)>>;
|
2021-10-20 19:06:31 +02:00
|
|
|
|
|
|
|
// The unique_ptr_slab variation of the std::unique_ptr<T> addresses the issue of
|
|
|
|
// new/delete mismatches in code that allocates a _memory slab_ with the global
|
|
|
|
// _operator_ new but de-allocates an _object_ with a delete _expression_. The use
|
|
|
|
// case that manifests the mismatch is as follows: Allocate a slab of memory that has
|
|
|
|
// a C struct at the head of the slab, with a "my_type mt[0];" as the head's last
|
|
|
|
// member. The slab is cast to the type of the C struct, but sized to contain N
|
|
|
|
// my_type items.
|
|
|
|
//
|
|
|
|
// The object based de-allocation is the behavior of the std::default_delete<T>
|
|
|
|
// template; it is used by the std::unique_ptr<T> if the user does not specify an
|
|
|
|
// alternative deleter. The unique_ptr_slab resolves the mismatch with an alternative
|
|
|
|
// deleter that de-allocates the _memory slab_ with the global _operator_ delete.
|
|
|
|
template <typename T>
|
|
|
|
void delete_slab(T *ptr)
|
|
|
|
{
|
|
|
|
::operator delete(const_cast<typename std::remove_cv<T>::type *>(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class slab_deleter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
slab_deleter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void operator()(T *ptr)
|
2023-01-11 20:43:22 +01:00
|
|
|
{
|
2021-10-20 19:06:31 +02:00
|
|
|
delete_slab(ptr);
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
2021-10-20 19:06:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using unique_ptr_slab = std::unique_ptr<T, slab_deleter<T>>;
|
2012-03-08 11:30:43 +01:00
|
|
|
} // namespace openvpn
|
|
|
|
|
|
|
|
#endif
|