STL学习笔记:空间配置器allocator

allocator必要接口:

allocator::value_type

allocator::pointer

allocator::const_pointer

allocator::reference

allocator::const_reference

allocator::size_type

allocator::difference_type

allocator::rebind

自定义allocator,书上说此空间配置其完全无法应用于SGI STL allocator,但是现在应该修改了,默认的空间配置器也是std::allocator

//2jjalloca.h

 #ifndef _2JJALLOCA_H_
#define _2JJALLOCA_H_ #include <new>
#include <cstddef>
#include <cstdlib>
#include <climits>
#include <iostream> using namespace std; namespace JJ { template<class T>
inline T* _allocate(ptrdiff_t size, T*) {
//set_new_handler(0);//不知道哪里的?
T* tmp = (T*) (::operator new((size_t) (size * sizeof(T))));
//operator new可以被重载
if (tmp == ) {
cerr << "out of memory" << endl;
exit();
}
return tmp;
} template<class T>
inline void _deallocate(T* buffer) {
::operator delete(buffer);
//operator delete可以被重载
// operator delete(buffer);
} template<class T1, class T2>
inline void _construct(T1* p, const T2& value) {
new (p) T1(value);
} template<class T>
inline void _destroy(T* ptr) {
ptr->~T();
} template<class T>
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; //rebind allocator of type U
template<class U>
struct rebind {
typedef allocator<U> other;
}; //hint used for locality. ref.[Austern],p189
pointer allocate(size_type n, const void* hint = ) {
return _allocate((difference_type) n, (pointer) );
} void deallocate(pointer p, size_type n) {
_deallocate(p);
} void construct(pointer p, const T& value) {
_construct(p, value);
} void destroy(pointer p) {
_destroy(p);
} pointer address(reference x) {
return (pointer) &x;
} const_pointer const_address(const_reference x) {
return (const_pointer) &x;
} size_type max_size() const {
return size_type(UINT_MAX / sizeof(T));
} }; }//end of namespace JJ #endif /* _2JJALLOCA_H_ */

//main.cpp

 #include <iostream>
#include <vector>
#include "2jjalloca.h" using namespace std; int main(int argc, char **argv) {
int ia[] = { , , , , };
unsigned int i;
fprintf(stderr, "ia addr:%p\n", ia);
vector<int, JJ::allocator<int>> iv(ia, ia + );
for (i = ; i < iv.size(); i++) {
cout << iv[i] << ' ';
}
cout << endl;
return ;
}

运行结果也是正常的

================>现在由此引申出一个问题,operator new

 #include <iostream>
#include <new>
#include <limits.h>
#include <stddef.h> using namespace std; template<class T>
inline T* allocate(ptrdiff_t size, T *) {
set_new_handler();
T* tmp = (T*) (::operator new((size_t) (size * sizeof(T))));
if (tmp == ) {
cerr << "out of memory" << endl;
exit();
}
return tmp;
} template<class T>
inline void deallocate(T* buffer) {
::operator delete(buffer);
} template<class T>
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; pointer allocate(size_type n) {
return ::allocate((difference_type) n, (T*) );
} void deallocate(pointer p) {
::deallocate(p);
} pointer address(reference x) {
return (pointer) &x;
} const_pointer const_address(const_reference x) {
return (const_pointer) &x;
} size_type init_page_size() {
return max(size_type(), size_type( / sizeof(T)));
} size_type max_size() const {
return max(size_type(), size_type(UINT_MAX / sizeof(T)));
}
}; template<>
class Allocator<void> {
public:
typedef void* pointer;
}; int main(int argc, char **argv) { return ;
}

STL源码po析所说,SGI定义了一个有部分符合标准的allocator配置器(上面的代码),但是我看了自己本地的代码,似乎很符合标准呀。

上一篇:Service学习笔记


下一篇:STL源码剖析 — 空间配置器(allocator)