Skip to content

Commit

Permalink
Refactored ScopedPtr usage to std::unique_ptr.
Browse files Browse the repository at this point in the history
Removed ScopedPtr.
  • Loading branch information
jamesyonan committed May 17, 2015
1 parent fd6892f commit 68b8f12
Show file tree
Hide file tree
Showing 21 changed files with 86 additions and 262 deletions.
1 change: 0 additions & 1 deletion client/ovpncli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@

#include <openvpn/init/initprocess.hpp>
#include <openvpn/common/types.hpp>
#include <openvpn/common/scoped_ptr.hpp>
#include <openvpn/common/platform_string.hpp>
#include <openvpn/client/cliconnect.hpp>
#include <openvpn/client/cliopthelper.hpp>
Expand Down
5 changes: 3 additions & 2 deletions openvpn/client/cliconnect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
#ifndef OPENVPN_CLIENT_CLICONNECT_H
#define OPENVPN_CLIENT_CLICONNECT_H

#include <memory>

#include <openvpn/common/rc.hpp>
#include <openvpn/common/scoped_ptr.hpp>
#include <openvpn/error/excode.hpp>
#include <openvpn/time/asiotimer.hpp>
#include <openvpn/client/cliopt.hpp>
Expand Down Expand Up @@ -551,7 +552,7 @@ namespace openvpn {
AsioTimer restart_wait_timer;
AsioTimer conn_timer;
bool conn_timer_pending;
ScopedPtr<boost::asio::io_service::work> asio_work;
std::unique_ptr<boost::asio::io_service::work> asio_work;
RemoteList::PreResolve::Ptr pre_resolve;
};

Expand Down
19 changes: 5 additions & 14 deletions openvpn/common/enumdir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,27 @@
#include <string>
#include <vector>
#include <utility>
#include <memory>

#include <openvpn/common/types.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/scoped_ptr.hpp>
#include <openvpn/common/uniqueptr.hpp>

