0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-20 04:02:15 +02:00

BufferAllocated: improve movability

Added BufferAllocated move constructor for foreign
BufferAllocated template classes.

In order to make this work, we need to:

(a) generally friend BufferType and BufferAllocatedType
    to all BufferAllocatedType template classes, and

(b) require typename R (thread_unsafe_refcount or
    thread_safe_refcount) to be specified for
    BufferAllocatedType (previously it was optional
    and defaulted to thread_unsafe_refcount).

Signed-off-by: James Yonan <james@openvpn.net>
This commit is contained in:
James Yonan 2018-01-12 21:00:17 -07:00 committed by Antonio Quartulli
parent 8cb8d52cda
commit f9fc2f54e6
No known key found for this signature in database
GPG Key ID: 07A53C580EF2CD74
3 changed files with 26 additions and 16 deletions

View File

@ -33,7 +33,7 @@ namespace openvpn {
namespace CF {
// essentially a vector of void *, used as source for array and dictionary constructors
typedef BufferAllocatedType<CFTypeRef> SrcList;
typedef BufferAllocatedType<CFTypeRef, thread_unsafe_refcount> SrcList;
inline Array array(const SrcList& values)
{

View File

@ -136,8 +136,13 @@ namespace openvpn {
Status status_;
};
template <typename T, typename R>
class BufferAllocatedType;
template <typename T>
class BufferType {
template <typename, typename> friend class BufferAllocatedType;
public:
typedef T* type;
typedef const T* const_type;
@ -575,7 +580,7 @@ namespace openvpn {
size_t capacity_; // maximum number of array objects of type T for which memory is allocated, starting at data_
};
template <typename T, typename R = thread_unsafe_refcount>
template <typename T, typename R>
class BufferAllocatedType : public BufferType<T>, public RC<R>
{
using BufferType<T>::data_;
@ -583,6 +588,8 @@ namespace openvpn {
using BufferType<T>::size_;
using BufferType<T>::capacity_;
template <typename, typename> friend class BufferAllocatedType;
public:
enum {
CONSTRUCT_ZERO = (1<<0), // if enabled, constructors/init will zero allocated space
@ -636,19 +643,19 @@ namespace openvpn {
}
}
template <typename OT>
BufferAllocatedType(const BufferType<OT>& other, const unsigned int flags)
template <typename T_>
BufferAllocatedType(const BufferType<T_>& other, const unsigned int flags)
{
static_assert(sizeof(T) == sizeof(OT), "size inconsistency");
offset_ = other.offset();
size_ = other.size();
capacity_ = other.capacity();
static_assert(sizeof(T) == sizeof(T_), "size inconsistency");
offset_ = other.offset_;
size_ = other.size_;
capacity_ = other.capacity_;
flags_ = flags;
if (capacity_)
{
data_ = new T[capacity_];
if (size_)
std::memcpy(data_ + offset_, other.c_data(), size_ * sizeof(T));
std::memcpy(data_ + offset_, other.data_, size_ * sizeof(T));
}
}
@ -724,16 +731,17 @@ namespace openvpn {
BufferType<T>::init_headroom(headroom);
}
void move(BufferAllocatedType& other)
template <typename T_, typename R_>
void move(BufferAllocatedType<T_, R_>& other)
{
if (data_)
delete_(data_, capacity_, flags_);
move_(other);
}
RCPtr<BufferAllocatedType<T>> move_to_ptr()
RCPtr<BufferAllocatedType<T, R>> move_to_ptr()
{
RCPtr<BufferAllocatedType<T>> bp = new BufferAllocatedType<T>();
RCPtr<BufferAllocatedType<T, R>> bp = new BufferAllocatedType<T, R>();
bp->move(*this);
return bp;
}
@ -747,7 +755,8 @@ namespace openvpn {
std::swap(flags_, other.flags_);
}
BufferAllocatedType(BufferAllocatedType&& other) noexcept
template <typename T_, typename R_>
BufferAllocatedType(BufferAllocatedType<T_, R_>&& other) noexcept
{
move_(other);
}
@ -812,7 +821,8 @@ namespace openvpn {
capacity_ = newcap;
}
void move_(BufferAllocatedType& other)
template <typename T_, typename R_>
void move_(BufferAllocatedType<T_, R_>& other)
{
data_ = other.data_;
offset_ = other.offset_;
@ -846,7 +856,7 @@ namespace openvpn {
typedef BufferType<unsigned char> Buffer;
typedef BufferType<const unsigned char> ConstBuffer;
typedef BufferAllocatedType<unsigned char> BufferAllocated;
typedef BufferAllocatedType<unsigned char, thread_unsafe_refcount> BufferAllocated;
typedef RCPtr<BufferAllocated> BufferPtr;
template <typename T>

View File

@ -40,7 +40,7 @@ namespace openvpn {
class StaticKey
{
friend class OpenVPNStaticKey;
typedef BufferAllocatedType<unsigned char> key_t;
typedef BufferAllocated key_t;
public:
StaticKey() {}