Skip to content

Commit c3d5f12

Browse files
authored
Use std::source_location instead of preprocessor macros (#8918)
1 parent f9e2c9f commit c3d5f12

40 files changed

Lines changed: 140 additions & 138 deletions

src/alice/alice.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ int alice(Firebird::UtilSvc* uSvc)
600600
#if defined(DEBUG_GDS_ALLOC)
601601
if (!uSvc->isService())
602602
{
603-
gds_alloc_report(0 ALLOC_ARGS);
603+
gds_alloc_report(0, __FILE__, __LINE__);
604604
}
605605
#endif
606606

src/alice/exe.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int EXE_action(const TEXT* database, const SINT64 switches)
7373
bool error = false;
7474
AliceGlobals* tdgbl = AliceGlobals::getSpecific();
7575
{
76-
Firebird::AutoMemoryPool newPool(MemoryPool::createPool(ALLOC_ARGS0));
76+
Firebird::AutoMemoryPool newPool(MemoryPool::createPool());
7777
AliceContextPoolHolder context(tdgbl, newPool);
7878

7979
for (USHORT i = 0; i < MAX_VAL_ERRORS; i++)
@@ -145,7 +145,7 @@ int EXE_two_phase(const TEXT* database, const SINT64 switches)
145145
bool error = false;
146146
AliceGlobals* tdgbl = AliceGlobals::getSpecific();
147147
{
148-
Firebird::AutoMemoryPool newPool(MemoryPool::createPool(ALLOC_ARGS0));
148+
Firebird::AutoMemoryPool newPool(MemoryPool::createPool());
149149
AliceContextPoolHolder context(tdgbl, newPool);
150150

151151
for (USHORT i = 0; i < MAX_VAL_ERRORS; i++)

src/burp/burp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ class GblPool
985985
}
986986

987987
explicit GblPool(bool ownPool)
988-
: gbl_pool(ownPool ? MemoryPool::createPool(ALLOC_ARGS1 getDefaultMemoryPool()) : getDefaultMemoryPool())
988+
: gbl_pool(ownPool ? MemoryPool::createPool(getDefaultMemoryPool()) : getDefaultMemoryPool())
989989
{ }
990990

991991
~GblPool()
@@ -1386,13 +1386,13 @@ class OutputVersion : public Firebird::IVersionCallbackImpl<OutputVersion, Fireb
13861386
static inline UCHAR* BURP_alloc(ULONG size)
13871387
{
13881388
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
1389-
return (UCHAR*)(tdgbl->getPool().allocate(size ALLOC_ARGS));
1389+
return (UCHAR*)(tdgbl->getPool().allocate(size));
13901390
}
13911391

13921392
static inline UCHAR* BURP_alloc_zero(ULONG size)
13931393
{
13941394
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
1395-
return (UCHAR*)(tdgbl->getPool().calloc(size ALLOC_ARGS));
1395+
return (UCHAR*)(tdgbl->getPool().calloc(size));
13961396
}
13971397

13981398
static inline void BURP_free(void* block) noexcept

src/common/StdHelper.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <utility>
3131
#include <variant>
3232
#include <cstddef>
33+
#include <source_location>
3334
#include <stdexcept>
3435
#include "boost/type_traits/copy_cv.hpp"
3536

@@ -157,6 +158,22 @@ constexpr auto getVariantIndexAndSpan(V& message)
157158
}
158159

159160

161+
struct CustomSourceLocation
162+
{
163+
static constexpr CustomSourceLocation current(
164+
const std::source_location& location = std::source_location::current())
165+
{
166+
return {
167+
.fileName = location.file_name(),
168+
.line = static_cast<int>(location.line())
169+
};
170+
}
171+
172+
const char* fileName;
173+
int line;
174+
};
175+
176+
160177
} // namespace Firebird
161178

162179
#endif // FB_COMMON_STD_HELPER_H

src/common/classes/alloc.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ class MemHeader
293293

