标签:
template< class T, calss... Args >
shared_ptr< T> make_shared(Args && ... args);
shared_ptr< string > sps(new string("smart"));
assert(sps->size() == 5);
shared_ptr< string> sp = make_shared< string > ("make_shared");
shared_ptr< vector< int >> spv = make_shared< vector< int >> (10,2);
assert(spv->size() == 10);
#include < boost/make_shared.hpp>
int main()
{
typedef vector< shared_ptr< int>> vs;
vs v(10);
int i=0;
for (vs::iterator pos = v.begin(); pos != v.end(); ++pos)
{
(*pos) = make_shared< int>(++i);
cout << *(*pos) << ",";
}
cout << endl;
shared_ptr < int > p = v[9];
*p = 100;
cout << *v[9] << endl;
}
int testWeakPtr()
{
boost::shared_ptr<int> sp(new int(10));
assert(sp.use_count() == 1);
boost::weak_ptr<int> wp(sp);
assert(wp.use_count() == 1);
if (!wp.expired())
{
boost::shared_ptr<int> sp2 = wp.lock();
*sp2 = 100;
assert(sp2.use_count() == 2);
assert(wp.use_count() == 2);
}
assert(wp.use_count() == 1);
sp.reset();
assert(wp.expired());
assert(!wp.lock());
return 0;
}
// 循环引用的情形
class node
{
public:
boost::shared_ptr<node> next;
~node()
{
cout << "delete node" << endl;
}
};
int testWeakPtrRecycleWrong()
{
auto n1 = boost::make_shared<node>();
auto n2 = boost::make_shared<node>();
n1->next = n2;
n2->next = n1;
assert(n1.use_count() == 2);
assert(n2.use_count() == 2);
return 0; // 循环引用,析构异常,程序退出时仍未析构
}
// 避免循环引用的情形,主要就是node里的shared_ptr换成weak_ptr
class node1
{
public:
boost::weak_ptr<node1> next;
~node1()
{
cout << "delete node1" << endl;
}
};
int testWeakPtrRecycleRight()
{
auto n1 = boost::make_shared<node1>();
auto n2 = boost::make_shared<node1>();
n1->next = n2;
n2->next = n1;
assert(n1.use_count() == 1);
assert(n2.use_count() == 1);
if (!n1->next.expired())
{
// 调用lock()获得强引用,计数加1
auto n3 = n1->next.lock();
assert(n2.use_count() == 2);
}
assert(n2.use_count() == 1);
return 0;
}
int testPool()
{
pool<> pl(sizeof(int));
int *p = static_cast<int*>(pl.malloc()); // void*->int*
assert(pl.is_from(p));
pl.free(p);
for (int i = 0; i < 100;i++)
{
pl.ordered_malloc(10);
}
return 0;
}
struct demo_class
{
public:
int a, b, c;
demo_class(int x = 1, int y = 2, int z = 3) : a(x), b(y), c(z) {}
~demo_class()
{
cout << "destruct" << endl;
}
};
int testObjPool()
{
object_pool<demo_class> pl;
demo_class *p = pl.malloc();
assert(pl.is_from(p));
// malloc 创建时内存并未初始化
assert(p->a!= 1 || p->b != 2 || p->c != 3);
p = pl.construct();
// boost自带的construct只能支持3个及以内的参数调用
p = pl.construct(7, 8, 9);
assert(p->a == 7);
object_pool<string> pls;
for (int i = 0; i < 10; i++)
{
string *ps = pls.construct("hello object_pool");
cout << *ps << endl;
}
return 0;
}
析构会调用三次,malloc和两次construct均需要调用析构。是object_pool判断出了作用域自动调整的。
struct pool_tag{
int tag;
};
typedef singleton_pool<pool_tag, sizeof(pool_tag*)> spl;
int testSingletonPool()
{
pool_tag **p = (pool_tag **)spl::malloc();
assert(spl::is_from(p));
pool_tag ptag;
ptag.tag = 3;
*p = &ptag;
cout << "testSingletonPool : " << (*p)->tag << endl;
spl::release_memory();
return 0;
}
标签:
原文地址:http://blog.csdn.net/lonelyrains/article/details/50966713