|
BDE 4.39.x Production Release
|
Provide a pure abstract interface for memory-allocation mechanisms.
Provide a pure abstract interface for memory-allocation mechanisms.
This component provides the base-level protocol (pure abstract interface) class, bslma_allocator , that serves as a ubiquitous vocabulary type for various memory allocation mechanisms. The functional capabilities documented by this protocol are similar to those afforded by global operators new and delete: sufficiently (but not necessarily maximally) aligned memory is guaranteed for any object of a given size. Clients of this abstract base class will typically accept a supplied allocator (often at construction) and use its allocate and deallocate methods instead of new and delete directly.
The use of (abstract) allocators provides at least three distinct advantages over direct (hard-coded) calls to global new and delete (see bslma_newdeleteallocator ):
new and delete) in the derived class (see bslma_managedallocator ), we can bypass the individual destructors in a dynamically allocated type and remove all memory for one or more associated object almost instantly.bslma::Allocator protocol, like any other protocol, isolates clients from direct coupling with platform level facilities that are not fully under their control. By installing a test allocator (see bslma_testallocator ), we are able to orchestrate the white-box testing of internal calls to global operators new and delete in a platform-neutral manner.Unless otherwise documented, a single allocator object is not safe for concurrent access by multiple threads. Classes derived from bslma::Allocator that are specifically designed for concurrent access must be documented as such. Unless specifically documented otherwise, separate objects of classes derived from bslma::Allocator may safely be used in separate threads.
An allocator and a pool are quite different. For starters, bslma::Allocator is an abstract class used to obtain "raw" memory of arbitrary size. A pool is a concrete data structure used to organize and supply memory according to specific needs (e.g., a consistent size). Concrete allocators may use pools in their implementations, and pools will always take a base bslma::Allocator protocol in their interface. You can think of an allocator as a stream of memory that flows into a pool of memory. Memory is allocated from the pool until it is dry; only then does new memory flow into the pool from the allocator.
This component overloads the global operator new to allow convenient syntax for the construction of objects using the bslma::Allocator protocol. The overloaded new operator defined in this component has a second parameter, bslma::Allocator&, that identifies the concrete (derived) allocator that will be used to supply memory.
Consider the following use of standard placement syntax (supplied by #include <new>) along with a bslma::Allocator, used to allocate an arbitrary TYPE.
This style of usage is inconvenient and error prone; it is also not exception safe: If the constructor of TYPE throws an exception, the basicAllocator->deallocate method is never called.
Providing an overloaded global operator new, taking a reference to a modifiable bslma::Allocator as an explicit argument allows for cleaner usage and guarantees that the basicAllocator->deallocate method is called in case of an exception:
Finally, the analogous version of operator delete should not be called directly: The overloaded operator delete supplied in this component is solely for the compiler to invoke in the event an exception is thrown during a failed construction. Instead, the bslma::Allocator protocol provides deleteObject (a template member function parameterized by the type of the object being deleted), which is implemented conceptually as follows:
Note that there is also a deleteObjectRaw which is more efficient when it is known that the address does not refer to a secondary base class of the object being deleted.
The bslma::Allocator protocol provided in this component defines a bilateral contract between suppliers and consumers of raw memory. The following subsections illustrate (1) use, and (2) implementation of the abstract bslma::Allocator base class:
In order for the bslma::Allocator interface to be useful, we must supply a concrete allocator that implements it. In this example we demonstrate how to adapt operator new and operator delete to this protocol base class.
First, in a component .h file, we define a class, derived from bslma::Allocator, that provides concrete implementations of the virtual allocate and deallocate methods:
Next, in the component .cpp file, we define the singleton() method, which provides the typical way of obtaining an instance of this allocator:
Next, we implement the bslma::Allocator protocol by defining (also in the component .cpp file) the virtual methods:
Now we can use my_NewDeleteAllocator to allocate and deallocate storage for (in this case, int) objects:
Finally, we repeat the previous example using the singleton object instead of constructing a new my_NewDeleteAllocator and using the operator new and deleteObject interface instead of raw allocate-construct and destroy-deallocate. Note that these interfaces can be mixed and matched (e.g., singleton can be used with allocate):
Allocators are often supplied to objects requiring dynamically-allocated memory at construction. For example, consider the following my_DoubleStack class, which uses a bslma::Allocator to allocate memory.
First, we define the class interface, which is a minimal subset of a typical container interface:
Next, we define the constructor, which takes an optional basicAllocator supplied only at construction. (We avoid use of the name allocator so as not to conflict with the STL use of the word, which differs slightly.) If non-zero, the stack holds a pointer to this allocator, but does not own it. If no allocator is supplied, the implementation itself must either conditionally invoke global new and delete explicitly whenever dynamic memory must be managed (BAD IDEA) or (GOOD IDEA) install a default allocator that adapts use of these global operators to the bslma_allocator interface (see bslma_default ). The constructor uses the selected allocator to allocate memory via the allocate method.
Next, we define a destructor that frees the memory held by the container using the allocator's deallocate method:
Next, we define a reallocation function that expands a dynamic array of doubles. Even in this simplified implementation, all use of the allocator protocol is relegated to the .cpp file:
Next, we define the private increaseSize method to allocate more space for container elements as needed:
Now we have what we need to implement the push method:
Now, to test our stack class, we first verify that its constructor captures the allocator correctly; if supplied an allocator pointer, it holds on to that pointer, otherwise it uses my_NewDeleteAllocator::singleton():
Finally, we exercise and verify the behavior of the manipulators and accessors:
Typedefs | |
| typedef bslma::Allocator | bslma_Allocator |
| typedef bslma::Allocator | bdema_Allocator |
Functions | |
| void * | operator new (std::size_t size, BloombergLP::bslma::Allocator &basicAllocator) |
| void | operator delete (void *address, BloombergLP::bslma::Allocator &basicAllocator) |
| void * | operator new (std::size_t size, BloombergLP::bslma::Allocator *basicAllocator) BSLS_KEYWORD_DELETED |
| void | operator delete (void *address, BloombergLP::bslma::Allocator *basicAllocator) BSLS_KEYWORD_DELETED |
| typedef bslma::Allocator bdema_Allocator |
| typedef bslma::Allocator bslma_Allocator |
These aliases are defined for backward compatibility.
|
inline |
Use the specified basicAllocator to deallocate the memory at the specified address.
address was allocated using basicAllocator and has not already been deallocated. This operator is supplied solely to allow the compiler to arrange for it to be called in case of an exception. | void operator delete | ( | void * | address, |
| BloombergLP::bslma::Allocator * | basicAllocator | ||
| ) |
|
inline |
Return the memory allocated from the specified basicAllocator of at least the specified size bytes, or 0 if size is 0.
0 <= static_cast<bslma::Allocator::size_type>(size).delete should not be called directly. Instead, this component provides a template member function deleteObject parameterized by TYPE that effectively performs the following operations: deleteObjectRaw for better performance when address is known not to be a secondary base type of the object being deleted. | void * operator new | ( | std::size_t | size, |
| BloombergLP::bslma::Allocator * | basicAllocator | ||
| ) |