Skip to content

Commit ddb1eb3

Browse files
committed
noexcept + const in MetaName
1 parent 95b77a4 commit ddb1eb3

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

src/jrd/MetaName.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "firebird.h"
3030

3131
#include <stdarg.h>
32+
#include <algorithm>
3233

3334
#include "../jrd/MetaName.h"
3435
#include "../common/classes/MetaString.h"
@@ -42,7 +43,7 @@ int MetaName::compare(const char* s, FB_SIZE_T len) const
4243
if (s)
4344
{
4445
adjustLength(s, len);
45-
FB_SIZE_T x = length() < len ? length() : len;
46+
const FB_SIZE_T x = std::min(length(), len);
4647
int rc = memcmp(c_str(), s, x);
4748
if (rc)
4849
{
@@ -55,11 +56,11 @@ int MetaName::compare(const char* s, FB_SIZE_T len) const
5556
return length() - len;
5657
}
5758

58-
void MetaName::adjustLength(const char* const s, FB_SIZE_T& len)
59+
void MetaName::adjustLength(const char* const s, FB_SIZE_T& len) noexcept
5960
{
61+
fb_assert(s || len == 0);
6062
if (len > MAX_SQL_IDENTIFIER_LEN)
6163
{
62-
fb_assert(s);
6364
#ifdef DEV_BUILD
6465
for (FB_SIZE_T i = MAX_SQL_IDENTIFIER_LEN; i < len; ++i)
6566
fb_assert(s[i] == '\0' || s[i] == ' ');
@@ -81,7 +82,7 @@ void MetaName::printf(const char* format, ...)
8182
char data[MAX_SQL_IDENTIFIER_LEN + 1];
8283
va_list params;
8384
va_start(params, format);
84-
int len = VSNPRINTF(data, MAX_SQL_IDENTIFIER_LEN, format, params);
85+
int len = vsnprintf(data, MAX_SQL_IDENTIFIER_LEN, format, params);
8586
va_end(params);
8687

8788
if (len < 0 || FB_SIZE_T(len) > MAX_SQL_IDENTIFIER_LEN)
@@ -105,7 +106,7 @@ FB_SIZE_T MetaName::copyTo(char* to, FB_SIZE_T toSize) const
105106
return toSize;
106107
}
107108

108-
MetaName::operator Firebird::MetaString() const
109+
MetaName::operator Firebird::MetaString() const noexcept
109110
{
110111
return Firebird::MetaString(c_str(), length());
111112
}
@@ -125,7 +126,7 @@ void MetaName::test()
125126
#if defined(DEV_BUILD) || GROW_DEBUG > 0
126127
if (word)
127128
{
128-
Dictionary::Word* checkWord = get(word->c_str(), word->length());
129+
const Dictionary::Word* checkWord = get(word->c_str(), word->length());
129130
fb_assert(checkWord == word);
130131
#ifndef DEV_BUILD
131132
if (word != checkWord)
@@ -138,14 +139,14 @@ void MetaName::test()
138139
const char* MetaName::EMPTY = "";
139140

140141
#if GROW_DEBUG > 1
141-
static const unsigned int hashSize[] = {1000, 2000, 4000, 6000, 8000, 10000,
142+
static constexpr unsigned int hashSize[] = {1000, 2000, 4000, 6000, 8000, 10000,
142143
12000, 14000, 16000, 18000, 20000,
143144
22000, 24000, 26000, 28000, 30000,
144145
32000, 34000, 36000, 38000, 40000,
145146
42000, 44000, 46000, 48000, 50000,
146147
52000, 54000, 56000, 58000, 60000};
147148
#else
148-
static const unsigned int hashSize[] = { 10007, 100003, 1000003 };
149+
static constexpr unsigned int hashSize[] = { 10007, 100003, 1000003 };
149150
#endif
150151

151152
Dictionary::Dictionary(MemoryPool& p)
@@ -187,7 +188,7 @@ Dictionary::HashTable::HashTable(MemoryPool& p, unsigned lvl)
187188
table[n].store(nullptr, std::memory_order_relaxed);
188189
}
189190

190-
unsigned Dictionary::HashTable::getMaxLevel()
191+
unsigned Dictionary::HashTable::getMaxLevel() noexcept
191192
{
192193
return FB_NELEM(hashSize) - 1;
193194
}
@@ -223,7 +224,7 @@ Dictionary::TableData* Dictionary::HashTable::getEntryByHash(const char* s, FB_S
223224
return &table[h];
224225
}
225226

226-
bool Dictionary::checkConsistency(Dictionary::HashTable* oldValue)
227+
bool Dictionary::checkConsistency(const Dictionary::HashTable* oldValue) noexcept
227228
{
228229
return oldValue->level == nextLevel.load();
229230
}
@@ -291,7 +292,7 @@ Dictionary::Word* Dictionary::get(const char* s, FB_SIZE_T len)
291292
{
292293
segment = FB_NEW_POOL(getPool()) Segment;
293294
++segCount;
294-
unsigned lvl = nextLevel.load();
295+
const unsigned lvl = nextLevel.load();
295296
if (lvl < HashTable::getMaxLevel() &&
296297
segCount * Segment::getWordCapacity() > hashSize[lvl])
297298
{
@@ -366,7 +367,7 @@ void Dictionary::growHash()
366367

367368
// move one level up size of hash table
368369
HashTable* tab = hashTable.load();
369-
unsigned lvl = ++nextLevel;
370+
const unsigned lvl = ++nextLevel;
370371
fb_assert(lvl == tab->level + 1);
371372
fb_assert(lvl <= HashTable::getMaxLevel());
372373

@@ -421,7 +422,7 @@ Dictionary::HashTable* Dictionary::waitForMutex(Jrd::Dictionary::Word** checkWor
421422
return t;
422423

423424
// may be we already have that word in new table
424-
FB_SIZE_T len = (*checkWordPtr)->length();
425+
const FB_SIZE_T len = (*checkWordPtr)->length();
425426
const char* s = (*checkWordPtr)->c_str();
426427
Word* word = t->getEntryByHash(s, len)->load();
427428
while (word)
@@ -441,25 +442,25 @@ Dictionary::HashTable* Dictionary::waitForMutex(Jrd::Dictionary::Word** checkWor
441442
return t;
442443
}
443444

444-
Dictionary::Segment::Segment()
445+
Dictionary::Segment::Segment() noexcept
445446
{
446447
position.store(0, std::memory_order_relaxed);
447448
}
448449

449-
unsigned Dictionary::Segment::getWordCapacity()
450+
unsigned constexpr Dictionary::Segment::getWordCapacity() noexcept
450451
{
451-
const unsigned AVERAGE_BYTES_LEN = 16;
452+
constexpr unsigned AVERAGE_BYTES_LEN = 16;
452453
return SEG_BUFFER_SIZE / getWordLength(AVERAGE_BYTES_LEN);
453454
}
454455

455-
unsigned Dictionary::Segment::getWordLength(FB_SIZE_T len)
456+
unsigned constexpr Dictionary::Segment::getWordLength(FB_SIZE_T len) noexcept
456457
{
457458
// calculate length in sizeof(Word*)
458459
len += 2;
459-
return 1 + (len / sizeof(Word*)) + (len % sizeof(Word*) ? 1 : 0);
460+
return 1 + (len / sizeof(Word*)) + ((len % sizeof(Word*)) ? 1 : 0);
460461
}
461462

462-
Dictionary::Word* Dictionary::Segment::getSpace(FB_SIZE_T len DIC_STAT_SEGMENT_PAR)
463+
Dictionary::Word* Dictionary::Segment::getSpace(FB_SIZE_T len DIC_STAT_SEGMENT_PAR) noexcept
463464
{
464465
len = getWordLength(len);
465466

@@ -470,7 +471,7 @@ Dictionary::Word* Dictionary::Segment::getSpace(FB_SIZE_T len DIC_STAT_SEGMENT_P
470471
for(;;)
471472
{
472473
// calculate and check new position
473-
unsigned newPos = oldPos + len;
474+
const unsigned newPos = oldPos + len;
474475
if (newPos >= SEG_BUFFER_SIZE)
475476
break;
476477

src/jrd/MetaName.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ class Dictionary : public Firebird::PermanentStorage
7373
char text[1];
7474

7575
public:
76-
const char* c_str() const
76+
const char* c_str() const noexcept
7777
{
7878
return text;
7979
}
8080

81-
FB_SIZE_T length() const
81+
FB_SIZE_T length() const noexcept
8282
{
8383
return textLen;
8484
}
@@ -102,31 +102,31 @@ class Dictionary : public Firebird::PermanentStorage
102102
public:
103103
HashTable(MemoryPool& p, unsigned lvl);
104104
Dictionary::TableData* getEntryByHash(const char* s, FB_SIZE_T len);
105-
static unsigned getMaxLevel();
105+
static unsigned getMaxLevel() noexcept;
106106

107107
const unsigned level;
108108
TableData* table;
109109
};
110110
std::atomic<HashTable*> hashTable;
111111
std::atomic<unsigned> nextLevel;
112112

113-
bool checkConsistency(HashTable* oldValue);
113+
bool checkConsistency(const HashTable* oldValue) noexcept;
114114
HashTable* waitForMutex(Word** checkWordPtr = nullptr);
115115

116116
class Segment
117117
{
118118
public:
119-
Segment();
120-
Word* getSpace(FB_SIZE_T l DIC_STAT_SEGMENT_PAR);
121-
static unsigned getWordCapacity();
119+
Segment() noexcept;
120+
Word* getSpace(FB_SIZE_T len DIC_STAT_SEGMENT_PAR) noexcept;
121+
static constexpr unsigned getWordCapacity() noexcept;
122122

123123
private:
124-
static unsigned getWordLength(FB_SIZE_T len);
124+
static constexpr unsigned getWordLength(FB_SIZE_T len) noexcept;
125125

126126
#if GROW_DEBUG > 1
127-
static const unsigned SEG_BUFFER_SIZE = 256; // size in sizeof(pointer)
127+
static constexpr unsigned SEG_BUFFER_SIZE = 256; // size in sizeof(pointer)
128128
#else
129-
static const unsigned SEG_BUFFER_SIZE = 16384; // size in sizeof(pointer)
129+
static constexpr unsigned SEG_BUFFER_SIZE = 16384; // size in sizeof(pointer)
130130
#endif
131131
void* buffer[SEG_BUFFER_SIZE];
132132
std::atomic<unsigned> position;
@@ -152,7 +152,7 @@ class MetaName
152152
}
153153

154154
public:
155-
MetaName()
155+
MetaName() noexcept
156156
: word(nullptr)
157157
{ }
158158

@@ -177,7 +177,7 @@ class MetaName
177177
{ }
178178

179179

180-
explicit MetaName(MemoryPool&)
180+
explicit MetaName(MemoryPool&) noexcept
181181
: word(nullptr)
182182
{ }
183183

@@ -233,43 +233,43 @@ class MetaName
233233

234234
MetaName& operator=(const Firebird::MetaString& s);
235235

236-
FB_SIZE_T length() const
236+
FB_SIZE_T length() const noexcept
237237
{
238238
return word ? word->length() : 0;
239239
}
240240

241-
const char* c_str() const
241+
const char* c_str() const noexcept
242242
{
243243
return word ? word->c_str() : EMPTY;
244244
}
245245

246-
const char* nullStr() const
246+
const char* nullStr() const noexcept
247247
{
248248
return word ? word->c_str() : nullptr;
249249
}
250250

251-
bool isEmpty() const
251+
bool isEmpty() const noexcept
252252
{
253253
return !word;
254254
}
255255

256-
bool hasData() const
256+
bool hasData() const noexcept
257257
{
258258
return word;
259259
}
260260

261-
char operator[](unsigned n) const
261+
char operator[](unsigned n) const noexcept
262262
{
263263
fb_assert(n < length());
264264
return word->c_str()[n];
265265
}
266266

267-
const char* begin() const
267+
const char* begin() const noexcept
268268
{
269269
return word ? word->c_str() : EMPTY;
270270
}
271271

272-
const char* end() const
272+
const char* end() const noexcept
273273
{
274274
return word ? &word->c_str()[length()] : EMPTY;
275275
}
@@ -354,22 +354,22 @@ class MetaName
354354
return compare(m) > 0;
355355
}
356356

357-
bool operator==(const MetaName& m) const
357+
bool operator==(const MetaName& m) const noexcept
358358
{
359359
return word == m.word;
360360
}
361361

362-
bool operator!=(const MetaName& m) const
362+
bool operator!=(const MetaName& m) const noexcept
363363
{
364364
return word != m.word;
365365
}
366366

367367
void printf(const char*, ...);
368368
FB_SIZE_T copyTo(char* to, FB_SIZE_T toSize) const;
369-
operator Firebird::MetaString() const;
369+
operator Firebird::MetaString() const noexcept;
370370

371371
protected:
372-
static void adjustLength(const char* const s, FB_SIZE_T& l);
372+
static void adjustLength(const char* const s, FB_SIZE_T& l) noexcept;
373373
};
374374

375375
typedef Firebird::Pair<Firebird::Full<MetaName, MetaName> > MetaNamePair;

0 commit comments

Comments
 (0)