Skip to content

Commit 95a4d87

Browse files
NoremosArtyom Abakumov
andauthored
Move impure-related string allocation to helper methods (#8609)
* Move impure-related string allocation to helper methods * Use better name for impure string make method * Check dsc_length via type_lengths array * Use different names for new impure helper methods --------- Co-authored-by: Artyom Abakumov <[email protected]>
1 parent c04fc38 commit 95a4d87

File tree

5 files changed

+50
-118
lines changed

5 files changed

+50
-118
lines changed

src/dsql/ExprNodes.cpp

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3755,25 +3755,8 @@ dsc* CastNode::perform(thread_db* tdbb, impure_value* impure, dsc* value,
37553755
impure->vlu_desc.dsc_length = length;
37563756
}
37573757

3758-
length = impure->vlu_desc.dsc_length;
3759-
37603758
// Allocate a string block of sufficient size.
3761-
3762-
auto string = impure->vlu_string;
3763-
3764-
if (string && string->str_length < length)
3765-
{
3766-
delete string;
3767-
string = nullptr;
3768-
}
3769-
3770-
if (!string)
3771-
{
3772-
string = impure->vlu_string = FB_NEW_RPT(*tdbb->getDefaultPool(), length) VaryingString();
3773-
string->str_length = length;
3774-
}
3775-
3776-
impure->vlu_desc.dsc_address = string->str_data;
3759+
impure->makeTextValueAddress(*tdbb->getDefaultPool());
37773760
}
37783761

37793762
EVL_validate(tdbb, Item(Item::TYPE_CAST), itemInfo,
@@ -7143,29 +7126,7 @@ dsc* FieldNode::execute(thread_db* tdbb, Request* request) const
71437126
dsc desc = impure->vlu_desc;
71447127
impure->vlu_desc = format->fmt_desc[fieldId];
71457128

7146-
if (impure->vlu_desc.isText())
7147-
{
7148-
// Allocate a string block of sufficient size.
7149-
VaryingString* string = impure->vlu_string;
7150-
7151-
if (string && string->str_length < impure->vlu_desc.dsc_length)
7152-
{
7153-
delete string;
7154-
string = NULL;
7155-
}
7156-
7157-
if (!string)
7158-
{
7159-
string = impure->vlu_string = FB_NEW_RPT(*tdbb->getDefaultPool(),
7160-
impure->vlu_desc.dsc_length) VaryingString();
7161-
string->str_length = impure->vlu_desc.dsc_length;
7162-
}
7163-
7164-
impure->vlu_desc.dsc_address = string->str_data;
7165-
}
7166-
else
7167-
impure->vlu_desc.dsc_address = (UCHAR*) &impure->vlu_misc;
7168-
7129+
impure->makeValueAddress(*tdbb->getDefaultPool());
71697130
MOV_move(tdbb, &desc, &impure->vlu_desc);
71707131
}
71717132

@@ -13694,30 +13655,7 @@ dsc* UdfCallNode::execute(thread_db* tdbb, Request* request) const
1369413655
const Parameter* const returnParam = function->getOutputFields()[0];
1369513656
value->vlu_desc = returnParam->prm_desc;
1369613657

13697-
// If the return data type is any of the string types, allocate space to hold value.
13698-
13699-
if (value->vlu_desc.dsc_dtype <= dtype_varying)
13700-
{
13701-
const USHORT retLength = value->vlu_desc.dsc_length;
13702-
VaryingString* string = value->vlu_string;
13703-
13704-
if (string && string->str_length < retLength)
13705-
{
13706-
delete string;
13707-
string = NULL;
13708-
}
13709-
13710-
if (!string)
13711-
{
13712-
string = FB_NEW_RPT(*tdbb->getDefaultPool(), retLength) VaryingString;
13713-
string->str_length = retLength;
13714-
value->vlu_string = string;
13715-
}
13716-
13717-
value->vlu_desc.dsc_address = string->str_data;
13718-
}
13719-
else
13720-
value->vlu_desc.dsc_address = (UCHAR*) &value->vlu_misc;
13658+
value->makeValueAddress(*tdbb->getDefaultPool());
1372113659

