c – 用于STL的TCMalloc分配器
发布时间:2020-12-16 09:33:22 所属栏目:百科 来源:网络整理
导读:我想将TCMalloc与STL容器一起使用,所以我需要一个用TCMalloc构建的分配器(就像带有TBB malloc的tbb_allocator).我找不到任何东西 TCMalloc documentation(如果它被称为文档).所以我开始探索头文件并找到一个名为STL_Allocator的类.但有些事情对我来说并不清
我想将TCMalloc与STL容器一起使用,所以我需要一个用TCMalloc构建的分配器(就像带有TBB malloc的tbb_allocator).我找不到任何东西
TCMalloc documentation(如果它被称为文档).所以我开始探索头文件并找到一个名为STL_Allocator的类.但有些事情对我来说并不清楚.来自stl_allocator.h的报价:
// Generic allocator class for STL objects // that uses a given type-less allocator Alloc,which must provide: // static void* Alloc::Allocate(size_t size); // static void Alloc::Free(void* ptr,size_t size); // // STL_Allocator<T,MyAlloc> provides the same thread-safety // guarantees as MyAlloc. // // Usage example: // set<T,less<T>,STL_Allocator<T,MyAlloc> > my_set; // CAVEAT: Parts of the code below are probably specific // to the STL version(s) we are using. // The code is simply lifted from what std::allocator<> provides. 而STL_Allocator模板类的定义是: template <typename T,class Alloc> class STL_Allocator { //... } 我不知道Alloc的论点是什么.我是否应该为一些内存分配函数编写一个包装类?有人用过TCMalloc吗? 解决方法
TCMalloc中的STL_Allocator类是一个适配器类:你
使用(更简单的)Alloc类来提供Allocate和 你引用的评论中的免费方法,以及-voila-你得到了 实现所有要求的类 STL allocator(点击链接获取介绍性文章 什么STL分配器以及如何实现一个). 使用示例包括Null Set的simple_alloc类 但请注意,定义STL_Allocator的头文件是 也就是说,请注意,不需要使用自定义分配器 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |