我已经获得了一个自定义分配器,它实现了它自己的分配策略,它自己的malloc/free等等.现在,我被要求使用这个自定义分配器和一个STL容器(它是一个向量或其他).我创建了一个类,比如说my_stdAllocator,这是一个符合ISO C++标准的接口.通过这个类我调用我的分配器的方法.例如:
template <class T>
class my_stdAllocator {
// ...other required stuff...
// allocate 'num' elements of type T
pointer allocate(size_type num, const_pointer = 0) {
return static_cast<T*>(MYAllocator::instance()->malloc(num));
}
// deallocate memory at 'p'
void deallocate(pointer p, size_type num=0) {
MYAllocator::instance()->free(p);
}
// initialize allocated memory at 'p' with value 'value'
void construct(pointer p, const T& value) {
::new ((void*)p) T(value);
}
// destroy elements of initialized memory at p
void destroy(pointer p) {
p->~T();
}
// ...other required stuff...
} // end my_stdAllocator
Run Code Online (Sandbox Code Playgroud)
自定义分配器通常像魅力一样工作:它已经过广泛测试,它肯定会提高性能,限制碎片等.当我使用它作为stl容器的分配器(比如一个向量)时,它有这种奇怪的行为它有时可以正常工作,而其他时候崩溃时会出现段错误.
举个例子,它使用以下向量正确分配和释放char:
typedef char TP;
int main(int argc, char* argv[]) {
std::vector<TP, my_stdAllocator<TP> > vec;
std::string s ("Whatever string, no matter how long...");
std::string::iterator it;
for (it=s.begin(); it<s.end(); ++it)
vec.push_back(*it);
...
Run Code Online (Sandbox Code Playgroud)
在矢量中按"手动"数字时可以
typedef double TP;
int main(int argc, char* argv[]) {
std::vector<TP, my_stdAllocator<TP> > vec;
// "manual" push_back
vec.push_back(3.2);
vec.push_back(6.4);
vec.push_back(9.6);
vec.push_back(12.8);
vec.push_back(15.1);
vec.push_back(18.3);
vec.push_back(21.5);
...
Run Code Online (Sandbox Code Playgroud)
通过循环插入元素时,它会因Segmentation故障而停止:
typedef int TP;
int main(int argc, char* argv[]) {
std::vector<TP, ff_stdAllocatorInst<TP> > vec;
for(unsigned int i=0; i<size; ++i)
vec.push_back( (TP) i );
...
Run Code Online (Sandbox Code Playgroud)
当为至少一定数量的元素保留空间时,它就像一个魅力:
typedef int TP;
int main(int argc, char* argv[]) {
std::vector<TP, ff_stdAllocatorInst<TP> > vec;
vec.reserve(size);
for(unsigned int i=0; i<size+150; ++i)
vec.push_back( (TP) i );
...
Run Code Online (Sandbox Code Playgroud)
请注意,使用像这样的新位置时,上面提到的段错误不会发生:
void *p = MYAllocator::instance()->malloc(size);
std::vector<TP> *vec = new (p) std::vector<TP>
for(unsigned int i=0; i<size; ++i)
vec->push_back( (TP) i );
...
Run Code Online (Sandbox Code Playgroud)
正如我已经说过的,自定义分配器已经过测试并且工作正常.我的类是C++标准和自定义分配器之间的简单接口.我已经尝试用gdb调试它但它没有帮助:底层分配器没问题,我的代码中一定有一些错误但我无法理解错误!
Kur*_*man 12
在调用自定义分配器的malloc函数时,需要乘以要分配的对象的大小:
pointer allocate(size_type num, const_pointer = 0) {
return static_cast<T*>(MYAllocator::instance()->malloc(num*sizeof(T)));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1134 次 |
| 最近记录: |