1372213660
if (!impureArea->temp)
1372313661
{

src/dsql/StmtNodes.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,19 +2248,7 @@ const StmtNode* DeclareVariableNode::execute(thread_db* tdbb, Request* request,
22482248
variable->vlu_desc = varDesc;
22492249
variable->vlu_desc.clearFlags();
22502250

2251-
if (variable->vlu_desc.dsc_dtype <= dtype_varying)
2252-
{
2253-
if (!variable->vlu_string)
2254-
{
2255-
const USHORT len = variable->vlu_desc.dsc_length;
2256-
variable->vlu_string = FB_NEW_RPT(*tdbb->getDefaultPool(), len) VaryingString();
2257-
variable->vlu_string->str_length = len;
2258-
}
2259-
2260-
variable->vlu_desc.dsc_address = variable->vlu_string->str_data;
2261-
}
2262-
else
2263-
variable->vlu_desc.dsc_address = (UCHAR*) &variable->vlu_misc;
2251+
variable->makeValueAddress(*tdbb->getDefaultPool());
22642252

22652253
request->req_operation = Request::req_return;
22662254
}

src/jrd/SysFunction.cpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5545,31 +5545,7 @@ dsc* evlMaxMinValue(thread_db* tdbb, const SysFunction* function, const NestValu
55455545

55465546
DataTypeUtil(tdbb).makeFromList(&impure->vlu_desc, function->name, argTypes.getCount(), argTypes.begin());
55475547

5548-
if (impure->vlu_desc.isText())
5549-
{
5550-
const USHORT length = impure->vlu_desc.dsc_length;
5551-
5552-
// Allocate a string block of sufficient size
5553-
5554-
auto string = impure->vlu_string;
5555-
5556-
if (string && string->str_length < length)
5557-
{
5558-
delete string;
5559-
string = nullptr;
5560-
}
5561-
5562-
if (!string)
5563-
{
5564-
string = impure->vlu_string = FB_NEW_RPT(*tdbb->getDefaultPool(), length) VaryingString();
5565-
string->str_length = length;
5566-
}
5567-
5568-
impure->vlu_desc.dsc_address = string->str_data;
5569-
}
5570-
else
5571-
impure->vlu_desc.dsc_address = (UCHAR*) &impure->vlu_misc;
5572-
5548+
impure->makeValueAddress(*tdbb->getDefaultPool());
55735549
MOV_move(tdbb, result, &impure->vlu_desc);
55745550

55755551
if (impure->vlu_desc.dsc_dtype == dtype_text)

src/jrd/evl.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -573,22 +573,10 @@ void EVL_make_value(thread_db* tdbb, const dsc* desc, impure_value* value, Memor
573573

574574
// Allocate a string block of sufficient size.
575575

576-
VaryingString* string = value->vlu_string;
576+
if (!pool)
577+
pool = tdbb->getDefaultPool();
577578

578-
if (string && string->str_length < length)
579-
{
580-
delete string;
581-
string = NULL;
582-
}
583-
584-
if (!string)
585-
{
586-
if (!pool)
587-
pool = tdbb->getDefaultPool();
588-
589-
string = value->vlu_string = FB_NEW_RPT(*pool, length) VaryingString();
590-
string->str_length = length;
591-
}
579+
VaryingString* string = value->getString(*pool, length);
592580

593581
value->vlu_desc.dsc_length = length;
594582
UCHAR* target = string->str_data;

src/jrd/val.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "../jrd/MetaName.h"
3636
#include "../jrd/RecordNumber.h"
3737
#include "../common/dsc.h"
38+
#include "../jrd/align.h"
3839

3940
#define FLAG_BYTES(n) (((n + BITS_PER_LONG) & ~((ULONG)BITS_PER_LONG - 1)) >> 3)
4041

@@ -168,6 +169,13 @@ struct impure_value
168169
void make_double(const double val);
169170
void make_decimal128(const Firebird::Decimal128 val);
170171
void make_decimal_fixed(const Firebird::Int128 val, const signed char scale);
172+
173+
template<class T>
174+
VaryingString* getString(MemoryPool& pool, const T length) = delete; // Prevent dangerous length shrink
175+
VaryingString* getString(MemoryPool& pool, const USHORT length);
176+
177+
void makeValueAddress(MemoryPool& pool);
178+
void makeTextValueAddress(MemoryPool& pool);
171179
};
172180

173181
// Do not use these methods where dsc_sub_type is not explicitly set to zero.
@@ -221,6 +229,40 @@ inline void impure_value::make_decimal_fixed(const Firebird::Int128 val, const s
221229
this->vlu_desc.dsc_address = reinterpret_cast<UCHAR*>(&this->vlu_misc.vlu_int128);
222230
}
223231

232+
inline VaryingString* impure_value::getString(MemoryPool& pool, const USHORT length)
233+
{
234+
if (vlu_string && vlu_string->str_length < length)
235+
{
236+
delete vlu_string;
237+
vlu_string = nullptr;
238+
}
239+
240+
if (!vlu_string)
241+
{
242+
vlu_string = FB_NEW_RPT(pool, length) VaryingString();
243+
vlu_string->str_length = length;
244+
}
245+
246+
return vlu_string;
247+
}
248+
249+
inline void impure_value::makeValueAddress(MemoryPool& pool)
250+
{
251+
if (type_lengths[vlu_desc.dsc_dtype] == 0)
252+
{
253+
// If the data type is any of the string types, allocate space to hold value.
254+
makeTextValueAddress(pool);
255+
}
256+
else
257+
vlu_desc.dsc_address = (UCHAR*) &vlu_misc;
258+
}
259+
260+
inline void impure_value::makeTextValueAddress(MemoryPool& pool)
261+
{
262+
vlu_desc.dsc_address = getString(pool, vlu_desc.dsc_length)->str_data;
263+
}
264+
265+
224266
struct impure_value_ex : public impure_value
225267
{
226268
SINT64 vlux_count;

0 commit comments

Comments
 (0)