294294
public:
295295
#ifdef DEBUG_GDS_ALLOC
296-
INT32 lineNumber = -1;
297-
const char *fileName = nullptr;
296+
Firebird::CustomSourceLocation location;
298297
#elif (SIZEOF_VOID_P == 4)
299298
FB_UINT64 dummyAlign;
300299
#endif
@@ -412,16 +411,16 @@ class MemHeader
412411
{
413412
bool filter = filter_path != NULL;
414413

415-
if (isActive() && filter && fileName)
416-
filter = strncmp(filter_path, fileName, filter_len) != 0;
414+
if (isActive() && filter && location.fileName)
415+
filter = strncmp(filter_path, location.fileName, filter_len) != 0;
417416

418417
if (!filter)
419418
{
420419
if (isActive() || redirected())
421420
{
422421
fprintf(file, "%s %p: size=%" SIZEFORMAT " allocated at %s:%d",
423422
isExtent() ? "EXTN" : redirected() ? "RDIR" : "USED",
424-
this, getSize(), fileName, lineNumber);
423+
this, getSize(), location.fileName, location.line);
425424
}
426425
else
427426
fprintf(file, "FREE %p: size=%" SIZEFORMAT, this, getSize());
@@ -2126,16 +2125,15 @@ void MemPool::newExtent(size_t& size, Extent** linkedList)
21262125
size = extent->spaceRemaining;
21272126
}
21282127

2129-
MemoryPool* MemoryPool::createPool(ALLOC_PARAMS1 MemoryPool* parentPool, MemoryStats& stats)
2128+
MemoryPool* MemoryPool::createPool(MemoryPool* parentPool, MemoryStats& stats ALLOC_PARAMS_DEF)
21302129
{
21312130
if (!parentPool)
21322131
parentPool = getDefaultMemoryPool();
21332132

21342133
MemPool* p = new(*parentPool ALLOC_PASS_ARGS) MemPool(*(parentPool->pool), stats, &defaultExtentsCache);
21352134
#ifdef MEM_DEBUG
21362135
#ifdef DEBUG_LOST_POOLS
2137-
p->fileName = file;
2138-
p->lineNum = line;
2136+
p->location = location;
21392137

21402138
static std::atomic<int> seqGen = 0;
21412139
p->seq = ++seqGen;
@@ -2231,7 +2229,7 @@ MemBlock* MemPool::allocateInternal2(size_t from, size_t& length, bool flagRedir
22312229
return hunk->block;
22322230
}
22332231

2234-
MemBlock* MemPool::allocateRange(size_t from, size_t& size ALLOC_PARAMS)
2232+
MemBlock* MemPool::allocateRange(size_t from, size_t& size ALLOC_PARAMS_DEF)
22352233
{
22362234
size_t length = from ? size : ROUNDUP(size + VALGRIND_REDZONE, roundingSize) + GUARD_BYTES;
22372235
MemBlock* memory = allocateInternal(from, length, true);
@@ -2242,8 +2240,7 @@ MemBlock* MemPool::allocateRange(size_t from, size_t& size ALLOC_PARAMS)
22422240
#endif
22432241

22442242
#ifdef DEBUG_GDS_ALLOC
2245-
memory->fileName = file;
2246-
memory->lineNumber = line;
2243+
memory->location = location;
22472244
#endif
22482245

22492246
#ifdef MEM_DEBUG
@@ -2260,7 +2257,7 @@ MemBlock* MemPool::allocateRange(size_t from, size_t& size ALLOC_PARAMS)
22602257
}
22612258

22622259

2263-
void* MemPool::allocate(size_t size ALLOC_PARAMS)
2260+
void* MemPool::allocate(size_t size ALLOC_PARAMS_DEF)
22642261
{
22652262
#ifdef VALIDATE_POOL
22662263
MutexLockGuard guard(mutex, "MemPool::allocate");
@@ -2335,7 +2332,7 @@ void MemPool::releaseMemory(void* object, bool flagExtent) noexcept
23352332
block->valgrindInternal();
23362333

23372334
#ifdef DEBUG_GDS_ALLOC
2338-
block->fileName = NULL;
2335+
block->location.fileName = nullptr;
23392336
#endif
23402337

23412338
// Finally delete it
@@ -2506,7 +2503,7 @@ void* MemPool::getExtent(size_t from, size_t& to) // pass desired minimum size,
25062503
#ifdef VALIDATE_POOL
25072504
MutexLockGuard guard(mutex, "MemPool::getExtent");
25082505
#endif
2509-
MemBlock* extent = allocateRange(from, to ALLOC_ARGS);
2506+
MemBlock* extent = allocateRange(from, to);
25102507
extent->setExtent();
25112508
return &extent->body;
25122509
}
@@ -2622,7 +2619,7 @@ void MemPool::globalFree(void* block) noexcept
26222619
deallocate(block);
26232620
}
26242621

