This is the talk page for discussing improvements to the Allocator (C++) article. This is not a forum for general discussion of the article's subject. |
Article policies
|
Find sources: Google (books · news · scholar · free images · WP refs) · FENS · JSTOR · TWL |
![]() | This article is rated GA-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | ||||||||||||||||||||||||||||||||||||||||
|
![]() | Allocator (C++) has been listed as one of the Engineering and technology good articles under the good article criteria. If you can improve it further, please do so. If it no longer meets these criteria, you can reassess it. | |||||||||
| ||||||||||
![]() | A fact from this article appeared on Wikipedia's Main Page in the "Did you know?" column on May 17, 2009. The text of the entry was: Did you know ... that custom allocators may greatly improve the performance of a computer program written in C++? |
Reference implementation
editThe following is a fully conforming allocator implementation using malloc
and free
as its allocation primitives. Largely based on Austern's example and the default allocator example, with only minor adjustments of style (such as the idiomatic use of the keyword typename
in the type-parameter of the template-declarations).
#include <cstddef> // size_t, ptrdiff_t
#include <cstdlib> // malloc, free
#include <new> // bad_alloc
template<typename T> struct allocator;
// the allocator must be able to be instantiated for void
template<> struct allocator<void> {
typedef void value_type;
typedef void* pointer;
typedef const void* const_pointer;
template <typename U> struct rebind { typedef allocator<U> other; };
};
template <typename T> struct allocator {
typedef T* pointer;
typedef T& reference;
typedef const T* const_pointer;
typedef const T& const_reference;
typedef T value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template <typename U> struct rebind { typedef allocator<U> other; };
allocator() {}
// an allocator can be constructed from an allocator instantiated on a different type
template <typename U> allocator(const allocator<U>&) {}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const { return &x; }
pointer allocate(size_type n, allocator<void>::pointer = 0) {
void* p = std::malloc(n * sizeof(T));
if (!p) // allocate may throw in case of failure
throw std::bad_alloc();
return pointer(p);
}
void deallocate(pointer p, size_type) throw() { std::free(p); }
// any larger number will cause the result of the computation (n * sizeof(T)) to wrap around
size_type max_size() const { return size_type(-1) / sizeof(T); }
void construct(pointer p, const value_type& x) { new(p) T(x); }
void destroy(pointer p) { p->~T(); }
};
// all allocators should be equivalent
template <typename T, typename U>
bool operator==(const allocator<T>&, const allocator<U>&) { return true; }
template <typename T, typename U>
bool operator!=(const allocator<T>&, const allocator<U>&) { return false; }
While I do not believe this example is eligible for copyright because a correct and minimal example can only be expressed in a very limited number of ways, I nevertheless grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law. decltype (talk) 05:34, 29 May 2009 (UTC)
WikiProjects
editI've added the WikiProject banner for WikiProject Computer Science as I believe this article falls under their scope, and I've added a banner shell for the WikiProjects, feel free to revert if you disagree with this classification. -- M2Ys4U (talk) 17:16, 30 May 2009 (UTC)