8#ifndef INCLUDED_BSLMA_TESTALLOCATOR
9#define INCLUDED_BSLMA_TESTALLOCATOR
355#include <bslscm_version.h>
369# pragma bde_verify -AQK01
376struct TestAllocator_BlockHeader;
377class TestAllocatorStashedStatistics;
408 k_INT64_MAXDIGITS = 20,
409 k_LABLETEXT_LEN = 51,
410 k_NUM_STATLINES = 11,
412 k_NUM_STATLINES * (k_LABLETEXT_LEN + 2 * k_INT64_MAXDIGITS) + 1,
416 k_BLOCKID_LINE_SZ = 8 * (k_INT64_MAXDIGITS + 1) + 1 + 1
423 const char *d_name_p;
497 d_lastAllocatedNumBytes;
501 d_lastDeallocatedNumBytes;
506 d_lastAllocatedAddress_p;
510 d_lastDeallocatedAddress_p;
516 TestAllocator_BlockHeader
519 TestAllocator_BlockHeader
529 bool d_hasFillPattern;
547 formatEightBlockIds(
const TestAllocator_BlockHeader** blockList,
548 char* output )
const;
556 std::size_t formatStats(
char *output)
const;
562 template <
class t_OS>
563 t_OS& printToStream(t_OS& stream)
const;
782 const
char *
name() const;
841 void print(
std::FILE *f = stdout) const;
850#ifndef BDE_OPENSOURCE_PUBLICATION
911 template <
class t_OS>
913 {
return ta.printToStream(stream); }
1005#ifdef BDE_BUILD_TARGET_EXC
1016class TestAllocator_ProxyBase {
1020 virtual ~TestAllocator_ProxyBase()
1034template <
class BSLMA_ALLOC_TYPE>
1035class TestAllocator_Proxy :
public TestAllocator_ProxyBase {
1038 BSLMA_ALLOC_TYPE *d_allocator_p;
1043 explicit TestAllocator_Proxy(BSLMA_ALLOC_TYPE *allocator)
1044 : d_allocator_p(allocator)
1056 d_allocator_p->setAllocationLimit(limit);
1062template <
class BSLMA_ALLOC_TYPE>
1064TestAllocator_Proxy<BSLMA_ALLOC_TYPE>
1065TestAllocator_getProxy(BSLMA_ALLOC_TYPE *allocator)
1067 return TestAllocator_Proxy<BSLMA_ALLOC_TYPE>(allocator);
1072#ifndef BSLMA_TESTALLOCATOR_EXCEPTION_TEST_BEGIN
1078#define BSLMA_TESTALLOCATOR_EXCEPTION_TEST_BEGIN(BSLMA_TESTALLOCATOR) { \
1080 static int firstTime = 1; \
1081 if (veryVerbose && firstTime) { \
1082 std::puts("\t\tBSLMA EXCEPTION TEST -- (ENABLED) --"); \
1086 if (veryVeryVerbose) { \
1087 std::puts("\t\tBegin bslma exception test."); \
1089 int bslmaExceptionCounter = 0; \
1090 const BloombergLP::bslma::TestAllocator_ProxyBase& \
1091 bslmaExceptionTestAllocator = \
1092 BloombergLP::bslma::TestAllocator_getProxy(&BSLMA_TESTALLOCATOR); \
1093 bslmaExceptionTestAllocator.setAllocationLimit(bslmaExceptionCounter); \
1094 bool bslmaKeepLoopingInTestAllocatorExceptionTest = true; \
1095 while(bslmaKeepLoopingInTestAllocatorExceptionTest) { \
1096 bslmaKeepLoopingInTestAllocatorExceptionTest = false; \
1102#ifndef BSLMA_TESTALLOCATOR_EXCEPTION_TEST_BEGIN
1103#define BSLMA_TESTALLOCATOR_EXCEPTION_TEST_BEGIN(BSLMA_TESTALLOCATOR) \
1105 static int firstTime = 1; \
1106 if (verbose && firstTime) { \
1107 std::puts("\t\tBSLMA EXCEPTION TEST -- (NOT ENABLED) --"); \
1119#ifdef BDE_BUILD_TARGET_EXC
1121#ifndef BSLMA_TESTALLOCATOR_EXCEPTION_TEST_END
1122#define BSLMA_TESTALLOCATOR_EXCEPTION_TEST_END \
1123 } catch (BloombergLP::bslma::TestAllocatorException& e) { \
1124 if (veryVeryVerbose) { \
1125 std::printf("\t*** BSLMA_EXCEPTION: " \
1126 "alloc limit = %d, last alloc size = %d ***\n", \
1127 bslmaExceptionCounter, \
1128 static_cast<int>(e.numBytes())); \
1130 bslmaExceptionTestAllocator.setAllocationLimit( \
1131 ++bslmaExceptionCounter); \
1132 bslmaKeepLoopingInTestAllocatorExceptionTest = true; \
1135 bslmaExceptionTestAllocator.setAllocationLimit(-1); \
1136 if (veryVeryVerbose) { \
1137 std::puts("\t\tEnd bslma exception test."); \
1145#ifndef BSLMA_TESTALLOCATOR_EXCEPTION_TEST_END
1146#define BSLMA_TESTALLOCATOR_EXCEPTION_TEST_END
1162template <
class t_OS>
1163t_OS& TestAllocator::printToStream(t_OS& stream)
const
1167 char buffer[k_PRINTED_STATS_SZ];
1168 std::size_t cnt = formatStats(buffer);
1169 stream.write(buffer, cnt);
1171 static const char k_ID_STR[] =
1172 " Indices of Outstanding Memory Allocations:\n";
1173 static const std::size_t k_ID_STR_LEN =
sizeof(k_ID_STR) - 1;
1175 if (d_blockListHead_p) {
1176 stream.write(k_ID_STR, k_ID_STR_LEN);
1180 const TestAllocator_BlockHeader *next_p = d_blockListHead_p;
1182 cnt = formatEightBlockIds(&next_p, buffer);
1183 stream.write(buffer, cnt);
1279 d_hasFillPattern =
true;
1280 d_fillPattern = pattern;
1288 d_hasFillPattern =
false;
1321 return d_hasFillPattern;
1330 return d_fillPattern;
1336 return reinterpret_cast<void *
>(d_lastAllocatedAddress_p.
loadRelaxed());
1348 return reinterpret_cast<void *
>(d_lastDeallocatedAddress_p.
loadRelaxed());
1423#ifndef BDE_OPENSOURCE_PUBLICATION
1472: d_origin_p(testAllocator)
1473, d_numBlocksInUse(testAllocator->numBlocksInUse())
1474, d_numBytesInUse(testAllocator->numBytesInUse())
1475, d_numAllocations(testAllocator->numAllocations())
1476, d_numDeallocations(testAllocator->numDeallocations())
1477, d_numMismatches(testAllocator->numMismatches())
1478, d_numBoundsErrors(testAllocator->numBoundsErrors())
1479, d_numBlocksMax(testAllocator->numBlocksMax())
1480, d_numBytesMax(testAllocator->numBytesMax())
1481, d_numBlocksTotal(testAllocator->numBlocksTotal())
1482, d_numBytesTotal(testAllocator->numBytesTotal())
1507 return d_numBlocksInUse;
1513 return d_numBytesInUse;
1519 return d_numAllocations;
1525 return d_numDeallocations;
1531 return d_numMismatches;
1537 return d_numBoundsErrors;
1543 return d_numBlocksMax;
1549 return d_numBytesMax;
1555 return d_numBlocksTotal;
1561 return d_numBytesTotal;
1572#ifndef BDE_OPENSOURCE_PUBLICATION
1583#ifndef BEGIN_BSLMA_EXCEPTION_TEST
1584#define BEGIN_BSLMA_EXCEPTION_TEST \
1585 BSLMA_TESTALLOCATOR_EXCEPTION_TEST_BEGIN(testAllocator)
1588#ifndef END_BSLMA_EXCEPTION_TEST
1589#define END_BSLMA_EXCEPTION_TEST BSLMA_TESTALLOCATOR_EXCEPTION_TEST_END
Definition bslma_allocator.h:545
std::size_t size_type
Definition bslma_allocator.h:593
Definition bslma_testallocator.h:926
bsls::Types::Int64 numBytesMax() const
Return the numBytesMax attribute value.
Definition bslma_testallocator.h:1547
bsls::Types::Int64 numBlocksInUse() const
Return the numBlocksInUse attribute value.
Definition bslma_testallocator.h:1505
void markRestored()
Definition bslma_testallocator.h:1488
bsls::Types::Int64 numBlocksMax() const
Return the numBlocksMax attribute value.
Definition bslma_testallocator.h:1541
TestAllocatorStashedStatistics(TestAllocator *testAllocator)
Definition bslma_testallocator.h:1470
bsls::Types::Int64 numMismatches() const
Return the numMismatches attribute value.
Definition bslma_testallocator.h:1529
bsls::Types::Int64 numAllocations() const
Return the numAllocations attribute value.
Definition bslma_testallocator.h:1517
bsls::Types::Int64 numDeallocations() const
Return the numDeallocations attribute value.
Definition bslma_testallocator.h:1523
bsls::Types::Int64 numBoundsErrors() const
Return the numBoundsErrors attribute value.
Definition bslma_testallocator.h:1535
TestAllocator * origin() const
Return a const pointer to the test allocator that was saved.
Definition bslma_testallocator.h:1565
void restore()
Call origin->restoreStatistics(*this).
Definition bslma_testallocator.h:1496
bsls::Types::Int64 numBytesInUse() const
Return the numBytesInUse attribute value.
Definition bslma_testallocator.h:1511
bsls::Types::Int64 numBytesTotal() const
Return the numBytesTotal attribute value.
Definition bslma_testallocator.h:1559
bsls::Types::Int64 numBlocksTotal() const
Return the numBlocksTotal attribute value.
Definition bslma_testallocator.h:1553
Definition bslma_testallocator.h:402
TestAllocator(const char *name, Allocator *basicAllocator=0)
void deallocate(void *address) BSLS_KEYWORD_OVERRIDE
TestAllocator(Allocator *basicAllocator=0)
void setAllocationLimit(bsls::Types::Int64 limit)
Definition bslma_testallocator.h:1251
bsls::Types::Int64 numAllocation() const
Definition bslma_testallocator.h:1451
void restoreStatistics(TestAllocatorStashedStatistics *savedStatistics)
Definition bslma_testallocator.h:1212
bsls::Types::Int64 numDeallocations() const
Definition bslma_testallocator.h:1412
bool hasFillPattern() const
Definition bslma_testallocator.h:1317
void setFillPattern(bsls::Types::Uint64 pattern)
Definition bslma_testallocator.h:1275
bsls::Types::Int64 numBlocksInUse() const
Definition bslma_testallocator.h:1370
bsls::Types::Int64 numDeallocation() const
Definition bslma_testallocator.h:1457
bsls::Types::Uint64 getFillPattern() const
Definition bslma_testallocator.h:1325
bsls::Types::Int64 numBlocksMax() const
Definition bslma_testallocator.h:1376
void * lastAllocatedAddress() const
Definition bslma_testallocator.h:1334
bsls::Types::Int64 allocationLimit() const
Definition bslma_testallocator.h:1293
size_type lastAllocatedNumBytes() const
Return the number of bytes of the most recent allocation request.
Definition bslma_testallocator.h:1340
bsls::Types::Int64 numBytesInUse() const
Definition bslma_testallocator.h:1394
bool isNoAbort() const
Definition bslma_testallocator.h:1299
TestAllocator(const char *name, bool verboseFlag, Allocator *basicAllocator=0)
size_type lastDeallocatedNumBytes() const
Definition bslma_testallocator.h:1352
~TestAllocator() BSLS_KEYWORD_OVERRIDE
bsls::Types::Int64 numBlocksTotal() const
Definition bslma_testallocator.h:1382
bsls::Types::Int64 numBytesTotal() const
Definition bslma_testallocator.h:1406
void * lastAllocateAddress() const
Definition bslma_testallocator.h:1425
friend t_OS & operator<<(t_OS &stream, const TestAllocator &ta)
Definition bslma_testallocator.h:912
size_type lastDeallocateNumBytes() const
Definition bslma_testallocator.h:1445
void setQuiet(bool flagValue)
Definition bslma_testallocator.h:1263
bsls::Types::Int64 numMismatches() const
Definition bslma_testallocator.h:1418
void unsetFillPattern()
Definition bslma_testallocator.h:1284
TestAllocatorStashedStatistics stashStatistics()
Definition bslma_testallocator.h:1192
bool isQuiet() const
Definition bslma_testallocator.h:1305
void * lastDeallocatedAddress() const
Definition bslma_testallocator.h:1346
void * allocate(size_type size) BSLS_KEYWORD_OVERRIDE
TestAllocator(bool verboseFlag, Allocator *basicAllocator=0)
void setNoAbort(bool flagValue)
Definition bslma_testallocator.h:1257
void setVerbose(bool flagValue)
Definition bslma_testallocator.h:1269
bsls::Types::Int64 numBoundsErrors() const
Definition bslma_testallocator.h:1388
size_type lastAllocateNumBytes() const
Definition bslma_testallocator.h:1432
void * lastDeallocateAddress() const
Definition bslma_testallocator.h:1438
const char * name() const
Definition bslma_testallocator.h:1358
bsls::Types::Int64 numAllocations() const
Definition bslma_testallocator.h:1364
bool isVerbose() const
Definition bslma_testallocator.h:1311
bsls::Types::Int64 numBytesMax() const
Definition bslma_testallocator.h:1400
Definition bsls_atomic.h:896
Types::Int64 loadRelaxed() const
Definition bsls_atomic.h:1935
void storeRelaxed(Types::Int64 value)
Definition bsls_atomic.h:1854
Definition bsls_atomic.h:744
int loadRelaxed() const
Definition bsls_atomic.h:1759
void storeRelaxed(int value)
Definition bsls_atomic.h:1681
Definition bsls_atomic.h:1362
TYPE * loadRelaxed() const
Definition bsls_atomic.h:2423
Definition bsls_bsllock.h:232
Definition bsls_bsllock.h:176
bslma::TestAllocator bslma_TestAllocator
This alias is defined for backward compatibility.
Definition bslma_testallocator.h:1578
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#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
Definition bdlt_iso8601util.h:707
Definition bdldfp_decimal.h:5549
unsigned long long Uint64
Definition bsls_types.h:139
long long Int64
Definition bsls_types.h:134