找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3254|回复: 2

boost 内存管理学习笔记1

[复制链接]

307

主题

228

回帖

7345

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5595 个
贡献
253 次
宅之契约
0 份
在线时间
949 小时
注册时间
2014-1-25
发表于 2015-2-24 14:20:42 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
本帖最后由 元始天尊 于 2015-2-24 15:41 编辑

    学BOOST和STL对于做应用层软件十分重要,相信不用上述2个库的C++应用软件,在未来会因为功能维护、测试、安全性、重构等问题而慢慢退出历史舞台。
    简单说下boost使用,我下的是最新版boost1.57的7z格式50M,解压出来400M,VS2013中设置包含文件目录和库目录为boost下的Include和lib即可使用。当然用的时候要添加头文件了。
    C++中可以做到完全不用指针,也不会出现内存泄漏/野指针/访问越界等问题,而JAVA,C#等语言有垃圾回收机制,虽然C++ 标准提供了auto_ptr但是并不能解决所有问题。不光是指针,资源也包括文件描述符/socket/handle/数据库等。申请忘记释放会产生未知后果。为了方便管理资源,C++程序员通常采用RAII机制(资源获取即初始化),即类构造时申请资源,类析构时释放资源。这分为2种情况:栈对象离开作用域后会自动析构没有问题,而堆对象则需手动释放,也就存在资源泄露隐患,这个可能是逻辑复杂而忘记delete或者异常导致未执行delete。即使熟练的程序员也可能未能正确使用delete。
    智能指针作用:减少因错误操作指针引起的内存泄漏/野指针/访问越界的发生,避免因异常导致程序流程的改变(不执行delete)而到处添加异常捕获代码以释放资源的劳动。正确对待指针的做法是return之前调用delete并使用try捕获所有异常,在catch中调用delete。
    标准中的auto_ptr被广泛使用,然而无法做到引用计数型智能指针。boost.smart_ptr是C++98的绝佳补充,提供了6种智能指针:scoped_ptr,scoped_array,shared_ptr,shared_array,weak_ptr,intrusive_ptr,其中shared_ptr和weak_ptr已被收入C++新标准中。他们都是轻量级对象,速度和原始指针相差不大,对于类型T也有个基本要求:~T()不能抛出异常。

先来看scoped_ptr
  1. template<class T>
  2. class scoped_ptr
  3. {
  4. private:
  5.         T* px;
  6.         scoped_ptr(scoped_ptr const&);//禁止拷贝,转让所有权
  7.         scoped_ptr& operator=(scoped_ptr const&);//禁止拷贝,转让所有权
  8. public:
  9.         explicit scoped_ptr(T* p=0);//禁止scoped_ptr p=q形式的隐式转换,强制scoped_ptr p(q)形式
  10.         ~scoped_ptr();
  11.         void reset(T* p=0);
  12.         T& operator*() const;
  13.         T* operator->() const;
  14.         T* get() const;
  15.         operator unspecified-bool-type() const;
  16.         void swap(scoped_ptr& b);
  17. };
复制代码


例:
  1. #include <boost/scoped_ptr.hpp>
  2. #include <iostream>

  3. struct Shoe { ~Shoe() { std::cout << "Buckle my shoe\n"; } };

  4. class MyClass {
  5.     boost::scoped_ptr<int> ptr;
  6.   public:
  7.     MyClass() : ptr(new int) { *ptr = 0; }
  8.     int add_one() { return ++*ptr; }
  9. };

  10. int main()
  11. {
  12.     boost::scoped_ptr<Shoe> x(new Shoe);
  13.     MyClass my_instance;
  14.     std::cout << my_instance.add_one() << '\n';
  15.     std::cout << my_instance.add_one() << '\n';
  16. }

复制代码
输出
1
2
Buckle my shoe

《Boost程序库完全开发指南:深入C++准标准库》中说道:
    scoped_ptr是针对单个元素对象的,不支持转让所有权,不支持比较操作,不支持拷贝、赋值,不能作为容器的元素(不支持拷贝和赋值),
    reset函数会删除原来的指针,再保存新指针值,如果新指针为空则scoped_ptr不持有任何指针,一般情况下reset不应该被调用——资源应该自动管理。
    不过我还是很好奇reset在大师们的手里是如何实现的:
  1.    typedef scoped_ptr<T> this_type;
  2.     void reset(T * p = 0) // never throws
  3.     {
  4.         BOOST_ASSERT( p == 0 || p != px ); // 自身不能重复赋值
  5.         this_type(p).swap(*this);
  6.     }
  7.     void swap(scoped_ptr & b) BOOST_NOEXCEPT
  8.     {
  9.         T * tmp = b.px;
  10.         b.px = px;
  11.         px = tmp;
  12.     }
