Skip to content

Commit 54689e1

Browse files
committed
MDEV-8715 - Obsolete sql_alloc() in favor of THD::alloc() and thd_alloc()
The following left in semi-improved state to keep patch size reasonable: - Field operator new: left thd_alloc(current_thd) - Sql_alloc operator new: left thd_alloc(thd_get_current_thd()) - Item_args constructors: left thd_alloc(thd) - Item_func_interval::fix_length_and_dec(): no THD arg, have to call current_thd - Item_func_dyncol_exists::val_int(): same - Item_dyncol_get::val_str(): same - Item_dyncol_get::val_int(): same - Item_dyncol_get::val_real(): same - Item_dyncol_get::val_decimal(): same - Item_singlerow_subselect::fix_length_and_dec(): same
1 parent 753d1f8 commit 54689e1

40 files changed

+113
-160
lines changed

client/mysqlbinlog.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,9 +2443,6 @@ static Exit_status dump_local_log_entries(PRINT_EVENT_INFO *print_event_info,
24432443
return retval;
24442444
}
24452445

2446-
/* Used in sql_alloc(). Inited and freed in main() */
2447-
MEM_ROOT s_mem_root;
2448-
24492446
int main(int argc, char** argv)
24502447
{
24512448
char **defaults_argv;
@@ -2458,7 +2455,6 @@ int main(int argc, char** argv)
24582455
my_init_time(); // for time functions
24592456
tzset(); // set tzname
24602457

2461-
init_alloc_root(&s_mem_root, 16384, 0, MYF(0));
24622458
if (load_defaults("my", load_groups, &argc, &argv))
24632459
exit(1);
24642460

@@ -2569,7 +2565,6 @@ int main(int argc, char** argv)
25692565
my_fclose(result_file, MYF(0));
25702566
cleanup();
25712567
free_annotate_event();
2572-
free_root(&s_mem_root, MYF(0));
25732568
free_defaults(defaults_argv);
25742569
my_free_open_file_info();
25752570
load_processor.destroy();
@@ -2582,11 +2577,6 @@ int main(int argc, char** argv)
25822577
}
25832578

25842579

