Click here to Skip to main content
15,884,353 members
Articles / Programming Languages / C++

C++ Standard Allocator, An Introduction and Implementation

Rate me:
Please Sign up or sign in to vote.
4.91/5 (26 votes)
18 Aug 2003CPOL10 min read 293.7K   1.8K   102  
Introduction to the allocator concept, as well as implementing a policy-driven allocator template class
#include "allocator.hpp"
#include "typetraits.hpp"
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <deque>
#include <stack>

int main() {
	std::stack<int, Dot::Allocator<int> > s;
	std::deque<int, Dot::Allocator<int> > d;
	std::map<int, int, Dot::Allocator<int> > m;

	std::vector<int, Dot::Allocator<int, Dot::TrackAllocPolicy<int> > > v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	v.push_back(6);
	v.push_back(7);
	v.push_back(8);
	v.push_back(9);
	v.push_back(10);
	std::cout << static_cast<Dot::Traits::StreamTypeTraits<std::size_t>::StreamT>(v.get_allocator().CurrentAllocations()) << std::endl;
	std::cout << static_cast<Dot::Traits::StreamTypeTraits<std::size_t>::StreamT>(v.get_allocator().TotalAllocations()) << std::endl;
	std::cout << static_cast<Dot::Traits::StreamTypeTraits<std::size_t>::StreamT>(v.get_allocator().PeakAllocations()) << std::endl;

	std::vector<int, Dot::Allocator<int, Dot::FixedArrayAllocPolicy<int, 10> > > v2;
	v2.reserve(10);
	v2.push_back(1);
	v2.push_back(2);
	v2.push_back(3);
	v2.push_back(4);
	v2.push_back(5);
	v2.push_back(6);
	v2.push_back(7);
	v2.push_back(8);
	v2.push_back(9);
	v2.push_back(10);

	std::list<int, Dot::Allocator<int, Dot::SmallObjectAllocPolicy<int, 2> > > vTest;

	std::cout << "Pre" << std::endl;
	std::list<int, Dot::Allocator<int, Dot::SmallObjectAllocPolicy<int, 2> > >::iterator p = vTest.begin();
	std::list<int, Dot::Allocator<int, Dot::SmallObjectAllocPolicy<int, 2> > >::iterator pEnd = vTest.end();
	while ( p != pEnd ) {
		std::cout << (*p) << ", ";
		++p;
	}
	std::cout << std::endl;

	vTest.push_back(5);
	vTest.push_back(10);
	vTest.push_back(20);

	std::cout << "During" << std::endl;
	p = vTest.begin();
	pEnd = vTest.end();
	while ( p != pEnd ) {
		std::cout << (*p) << ", ";
		++p;
	}
	std::cout << std::endl;

	vTest.pop_front();
	vTest.pop_front();
	vTest.pop_front();

	std::cout << "After" << std::endl;
	p = vTest.begin();
	pEnd = vTest.end();
	while ( p != pEnd ) {
		std::cout << (*p) << ", ";
		++p;
	}
	std::cout << std::endl;

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions