Skip to content

Commit d2caf98

Browse files
committed
noexcept + const in MetaName
1 parent 538b6ce commit d2caf98

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

src/jrd/MetaName.cpp

Lines changed: 21 additions & 22 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);
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)
@@ -135,17 +136,15 @@ void MetaName::test()
135136
#endif
136137
}
137138

138-
const char* MetaName::EMPTY = "";
139-
140139
#if GROW_DEBUG > 1
141-
static const unsigned int hashSize[] = {1000, 2000, 4000, 6000, 8000, 10000,
140+
static constexpr unsigned int hashSize[] = {1000, 2000, 4000, 6000, 8000, 10000,
142141
12000, 14000, 16000, 18000, 20000,
143142
22000, 24000, 26000, 28000, 30000,
144143
32000, 34000, 36000, 38000, 40000,
145144
42000, 44000, 46000, 48000, 50000,
146145
52000, 54000, 56000, 58000, 60000};
147146
#else
148-
static const unsigned int hashSize[] = { 10007, 100003, 1000003 };
147+
static constexpr unsigned int hashSize[] = { 10007, 100003, 1000003 };
149148
#endif
150149

151150
Dictionary::Dictionary(MemoryPool& p)
@@ -187,7 +186,7 @@ Dictionary::HashTable::HashTable(MemoryPool& p, unsigned lvl)
187186
table[n].store(nullptr, std::memory_order_relaxed);
188187
}
189188

190-
unsigned Dictionary::HashTable::getMaxLevel()
189+
unsigned Dictionary::HashTable::getMaxLevel() noexcept
191190
{
192191
return FB_NELEM(hashSize) - 1;
193192
}
@@ -223,7 +222,7 @@ Dictionary::TableData* Dictionary::HashTable::getEntryByHash(const char* s, FB_S
223222
return &table[h];
224223
}
225224

226-
bool Dictionary::checkConsistency(Dictionary::HashTable* oldValue)
225+
bool Dictionary::checkConsistency(const Dictionary::HashTable* oldValue) noexcept
227226
{
228227
return oldValue->level == nextLevel.load();
229228
}
@@ -291,7 +290,7 @@ Dictionary::Word* Dictionary::get(const char* s, FB_SIZE_T len)
291290
{
292291
segment = FB_NEW_POOL(getPool()) Segment;
293292
++segCount;
294-
unsigned lvl = nextLevel.load();
293+
const unsigned lvl = nextLevel.load();
295294
if (lvl < HashTable::getMaxLevel() &&
296295
segCount * Segment::getWordCapacity() > hashSize[lvl])
297296
{
@@ -366,7 +365,7 @@ void Dictionary::growHash()
366365

367366
// move one level up size of hash table
368367
HashTable* tab = hashTable.load();
369-
unsigned lvl = ++nextLevel;
368+
const unsigned lvl = ++nextLevel;
370369
fb_assert(lvl == tab->level + 1);
371370
fb_assert(lvl <= HashTable::getMaxLevel());
372371

@@ -421,7 +420,7 @@ Dictionary::HashTable* Dictionary::waitForMutex(Jrd::Dictionary::Word** checkWor
421420
return t;
422421

423422
// may be we already have that word in new table
424-
FB_SIZE_T len = (*checkWordPtr)->length();
423+
const FB_SIZE_T len = (*checkWordPtr)->length();
425424
const char* s = (*checkWordPtr)->c_str();
426425
Word* word = t->getEntryByHash(s, len)->load();
427426
while (word)
@@ -441,25 +440,25 @@ Dictionary::HashTable* Dictionary::waitForMutex(Jrd::Dictionary::Word** checkWor
441440
return t;
442441
}
443442

444-
Dictionary::Segment::Segment()
443+
Dictionary::Segment::Segment() noexcept
445444
{
446445
position.store(0, std::memory_order_relaxed);
447446
}
448447

449-
unsigned Dictionary::Segment::getWordCapacity()
448+
constexpr unsigned Dictionary::Segment::getWordCapacity() noexcept
450449
{
451-
const unsigned AVERAGE_BYTES_LEN = 16;
450+
constexpr unsigned AVERAGE_BYTES_LEN = 16;
452451
return SEG_BUFFER_SIZE / getWordLength(AVERAGE_BYTES_LEN);
453452
}
454453

455-
unsigned Dictionary::Segment::getWordLength(FB_SIZE_T len)
454+
constexpr unsigned Dictionary::Segment::getWordLength(FB_SIZE_T len) noexcept
456455
{
457456
// calculate length in sizeof(Word*)
458457
len += 2;
459-
return 1 + (len / sizeof(Word*)) + (len % sizeof(Word*) ? 1 : 0);
458+
return 1 + (len / sizeof(Word*)) + ((len % sizeof(Word*)) ? 1 : 0);
460459
}
461460

462-
Dictionary::Word* Dictionary::Segment::getSpace(FB_SIZE_T len DIC_STAT_SEGMENT_PAR)
461+
Dictionary::Word* Dictionary::Segment::getSpace(FB_SIZE_T len DIC_STAT_SEGMENT_PAR) noexcept
463462
{
464463
len = getWordLength(len);
465464

@@ -470,7 +469,7 @@ Dictionary::Word* Dictionary::Segment::getSpace(FB_SIZE_T len DIC_STAT_SEGMENT_P
470469
for(;;)
471470
{
472471
// calculate and check new position
473-
unsigned newPos = oldPos + len;
472+
const unsigned newPos = oldPos + len;
474473
if (newPos >= SEG_BUFFER_SIZE)
475474
break;
476475

src/jrd/MetaName.h

Lines changed: 25 additions & 25 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;
@@ -141,7 +141,7 @@ class MetaName
141141
{
142142
private:
143143
Dictionary::Word* word;
144-
static const char* EMPTY;
144+
static constexpr const char* EMPTY = "";
145145

146146
void test();
147147
Dictionary::Word* get(const char* s, FB_SIZE_T l);
@@ -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)