based on <STL源码剖析>
all.h
#ifndef _ALL_
#define _ALL_
#include <new>
#include <cstddef>
#include <cstdlib>
#include <climits>
#include <iostream>
namespace JJ{
template <class T>
inline T* _allocate(ptrdiff_t size, T*)
{
std::cout << "in _allocate()" << std::endl;
T* temp = (T*)(:: operator new((size_t)(size * sizeof(T))));
if(temp == 0){
std::cerr << "out of memory"<< std::endl;
exit(1);
}
return temp;
}
template <class T>
inline void _deallocate(T* p)
{
std::cout << "in _deallocate()" << std::endl;
::operator delete(p);
return;
}
template <class T1, class T2>
inline void _construct(T1 *p, T2& value)
{
std::cout << "in _construct()" << std::endl;
new(p) T1(value);
return;
}
template <class T>
inline void _destroy(T* p)
{
std::cout << "in _destroy()" << std::endl;
p->~T();
return;
}
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;
template <class U>
struct rebind{
typedef allocator<U> other;
};
pointer allocate(size_type n, const void* hint = 0){
std::cout << "in allocate()" << std::endl;
return _allocate((difference_type)n, (pointer)0);
}
void deallocate(pointer p, size_type n){
std::cout << "in deallocate()" << std::endl;
_deallocate(p);return;
}
void construct(pointer p, const T& value)
{
std::cout << "in constructor()" << std::endl;
_construct(p, value);
return;
}
void destroy(pointer p){
std::cout << "in destroy()" << std::endl;
_destroy(p);
return;
}
pointer address(reference x){
std::cout << "in address()" << std::endl;
return (pointer)&x;
}
const_pointer const_address(reference x){
std::cout << "in const_address()" << std::endl;
return (const_reference)&x;
}
size_type max_size()const{
std::cout << "in max_size()" << std::endl;
return size_type(UINT_MAX/sizeof(T));
}
};
}
#endif
a.cpp
#include "all.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int ia[2] = {1,2};
unsigned int i;
vector<int, JJ::allocator<int> > iv(ia, ia+2);
for(i = 0; i < 5; i++){
cout << iv[i] << " ";
}
cout << endl;
return 0;
}
=====================================================================================================================================
/*defalloc.h*/
#ifndef DEFALLOC_H
#define DEFALLOC_H
#include <new.h>
#include <stddef.h>
#include <stdlib.h>
#include <limits.h>
#include <iostream.h>
#include <algobase.h>
template <class T>
inline T* allocate(ptrdiff_t size, T*) {
set_new_handler(0);
T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));
if (tmp == 0) {
cerr << "out of memory" << endl;
exit(1);
}
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, (pointer)0);
}
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(1), size_type(4096/sizeof(T)));
}
size_type max_size() const {
return max(size_type(1), size_type(UINT_MAX/sizeof(T)));
}
};
class allocator<void> {
public:
typedef void* pointer;
};
#endif