BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslma_mallocfreeallocator.h
Go to the documentation of this file.
1/// @file bslma_mallocfreeallocator.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_mallocfreeallocator.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_MALLOCFREEALLOCATOR
9#define INCLUDED_BSLMA_MALLOCFREEALLOCATOR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslma_mallocfreeallocator bslma_mallocfreeallocator
15/// @brief Provide malloc/free adaptor to `bslma::Allocator` protocol.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_mallocfreeallocator
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_mallocfreeallocator-purpose"> Purpose</a>
25/// * <a href="#bslma_mallocfreeallocator-classes"> Classes </a>
26/// * <a href="#bslma_mallocfreeallocator-description"> Description </a>
27/// * <a href="#bslma_mallocfreeallocator-thread-safety"> Thread Safety </a>
28/// * <a href="#bslma_mallocfreeallocator-usage"> Usage </a>
29///
30/// # Purpose {#bslma_mallocfreeallocator-purpose}
31/// Provide malloc/free adaptor to `bslma::Allocator` protocol.
32///
33/// # Classes {#bslma_mallocfreeallocator-classes}
34///
35/// - bslma::MallocFreeAllocator: support malloc/free style allocate/deallocate
36///
37/// @see bslma_newdeleteallocator, bslma_allocator
38///
39/// # Description {#bslma_mallocfreeallocator-description}
40/// This component provides an allocator,
41/// `bslma::MallocFreeAllocator`, that implements the `bslma::Allocator`
42/// protocol and supplies memory using the system-supplied (native)
43/// `std::malloc` and `std::free` operators.
44/// @code
45/// ,--------------------------.
46/// ( bslma::MallocFreeAllocator )
47/// `--------------------------'
48/// | ctor/dtor
49/// | singleton
50/// V
51/// ,----------------.
52/// ( bslma::Allocator )
53/// `----------------'
54/// allocate
55/// deallocate
56/// @endcode
57/// The key purpose of this component is to facilitate the use of `malloc` and
58/// `free` when the default `bslma::NewDeleteAllocator` is not desirable (such
59/// as the case of `bslma::TestAllocator`). To accomplish this goal, `malloc`
60/// and `free` are wrapped in a singleton object whose lifetime is guaranteed to
61/// exceed any possibility of its use.
62///
63/// ## Thread Safety {#bslma_mallocfreeallocator-thread-safety}
64///
65///
66/// A single object of `bslma::MallocFreeAllocator` is safe for concurrent
67/// access by multiple threads. The underlying implementation of `malloc` and
68/// `free` will ensure that heap corruption does not occur. Note that this
69/// allocator therefore has a stronger thread safety guarantee than is required
70/// by the base-class contract or than is provided by other allocators.
71///
72/// ## Usage {#bslma_mallocfreeallocator-usage}
73///
74///
75/// This component is intended to be used when the use of `new` and `delete` are
76/// not desirable, such as the case of `bslma::TestAllocator`. Instead of using
77/// `bslma::Default` which uses the `bslma::NewDeleteAllocator`, this component
78/// can be used to bypass the use of `new` and `delete`.
79///
80/// The following example demonstrates the use of this component for a user
81/// defined allocator instead of using the default allocator:
82/// @code
83/// // my_allocator.h
84/// // ...
85///
86/// /// This class provides a mechanism for allocation and deallocation.
87/// class my_Allocator : public bslma::Allocator {
88///
89/// // DATA
90/// bslma::Allocator *d_allocator_p; // allocator (held, not owned)
91///
92/// public:
93/// // CREATORS
94///
95/// /// Create a `my_Allcoator`. Optionally specify `basicAllocator` to
96/// /// supply memory. If `basicAllocator` is 0, the
97/// /// `bslma::MallocFreeAllocator` will be used.
98/// my_Allocator(bslma::Allocator *basicAllocator = 0);
99///
100/// /// Destroy this allocator. Note that the behavior of destroying an
101/// /// allocator while memory is allocated from it is not specified.
102/// /// (Unless you *know* that it is valid to do so, don`t!)
103/// ~my_Allocator();
104///
105/// // MANIPULATORS
106///
107/// /// Return a newly allocated block of memory of (at least) the
108/// /// specified positive `size` (bytes). If `size` is 0, a null
109/// /// pointer is returned with no effect. Note that the alignment of
110/// /// the address returned is the maximum alignment for any
111/// /// fundamental type defined for this platform.
112/// void *allocate(size_type size);
113///
114/// /// Return the memory at the specified `address` back to this
115/// /// allocator. If `address` is 0, this function has no effect. The
116/// /// behavior is undefined if `address` was not allocated using this
117/// /// allocator, or has already been deallocated.
118/// void deallocate(void *address);
119/// };
120/// @endcode
121/// The constructor is implemented using `bslma::MallocFreeAllocator`.
122/// @code
123/// // my_allocator.cpp
124/// // ...
125///
126/// // CREATORS
127/// my_Allocator::my_Allocator(bslma::Allocator *basicAllocator)
128/// : d_allocator_p(basicAllocator
129/// ? basicAllocator
130/// : &bslma::MallocFreeAllocator::singleton())
131/// {
132/// }
133///
134/// // ...
135/// @endcode
136/// When the `basicAllocator` is not specified, the `bslma::MallocFreeAllocator`
137/// will be used. That allocator then then calls `std::malloc` and `std::free`
138/// for allocating and deallocating memory.
139/// @}
140/** @} */
141/** @} */
142
143/** @addtogroup bsl
144 * @{
145 */
146/** @addtogroup bslma
147 * @{
148 */
149/** @addtogroup bslma_mallocfreeallocator
150 * @{
151 */
152
153#include <bslscm_version.h>
154
155#include <bslma_allocator.h>
156
157#include <bsls_keyword.h>
158
159#include <cstdlib> // 'std::malloc', 'std::free'
160
161
162
163namespace bslma {
164
165 // =========================
166 // class MallocFreeAllocator
167 // =========================
168
169/// This class provides direct access to the system-supplied (native) global
170/// `std::malloc` and `std::free`. A `static` method is provided for
171/// obtaining a unique, process wide object of this class, which is valid
172/// from the time the method is called until after the program (not just
173/// `main`) exits.
174///
175/// See @ref bslma_mallocfreeallocator
177
178 private:
179 // NOT IMPLEMENTED
181 MallocFreeAllocator& operator=(const MallocFreeAllocator&);
182
183 public:
184 // CLASS METHODS
185
186 /// Return a reference to a valid object of this class. Note that this
187 /// object is guaranteed to be valid from the time of this call onward
188 /// (i.e., not just until exiting `main`).
190
191 // CREATORS
192
193 /// Create an allocator that uses `std::malloc` and `std::free` to
194 /// supply memory. Note that all objects of this class share the same
195 /// underlying resource.
197
198 /// Destroy this allocator. Note that the behavior of destroying an
199 /// allocator while memory is allocated from it is not specified.
200 /// (Unless you *know* that it is valid to do so, don't!)
201 ///
202 /// For this concrete implementation, destroying this allocator object
203 /// has no effect on allocated memory.
205
206 // MANIPULATORS
207
208 /// Return a newly allocated block of memory of (at least) the specified
209 /// positive `size` (in bytes). If `size` is 0, a null pointer is
210 /// returned with no other effect. If this allocator cannot return the
211 /// requested number of bytes, then it will return a null pointer. The
212 /// behavior is undefined unless `0 <= size`. Note that the alignment
213 /// of the address returned is the maximum alignment for any type
214 /// defined on this platform. Also note that `std::malloc` is *not*
215 /// called when `size` is 0 (in order to avoid having to acquire a lock,
216 /// and potential contention in multi-treaded programs).
218
219 /// Return the memory block at the specified `address` back to this
220 /// allocator. If `address` is 0, this function has no effect. The
221 /// behavior is undefined unless `address` was allocated using this
222 /// allocator object and has not already been deallocated. Note that
223 /// `std::free` is *not* called when `address` is 0 (in order to avoid
224 /// having to acquire a lock, and potential contention in multi-treaded
225 /// programs).
226 void deallocate(void *address) BSLS_KEYWORD_OVERRIDE;
227};
228
229// ============================================================================
230// INLINE DEFINITIONS
231// ============================================================================
232
233 // -------------------------
234 // class MallocFreeAllocator
235 // -------------------------
236
237// CREATORS
238inline
242
243inline
247
248// MANIPULATORS
249inline
251{
252 // While the C and C++ standard guarantees that calling free(0) is safe
253 // (3.7.3.2 paragraph 3), some libc implementations take out a lock to deal
254 // with the free(0) case, so this check can improve efficiency of threaded
255 // programs.
256
257 if (address) {
258 std::free(address);
259 }
260}
261
262} // close package namespace
263
264#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
265// ============================================================================
266// BACKWARD COMPATIBILITY
267// ============================================================================
268
269/// This alias is defined for backward compatibility.
271#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
272
273
274
275#endif
276
277// ----------------------------------------------------------------------------
278// Copyright 2013 Bloomberg Finance L.P.
279//
280// Licensed under the Apache License, Version 2.0 (the "License");
281// you may not use this file except in compliance with the License.
282// You may obtain a copy of the License at
283//
284// http://www.apache.org/licenses/LICENSE-2.0
285//
286// Unless required by applicable law or agreed to in writing, software
287// distributed under the License is distributed on an "AS IS" BASIS,
288// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
289// See the License for the specific language governing permissions and
290// limitations under the License.
291// ----------------------------- END-OF-FILE ----------------------------------
292
293/** @} */
294/** @} */
295/** @} */
Definition bslma_allocator.h:457
std::size_t size_type
Definition bslma_allocator.h:499
Definition bslma_mallocfreeallocator.h:176
void * allocate(size_type size) BSLS_KEYWORD_OVERRIDE
void deallocate(void *address) BSLS_KEYWORD_OVERRIDE
Definition bslma_mallocfreeallocator.h:250
~MallocFreeAllocator() BSLS_KEYWORD_OVERRIDE
Definition bslma_mallocfreeallocator.h:244
MallocFreeAllocator()
Definition bslma_mallocfreeallocator.h:239
static MallocFreeAllocator & singleton()
bslma::MallocFreeAllocator bslma_MallocFreeAllocator
This alias is defined for backward compatibility.
Definition bslma_mallocfreeallocator.h:270
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition balxml_encoderoptions.h:68