2585-
void *sql_alloc(size_t size)
2586-
{
2587-
return alloc_root(&s_mem_root, size);
2588-
}
2589-
25902580
struct encryption_service_st encryption_handler=
25912581
{
25922582
0, 0, 0, 0, 0, 0, 0

sql/field.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ class Field: public Value_source
610610
static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
611611
{ return alloc_root(mem_root, size); }
612612
static void *operator new(size_t size) throw ()
613-
{ return sql_alloc(size); }
613+
{ return thd_alloc(current_thd, size); }
614614
static void operator delete(void *ptr_arg, size_t size) { TRASH(ptr_arg, size); }
615615
static void operator delete(void *ptr, MEM_ROOT *mem_root)
616616
{ DBUG_ASSERT(0); }

sql/ha_partition.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,8 @@ static bool print_admin_msg(THD* thd, uint len,
12811281
length=(uint) (strxmov(name, db_name, ".", table_name.c_ptr_safe(), NullS) - name);
12821282
/*
12831283
TODO: switch from protocol to push_warning here. The main reason we didn't
1284-
it yet is parallel repair. Due to following trace:
1285-
mi_check_print_msg/push_warning/sql_alloc/my_pthread_getspecific_ptr.
1284+
it yet is parallel repair, which threads have no THD object accessible via
1285+
current_thd.
12861286
12871287
Also we likely need to lock mutex here (in both cases with protocol and
12881288
push_warning).
@@ -9058,7 +9058,7 @@ int ha_partition::check_for_upgrade(HA_CHECK_OPT *check_opt)
90589058
}
90599059
m_part_info->key_algorithm= partition_info::KEY_ALGORITHM_51;
90609060
if (skip_generation ||
9061-
!(part_buf= generate_partition_syntax(m_part_info,
9061+
!(part_buf= generate_partition_syntax(thd, m_part_info,
90629062
&part_buf_len,
90639063
true,
90649064
true,

sql/item.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ void Item::set_name(THD *thd, const char *str, uint length, CHARSET_INFO *cs)
10181018
if (!my_charset_same(cs, system_charset_info))
10191019
{
10201020
size_t res_length;
1021-
name= sql_strmake_with_convert(str, length, cs,
1021+
name= sql_strmake_with_convert(thd, str, length, cs,
10221022
MAX_ALIAS_NAME, system_charset_info,
10231023
&res_length);
10241024
name_length= res_length;
@@ -1034,7 +1034,7 @@ void Item::set_name_no_truncate(THD *thd, const char *str, uint length,
10341034
if (!my_charset_same(cs, system_charset_info))
10351035
{
10361036
size_t res_length;
1037-
name= sql_strmake_with_convert(str, length, cs,
1037+
name= sql_strmake_with_convert(thd, str, length, cs,
10381038
UINT_MAX, system_charset_info,
10391039
&res_length);
10401040
name_length= res_length;
@@ -8483,7 +8483,7 @@ void resolve_const_item(THD *thd, Item **ref, Item *comp_item)
84838483

84848484
Item *new_item= NULL;
84858485
Item_result res_type= item_cmp_type(comp_item, item);
8486-
char *name=item->name; // Alloced by sql_alloc
8486+
char *name= item->name; // Alloced on THD::mem_root
84878487
MEM_ROOT *mem_root= thd->mem_root;
84888488

84898489
switch (res_type) {

sql/item.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ class Item: public Value_source,
696696
bool with_subselect; /* If this item is a subselect or some
697697
of its arguments is or contains a
698698
subselect */
699-
// alloc & destruct is done as start of select using sql_alloc
699+
// alloc & destruct is done as start of select on THD::mem_root
700700
Item(THD *thd);
701701
/*
702702
Constructor used by Item_field, Item_ref & aggregate (sum) functions.
@@ -3560,28 +3560,28 @@ class Item_args
35603560
{
35613561
args[0]= a; args[1]= b;
35623562
}
3563-
Item_args(Item *a, Item *b, Item *c)
3563+
Item_args(THD *thd, Item *a, Item *b, Item *c)
35643564
{
35653565
arg_count= 0;
3566-
if ((args= (Item**) sql_alloc(sizeof(Item*) * 3)))
3566+
if ((args= (Item**) thd_alloc(thd, sizeof(Item*) * 3)))
35673567
{
35683568
arg_count= 3;
35693569
args[0]= a; args[1]= b; args[2]= c;
35703570
}
35713571
}
3572-
Item_args(Item *a, Item *b, Item *c, Item *d)
3572+
Item_args(THD *thd, Item *a, Item *b, Item *c, Item *d)
35733573
{
35743574
arg_count= 0;
3575-
if ((args= (Item**) sql_alloc(sizeof(Item*) * 4)))
3575+
if ((args= (Item**) thd_alloc(thd, sizeof(Item*) * 4)))
35763576
{
35773577
arg_count= 4;
35783578
args[0]= a; args[1]= b; args[2]= c; args[3]= d;
35793579
}
35803580
}
3581-
Item_args(Item *a, Item *b, Item *c, Item *d, Item* e)
3581+
Item_args(THD *thd, Item *a, Item *b, Item *c, Item *d, Item* e)
35823582
{
35833583
arg_count= 5;
3584-
if ((args= (Item**) sql_alloc(sizeof(Item*) * 5)))
3584+
if ((args= (Item**) thd_alloc(thd, sizeof(Item*) * 5)))
35853585
{
35863586
arg_count= 5;
35873587
args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
@@ -3791,11 +3791,11 @@ class Item_func_or_sum: public Item_result_field,
37913791
Item_func_or_sum(THD *thd, Item *a, Item *b):
37923792
Item_result_field(thd), Item_args(a, b) { }
37933793
Item_func_or_sum(THD *thd, Item *a, Item *b, Item *c):
3794-
Item_result_field(thd), Item_args(a, b, c) { }
3794+
Item_result_field(thd), Item_args(thd, a, b, c) { }
37953795
Item_func_or_sum(THD *thd, Item *a, Item *b, Item *c, Item *d):
3796-
Item_result_field(thd), Item_args(a, b, c, d) { }
3796+
Item_result_field(thd), Item_args(thd, a, b, c, d) { }
37973797
Item_func_or_sum(THD *thd, Item *a, Item *b, Item *c, Item *d, Item *e):
3798-
Item_result_field(thd), Item_args(a, b, c, d, e) { }
3798+
Item_result_field(thd), Item_args(thd, a, b, c, d, e) { }
37993799
Item_func_or_sum(THD *thd, Item_func_or_sum *item):
38003800
Item_result_field(thd, item), Item_args(thd, item),
38013801
Used_tables_and_const_cache(item) { }

sql/item_cmpfunc.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,8 +1846,8 @@ void Item_func_interval::fix_length_and_dec()
18461846
}
18471847

18481848
if (not_null_consts &&
1849-
(intervals=
1850-
(interval_range*) sql_alloc(sizeof(interval_range) * (rows - 1))))
1849+
(intervals= (interval_range*) current_thd->alloc(sizeof(interval_range) *
1850+
(rows - 1))))
18511851
{
18521852
if (use_decimal_comparison)
18531853
{
@@ -3462,7 +3462,7 @@ in_string::~in_string()
34623462
{
34633463
if (base)
34643464
{
3465-
// base was allocated with help of sql_alloc => following is OK
3465+
// base was allocated on THD::mem_root => following is OK
34663466
for (uint i=0 ; i < count ; i++)
34673467
((String*) base)[i].free();
34683468
}
@@ -6567,10 +6567,9 @@ longlong Item_func_dyncol_exists::val_int()
65676567
}
65686568
else
65696569
{
6570-
uint strlen;
6570+
uint strlen= nm->length() * my_charset_utf8_general_ci.mbmaxlen + 1;
65716571
uint dummy_errors;
6572-
buf.str= (char *)sql_alloc((strlen= nm->length() *
6573-
my_charset_utf8_general_ci.mbmaxlen + 1));
6572+
buf.str= (char *) current_thd->alloc(strlen);
65746573
if (buf.str)
65756574
{
65766575
buf.length=

sql/item_func.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3553,7 +3553,7 @@ udf_handler::fix_fields(THD *thd, Item_func_or_sum *func,
35533553
func->used_tables_and_const_cache_join(item);
35543554
f_args.arg_type[i]=item->result_type();
35553555
}
3556-
//TODO: why all following memory is not allocated with 1 call of sql_alloc?
3556+
//TODO: why all following memory is not allocated with 1 thd->alloc() call?
35573557
if (!(buffers=new String[arg_count]) ||
35583558
!(f_args.args= (char**) thd->alloc(arg_count * sizeof(char *))) ||
35593559
!(f_args.lengths= (ulong*) thd->alloc(arg_count * sizeof(long))) ||

sql/item_strfunc.cc

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4436,11 +4436,9 @@ bool Item_func_dyncol_create::prepare_arguments(THD *thd, bool force_names_arg)
44364436
}
44374437
else
44384438
{
4439-
uint strlen;
4439+
uint strlen= res->length() * my_charset_utf8_general_ci.mbmaxlen + 1;
44404440
uint dummy_errors;
4441-
char *str=
4442-
(char *)sql_alloc((strlen= res->length() *
4443-
my_charset_utf8_general_ci.mbmaxlen + 1));
4441+
char *str= (char *) thd->alloc(strlen);
44444442
if (str)
44454443
{
44464444
keys_str[i].length=
@@ -4742,7 +4740,8 @@ void Item_func_dyncol_add::print(String *str,
47424740
This function ensures that null_value is set correctly
47434741
*/
47444742

4745-
bool Item_dyncol_get::get_dyn_value(DYNAMIC_COLUMN_VALUE *val, String *tmp)
4743+
bool Item_dyncol_get::get_dyn_value(THD *thd, DYNAMIC_COLUMN_VALUE *val,
4744+
String *tmp)
47464745
{
47474746
DYNAMIC_COLUMN dyn_str;
47484747
String *res;
@@ -4770,10 +4769,9 @@ bool Item_dyncol_get::get_dyn_value(DYNAMIC_COLUMN_VALUE *val, String *tmp)
47704769
}
47714770
else
47724771
{
4773-
uint strlen;
4772+
uint strlen= nm->length() * my_charset_utf8_general_ci.mbmaxlen + 1;
47744773
uint dummy_errors;
4775-
buf.str= (char *)sql_alloc((strlen= nm->length() *
4776-
my_charset_utf8_general_ci.mbmaxlen + 1));
4774+
buf.str= (char *) thd->alloc(strlen);
47774775
if (buf.str)
47784776
{
47794777
buf.length=
@@ -4823,7 +4821,7 @@ String *Item_dyncol_get::val_str(String *str_result)
48234821
char buff[STRING_BUFFER_USUAL_SIZE];
48244822
String tmp(buff, sizeof(buff), &my_charset_bin);
48254823

4826-
if (get_dyn_value(&val, &tmp))
4824+
if (get_dyn_value(current_thd, &val, &tmp))
48274825
return NULL;
48284826

48294827
switch (val.type) {
@@ -4905,11 +4903,12 @@ String *Item_dyncol_get::val_str(String *str_result)
49054903

49064904
longlong Item_dyncol_get::val_int()
49074905
{
4906+
THD *thd= current_thd;
49084907
DYNAMIC_COLUMN_VALUE val;
49094908
char buff[STRING_BUFFER_USUAL_SIZE];
49104909
String tmp(buff, sizeof(buff), &my_charset_bin);
49114910

4912-
if (get_dyn_value(&val, &tmp))
4911+
if (get_dyn_value(thd, &val, &tmp))
49134912
return 0;
49144913

49154914
switch (val.type) {
@@ -4930,7 +4929,6 @@ longlong Item_dyncol_get::val_int()
49304929
num= double_to_longlong(val.x.double_value, unsigned_flag, &error);
49314930
if (error)
49324931
{
4933-
THD *thd= current_thd;
49344932
char buff[30];
49354933
sprintf(buff, "%lg", val.x.double_value);
49364934
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
@@ -4950,7 +4948,6 @@ longlong Item_dyncol_get::val_int()
49504948
num= my_strtoll10(val.x.string.value.str, &end, &error);
49514949
if (end != org_end || error > 0)
49524950
{
4953-
THD *thd= current_thd;
49544951
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
49554952
ER_BAD_DATA,
49564953
ER_THD(thd, ER_BAD_DATA),
@@ -4987,11 +4984,12 @@ longlong Item_dyncol_get::val_int()
49874984

49884985
double Item_dyncol_get::val_real()
49894986
{
4987+
THD *thd= current_thd;
49904988
DYNAMIC_COLUMN_VALUE val;
49914989
char buff[STRING_BUFFER_USUAL_SIZE];
49924990
String tmp(buff, sizeof(buff), &my_charset_bin);
49934991

4994-
if (get_dyn_value(&val, &tmp))
4992+
if (get_dyn_value(thd, &val, &tmp))
49954993
return 0.0;
49964994

49974995
switch (val.type) {
@@ -5014,7 +5012,6 @@ double Item_dyncol_get::val_real()
50145012
if (end != (char*) val.x.string.value.str + val.x.string.value.length ||
50155013
error)
50165014
{
5017-
THD *thd= current_thd;
50185015
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
50195016
ER_BAD_DATA,
50205017
ER_THD(thd, ER_BAD_DATA),
@@ -5046,11 +5043,12 @@ double Item_dyncol_get::val_real()
50465043

50475044
my_decimal *Item_dyncol_get::val_decimal(my_decimal *decimal_value)
50485045
{
5046+
THD *thd= current_thd;
50495047
DYNAMIC_COLUMN_VALUE val;
50505048
char buff[STRING_BUFFER_USUAL_SIZE];
50515049
String tmp(buff, sizeof(buff), &my_charset_bin);
50525050

5053-
if (get_dyn_value(&val, &tmp))
5051+
if (get_dyn_value(thd, &val, &tmp))
50545052
return NULL;
50555053

50565054
switch (val.type) {
@@ -5075,7 +5073,6 @@ my_decimal *Item_dyncol_get::val_decimal(my_decimal *decimal_value)
50755073
if (rc != E_DEC_OK ||
50765074
end != val.x.string.value.str + val.x.string.value.length)
50775075
{
5078-
THD *thd= current_thd;
50795076
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
50805077
ER_BAD_DATA,
50815078
ER_THD(thd, ER_BAD_DATA),
@@ -5110,7 +5107,7 @@ bool Item_dyncol_get::get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
51105107
String tmp(buff, sizeof(buff), &my_charset_bin);
51115108
bool signed_value= 0;
51125109

5113-
if (get_dyn_value(&val, &tmp))
5110+
if (get_dyn_value(current_thd, &val, &tmp))
51145111
return 1; // Error
51155112

51165113
switch (val.type) {

sql/item_strfunc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ class Item_dyncol_get: public Item_str_func
12531253
longlong val_int();
12541254
double val_real();
12551255
my_decimal *val_decimal(my_decimal *);
1256-
bool get_dyn_value(DYNAMIC_COLUMN_VALUE *val, String *tmp);
1256+
bool get_dyn_value(THD *thd, DYNAMIC_COLUMN_VALUE *val, String *tmp);
12571257
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
12581258
void print(String *str, enum_query_type query_type);
12591259
};

sql/item_subselect.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,8 @@ void Item_singlerow_subselect::fix_length_and_dec()
11491149
}
11501150
else
11511151
{
1152-
if (!(row= (Item_cache**) sql_alloc(sizeof(Item_cache*)*max_columns)))
1152+
if (!(row= (Item_cache**) current_thd->alloc(sizeof(Item_cache*) *
1153+
max_columns)))
11531154
return;
11541155
engine->fix_length_and_dec(row);
11551156
value= *row;

0 commit comments

Comments
 (0)