返回   cpper编程论坛 > C/CPP/TMP/GP
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2007-05-10
初级会员
 
注册日期: 2007-03-22
帖子: 15
graphicboy 正向着好的方向发展
默认 自定义stl 常用容器的内存分配

突发其想,也是预谋已久,把我想到方法贴出来和大家分享,有兴趣看看,水平有限,多指教。
代码:
// TEMPLATE FUNCTION _Allocate template<class _Ty> inline _Ty _FARQ *POD_Allocate(_SIZT _Count, _Ty _FARQ *) { // allocate storage for _Count elements of type _Ty return ((_Ty _FARQ *)appMemAlloc( _Count * sizeof (_Ty), typeid(_Ty).name(), __FILE__, __LINE__ )); } // TEMPLATE FUNCTION _Construct template<class _T1, class _T2> inline void POD_Construct(_T1 _FARQ *_Ptr, const _T2& _Val) { // construct object at _Ptr with value _Val new ((void _FARQ *)_Ptr) _T1(_Val); } // TEMPLATE FUNCTION _Destroy template<class _Ty> inline void POD_Destroy(_Ty _FARQ *_Ptr) { // destroy POD object at _Ptr (do nothing) } template <class _Ty> class POD_allocator : public _Allocator_base<_Ty> { public: typedef _Allocator_base<_Ty> _Mybase; typedef typename _Mybase::value_type value_type; typedef value_type _FARQ *pointer; typedef value_type _FARQ& reference; typedef const value_type _FARQ *const_pointer; typedef const value_type _FARQ& const_reference; typedef _SIZT size_type; typedef _PDFT difference_type; public: template<class _Other> struct rebind { // convert an allocator<_Ty> to an allocator <_Other> typedef POD_allocator<_Other> other; }; public: pointer address(reference _Val) const { // return address of mutable _Val return (&_Val); } const_pointer address(const_reference _Val) const { // return address of nonmutable _Val return (&_Val); } pointer allocate(size_type _Count) { // allocate array of _Count elements return (POD_Allocate(_Count, (pointer)0)); } pointer allocate(size_type _Count, const void _FARQ *) { // allocate array of _Count elements, ignore hint return (allocate(_Count)); } void deallocate(pointer _Ptr, size_type) { // deallocate object at _Ptr, ignore size appMemFree(_Ptr); } void construct(pointer _Ptr, const _Ty& _Val) { // construct object at _Ptr with value _Val POD_Construct(_Ptr, _Val); } void destroy(pointer _Ptr) { // destroy object at _Ptr POD_Destroy(_Ptr); } _SIZT max_size() const { // estimate maximum array size _SIZT _Count = (_SIZT)(-1) / sizeof (_Ty); return (0 < _Count ? _Count : 1); } public: POD_allocator() { // construct default allocator (do nothing) } POD_allocator(const POD_allocator<_Ty>&) { // construct by copying (do nothing) } template<class _Other> POD_allocator(const POD_allocator<_Other>&) { // construct from a related allocator (do nothing) } template<class _Other> POD_allocator<_Ty>& operator=(const POD_allocator<_Other>&) { // assign from a related allocator (do nothing) return (*this); } }; // allocator TEMPLATE OPERATORS template<class _Ty, class _Other> inline bool operator==(const POD_allocator<_Ty>&, const POD_allocator<_Other>&) { // test for allocator equality (always true) return (true); } template<class _Ty, class _Other> inline bool operator!=(const POD_allocator<_Ty>&, const POD_allocator<_Other>&) { // test for allocator inequality (always false) return (false); } // CLASS allocator<void> template<> class _CRTIMP2 POD_allocator<void> { // generic allocator for type void public: typedef void _Ty; typedef _Ty _FARQ *pointer; typedef const _Ty _FARQ *const_pointer; typedef _Ty value_type; template<class _Other> struct rebind { // convert an allocator<void> to an allocator <_Other> typedef POD_allocator<_Other> other; }; POD_allocator() { // construct default allocator (do nothing) } POD_allocator(const POD_allocator<_Ty>&) { // construct by copying (do nothing) } template<class _Other> POD_allocator(const POD_allocator<_Other>&) { // construct from related allocator (do nothing) } template<class _Other> POD_allocator<_Ty>& operator=(const POD_allocator<_Other>&) { // assign from a related allocator (do nothing) return (*this); } }; // TEMPLATE FUNCTION _Destroy_range template<class _Ty> inline void _Destroy_range(_Ty *_First, _Ty *_Last, POD_allocator<_Ty>& _Al) { // destroy [_First, _Last), POD type (do nothing) } //! This macro help to define PodVector which will not call dtor when elements destroy #ifdef __USE_CRT_ALLOCATOR__ # define PodVector(_Ty) std::vector< _Ty > #else # define PodVector(_Ty) std::vector< _Ty, std::POD_allocator<_Ty> > #endif //! This macro help to define PodList which will not call dtor when elements destroy #ifdef __USE_CRT_ALLOCATOR__ # define PodList(_Ty) std::list< _Ty > #else # define PodList(_Ty) std::list< _Ty, std::POD_allocator<_Ty> > #endif //! This macro help to define PodMap which will not call dtor when elements destroy #ifdef __USE_CRT_ALLOCATOR__ # define PodMap(_K, _Ty) std::map< _K, _Ty, std::less<_K> > #else # define PodMap(_K, _Ty) std::map< _K, _Ty, std::less<_K>, std::POD_allocator<_Ty> > #endif
代码:
// TEMPLATE FUNCTION _Allocate template<class _Ty> inline _Ty _FARQ *OBJ_Allocate(_SIZT _Count, _Ty _FARQ *) { // allocate storage for _Count elements of type _Ty return ((_Ty _FARQ *)appMemAlloc( _Count * sizeof (_Ty), typeid(_Ty).name(), __FILE__, __LINE__ )); } // TEMPLATE FUNCTION _Construct template<class _T1, class _T2> inline void OBJ_Construct(_T1 _FARQ *_Ptr, const _T2& _Val) { // construct object at _Ptr with value _Val new ((void _FARQ *)_Ptr) _T1(_Val); } // TEMPLATE FUNCTION _Destroy template<class _Ty> inline void OBJ_Destroy(_Ty _FARQ *_Ptr) { // destroy OBJ object at _Ptr _DESTRUCTOR(_Ty, _Ptr); } template <class _Ty> class OBJ_allocator : public _Allocator_base<_Ty> { public: typedef _Allocator_base<_Ty> _Mybase; typedef typename _Mybase::value_type value_type; typedef value_type _FARQ *pointer; typedef value_type _FARQ& reference; typedef const value_type _FARQ *const_pointer; typedef const value_type _FARQ& const_reference; typedef _SIZT size_type; typedef _PDFT difference_type; public: template<class _Other> struct rebind { // convert an allocator<_Ty> to an allocator <_Other> typedef OBJ_allocator<_Other> other; }; public: pointer address(reference _Val) const { // return address of mutable _Val return (&_Val); } const_pointer address(const_reference _Val) const { // return address of nonmutable _Val return (&_Val); } pointer allocate(size_type _Count) { // allocate array of _Count elements return (OBJ_Allocate(_Count, (pointer)0)); } pointer allocate(size_type _Count, const void _FARQ *) { // allocate array of _Count elements, ignore hint return (allocate(_Count)); } void deallocate(pointer _Ptr, size_type) { // deallocate object at _Ptr, ignore size appMemFree(_Ptr); } void construct(pointer _Ptr, const _Ty& _Val) { // construct object at _Ptr with value _Val OBJ_Construct(_Ptr, _Val); } void destroy(pointer _Ptr) { // destroy object at _Ptr OBJ_Destroy(_Ptr); } _SIZT max_size() const { // estimate maximum array size _SIZT _Count = (_SIZT)(-1) / sizeof (_Ty); return (0 < _Count ? _Count : 1); } public: OBJ_allocator() { // construct default allocator (do nothing) } OBJ_allocator(const OBJ_allocator<_Ty>&) { // construct by copying (do nothing) } template<class _Other> OBJ_allocator(const OBJ_allocator<_Other>&) { // construct from a related allocator (do nothing) } template<class _Other> OBJ_allocator<_Ty>& operator=(const OBJ_allocator<_Other>&) { // assign from a related allocator (do nothing) return (*this); } }; // allocator TEMPLATE OPERATORS template<class _Ty, class _Other> inline bool operator==(const OBJ_allocator<_Ty>&, const OBJ_allocator<_Other>&) { // test for allocator equality (always true) return (true); } template<class _Ty, class _Other> inline bool operator!=(const OBJ_allocator<_Ty>&, const OBJ_allocator<_Other>&) { // test for allocator inequality (always false) return (false); } // CLASS allocator<void> template<> class _CRTIMP2 OBJ_allocator<void> { // generic allocator for type void public: typedef void _Ty; typedef _Ty _FARQ *pointer; typedef const _Ty _FARQ *const_pointer; typedef _Ty value_type; template<class _Other> struct rebind { // convert an allocator<void> to an allocator <_Other> typedef OBJ_allocator<_Other> other; }; OBJ_allocator() { // construct default allocator (do nothing) } OBJ_allocator(const OBJ_allocator<_Ty>&) { // construct by copying (do nothing) } template<class _Other> OBJ_allocator(const OBJ_allocator<_Other>&) { // construct from related allocator (do nothing) } template<class _Other> OBJ_allocator<_Ty>& operator=(const OBJ_allocator<_Other>&) { // assign from a related allocator (do nothing) return (*this); } }; // TEMPLATE FUNCTION _Destroy_range template<class _Ty> inline void _Destroy_range(_Ty *_First, _Ty *_Last, OBJ_allocator<_Ty>& _Al) { // destroy [_First, _Last), OBJ type for (; _First != _Last; ++_First) _Al.destroy(_First); } //! This macro help to define PodVector which will call dtor when elements destroy #ifdef __USE_CRT_ALLOCATOR__ # define ObjVector(_Ty) std::vector< _Ty > #else # define ObjVector(_Ty) std::vector< _Ty, std::OBJ_allocator<_Ty> > #endif //! This macro help to define ObjList which will call dtor when elements destroy #ifdef __USE_CRT_ALLOCATOR__ # define ObjList(_Ty) std::list< _Ty > #else # define ObjList(_Ty) std::list< _Ty, std::OBJ_allocator<_Ty> > #endif //! This macro help to define ObjMap which will call dtor when elements destroy #ifdef __USE_CRT_ALLOCATOR__ # define ObjMap(_K, _Ty) std::map< _K, _Ty, std::less<_K> > #else # define ObjMap(_K, _Ty) std::map< _K, _Ty, std::less<_K>, std::OBJ_allocator<_Ty> > #endif
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2007-05-11
Samark 的头像
普通会员
 
注册日期: 2006-03-07
帖子: 84
Samark 正向着好的方向发展
默认 回复: 自定义stl 常用容器的内存分配

支持 :)
用stl 的vector list做池速度还是慢不少,考虑性能最好用自己设计链表或者数组。写个能高效分配内存内存池跟配套的allocator吧,应该是需要应用的场合挺多的,虽然boost之类的库都有, 但是很不方便, 能有几个简单的文件直接include过来用的话还是相当实用啊

此帖于 2007-05-11 08:56 PM 被 Samark 编辑.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为格林尼治时间 +9。现在的时间是 08:41 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2009,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0