Implementation of STL-Allocator

Source: Internet
Author: User
Writing your Own Allocator is not very hard. The most important issue is how you allocate or
Deallocate the storage. The rest is more or less obvious. As an example, let's look at a naive
Implementation of the default Allocator:
// Util/defalloc. HPP

Namespace STD {
Template <class T>
Class Allocator
{
Public:
// Type Definitions
Typedef size_t size_type;
Typedef ptrdiff_t difference_type;
Typedef T * pointer;
Typedef const T * const_pointer;
Typedef T & reference;
Typedef const T & const_reference;
Typedef t value_type;
// Rebind allocator to type U
Template <Class U>
Struct rebind
{
Typedef Allocator <u> Other;
};
// Return address of Values
Pointer address (reference value) const
{
Return (& value );
}
Const_pointer address (const_reference value) const
{
Return (& value );
}
/* Constructors and destructor
*-Nothing to do because the Allocator has no state
*/
Allocator () Throw ()
{
}
Allocator (const Allocator &) Throw ()
{
}
Template <Class U>
Allocator (const Allocator <u> &) Throw ()
{
}
~ Allocator () Throw ()
{
}
// Return maximum number of elements that can be allocated
Size_type max_size () const throw ()
{
// For numeric_limits see section 4.3, page 59
Return (numeric_limits <size_t>: max ()/sizeof (t ));
}
// Allocate but don't initialize num elements of type T
Pointer allocate (size_type num,
Allocator <void >:: const_pointer hint = 0)
{
// Allocate memory with global new
Return (pointer) (: perator new (Num * sizeof (t )));
}
// Initialize elements of allocated storage P with value
Void construct (pointer P, const T & value)
{
// Initialize memory with placement new
New (void *) P) T (value );
}
// Destroy elements of initialized storage P
Void destroy (pointer P)
{
// Destroy objects by calling their destructor
P-> ~ T ();
}
// Deallocate storage P of deleted elements
Void deallocate (pointer P, size_type num)
{
// Deallocate memory with global Delete
: Perator Delete (void *) p ));
}
};
// Return that all specializations of this Allocator are
Interchangeable
Template <class T1, class T2>
Bool operator = (const Allocator <t1> &,
Const Allocator <t2> &) Throw ()
{
Return (true );
}
Template <class T1, class T2>
Bool Operator! = (Const Allocator <t1> &,
Const Allocator <t2> &) Throw ()
{
Return (false );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.