BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslma::MallocFreeAllocator Class Reference

#include <bslma_mallocfreeallocator.h>

Inheritance diagram for bslma::MallocFreeAllocator:
bslma::Allocator bsl::memory_resource

Detailed Description

This class provides direct access to the system-supplied (native) global std::malloc and std::free. A static method is provided for obtaining a unique, process wide object of this class, which is valid from the time the method is called until after the program (not just main) exits.

See bslma_mallocfreeallocator

Public Member Functions

 MallocFreeAllocator ()
 
 ~MallocFreeAllocator () BSLS_KEYWORD_OVERRIDE
 
void * allocate (size_type size) BSLS_KEYWORD_OVERRIDE
 
void deallocate (void *address) BSLS_KEYWORD_OVERRIDE
 
- Public Member Functions inherited from bslma::Allocator
 ~Allocator () BSLS_KEYWORD_OVERRIDE
 
template<class TYPE >
void deleteObject (const TYPE *object)
 
template<class TYPE >
void deleteObjectRaw (const TYPE *object)
 
void deleteObject (bsl::nullptr_t)
 
void deleteObjectRaw (bsl::nullptr_t)
 
- Public Member Functions inherited from bsl::memory_resource
 memory_resource () BSLS_KEYWORD_DEFAULT
 
 memory_resource (const memory_resource &) BSLS_KEYWORD_DEFAULT
 
virtual ~memory_resource ()
 
memory_resourceoperator= (const memory_resource &) BSLS_KEYWORD_DEFAULT
 
BSLA_NODISCARD void * allocate (size_t bytes, size_t alignment=k_MAX_ALIGN)
 
void deallocate (void *p, size_t bytes, size_t alignment=k_MAX_ALIGN)
 
bool is_equal (const memory_resource &other) const BSLS_KEYWORD_NOEXCEPT
 

Static Public Member Functions

static MallocFreeAllocatorsingleton ()
 
- Static Public Member Functions inherited from bslma::Allocator
static void throwBadAlloc ()
 

Additional Inherited Members

- Public Types inherited from bslma::Allocator
typedef std::size_t size_type
 
- Protected Member Functions inherited from bslma::Allocator
void * do_allocate (std::size_t bytes, std::size_t alignment) BSLS_KEYWORD_OVERRIDE
 
void do_deallocate (void *p, std::size_t bytes, std::size_t alignment) BSLS_KEYWORD_OVERRIDE
 
bool do_is_equal (const memory_resource &other) const BSLS_KEYWORD_NOEXCEPT BSLS_KEYWORD_OVERRIDE
 

Constructor & Destructor Documentation

◆ MallocFreeAllocator()

bslma::MallocFreeAllocator::MallocFreeAllocator ( )
inline

Create an allocator that uses std::malloc and std::free to supply memory.

Note
Note that all objects of this class share the same underlying resource.

◆ ~MallocFreeAllocator()

bslma::MallocFreeAllocator::~MallocFreeAllocator ( )
inline

Destroy this allocator.

Note
Note that the behavior of destroying an allocator while memory is allocated from it is not specified. (Unless you know that it is valid to do so, don't!)

For this concrete implementation, destroying this allocator object has no effect on allocated memory.

Member Function Documentation

◆ allocate()

void * bslma::MallocFreeAllocator::allocate ( size_type  size)
virtual

Return a newly allocated block of memory of (at least) the specified positive size (in bytes). If size is 0, a null pointer is returned with no other effect. If this allocator cannot return the requested number of bytes, then it will return a null pointer.

Precondition
The behavior is undefined unless 0 <= size.
Note
Note that the alignment of the address returned is the maximum alignment for any type defined on this platform. Also note that std::malloc is not called when size is 0 (in order to avoid having to acquire a lock, and potential contention in multi-treaded programs).

Implements bslma::Allocator.

◆ deallocate()

void bslma::MallocFreeAllocator::deallocate ( void *  address)
inlinevirtual

Return the memory block at the specified address back to this allocator. If address is 0, this function has no effect.

Precondition
The behavior is undefined unless address was allocated using this allocator object and has not already been deallocated.
Note
Note that std::free is not called when address is 0 (in order to avoid having to acquire a lock, and potential contention in multi-treaded programs).

Implements bslma::Allocator.

◆ singleton()

static MallocFreeAllocator & bslma::MallocFreeAllocator::singleton ( )
static

Return a reference to a valid object of this class.

Note
Note that this object is guaranteed to be valid from the time of this call onward (i.e., not just until exiting main).

The documentation for this class was generated from the following file: