首页 > 编程知识 正文

c++ condition_variable,cat /proc/cpuinfo

时间:2023-05-03 07:45:45 阅读:162453 作者:3979

STL内存分配器- -分配器一、STL内存分配器二、STL分配器

另一方面,STL内存定位符(allocator ) )是c标准库的组件,并且主要用于处理所有给定的容器(vector、list、map等等)的内存分配和释放。 C标准库提供了默认使用的通用分发程序std:allocator,但开发人员可以自定义分发程序。

GNU STL除了默认分配器外,还提供了__pool_alloc、__mt_alloc、array_allocator、malloc_allocator存储器分配器。

__pool_alloc :SGI内存池插头

__mt_alloc :多线程内存池分配器

array_allocator :全局内存分配,不释放分配,交给系统进行释放

malloc _ allocator :使用STD :3360 malloc和std:free打包

二. STL allocator 1、STL allocator简介

new分配内存以执行对象构造函数,而delete执行对象构造函数以释放内存。 通过分离内存分配和对象结构,可以先分配较大的内存,然后仅在需要时实际执行对象结构函数。

STL为头文件memory提供了allocator类,以隔离分配和对象结构,从而提高性能和内存管理能力。 要定义allocator对象,必须指定allocator可以分配的对象类型。 当分配内存时,allocator将根据给定的对象类型确定合适的内存大小和对齐方式。

template typename _ tpclassallocator : public _ allocator _ base _ TP { public 3360 typedefsize _ tsize _ type; typedefptrdiff _ tdifference _ type; typedef _Tp* pointer; typedef const _Tp* const_pointer; typedef _Tp reference; typedef const _Tp const_reference; typedef _Tp value_type; template typename _ tp1 struct rebind { typedef allocator _ tp1 other; (; allocator(throw ) allocator (const allocator _ _ a ) throw ) ) :__allocator_base_TP ) }} throw () 根据C标准,STL中分配器的对外接口、成员变量都是相同的,但在接口内部的实现上存在差异。

allocator的实现在模板类new_allocator中如下所示:

template typename _ TP classnew _ allocator { public : typedefsize _ tsize _ type; typedefptrdiff _ tdifference _ type; typedef _Tp* pointer; typedef const _Tp* const_pointer; typedef _Tp reference; typedef const _Tp const_reference; typedef _Tp value_type; template typename _ tp1 struct rebind { typedef new _ allocator _ tp1 other; (; new _ allocator (_ glibc xx _ use _ no except { } new _ allocator ) constnew_allocator ) _glibcxx_se_noexcept

t _GLIBCXX_NOEXCEPT { return std::__addressof(__x); } const_pointer address(const_reference __x) const _GLIBCXX_NOEXCEPT { return std::__addressof(__x); } // NB: __n is permitted to be 0. The C++ standard says nothing // about what the return value is when __n == 0. pointer allocate(size_type __n, const void* = static_cast<const void*>(0)) {if (__n > this->max_size()) std::__throw_bad_alloc();return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); } // __p is not permitted to be a null pointer. void deallocate(pointer __p, size_type) { ::operator delete(__p); } size_type max_size() const _GLIBCXX_USE_NOEXCEPT { return size_t(-1) / sizeof(_Tp); }#if __cplusplus >= 201103L template<typename _Up, typename... _Args>voidconstruct(_Up* __p, _Args&&... __args){ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } template<typename _Up>voiddestroy(_Up* __p) { __p->~_Up(); }#else // _GLIBCXX_RESOLVE_LIB_DEFECTS // 402. wrong new expression in [some_] allocator::construct void construct(pointer __p, const _Tp& __val) { ::new((void *)__p) _Tp(__val); } void destroy(pointer __p) { __p->~_Tp(); }#endif }; template<typename _Tp> inline bool operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&) { return true; } template<typename _Tp> inline bool operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&) { return false; }_GLIBCXX_END_NAMESPACE_VERSION} // namespace

STL中容器默认分配器为std::allocator<_Tp>,内存分配和释放的接口allocate和deallocate内部实现只是将::operator new和::operator delete进行封装,没用做特殊处理。

3、STL allocator实例

4、自定义allocator
实现Allocator只需要实现allocate和deallocate,来实现自己的内存分配策略。

template<class T >inline T*_allocate(ptrdiff_t num,T*){ std::cout<<"_allocate"<<endl; return static_cast<T*>(::operator new(num*sizeof(T)));}template <class T>inline void _deallocate(T* buff){ std::cout<<"_deallocate"<<endl; ::operator delete(buff);}template <class T ,class U>inline void _construct(T* p,const U& value){ new(p)T(value);}template<class T>inline void _destory(T* p){ p->~T();}template<typename T>class MyAlloctor{ public: typedef T value_type; typedef T* pointer; typedef T& reference; typedef const T* const_pointer; typedef const T& const_reference; typedef size_t size_type; typedef ptrdiff_t difference_type; template<typename _Tp1>struct rebind{ typedef MyAlloctor<_Tp1> other; }; MyAlloctor()=default; ~MyAlloctor()=default; pointer address(reference x){ return static_cast<pointer>(&x); } const_pointer address(const_reference x){ return static_cast<const_pointer>(&x); } pointer allocate(size_type _n,const void* hit=0 ){ return _allocate(_n,(pointer)0); } void deallocate(pointer p,size_type _n){ _deallocate(p); } size_type max_size()const throw(){ return static_cast<size_type>(INT_MAX/sizeof(T)); } void construct(pointer p,const_reference value){ _construct(p,value); } void destroy(pointer p){ _destory(p); }};

测试代码:

#include <iostream>#include <string>#include <vector>#include <memory>using namespace std;using namespace __gnu_cxx;//引入new_allocatorclass base{ string name; int value;public: base():name("test"),value(0) { cout << "base ctor" << endl; } explicit base(int v):name("explicit name"),value(v){cout<<"base ctor value="<<value<<endl;} virtual ~base() { cout << "base dtor" << endl; } base(const base& value) { cout << "Copy ctor" << endl; } base(base&& value) { cout << "move ctor" << endl; } void who(void){cout<<name<<" value= "<<value<<endl;}};int main(void){new_allocator<base>alloc_test;new_allocator<base>test;base* p = alloc_test.allocate(3);cout<<"test1 start"<<endl;cout<<"size of base="<<sizeof(base)<<endl;cout<<"size of *p="<<sizeof(*p)<<endl;p->who();//(p+1)->who();cout<<"test1 endn"<<endl;cout<<"test2 start"<<endl;//test.construct(p,base(1));//test.construct(p+1,base(2));//test.construct(p+2,base(3));alloc_test.construct(p,base(1));alloc_test.construct(p+1,base(2));alloc_test.construct(p+2,base(3));p->who();(p+1)->who();(*p).who();alloc_test.deallocate(p, 3);getchar();}-----------------------------------// output://todobase dtor //析构对象(仍然占有空间)test deallocate//删除指针,释放空间。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。