BDE 4.39.x 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.
187 /// \note Note that this
188 /// object is guaranteed to be valid from the time of this call onward
189 /// (i.e., not just until exiting `main`).
191
192 // CREATORS
193
194 /// Create an allocator that uses `std::malloc` and `std::free` to supply memory.
195 ///
196 /// \note Note that all objects of this class share the same
197 /// underlying resource.
199
200 /// Destroy this allocator.
201 /// \note Note that the behavior of destroying an
202 /// allocator while memory is allocated from it is not specified.
203 /// (Unless you *know* that it is valid to do so, don't!)
204 ///
205 /// For this concrete implementation, destroying this allocator object
206 /// has no effect on allocated memory.
208
209 // MANIPULATORS
210
211 /// Return a newly allocated block of memory of (at least) the specified
212 /// positive `size` (in bytes). If `size` is 0, a null pointer is
213 /// returned with no other effect. If this allocator cannot return the
214 /// requested number of bytes, then it will return a null pointer.
215 ///
216 /// \pre The behavior is undefined unless `0 <= size`.
217 /// \note Note that the alignment
218 /// of the address returned is the maximum alignment for any type
219 /// defined on this platform. Also note that `std::malloc` is *not*
220 /// called when `size` is 0 (in order to avoid having to acquire a lock,
221 /// and potential contention in multi-treaded programs).
223
224 /// Return the memory block at the specified `address` back to this
225 /// allocator. If `address` is 0, this function has no effect.
226 ///
227 /// \pre The behavior is undefined unless `address` was allocated using this
228 /// allocator object and has not already been deallocated.
229 ///
230 /// \note Note that `std::free` is *not* called when `address` is 0 (in order to avoid
231 /// having to acquire a lock, and potential contention in multi-treaded
232 /// programs).
233 void deallocate(void *address) BSLS_KEYWORD_OVERRIDE;
234};
235
236// ============================================================================
237// INLINE DEFINITIONS
238// ============================================================================
239
240 // -------------------------
241 // class MallocFreeAllocator
242 // -------------------------
243
244// CREATORS
245inline
249
250inline
254
255// MANIPULATORS
256inline
258{
259 // While the C and C++ standard guarantees that calling free(0) is safe
260 // (3.7.3.2 paragraph 3), some libc implementations take out a lock to deal
261 // with the free(0) case, so this check can improve efficiency of threaded
262 // programs.
263
264 if (address) {
265 std::free(address);
266 }
267}
268
269} // close package namespace
270
271#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
272// ============================================================================
273// BACKWARD COMPATIBILITY
274// ============================================================================
275
276/// This alias is defined for backward compatibility.
278#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
279
280
281
282#endif
283
284// ----------------------------------------------------------------------------
285// Copyright 2013 Bloomberg Finance L.P.
286//
287// Licensed under the Apache License, Version 2.0 (the "License");
288// you may not use this file except in compliance with the License.
289// You may obtain a copy of the License at
290//
291// http://www.apache.org/licenses/LICENSE-2.0
292//
293// Unless required by applicable law or agreed to in writing, software
294// distributed under the License is distributed on an "AS IS" BASIS,
295// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
296// See the License for the specific language governing permissions and
297// limitations under the License.
298// ----------------------------- END-OF-FILE ----------------------------------
299
300/** @} */
301/** @} */
302/** @} */
Definition bslma_allocator.h:545
std::size_t size_type
Definition bslma_allocator.h:593
Definition bslma_mallocfreeallocator.h:176
void * allocate(size_type size) BSLS_KEYWORD_OVERRIDE
void deallocate(void *address) BSLS_KEYWORD_OVERRIDE
Definition bslma_mallocfreeallocator.h:257
~MallocFreeAllocator() BSLS_KEYWORD_OVERRIDE
Definition bslma_mallocfreeallocator.h:251
MallocFreeAllocator()
Definition bslma_mallocfreeallocator.h:246
static MallocFreeAllocator & singleton()
bslma::MallocFreeAllocator bslma_MallocFreeAllocator
This alias is defined for backward compatibility.
Definition bslma_mallocfreeallocator.h:277
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:695
Definition baljsn_encoder_testtypes.h:76