namespace openvpn {
OPENVPN_EXCEPTION(enum_dir_error);

template <typename T>
class FreeDIR {
public:
static void del(T* p)
{
// delete method for pointer returned by opendir
closedir(p);
}
};

std::vector<std::string> enum_dir(const std::string& dirname,
const size_t size_hint=0)
{
std::vector<std::string> ret;
if (size_hint)
ret.reserve(size_hint);
ScopedPtr<DIR, FreeDIR> dir(opendir(dirname.c_str()));
if (!dir.defined())
unique_ptr_del<DIR> dir(opendir(dirname.c_str()), [](DIR* d) { closedir(d); });
if (!dir)
throw enum_dir_error(dirname + ": cannot open directory");

struct dirent *e;
while ((e = readdir(dir())) != NULL)
while ((e = readdir(dir.get())) != NULL)
{
std::string fn(e->d_name);
if (fn != "." && fn != "..")
Expand Down
149 changes: 0 additions & 149 deletions openvpn/common/scoped_ptr.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions openvpn/common/unicode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

#include <string>
#include <algorithm> // for std::min
#include <memory>

#include <openvpn/common/types.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/scoped_ptr.hpp>
#include <openvpn/common/unicode-impl.hpp>
#include <openvpn/buffer/buffer.hpp>

Expand Down Expand Up @@ -197,7 +197,7 @@ namespace openvpn {
template <typename STRING>
inline BufferPtr string_to_utf16(const STRING& str)
{
ScopedPtr<UTF16, PtrArrayFree> utf16_dest(new UTF16[str.length()]);
std::unique_ptr<UTF16[]> utf16_dest(new UTF16[str.length()]);
const UTF8 *src = (UTF8 *)str.c_str();
UTF16 *dest = utf16_dest.get();
const ConversionResult res = ConvertUTF8toUTF16(&src, src + str.length(),
Expand Down
21 changes: 6 additions & 15 deletions openvpn/openssl/util/free.hpp → openvpn/common/uniqueptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,15 @@
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.

// deallocator for objects that were allocated by OpenSSL
#ifndef OPENVPN_COMMON_UNIQUEPTR_H
#define OPENVPN_COMMON_UNIQUEPTR_H

#ifndef OPENVPN_OPENSSL_UTIL_FREE_H
#define OPENVPN_OPENSSL_UTIL_FREE_H

#include <openssl/crypto.h>
#include <memory>
#include <functional>

namespace openvpn {

template <typename T>
class OpenSSLFree {
public:
static void del(T* p)
{
OPENSSL_free (p);
}
};

template<typename T>
using unique_ptr_del = std::unique_ptr<T, std::function<void(T*)>>;
}

#endif
27 changes: 15 additions & 12 deletions openvpn/netconf/enumiface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
#include <cstring>
#include <string>
#include <sstream>
#include <memory>

#include <openvpn/common/types.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/scoped_ptr.hpp>
#include <openvpn/addr/ip.hpp>

namespace openvpn {
Expand All @@ -50,8 +50,8 @@ namespace openvpn {
OPENVPN_EXCEPTION(enum_iface_error);

EnumIface()
: ifinfo(alloc_if_addrs(), free_if_addrs)
{
getifaddrs(ifinfo.ref());
}

std::string to_string() const
Expand Down Expand Up @@ -198,17 +198,20 @@ namespace openvpn {
return ret;
}

template <typename T>
class FreeIFAddrs {
public:
static void del(T* p)
{
// delete method for pointer returned by getifaddrs
freeifaddrs(p);
}
};
static ifaddrs* alloc_if_addrs()
{
ifaddrs* ifa = nullptr;
::getifaddrs(&ifa);
return ifa;
}

static void free_if_addrs(ifaddrs* p)
{
// delete method for pointer returned by getifaddrs
freeifaddrs(p);
}

ScopedPtr<ifaddrs, FreeIFAddrs> ifinfo;
std::unique_ptr<ifaddrs, decltype(&free_if_addrs)> ifinfo;
};
}

Expand Down
8 changes: 4 additions & 4 deletions openvpn/openssl/ssl/sslctx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
#include <openvpn/common/exception.hpp>
#include <openvpn/common/mode.hpp>
#include <openvpn/common/options.hpp>
#include <openvpn/common/scoped_ptr.hpp>
#include <openvpn/common/base64.hpp>
#include <openvpn/common/string.hpp>
#include <openvpn/common/uniqueptr.hpp>
#include <openvpn/frame/frame.hpp>
#include <openvpn/buffer/buffer.hpp>
#include <openvpn/pki/cclist.hpp>
Expand All @@ -56,7 +56,6 @@
#include <openvpn/openssl/pki/dh.hpp>
#include <openvpn/openssl/pki/x509store.hpp>
#include <openvpn/openssl/bio/bio_memq_stream.hpp>
#include <openvpn/openssl/util/free.hpp>

// An SSL Context is essentially a configuration that can be used
// to generate an arbitrary number of actual SSL connections objects.
Expand Down Expand Up @@ -991,8 +990,9 @@ namespace openvpn {

static std::string x509_get_subject(::X509 *cert)
{
ScopedPtr<char, OpenSSLFree> subject(X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0));
if (subject.defined())
unique_ptr_del<char> subject(X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0),
[](char* p) { OPENSSL_free(p); });
if (subject)
return std::string(subject.get());
else
return std::string("");
Expand Down
8 changes: 4 additions & 4 deletions openvpn/polarssl/ssl/sslctx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
#include <string>
#include <sstream>
#include <cstring>
#include <memory>

#include <polarssl/ssl.h>
#include <polarssl/oid.h>
#include <polarssl/sha1.h>

#include <openvpn/common/types.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/scoped_ptr.hpp>
#include <openvpn/common/base64.hpp>
#include <openvpn/common/binprefix.hpp>
#include <openvpn/frame/memq_stream.hpp>
Expand Down Expand Up @@ -1002,10 +1002,10 @@ namespace openvpn {
static std::string cert_info(const x509_crt *cert, const char *prefix = NULL)
{
const size_t buf_size = 4096;
ScopedPtr<char, PtrArrayFree> buf(new char[buf_size]);
const int size = x509_crt_info(buf(), buf_size, prefix ? prefix : "", cert);
std::unique_ptr<char[]> buf(new char[buf_size]);
const int size = x509_crt_info(buf.get(), buf_size, prefix ? prefix : "", cert);
if (size >= 0)
return std::string(buf());
return std::string(buf.get());
else
return "error rendering cert";
}
Expand Down
Loading

0 comments on commit 68b8f12

Please sign in to comment.