Skip to content

Commit 6808d2e

Browse files
committed
constexpr + noexcept TraceConfigStorage
1 parent 53d121c commit 6808d2e

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

src/jrd/trace/TraceConfigStorage.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "firebird.h"
2929

30+
#include <vector>
3031
#include "../../common/classes/TempFile.h"
3132
#include "../../common/StatusArg.h"
3233
#include "../../common/utils_proto.h"
@@ -64,7 +65,7 @@ using namespace Firebird;
6465

6566
namespace Jrd {
6667

67-
static const FB_UINT64 TOUCH_INTERVAL = 60 * 60; // in seconds, one hour should be enough
68+
static constexpr FB_UINT64 TOUCH_INTERVAL = 60 * 60; // in seconds, one hour should be enough
6869

6970
void checkFileError(const char* filename, const char* operation, ISC_STATUS iscError)
7071
{
@@ -309,7 +310,7 @@ void ConfigStorage::acquire()
309310
m_sharedMemory->mutexLock();
310311
}
311312

312-
TraceCSHeader* header = m_sharedMemory->getHeader();
313+
const TraceCSHeader* header = m_sharedMemory->getHeader();
313314
if (header->mem_allocated > m_sharedMemory->sh_mem_length_mapped)
314315
{
315316
#ifdef HAVE_OBJECT_MAP
@@ -384,7 +385,7 @@ ULONG ConfigStorage::allocSlot(ULONG slotSize)
384385
ULONG lenFound = 0;
385386
for (ULONG i = 0; i < header->slots_cnt; i++)
386387
{
387-
TraceCSHeader::Slot* slot = header->slots + i;
388+
const TraceCSHeader::Slot* slot = header->slots + i;
388389
if (!slot->used && slot->size >= slotSize &&
389390
(!lenFound || lenFound > slot->size))
390391
{
@@ -401,7 +402,7 @@ ULONG ConfigStorage::allocSlot(ULONG slotSize)
401402
// move free slot to the top position
402403
if (idxFound != header->slots_cnt - 1)
403404
{
404-
TraceCSHeader::Slot tmp = header->slots[idxFound];
405+
const TraceCSHeader::Slot tmp = header->slots[idxFound];
405406

406407
const FB_SIZE_T mv = sizeof(TraceCSHeader::Slot) * (header->slots_cnt - idxFound - 1);
407408
memmove(&header->slots[idxFound], &header->slots[idxFound + 1], mv);
@@ -450,7 +451,7 @@ struct SlotByOffset
450451
ULONG index; // slot index
451452
ULONG offset; // initial data ofset
452453

453-
static ULONG generate(const SlotByOffset& i) { return i.offset; }
454+
static ULONG generate(const SlotByOffset& i) noexcept { return i.offset; }
454455
};
455456

456457
typedef SortedArray<SlotByOffset, EmptyStorage<SlotByOffset>, ULONG, SlotByOffset>
@@ -651,10 +652,10 @@ bool ConfigStorage::validate()
651652
}
652653

653654

654-
ULONG ConfigStorage::getSessionSize(const TraceSession& session)
655+
ULONG ConfigStorage::getSessionSize(const TraceSession& session) noexcept
655656
{
656657
ULONG ret = 1; // tagEnd
657-
const ULONG sz = 1 + sizeof(ULONG); // sizeof tag + sizeof len
658+
constexpr ULONG sz = 1 + sizeof(ULONG); // sizeof tag + sizeof len
658659

659660
ULONG len = session.ses_name.length();
660661
if (len)
@@ -672,18 +673,17 @@ ULONG ConfigStorage::getSessionSize(const TraceSession& session)
672673
if ((len = session.ses_config.length()))
673674
ret += sz + len;
674675

675-
if ((len = sizeof(session.ses_start)))
676-
ret += sz + len;
677-
678676
if ((len = session.ses_logfile.length()))
679677
ret += sz + len;
680678

679+
ret += sz + sizeof(session.ses_start);
680+
681681
return ret;
682682
}
683683

684684
bool ConfigStorage::findSession(ULONG sesId, ULONG& idx)
685685
{
686-
TraceCSHeader* header = m_sharedMemory->getHeader();
686+
const TraceCSHeader* header = m_sharedMemory->getHeader();
687687

688688
ULONG hi = header->slots_cnt, lo = 0;
689689
while (hi > lo)
@@ -745,8 +745,8 @@ bool ConfigStorage::getSession(Firebird::TraceSession& session, GET_FLAGS getFla
745745
if (!findSession(session.ses_id, idx))
746746
return false;
747747

748-
TraceCSHeader* header = m_sharedMemory->getHeader();
749-
TraceCSHeader::Slot* slot = &header->slots[idx];
748+
const TraceCSHeader* header = m_sharedMemory->getHeader();
749+
const TraceCSHeader::Slot* slot = &header->slots[idx];
750750

751751
if (slot->ses_id != session.ses_id || !slot->used)
752752
return false;
@@ -756,11 +756,11 @@ bool ConfigStorage::getSession(Firebird::TraceSession& session, GET_FLAGS getFla
756756

757757
bool ConfigStorage::getNextSession(TraceSession& session, GET_FLAGS getFlag, ULONG& nextIdx)
758758
{
759-
TraceCSHeader* header = m_sharedMemory->getHeader();
759+
const TraceCSHeader* header = m_sharedMemory->getHeader();
760760

761761
while (nextIdx < header->slots_cnt)
762762
{
763-
TraceCSHeader::Slot* slot = header->slots + nextIdx;
763+
const TraceCSHeader::Slot* slot = header->slots + nextIdx;
764764
nextIdx++;
765765

766766
if (slot->used)
@@ -769,9 +769,9 @@ bool ConfigStorage::getNextSession(TraceSession& session, GET_FLAGS getFlag, ULO
769769
return false;
770770
}
771771

772-
bool ConfigStorage::readSession(TraceCSHeader::Slot* slot, TraceSession& session, GET_FLAGS getFlag)
772+
bool ConfigStorage::readSession(const TraceCSHeader::Slot* slot, TraceSession& session, GET_FLAGS getFlag)
773773
{
774-
const ULONG getMask[3] =
774+
constexpr ULONG getMask[3] =
775775
{
776776
MAX_ULONG, // ALL
777777
0, // FLAGS
@@ -780,7 +780,7 @@ bool ConfigStorage::readSession(TraceCSHeader::Slot* slot, TraceSession& session
780780
(1 << tagRole) // AUTH
781781
};
782782

783-
TraceCSHeader* header = m_sharedMemory->getHeader();
783+
const TraceCSHeader* header = m_sharedMemory->getHeader();
784784

785785
session.clear();
786786
session.ses_id = slot->ses_id;
@@ -789,7 +789,7 @@ bool ConfigStorage::readSession(TraceCSHeader::Slot* slot, TraceSession& session
789789
if (getFlag == FLAGS)
790790
return true;
791791

792-
char* p = reinterpret_cast<char*> (header) + slot->offset;
792+
const char* p = reinterpret_cast<const char*> (header) + slot->offset;
793793
Reader reader(p, slot->size);
794794

795795
while (true)
@@ -884,7 +884,7 @@ void ConfigStorage::markDeleted(TraceCSHeader::Slot* slot)
884884
slot->used = 0;
885885
}
886886

887-
void ConfigStorage::updateFlags(TraceSession& session)
887+
void ConfigStorage::updateFlags(const TraceSession& session)
888888
{
889889
ULONG idx;
890890
if (!findSession(session.ses_id, idx))
@@ -944,7 +944,7 @@ void ConfigStorage::Writer::write(ITEM tag, ULONG len, const void* data)
944944
m_mem += len;
945945
}
946946

947-
const void* ConfigStorage::Reader::read(ITEM& tag, ULONG& len)
947+
const void* ConfigStorage::Reader::read(ITEM& tag, ULONG& len) noexcept
948948
{
949949
if (m_mem + 1 > m_end)
950950
return NULL;

src/jrd/trace/TraceConfigStorage.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ namespace Jrd {
5252

5353
class StorageGuard;
5454

55-
struct TraceCSHeader : public Firebird::MemoryHeader
55+
struct TraceCSHeader final : public Firebird::MemoryHeader
5656
{
57-
static const USHORT TRACE_STORAGE_VERSION = 2;
58-
static const USHORT TRACE_STORAGE_MAX_SLOTS = 1000;
59-
static const ULONG TRACE_STORAGE_MIN_SIZE = 64 * 1024;
60-
static const ULONG TRACE_STORAGE_MAX_SIZE = 16 * 1024 * 1024;
57+
static constexpr USHORT TRACE_STORAGE_VERSION = 2;
58+
static constexpr USHORT TRACE_STORAGE_MAX_SLOTS = 1000;
59+
static constexpr ULONG TRACE_STORAGE_MIN_SIZE = 64 * 1024;
60+
static constexpr ULONG TRACE_STORAGE_MAX_SIZE = 16 * 1024 * 1024;
6161

6262
struct Slot
6363
{
@@ -94,7 +94,7 @@ class ConfigStorage final : public Firebird::GlobalStorage, public Firebird::Ipc
9494
~ConfigStorage();
9595

9696
void addSession(Firebird::TraceSession& session);
97-
void updateFlags(Firebird::TraceSession& session);
97+
void updateFlags(const Firebird::TraceSession& session);
9898
void removeSession(ULONG id);
9999

100100
// get session by sesion id
@@ -108,21 +108,21 @@ class ConfigStorage final : public Firebird::GlobalStorage, public Firebird::Ipc
108108

109109
void shutdown();
110110

111-
Firebird::Mutex m_localMutex;
111+
mutable Firebird::Mutex m_localMutex;
112112

113113
class Accessor
114114
{
115115
public:
116116
// Use when storage is not locked by caller
117-
explicit Accessor(ConfigStorage* storage) :
117+
explicit Accessor(ConfigStorage* storage) noexcept :
118118
m_storage(storage),
119119
m_guard(nullptr)
120120
{}
121121

122122
// Use when storage is locked by caller
123-
explicit Accessor(StorageGuard* guard);
123+
explicit Accessor(StorageGuard* guard) noexcept;
124124

125-
void restart()
125+
void restart() noexcept
126126
{
127127
m_change_number = 0;
128128
m_sesId = 0;
@@ -168,7 +168,7 @@ class ConfigStorage final : public Firebird::GlobalStorage, public Firebird::Ipc
168168
};
169169
Firebird::RefPtr<TouchFile> m_timer;
170170

171-
void checkDirty()
171+
void checkDirty() noexcept
172172
{
173173
m_dirty = false;
174174
}
@@ -205,10 +205,10 @@ class ConfigStorage final : public Firebird::GlobalStorage, public Firebird::Ipc
205205
void compact();
206206
bool validate();
207207

208-
ULONG getSessionSize(const Firebird::TraceSession& session);
208+
ULONG getSessionSize(const Firebird::TraceSession& session) noexcept;
209209

210210
bool findSession(ULONG sesId, ULONG& idx);
211-
bool readSession(TraceCSHeader::Slot* slot, Firebird::TraceSession& session, GET_FLAGS getFlag);
211+
bool readSession(const TraceCSHeader::Slot* slot, Firebird::TraceSession& session, GET_FLAGS getFlag);
212212

213213
// Search for used slot starting from nextIdx and increments nextIdx to point to the next slot
214214
// returns false, if used slot was not found
@@ -217,13 +217,13 @@ class ConfigStorage final : public Firebird::GlobalStorage, public Firebird::Ipc
217217
class Reader
218218
{
219219
public:
220-
Reader(const void* memory, ULONG size) :
221-
m_mem(reinterpret_cast<const char*>(memory)),
220+
Reader(const void* memory, ULONG size) noexcept :
221+
m_mem(static_cast<const char*>(memory)),
222222
m_end(m_mem + size)
223223
{}
224224

225225
// fill tag and len, returns pointer to data or NULL if data can't be read
226-
const void* read(ITEM& tag, ULONG& len);
226+
const void* read(ITEM& tag, ULONG& len) noexcept;
227227

228228
private:
229229
const char* m_mem;
@@ -233,8 +233,8 @@ class ConfigStorage final : public Firebird::GlobalStorage, public Firebird::Ipc
233233
class Writer
234234
{
235235
public:
236-
Writer(void* memory, ULONG size) :
237-
m_mem(reinterpret_cast<char*>(memory)),
236+
Writer(void* memory, ULONG size) noexcept :
237+
m_mem(static_cast<char*>(memory)),
238238
m_end(m_mem + size)
239239
{}
240240

@@ -284,7 +284,7 @@ class StorageInstance
284284
};
285285

286286

287-
class StorageGuard : public Firebird::MutexLockGuard
287+
class StorageGuard final : public Firebird::MutexLockGuard
288288
{
289289
public:
290290
explicit StorageGuard(ConfigStorage* storage) :
@@ -298,7 +298,7 @@ class StorageGuard : public Firebird::MutexLockGuard
298298
m_storage->release();
299299
}
300300

301-
ConfigStorage* getStorage()
301+
ConfigStorage* getStorage() noexcept
302302
{
303303
return m_storage;
304304
}
@@ -308,7 +308,7 @@ class StorageGuard : public Firebird::MutexLockGuard
308308
};
309309

310310

311-
inline ConfigStorage::Accessor::Accessor(StorageGuard* guard) :
311+
inline ConfigStorage::Accessor::Accessor(StorageGuard* guard) noexcept :
312312
m_storage(guard->getStorage()),
313313
m_guard(guard)
314314
{}

0 commit comments

Comments
 (0)