2625-
void* MemoryPool::calloc(size_t size ALLOC_PARAMS)
2622+
void* MemoryPool::calloc(size_t size ALLOC_PARAMS_DEF)
26262623
{
26272624
void* block = allocate(size ALLOC_PASS_ARGS);
26282625
memset(block, 0, size);
@@ -2768,7 +2765,7 @@ MemoryPool& AutoStorage::getAutoMemoryPool()
27682765
return *p;
27692766
}
27702767

2771-
void* MemoryPool::allocate(size_t size ALLOC_PARAMS)
2768+
void* MemoryPool::allocate(size_t size ALLOC_PARAMS_DEF)
27722769
{
27732770
return pool->allocate(size ALLOC_PASS_ARGS);
27742771
}
@@ -3037,12 +3034,12 @@ void AutoStorage::ProbeStack() const noexcept
30373034

30383035
void* operator new(size_t s)
30393036
{
3040-
return getExternalMemoryPool()->allocate(s ALLOC_ARGS);
3037+
return getExternalMemoryPool()->allocate(s);
30413038
}
30423039

30433040
void* operator new[](size_t s)
30443041
{
3045-
return getExternalMemoryPool()->allocate(s ALLOC_ARGS);
3042+
return getExternalMemoryPool()->allocate(s);
30463043
}
30473044

30483045
void operator delete(void* mem) noexcept

src/common/classes/alloc.h

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,13 @@
5858

5959
#include <memory.h>
6060
#include <memory>
61-
6261
#ifdef DEBUG_GDS_ALLOC
63-
#define FB_NEW new(*getDefaultMemoryPool(), __FILE__, __LINE__)
64-
#define FB_NEW_POOL(pool) new(pool, __FILE__, __LINE__)
65-
#define FB_NEW_RPT(pool, count) new(pool, count, __FILE__, __LINE__)
66-
#else // DEBUG_GDS_ALLOC
62+
#include "../common/StdHelper.h"
63+
#endif
64+
6765
#define FB_NEW new(*getDefaultMemoryPool())
6866
#define FB_NEW_POOL(pool) new(pool)
6967
#define FB_NEW_RPT(pool, count) new(pool, count)
70-
#endif // DEBUG_GDS_ALLOC
7168

