|
| 1 | +// Copyright 2014-2025 Bloomberg Finance L.P. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +// bmqtst_loggingallocator.h -*-C++-*- |
| 17 | +#ifndef INCLUDED_BMQTST_LOGGINGALLOCATOR |
| 18 | +#define INCLUDED_BMQTST_LOGGINGALLOCATOR |
| 19 | + |
| 20 | +//@PURPOSE: Provide a 'bslma::Allocator' caching allocation stack traces. |
| 21 | +// |
| 22 | +//@CLASSES: |
| 23 | +// bmqtst::LoggingAllocator : allocator adaptor. |
| 24 | +// |
| 25 | +//@DESCRIPTION: This component defines a mechanism, 'bmqtst::LoggingAllocator' |
| 26 | +// which implements the 'bslma::Allocator' protocol and which logs stack traces |
| 27 | +// on any allocation. |
| 28 | + |
| 29 | +// BDE |
| 30 | +#include <bsl_string.h> |
| 31 | +#include <bsl_vector.h> |
| 32 | +#include <bslma_allocator.h> |
| 33 | +#include <bsls_atomic.h> |
| 34 | +#include <bsls_keyword.h> |
| 35 | + |
| 36 | +namespace BloombergLP { |
| 37 | + |
| 38 | +namespace bmqtst { |
| 39 | + |
| 40 | +// ====================== |
| 41 | +// class LoggingAllocator |
| 42 | +// ====================== |
| 43 | + |
| 44 | +/// An allocator caching allocation stack traces. |
| 45 | +class LoggingAllocator BSLS_KEYWORD_FINAL : public bslma::Allocator { |
| 46 | + public: |
| 47 | + // PUBLIC TYPES |
| 48 | + |
| 49 | + /// Alias for a signed integral type capable of representing the number |
| 50 | + /// of bytes in this platform's virtual address space. |
| 51 | + typedef bsls::Types::size_type size_type; |
| 52 | + |
| 53 | + private: |
| 54 | + // DATA |
| 55 | + /// @brief Base allocator to use. |
| 56 | + bslma::Allocator* d_allocator_p; |
| 57 | + |
| 58 | + /// @brief The flag indicaing that the allocator must fail instantly if any |
| 59 | + /// undesired allocation happens. |
| 60 | + bool d_failFast; |
| 61 | + |
| 62 | + /// @brief The flag indicating that this allocator has failed. |
| 63 | + bsls::AtomicBool d_failed; |
| 64 | + |
| 65 | + private: |
| 66 | + // NOT IMPLEMENTED |
| 67 | + LoggingAllocator(const LoggingAllocator&) BSLS_KEYWORD_DELETED; |
| 68 | + LoggingAllocator& operator=(const LoggingAllocator&) BSLS_KEYWORD_DELETED; |
| 69 | + |
| 70 | + public: |
| 71 | + // CREATORS |
| 72 | + |
| 73 | + /// @brief Construct a logging allocator. |
| 74 | + /// @param failFast The flag indicating whether this allocator must raise |
| 75 | + /// an exception instantly on any undesired allocation. |
| 76 | + /// @param allocator The base allocator to use, or use the default |
| 77 | + /// allocator if this arg is null. |
| 78 | + explicit LoggingAllocator(bool failFast = false, |
| 79 | + bslma::Allocator* allocator = 0); |
| 80 | + |
| 81 | + /// Destroy this object. |
| 82 | + ~LoggingAllocator() BSLS_KEYWORD_OVERRIDE; |
| 83 | + |
| 84 | + // MANIPULATORS |
| 85 | + |
| 86 | + // (virtual bslma::Allocator) |
| 87 | + |
| 88 | + /// @brief Return a newly allocated block of memory. If this allocator |
| 89 | + /// cannot return the requested number of bytes, then it will |
| 90 | + /// throw a `bsl::bad_alloc` exception in an exception-enabled |
| 91 | + /// build, or else will abort the program in a non-exception build. |
| 92 | + /// The behavior is undefined unless `0 <= size`. Note that the |
| 93 | + /// alignment of the address returned conforms to the platform |
| 94 | + /// requirement for any object of the specified `size`. |
| 95 | + /// @param size The minimum size of the newly allocated block (in bytes). |
| 96 | + /// If `size` is 0, a null pointer is returned with no other |
| 97 | + /// effect. |
| 98 | + /// @return The memory address of the allocated block or a null pointer. |
| 99 | + void* allocate(size_type size) BSLS_KEYWORD_OVERRIDE; |
| 100 | + |
| 101 | + /// @brief Return the memory block at the specified `address` back to this |
| 102 | + /// allocator. The behavior is undefined unless `address` was |
| 103 | + /// allocated using this allocator object and has not already been |
| 104 | + /// deallocated. |
| 105 | + /// @param address The address of the memory block to deallocate. If this |
| 106 | + /// argument is a null pointer, the function doesn't have an effect. |
| 107 | + void deallocate(void* address) BSLS_KEYWORD_OVERRIDE; |
| 108 | + |
| 109 | + /// @brief Check whether the undesired allocations were recorded and raise |
| 110 | + /// an exception if any. |
| 111 | + void checkNoAllocations(); |
| 112 | +}; |
| 113 | + |
| 114 | +} // close package namespace |
| 115 | +} // close enterprise namespace |
| 116 | + |
| 117 | +#endif |
0 commit comments