STL设计——allocator以及iterator设计(一)
allocator设计
为了简化,不考虑内存池的问题,即不考虑SGI的第二配接器,只考虑第一层配接器:
allocator
的设计整体上可以分为两部分:
- 申请内存(malloc)或释放(free)
- 调用构造(construct)或析构(destruct)
构造函数的设计
实现几种构造函数的重载形式:
(1)接受一个模板类型的指针
template<class T>
inline void construct(T* p) {
new(p) T();
}
(2) 接受一个指向T1的指针以及val
template<class T1,class T2>
inline void construct(T1* p, const T2& val) {
//构造函数的第一个版本,接受一个指向T1的指针以及val,调用T1::T1(val)
p = new T1(val);//或者new(p) T1(val)
}
其实construct
函数的设计不止于此,后面会一一介绍。
allocator设计
allocator
应该包括的成员:
class allocator
{
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
public:
static T* allocate();
static T* allocate(size_type n);
static void construct(T* ptr);
static void construct(T* ptr, const T& value);
static void destroy(T* ptr);
static void destroy(T* first, T* last);
static void deallocate(T* ptr);
static void deallocate(T* ptr, size_type n);
};
allocate
的设计很简单,只是operator::new
的一层浅浅的包装
template <class T>
T* allocator<T>::allocate()
{
return static_cast<T*>(::operator new(sizeof(T)));
}
template <class T>
T* allocator<T>::allocate(size_type n)
{
if (n == 0)
return nullptr;
return static_cast<T*>(::operator new(n * sizeof(T)));
}
做到这里,我们可以简单的测试一下
#include<iostream>
using namespace std;
#include"allocator.h"
class tt {
public:
int a;
int b;
void func() {
cout <<"a = " << a << endl;
cout << "b = " << b << endl;
}
tt(int a) {
this->a = a;
this->b = 0;
}
tt() {
this->a = 20;
this->b = 20;
}
};
int main() {
qbystl::allocator<tt> allo;
auto T = allo.allocate();
tt* t_1 = new tt(15);
allo.construct(T, *t_1);
T->func();
system("pause");
}
输出结果:
看得出来内存的分配和构造都没有问题。