7269
namespace Firebird {
7370

@@ -185,29 +182,23 @@ friend class ExternalMemoryHandler;
185182

186183
public:
187184
#ifdef DEBUG_GDS_ALLOC
188-
#define ALLOC_ARGS , __FILE__, __LINE__
189-
#define ALLOC_ARGS1 __FILE__, __LINE__,
190-
#define ALLOC_ARGS0 __FILE__, __LINE__
191-
#define ALLOC_PARAMS , const char* file, int line
192-
#define ALLOC_PARAMS1 const char* file, int line,
193-
#define ALLOC_PARAMS0 const char* file, int line
194-
#define ALLOC_PASS_ARGS , file, line
195-
#define ALLOC_PASS_ARGS1 file, line,
196-
#define ALLOC_PASS_ARGS0 file, line
185+
#define ALLOC_PARAMS , const Firebird::CustomSourceLocation location = Firebird::CustomSourceLocation::current()
186+
#define ALLOC_PARAMS_NO_COMMA const Firebird::CustomSourceLocation location = Firebird::CustomSourceLocation::current()
187+
#define ALLOC_PARAMS_DEF , const Firebird::CustomSourceLocation location
188+
#define ALLOC_PARAMS_NO_COMMA_DEF const Firebird::CustomSourceLocation location
189+
#define ALLOC_PASS_ARGS , location
190+
#define ALLOC_PASS_ARGS_NO_COMMA location
197191
#else
198-
#define ALLOC_ARGS
199192
#define ALLOC_PARAMS
193+
#define ALLOC_PARAMS_NO_COMMA
194+
#define ALLOC_PARAMS_DEF
195+
#define ALLOC_PARAMS_NO_COMMA_DEF
200196
#define ALLOC_PASS_ARGS
201-
#define ALLOC_ARGS1
202-
#define ALLOC_PARAMS1
203-
#define ALLOC_PASS_ARGS1
204-
#define ALLOC_ARGS0
205-
#define ALLOC_PARAMS0
206-
#define ALLOC_PASS_ARGS0
197+
#define ALLOC_PASS_ARGS_NO_COMMA
207198
#endif // DEBUG_GDS_ALLOC
208199

209200
// Create memory pool instance
210-
static MemoryPool* createPool(ALLOC_PARAMS1 MemoryPool* parent = NULL, MemoryStats& stats = *default_stats_group);
201+
static MemoryPool* createPool(MemoryPool* parent = NULL, MemoryStats& stats = *default_stats_group ALLOC_PARAMS);
211202
// Delete memory pool instance
212203
static void deletePool(MemoryPool* pool);
213204

@@ -524,7 +515,7 @@ namespace Firebird
524515
public:
525516
constexpr pointer allocate(size_type n, const void* hint = nullptr)
526517
{
527-
return static_cast<T*>(pool.allocate(n * sizeof(T) ALLOC_ARGS));
518+
return static_cast<T*>(pool.allocate(n * sizeof(T)));
528519
}
529520

530521
constexpr void deallocate(pointer p, size_type n)

src/common/classes/array.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ class Array : public Storage
572572
fb_assert(newcapacity < FB_MAX_SIZEOF / sizeof(T));
573573

574574
T* newdata = static_cast<T*>
575-
(this->getPool().allocate(sizeof(T) * newcapacity ALLOC_ARGS));
575+
(this->getPool().allocate(sizeof(T) * newcapacity));
576576
if (preserve)
577577
memcpy(static_cast<void*>(newdata), data, sizeof(T) * count);
578578
freeData();

src/common/classes/misc/class_perf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static void testAllocatorMemoryPool()
204204
{
205205
printf("Test run for Firebird::MemoryPool...\n");
206206
start();
207-
Firebird::MemoryPool* pool = Firebird::MemoryPool::createPool(ALLOC_ARGS0);
207+
Firebird::MemoryPool* pool = Firebird::MemoryPool::createPool();
208208
MallocAllocator allocator;
209209
BePlusTree<AllocItem, AllocItem, MallocAllocator, DefaultKeyValue<AllocItem>, AllocItem> items(&allocator),
210210
bigItems(&allocator);

src/common/classes/misc/class_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ void testAllocator()
506506
printf("Allocate %d large items: ", LARGE_ITEMS);
507507
int i;
508508
for (i = 0; i<LARGE_ITEMS; i++) {
509-
la.add(pool->allocate(LARGE_ITEM_SIZE ALLOC_ARGS));
509+
la.add(pool->allocate(LARGE_ITEM_SIZE));
510510
VERIFY_POOL(pool);
511511
}
512512
VERIFY_POOL(pool);
@@ -518,7 +518,7 @@ void testAllocator()
518518
for (i = 0; i < ALLOC_ITEMS; i++) {
519519
n = n * 47163 - 57412;
520520
// n = n * 45578 - 17651;
521-
AllocItem temp = {n, pool->allocate((n % MAX_ITEM_SIZE + MAX_ITEM_SIZE) / 2 + 1 ALLOC_ARGS)};
521+
AllocItem temp = {n, pool->allocate((n % MAX_ITEM_SIZE + MAX_ITEM_SIZE) / 2 + 1)};
522522
items.add(temp);
523523
}
524524
printf(" DONE\n");
@@ -541,15 +541,15 @@ void testAllocator()
541541
for (i = 0; i < BIG_ITEMS; i++) {
542542
n = n * 47163 - 57412;
543543
// n = n * 45578 - 17651;
544-
AllocItem temp = {n, pool->allocate((n % BIG_SIZE + BIG_SIZE) / 2 + 1 ALLOC_ARGS)};
544+
AllocItem temp = {n, pool->allocate((n % BIG_SIZE + BIG_SIZE) / 2 + 1)};
545545
bigItems.add(temp);
546546
}
547547
printf(" DONE\n");
548548
VERIFY_POOL(pool);
549549
VERIFY_POOL(parent);
550550

551551
printf("Allocate max recommended medium buffer (%d bytes): ", MemoryPool::MAX_MEDIUM_BLOCK_SIZE);
552-
void* maxMedium = pool->allocate(MemoryPool::MAX_MEDIUM_BLOCK_SIZE ALLOC_ARGS);
552+
void* maxMedium = pool->allocate(MemoryPool::MAX_MEDIUM_BLOCK_SIZE);
553553
printf(" DONE\n");
554554
VERIFY_POOL(pool);
555555
VERIFY_POOL(parent);

src/common/classes/zip.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void* ZLib::allocFunc(void*, uInt items, uInt size)
6060
{
6161
try
6262
{
63-
return MemoryPool::globalAlloc(items * size ALLOC_ARGS);
63+
return MemoryPool::globalAlloc(items * size);
6464
}
6565
catch (const Exception&)
6666
{

0 commit comments

Comments
 (0)