复制代码

    和某些书上讲的高级指针操作一样巧妙,reset里定义了一个临时对象 this_type(p),等价于scoped_ptr<T> newobj(p),并进行swap,这里巧妙之处在于把要释放的指针(这里为原this->px=旧指针po),交换临时对象里的(newobj->px=新指针pn),交换之后自然原this->px为新指针pn,而临时对象newobj->px为要释放的指针,newobj是局部对象,reset结束后即释放,自动调用析构函数释放旧指针po。能看到这一层就说明C++基础学懂了。。。。。
    之所以reset要用swap实现除了避免异常导致资源泄漏外,同时提供了交换指针的实现,一举两得。

问题:为何常用的智能指针都是管理堆对象,而不管理栈对象?
前面已经说过,栈对象在作用域结束后自动释放,因此不需要操心,而堆对象必须手动释放因此需要额外管理

问题:为何支持第一种,而不支持第二种?例:
scoped_ptr ptr1(p1),ptr2(p2);
if(ptr1 == ptr2){}//编译失败
if(ptr1 == 0){}//编译成功
相当于operator()参数一个是scoped_ptr&,另一个是0,怎么做到区分这2种操作的?
经过研究发现2处定义:

  1. class scoped_ptr
  2. {
  3. private:
  4.     void operator==( scoped_ptr const& ) const;
  5. }

  6. template<class T> inline bool operator==( scoped_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
  7. {
  8.     return p.get() == 0;
  9. }
复制代码

    ==操作先要匹配类内操作符重载函数,再匹配全局操作符重载函数,类内操作符重载函数总是匹配失败,因为是Private的,而类外匹配ptr1==ptr2的情况,传入参数是ptr2,不属于boost::detail::sp_nullptr_t类型,而ptr1==0,0属于boost::detail::sp_nullptr_t类型。
看一下定义:typedef std::nullptr_t sp_nullptr_t;       std::nullptr_t是空指针类型,C++11引入的


再来看scoped_array
  1. namespace boost {

  2.   template<class T> class scoped_array : noncopyable {

  3.     public:
  4.       typedef T element_type;

  5.       explicit scoped_array(T * p = 0); // never throws
  6.       ~scoped_array(); // never throws

  7.       void reset(T * p = 0); // never throws

  8.       T & operator[](std::ptrdiff_t i) const; // never throws
  9.       T * get() const; // never throws
  10.      
  11.       operator unspecified-bool-type() const; // never throws

  12.       void swap(scoped_array & b); // never throws
  13.   };

  14.   template<class T> void swap(scoped_array<T> & a, scoped_array<T> & b); // never throws

  15. }
复制代码


    类似于scoped_ptr,不同的是它用来处理堆数组,而不是单个堆元素,数组操作和指针操作稍有不同,指针操作需要重载->和*,而数组需要重载[]
其他特性等同于scoped_ptr,例:
  1. #include <boost/smart_ptr.hpp>
  2. #include <algorithm>
  3. using namespace boost;
  4. using namespace std;

  5. int main()
  6. {
  7.         int *arr = new int[100];
  8.         scoped_array<int> sa(arr);
  9.         sa[10] = sa[20] + sa[30];
  10. }
复制代码


在使用动态数组时,不推荐scoped_array而推荐使用std::vector,它提支持动态增长,支持迭代器,支持STL算法


再来看shared_ptr,他是最重要的智能指针:
  1. namespace boost {

  2.   class bad_weak_ptr: public std::exception;

  3.   template<class T> class weak_ptr;

  4.   template<class T> class shared_ptr {

  5.     public:

  6.       typedef see below element_type;

  7.       shared_ptr(); // never throws
  8.       shared_ptr(std::nullptr_t); // never throws

  9.       template<class Y> explicit shared_ptr(Y * p);
  10.       template<class Y, class D> shared_ptr(Y * p, D d);
  11.       template<class Y, class D, class A> shared_ptr(Y * p, D d, A a);
  12.       template<class D> shared_ptr(std::nullptr_t p, D d);
  13.       template<class D, class A> shared_ptr(std::nullptr_t p, D d, A a);

  14.       ~shared_ptr(); // never throws

  15.       shared_ptr(shared_ptr const & r); // never throws
  16.       template<class Y> shared_ptr(shared_ptr<Y> const & r); // never throws

  17.       shared_ptr(shared_ptr && r); // never throws
  18.       template<class Y> shared_ptr(shared_ptr<Y> && r); // never throws

  19.       template<class Y> shared_ptr(shared_ptr<Y> const & r, element_type * p); // never throws

  20.       template<class Y> explicit shared_ptr(weak_ptr<Y> const & r);

  21.       template<class Y> explicit shared_ptr(std::auto_ptr<Y> & r);
  22.       template<class Y> shared_ptr(std::auto_ptr<Y> && r);

  23.       template<class Y, class D> shared_ptr(std::unique_ptr<Y, D> && r);

  24.       shared_ptr & operator=(shared_ptr const & r); // never throws
  25.       template<class Y> shared_ptr & operator=(shared_ptr<Y> const & r); // never throws

  26.       shared_ptr & operator=(shared_ptr const && r); // never throws
  27.       template<class Y> shared_ptr & operator=(shared_ptr<Y> const && r); // never throws

  28.       template<class Y> shared_ptr & operator=(std::auto_ptr<Y> & r);
  29.       template<class Y> shared_ptr & operator=(std::auto_ptr<Y> && r);

  30.       template<class Y, class D> shared_ptr & operator=(std::unique_ptr<Y, D> && r);

  31.       shared_ptr & operator=(std::nullptr_t); // never throws

  32.       void reset(); // never throws

  33.       template<class Y> void reset(Y * p);
  34.       template<class Y, class D> void reset(Y * p, D d);
  35.       template<class Y, class D, class A> void reset(Y * p, D d, A a);

  36.       template<class Y> void reset(shared_ptr<Y> const & r, element_type * p); // never throws

  37.       T & operator*() const; // never throws; only valid when T is not an array type
  38.       T * operator->() const; // never throws; only valid when T is not an array type

  39.       element_type & operator[](std::ptrdiff_t i) const; // never throws; only valid when T is an array type

  40.       element_type * get() const; // never throws

  41.       bool unique() const; // never throws
  42.       long use_count() const; // never throws

  43.       explicit operator bool() const; // never throws

  44.       void swap(shared_ptr & b); // never throws
  45.       
  46.       template<class Y> bool owner_before(shared_ptr<Y> const & rhs) const; // never throws
  47.       template<class Y> bool owner_before(weak_ptr<Y> const & rhs) const; // never throws
  48.   };
  49. }
复制代码



    shared_ptr实现的是引用计数型的智能指针,可以自由拷贝和赋值,可以作为迭代器,在任意地方共享,直到没有代码使用它时,此时引用计数为0,删除包装对象。
也就是说,shared_ptr支持拷贝,赋值,比较等操作。
回复

使用道具 举报

307

主题

228

回帖

7345

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5595 个
贡献
253 次
宅之契约
0 份
在线时间
949 小时
注册时间
2014-1-25
 楼主| 发表于 2015-2-24 20:27:23 | 显示全部楼层
本帖最后由 元始天尊 于 2015-2-24 20:46 编辑

shared_ptr更灵活,如下例:

  1. #include <boost/shared_ptr.hpp>
  2. #include <boost/make_shared.hpp>
  3. #include <vector>
  4. #include <iostream>
  5. using namespace boost;
  6. using namespace std;
  7. #include <malloc.h>

  8. class abstract
  9. {
  10. public:
  11.         virtual void f() = 0;
  12. protected:
  13.         virtual ~abstract(){};
  14. };
  15. class impl :public abstract
  16. {
  17. public:
  18.         virtual void f(){ cout << "f" << endl; };
  19. };

  20. void deleter(impl* ptr)
  21. {
  22.         cout << "deleter" << endl;
  23. }

  24. boost::shared_ptr<abstract> create()
  25. {
  26.         return boost::shared_ptr<abstract>(new impl);
  27. }

  28. int main()
  29. {
  30.         //应用于标准容器
  31.         vector<boost::shared_ptr<int>> v(10);
  32.         int i = 0;
  33.         for (vector<boost::shared_ptr<int>>::iterator pos = v.begin(); pos != v.end(); ++pos)
  34.         {
  35.                 (*pos) = boost::make_shared<int>(++i);
  36.                 cout << *(*pos) << ", ";
  37.         }

  38.         //应用于桥接模式
  39.         class sample
  40.         {
  41.         private:
  42.                 class impl
  43.                 {
  44.                 public:
  45.                         void print(){ cout << "bridge" << endl; };
  46.                 };
  47.                 boost::shared_ptr<impl> p;
  48.         public:
  49.                 void print(){ p->print(); };//交给impl去做
  50.         };
  51.         boost::shared_ptr<sample>()->print();

  52.         //应用于工厂模式
  53.         create()->f();

  54.         //删除器
  55.         boost::shared_ptr<impl> pp(new impl, deleter);

  56. }
复制代码


shared_array和scoped_array一样不推荐使用
weak_array配合shared_array,提供use_count函数观测后者引用计数,没有重载*和->,提供lock获得资源引用进行操作
shared_array和scoped_array一样不推荐使用
weak_array配合shared_array,提供use_count函数观测后者引用计数,没有重载*和->,提供lock获得资源引用进行操作

  1. #include <boost/shared_ptr.hpp>
  2. #include <boost/weak_ptr.hpp>
  3. #include <vector>
  4. #include <iostream>
  5. using namespace boost;
  6. using namespace std;

  7. int main()
  8. {
  9.         boost::shared_ptr<int> sp(new int(10));
  10.         boost::weak_ptr<int> wp(sp);
  11.         if (!wp.expired())
  12.         {
  13.                 boost::shared_ptr<int> sp2 = wp.lock();
  14.                 assert(wp.use_count() == 2);
  15.         }
  16. }
复制代码


weak_ptr可以让类产生的this指针自动管理自身
  1. #include <boost/shared_ptr.hpp>
  2. #include <boost/enable_shared_from_this.hpp>
  3. #include <boost/make_shared.hpp>
  4. #include <vector>
  5. #include <iostream>
  6. using namespace boost;
  7. using namespace std;

  8. class test :public boost::enable_shared_from_this<test>
  9. {
  10. public:
  11.         void printf(){ cout << "self_shared!" << endl; }
  12. };

  13. int main()
  14. {
  15.         boost::shared_ptr<test> sp = boost::make_shared<test>();
  16.         sp->printf();
  17.         boost::shared_ptr<test> p = sp->shared_from_this();
  18.         p->printf();
  19. }
复制代码


回复 赞! 靠!

使用道具 举报

307

主题

228

回帖

7345

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5595 个
贡献
253 次
宅之契约
0 份
在线时间
949 小时
注册时间
2014-1-25
 楼主| 发表于 2015-2-24 22:00:30 | 显示全部楼层
boost内存管理学习笔记2
1.什么是内存池?
Pool allocation is a memory allocation scheme that is very fast, but limited in its usage.
池是一种内存分配机制,可以在某些条件下快速分配内存。
2.为什么要使用内存池?
Using Pools gives you more control over how memory is used in your program. For example, you could have a situation where you want to allocate a bunch of small objects at one point, and then reach a point in your program where none of them are needed any more. Using pool interfaces, you can choose to run their destructors or just drop them off into oblivion; the pool interface will guarantee that there are no system memory leaks.
使用池可以更好地管理程序内存使用,例如,当你想在某处代码中分配一小块对象,而到达另一处代码时不再需要这块内存,就可以使用池。使用池接口后,可以运行析构函数或者直接无视他们,池接口会保证不会产生内存泄露。
3.什么时候需要使用内存池?
Pools are generally used when there is a lot of allocation and deallocation of small objects. Another common usage is the situation above, where many objects may be dropped out of memory.
In general, use Pools when you need a more efficient way to do unusual memory control.
池通常用于处理分配释放大量小对象,或者如上面那种情况,很多对象不再使用,不需要存在于内存。通常用池可以更高效地控制内存。
4.怎样选择池分配器?
pool_allocator is a more general-purpose solution, geared towards efficiently servicing requests for any number of contiguous chunks.
fast_pool_allocator is also a general-purpose solution but is geared towards efficiently servicing requests for one chunk at a time; it will work for contiguous chunks, but not as well as pool_allocator.
If you are seriously concerned about performance, use fast_pool_allocator when dealing with containers such as std::list, and use pool_allocator when dealing with containers such as std::vector.
pool_allocator是最常见的方式,可以高效处理任意数量连续数据块分配请求,fast_pool_allocator也较为通用,适用于每次分配一块内存,分配连续块没有pool_allocator性能好。如果非常关注性能,可以在std::list类容器中使用fast_pool_allocator,std::vector类容器中使用pool_allocator

pool用于普通数据类型分配,原型:
  1. struct default_user_allocator_new_delete
  2. {
  3.   typedef std::size_t size_type;
  4.   typedef std::ptrdiff_t difference_type;

  5.   static char * malloc(const size_type bytes)
  6.   { return new (std::nothrow) char[bytes]; }
  7.   static void free(char * const block)
  8.   { delete [] block; }
  9. };

  10. struct default_user_allocator_malloc_free
  11. {
  12.   typedef std::size_t size_type;
  13.   typedef std::ptrdiff_t difference_type;

  14.   static char * malloc(const size_type bytes)
  15.   { return reinterpret_cast<char *>(std::malloc(bytes)); }
  16.   static void free(char * const block)
  17.   { std::free(block); }
  18. };

  19. template <typename UserAllocator = default_user_allocator_new_delete>
  20. class pool
  21. {
  22.   private:
  23.     pool(const pool &);
  24.     void operator=(const pool &);

  25.   public:
  26.     typedef UserAllocator user_allocator;
  27.     typedef typename UserAllocator::size_type size_type;
  28.     typedef typename UserAllocator::difference_type difference_type;

  29.     explicit pool(size_type requested_size);
  30.     ~pool();

  31.     bool release_memory();
  32.     bool purge_memory();

  33.     bool is_from(void * chunk) const;
  34.     size_type get_requested_size() const;

  35.     void * malloc();
  36.     void * ordered_malloc();
  37.     void * ordered_malloc(size_type n);

  38.     void free(void * chunk);
  39.     void ordered_free(void * chunk);
  40.     void free(void * chunks, size_type n);
  41.     void ordered_free(void * chunks, size_type n);
  42. };
复制代码

  1. void func()
  2. {
  3.   boost::pool<> p(sizeof(int));
  4.   for (int i = 0; i < 10000; ++i)
  5.   {
  6.     int * const t = p.malloc();
  7.     ... // Do something with t; don't take the time to free() it.
  8.   }
  9. } // on function exit, p is destroyed, and all malloc()'ed ints are implicitly freed.
复制代码


对象池object_pool用于分配对象,和普通类型不同支出在于自动构造析构,原型:
  1. template <typename ElementType, typename UserAllocator = default_user_allocator_new_delete>
  2. class object_pool
  3. {
  4.   private:
  5.     object_pool(const object_pool &);
  6.     void operator=(const object_pool &);

  7.   public:
  8.     typedef ElementType element_type;
  9.     typedef UserAllocator user_allocator;
  10.     typedef typename pool<UserAllocator>::size_type size_type;
  11.     typedef typename pool<UserAllocator>::difference_type difference_type;

  12.     object_pool();
  13.     ~object_pool();

  14.     element_type * malloc();
  15.     void free(element_type * p);
  16.     bool is_from(element_type * p) const;

  17.     element_type * construct();
  18.     // other construct() functions
  19.     void destroy(element_type * p);
  20. };
复制代码
  1. void func()
  2. {
  3.   boost::object_pool<X> p;
  4.   for (int i = 0; i < 10000; ++i)
  5.   {
  6.     X * const t = p.malloc();
  7.     ... // Do something with t; don't take the time to free() it.
  8.   }
  9. } // on function exit, p is destroyed, and all destructors for the X objects are called.
复制代码


单例池,加锁池,确保全局只有一个singleton_pool对象p,有以下特点:
        线程安全,所有访问p的行为同步化
        确保p使用前已经构造
       
  1. typedef boost::singleton_pool<MyPoolTag, sizeof(int)> my_pool;
  2. void func()
  3. {
  4.   for (int i = 0; i < 10000; ++i)
  5.   {
  6.     int * const t = my_pool::malloc();
  7.     ... // Do something with t; don't take the time to free() it.
  8.   }
  9.   // Explicitly free all malloc()'ed ints.
  10.   my_pool::purge_memory();
  11. }
复制代码
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-29 14:56 , Processed in 0.038876 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表