[#BloombergLP-bslma-MallocFreeAllocator] = xref:BloombergLP.adoc[BloombergLP]::xref:BloombergLP/bslma.adoc[bslma]::MallocFreeAllocator :relfileprefix: ../../ :mrdocs: 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. == Synopsis Declared in `<bslma_mallocfreeallocator.h>` [source,cpp,subs="verbatim,replacements,macros,-callouts"] ---- class MallocFreeAllocator : public xref:BloombergLP/bslma/Allocator.adoc[Allocator] ---- == Base Classes [cols="1,4"] |=== | Name| Description | `xref:BloombergLP/bslma/Allocator.adoc[Allocator]` | This protocol class provides a pure abstract interface and contract for clients and suppliers of raw memory. If the requested memory cannot be returned, the contract requires that an `std::bad_alloc` exception be thrown. Note that memory is guaranteed to be sufficiently aligned for any object of the requested size on the current platform, which may be less than the maximal alignment guarantee afforded by global `operator new`. |=== == Type Aliases [cols="1,4"] |=== | Name| Description | xref:BloombergLP/bslma/Allocator/size_type.adoc[`size_type`] | Alias for an unsigned integral type capable of representing the number of bytes in this platform's virtual address space. |=== == Member Functions [cols="1,4"] |=== | Name| Description | xref:BloombergLP/bslma/MallocFreeAllocator/2constructor.adoc[`MallocFreeAllocator`] [.small]#[constructor]# | Create an allocator that uses `std::malloc` and `std::free` to supply memory. Note that all objects of this class share the same underlying resource. | xref:BloombergLP/bslma/MallocFreeAllocator/2destructor.adoc[`~MallocFreeAllocator`] [.small]#[destructor]# [.small]#[virtual]# | Destroy this allocator. 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!) | xref:BloombergLP/bslma/Allocator/operator_assign.adoc[`operator=`] | | xref:BloombergLP/bslma/MallocFreeAllocator/allocate-0b.adoc[`allocate`] | 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. The behavior is undefined unless `0 <= size`. 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). | xref:BloombergLP/bslma/MallocFreeAllocator/deallocate-07.adoc[`deallocate`] | Return the memory block at the specified `address` back to this allocator. If `address` is 0, this function has no effect. The behavior is undefined unless `address` was allocated using this allocator object and has not already been deallocated. 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). | xref:BloombergLP/bslma/Allocator/deleteObject-09.adoc[`deleteObject`] | `deleteObject` overloads | xref:BloombergLP/bslma/Allocator/deleteObjectRaw-06f.adoc[`deleteObjectRaw`] | `deleteObjectRaw` overloads | xref:BloombergLP/bslma/Allocator/is_equal.adoc[`is_equal`] | |=== == Static Member Functions [cols="1,4"] |=== | Name| Description | xref:BloombergLP/bslma/MallocFreeAllocator/singleton.adoc[`singleton`] | Return a reference to a valid object of this class. Note that this object is guaranteed to be valid from the time of this call onward (i.e., not just until exiting `main`). | xref:BloombergLP/bslma/Allocator/throwBadAlloc.adoc[`throwBadAlloc`] | Throw `std::bad_alloc` if exceptions are enabled or abort the program otherwise. Derived classes and helper functions will typically call this function when they are unable to satisfy an allocation request. This function never returns. |=== == Protected Member Functions [cols="1,4"] |=== | Name| Description | xref:BloombergLP/bslma/Allocator/do_allocate.adoc[`do_allocate`] [.small]#[virtual]# | Return a newly allocated block of memory of (at least) the specified positive `bytes` and having at least the specified `alignment`. Unless overriden in a derived class, the return value is `this‐>allocate(bytes)`. If this allocator cannot return the requested number of bytes or cannot satisfy the alignment request, then it will throw a `std::bad_alloc` exception in an exception‐enabled build, or else will abort the program in a non‐exception build. Unless overriden in a derived class, this function will forward the allocation request to the `allocate` virtual function, padding `bytes` and adjusting the return value as necessary to ensure sufficient alignment. Note that if `bytes` is `0`, the same non‐null value will be returned every time. | xref:BloombergLP/bslma/Allocator/do_deallocate.adoc[`do_deallocate`] [.small]#[virtual]# | Return the memory block at the specified `p` address, having the specified `bytes` and specified `alignment`, back to this allocator. Unless overriden in a derived class, this function will forward the deallocation request to the `deallocate` virtual function, padding `bytes` and adjusting `p` as necessary to account for `alignment` values other than the natural alignment for an object of size `bytes`. The behavior is undefined unless `address` is a block allocated from this allocator object using the same `bytes` and `alignment` and not already deallocated. | xref:BloombergLP/bslma/Allocator/do_is_equal.adoc[`do_is_equal`] [.small]#[virtual]# | Return `true` if this allocator is equal to the specified `other` allocator, meaning (at least) that a memory block allocated by one can be deallocated by the other; otherwise return `false`. Unless overriden, this method returns `this == &other`. |=== [.small]#Created with https://www.mrdocs.com[MrDocs]#