forked from boostorg/smart_ptr
Add allocate_shared and make_shared for shared_ptr arrays of runtime size. Fulfills need for allocate_shared_array and make_shared_array.
[SVN r81219]
This commit is contained in:
1 parent
2ba7b6b99b
commit
6662ae7242
13 files changed
+878
No files matched your search
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Glen Joseph Fernandes
|
||||
* glenfe at live dot com
|
||||
*
|
||||
* Distributed under the Boost Software License,
|
||||
* Version 1.0. (See accompanying file LICENSE_1_0.txt
|
||||
* or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_SMART_PTR_ALLOCATE_SHARED_ARRAY_HPP
|
||||
#define BOOST_SMART_PTR_ALLOCATE_SHARED_ARRAY_HPP
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/smart_ptr/detail/allocate_array_helper.hpp>
|
||||
#include <boost/smart_ptr/detail/array_deleter.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_if_array.hpp>
|
||||
#include <boost/type_traits/remove_cv.hpp>
|
||||
|
||||
namespace boost {
|
||||
template<typename T, typename A>
|
||||
inline typename detail::sp_if_array<T>::type
|
||||
allocate_shared(const A& allocator, size_t size) {
|
||||
typedef typename remove_cv<shared_ptr<T>::element_type>::type T1;
|
||||
T1* p1 = 0;
|
||||
detail::allocate_array_helper<A, T1> a1(allocator, size, &p1);
|
||||
detail::array_deleter<T1> d1;
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
detail::array_deleter<T1>* d2;
|
||||
d2 = get_deleter<detail::array_deleter<T1> >(s1);
|
||||
d2->construct(p1, size);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
|
||||
template<typename T, typename A, typename... Args>
|
||||
inline typename detail::sp_if_array<T>::type
|
||||
allocate_shared(const A& allocator, size_t size, Args&&... args) {
|
||||
typedef typename remove_cv<shared_ptr<T>::element_type>::type T1;
|
||||
T1* p1 = 0;
|
||||
detail::allocate_array_helper<A, T1> a1(allocator, size, &p1);
|
||||
detail::array_deleter<T1> d1;
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
detail::array_deleter<T1>* d2;
|
||||
d2 = get_deleter<detail::array_deleter<T1> >(s1);
|
||||
d2->construct(p1, size, std::forward<Args>(args)...);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Glen Joseph Fernandes
|
||||
* glenfe at live dot com
|
||||
*
|
||||
* Distributed under the Boost Software License,
|
||||
* Version 1.0. (See accompanying file LICENSE_1_0.txt
|
||||
* or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_SMART_PTR_DETAIL_ALLOCATE_ARRAY_HELPER_HPP
|
||||
#define BOOST_SMART_PTR_DETAIL_ALLOCATE_ARRAY_HELPER_HPP
|
||||
|
||||
#include <boost/type_traits/alignment_of.hpp>
|
||||
#include <cstddef>
|
||||
|
||||
namespace boost {
|
||||
namespace detail {
|
||||
template<typename A, typename T, typename Y = T>
|
||||
class allocate_array_helper {
|
||||
template<typename A, typename T, typename Y>
|
||||
friend class allocate_array_helper;
|
||||
typedef typename A::rebind<Y> ::other A2;
|
||||
typedef typename A::rebind<char>::other A3;
|
||||
public:
|
||||
typedef typename A2::value_type value_type;
|
||||
typedef typename A2::pointer pointer;
|
||||
typedef typename A2::const_pointer const_pointer;
|
||||
typedef typename A2::reference reference;
|
||||
typedef typename A2::const_reference const_reference;
|
||||
typedef typename A2::size_type size_type;
|
||||
typedef typename A2::difference_type difference_type;
|
||||
template<typename U>
|
||||
struct rebind {
|
||||
typedef allocate_array_helper<A, T, U> other;
|
||||
};
|
||||
allocate_array_helper(const A& allocator, std::size_t size, T** data)
|
||||
: allocator(allocator),
|
||||
size(sizeof(T) * size),
|
||||
data(data) {
|
||||
}
|
||||
allocate_array_helper(const allocate_array_helper& other)
|
||||
: allocator(other.allocator),
|
||||
size(other.size),
|
||||
data(other.data) {
|
||||
}
|
||||
template<class U>
|
||||
allocate_array_helper(const allocate_array_helper<A, T, U>& other)
|
||||
: allocator(other.allocator),
|
||||
size(other.size),
|
||||
data(other.data) {
|
||||
}
|
||||
pointer address(reference value) const {
|
||||
return allocator.address(value);
|
||||
}
|
||||
const_pointer address(const_reference value) const {
|
||||
return allocator.address(value);
|
||||
}
|
||||
size_type max_size() const {
|
||||
return allocator.max_size();
|
||||
}
|
||||
pointer allocate(size_type count, const void* value = 0) {
|
||||
std::size_t a1 = alignment_of<T>::value;
|
||||
std::size_t n1 = count * sizeof(Y) + a1 - 1;
|
||||
char* p1 = A3(allocator).allocate(n1 + size, value);
|
||||
char* p2 = p1 + n1;
|
||||
while (std::size_t(p2) % a1 != 0) {
|
||||
p2--;
|
||||
}
|
||||
*data = reinterpret_cast<T*>(p2);
|
||||
return reinterpret_cast<Y*>(p1);
|
||||
}
|
||||
void deallocate(pointer memory, size_type count) {
|
||||
std::size_t a1 = alignment_of<T>::value;
|
||||
std::size_t n1 = count * sizeof(Y) + a1 - 1;
|
||||
char* p1 = reinterpret_cast<char*>(memory);
|
||||
A3(allocator).deallocate(p1, n1 + size);
|
||||
}
|
||||
void construct(pointer memory, const Y& value) {
|
||||
allocator.construct(memory, value);
|
||||
}
|
||||
void destroy(pointer memory) {
|
||||
allocator.destroy(memory);
|
||||
}
|
||||
template<typename U>
|
||||
bool operator==(const allocate_array_helper<A, T, U>& other) const {
|
||||
return allocator == other.allocator;
|
||||
}
|
||||
template<typename U>
|
||||
bool operator!=(const allocate_array_helper<A, T, U>& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
private:
|
||||
A2 allocator;
|
||||
std::size_t size;
|
||||
T** data;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Glen Joseph Fernandes
|
||||
* glenfe at live dot com
|
||||
*
|
||||
* Distributed under the Boost Software License,
|
||||
* Version 1.0. (See accompanying file LICENSE_1_0.txt
|
||||
* or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
|
||||
#define BOOST_SMART_PTR_DETAIL_ARRAY_DELETER_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <cstddef>
|
||||
|
||||
namespace boost {
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
class array_deleter {
|
||||
public:
|
||||
array_deleter()
|
||||
: size(0) {
|
||||
}
|
||||
~array_deleter() {
|
||||
destroy();
|
||||
}
|
||||
void construct(T* memory, std::size_t count) {
|
||||
for (object = memory; size < count; size++) {
|
||||
void* p1 = object + size;
|
||||
::new(p1) T();
|
||||
}
|
||||
}
|
||||
#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
|
||||
template<typename... Args>
|
||||
void construct(T* memory, std::size_t count, Args&&... args) {
|
||||
for (object = memory; size < count; size++) {
|
||||
void* p1 = object + size;
|
||||
::new(p1) T(args...);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
void construct_noinit(T* memory, std::size_t count) {
|
||||
for (object = memory; size < count; size++) {
|
||||
void* p1 = object + size;
|
||||
::new(p1) T;
|
||||
}
|
||||
}
|
||||
void operator()(T*) {
|
||||
destroy();
|
||||
}
|
||||
private:
|
||||
void destroy() {
|
||||
while (size > 0) {
|
||||
object[--size].~T();
|
||||
}
|
||||
}
|
||||
std::size_t size;
|
||||
T* object;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Glen Joseph Fernandes
|
||||
* glenfe at live dot com
|
||||
*
|
||||
* Distributed under the Boost Software License,
|
||||
* Version 1.0. (See accompanying file LICENSE_1_0.txt
|
||||
* or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_SMART_PTR_DETAIL_MAKE_ARRAY_HELPER_HPP
|
||||
#define BOOST_SMART_PTR_DETAIL_MAKE_ARRAY_HELPER_HPP
|
||||
|
||||
#include <boost/type_traits/alignment_of.hpp>
|
||||
#include <cstddef>
|
||||
|
||||
namespace boost {
|
||||
namespace detail {
|
||||
template<typename T, typename Y = T>
|
||||
class make_array_helper {
|
||||
template<typename T, typename Y>
|
||||
friend class make_array_helper;
|
||||
public:
|
||||
typedef typename Y value_type;
|
||||
typedef typename Y* pointer;
|
||||
typedef typename const Y* const_pointer;
|
||||
typedef typename Y& reference;
|
||||
typedef typename const Y& const_reference;
|
||||
typedef typename std::size_t size_type;
|
||||
typedef typename ptrdiff_t difference_type;
|
||||
template<typename U>
|
||||
struct rebind {
|
||||
typedef make_array_helper<T, U> other;
|
||||
};
|
||||
make_array_helper(std::size_t size, T** data)
|
||||
: size(sizeof(T) * size),
|
||||
data(data) {
|
||||
}
|
||||
make_array_helper(const make_array_helper& other)
|
||||
: size(other.size),
|
||||
data(other.data) {
|
||||
}
|
||||
template<class U>
|
||||
make_array_helper(const make_array_helper<T, U>& other)
|
||||
: size(other.size),
|
||||
data(other.data) {
|
||||
}
|
||||
pointer address(reference value) const {
|
||||
return &value;
|
||||
}
|
||||
const_pointer address(const_reference value) const {
|
||||
return &value;
|
||||
}
|
||||
size_type max_size() const {
|
||||
return static_cast<std::size_t>(-1) / sizeof(Y);
|
||||
}
|
||||
pointer allocate(size_type count, const void* = 0) {
|
||||
std::size_t a1 = alignment_of<T>::value;
|
||||
std::size_t n1 = count * sizeof(Y) + a1 - 1;
|
||||
void* p1 = ::operator new(n1 + size);
|
||||
char* p2 = static_cast<char*>(p1) + n1;
|
||||
while (std::size_t(p2) % a1 != 0) {
|
||||
p2--;
|
||||
}
|
||||
*data = reinterpret_cast<T*>(p2);
|
||||
return reinterpret_cast<Y*>(p1);
|
||||
}
|
||||
void deallocate(pointer memory, size_type) {
|
||||
void* p1 = memory;
|
||||
::operator delete(p1);
|
||||
}
|
||||
void construct(pointer memory, const Y& value) {
|
||||
void* p1 = memory;
|
||||
::new(p1) Y(value);
|
||||
}
|
||||
void destroy(pointer memory) {
|
||||
memory->~Y();
|
||||
}
|
||||
template<typename U>
|
||||
bool operator==(const make_array_helper<T, U>& other) const {
|
||||
return true;
|
||||
}
|
||||
template<typename U>
|
||||
bool operator!=(const make_array_helper<T, U>& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
private:
|
||||
std::size_t size;
|
||||
T** data;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Glen Joseph Fernandes
|
||||
* glenfe at live dot com
|
||||
*
|
||||
* Distributed under the Boost Software License,
|
||||
* Version 1.0. (See accompanying file LICENSE_1_0.txt
|
||||
* or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_SMART_PTR_DETAIL_SP_IF_ARRAY_HPP
|
||||
#define BOOST_SMART_PTR_DETAIL_SP_IF_ARRAY_HPP
|
||||
|
||||
#include <boost/smart_ptr/shared_ptr.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
struct sp_if_array {
|
||||
};
|
||||
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
||||
template<typename T>
|
||||
struct sp_if_array<T[]> {
|
||||
typedef boost::shared_ptr<T[]> type;
|
||||
};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Glen Joseph Fernandes
|
||||
* glenfe at live dot com
|
||||
*
|
||||
* Distributed under the Boost Software License,
|
||||
* Version 1.0. (See accompanying file LICENSE_1_0.txt
|
||||
* or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_SMART_PTR_MAKE_SHARED_ARRAY_HPP
|
||||
#define BOOST_SMART_PTR_MAKE_SHARED_ARRAY_HPP
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/smart_ptr/detail/array_deleter.hpp>
|
||||
#include <boost/smart_ptr/detail/make_array_helper.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_if_array.hpp>
|
||||
#include <boost/type_traits/remove_cv.hpp>
|
||||
|
||||
namespace boost {
|
||||
template<typename T>
|
||||
inline typename detail::sp_if_array<T>::type
|
||||
make_shared(std::size_t size) {
|
||||
typedef typename remove_cv<shared_ptr<T>::element_type>::type T1;
|
||||
T1* p1 = 0;
|
||||
detail::make_array_helper<T1> a1(size, &p1);
|
||||
detail::array_deleter<T1> d1;
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
detail::array_deleter<T1>* d2;
|
||||
d2 = get_deleter<detail::array_deleter<T1> >(s1);
|
||||
d2->construct(p1, size);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
|
||||
template<typename T, typename... Args>
|
||||
inline typename detail::sp_if_array<T>::type
|
||||
make_shared(std::size_t size, Args&&... args) {
|
||||
typedef typename remove_cv<shared_ptr<T>::element_type>::type T1;
|
||||
T1* p1 = 0;
|
||||
detail::make_array_helper<T1> a1(size, &p1);
|
||||
detail::array_deleter<T1> d1;
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
detail::array_deleter<T1>* d2;
|
||||
d2 = get_deleter<detail::array_deleter<T1> >(s1);
|
||||
d2->construct(p1, size, std::forward<Args>(args)...);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
#endif
|
||||
template<typename T>
|
||||
inline typename detail::sp_if_array<T>::type
|
||||
make_shared_noinit(std::size_t size) {
|
||||
typedef typename remove_cv<shared_ptr<T>::element_type>::type T1;
|
||||
T1* p1 = 0;
|
||||
detail::make_array_helper<T1> a1(size, &p1);
|
||||
detail::array_deleter<T1> d1;
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
detail::array_deleter<T1>* d2;
|
||||
d2 = get_deleter<detail::array_deleter<T1> >(s1);
|
||||
d2->construct_noinit(p1, size);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user