Skip to content

Commit 3bca8db

Browse files
committed
MDEV-6152: Remove calls to current_thd while creating Item
- Part 4: Removing calls to sql_alloc() and sql_calloc() Other things: - Added current_thd in some places to make it clear that it's called (easier to remove later) - Move memory allocation from Item_func_case::fix_length_and_dec() to Item_func_case::fix_fields() - Added mem_root to some new calls - Fixed some wrong UNINIT_VAR() calls - Fixed a bug in generate_partition_syntax() in case of errors - Added mem_root to argument to new thread_info - Simplified my_parse_error() call in sql_yacc.yy
1 parent 3cb578c commit 3bca8db

22 files changed

+296
-253
lines changed

include/my_global.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,13 @@ extern "C" int madvise(void *addr, size_t len, int behav);
477477
#define UNINIT_VAR(x) x
478478
#endif
479479

480+
/* This is only to be used when reseting variables in a class constructor */
481+
#if defined(_lint) || defined(FORCE_INIT_OF_VARS)
482+
#define LINT_INIT(x) x= 0
483+
#else
484+
#define LINT_INIT(x)
485+
#endif
486+
480487
#if !defined(HAVE_UINT)
481488
#undef HAVE_UINT
482489
#define HAVE_UINT

sql/ha_partition.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,8 +1693,8 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info,
16931693
} while (++i < num_parts);
16941694
}
16951695
if (m_reorged_parts &&
1696-
!(m_reorged_file= (handler**)sql_calloc(sizeof(handler*)*
1697-
(m_reorged_parts + 1))))
1696+
!(m_reorged_file= (handler**) thd->calloc(sizeof(handler*)*
1697+
(m_reorged_parts + 1))))
16981698
{
16991699
mem_alloc_error(sizeof(handler*)*(m_reorged_parts+1));
17001700
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
@@ -1725,8 +1725,9 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info,
17251725
}
17261726
} while (++i < num_parts);
17271727
}
1728-
if (!(new_file_array= (handler**)sql_calloc(sizeof(handler*)*
1729-
(2*(num_remain_partitions + 1)))))
1728+
if (!(new_file_array= ((handler**)
1729+
thd->calloc(sizeof(handler*)*
1730+
(2*(num_remain_partitions + 1))))))
17301731
{
17311732
mem_alloc_error(sizeof(handler*)*2*(num_remain_partitions+1));
17321733
DBUG_RETURN(HA_ERR_OUT_OF_MEM);

sql/item.cc

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,18 +2397,21 @@ const char *Item_ident::full_name() const
23972397
char *tmp;
23982398
if (!table_name || !field_name)
23992399
return field_name ? field_name : name ? name : "tmp_field";
2400+
24002401
if (db_name && db_name[0])
24012402
{
2402-
tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
2403-
(uint) strlen(field_name)+3);
2403+
THD *thd= current_thd;
2404+
tmp=(char*) thd->alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
2405+
(uint) strlen(field_name)+3);
24042406
strxmov(tmp,db_name,".",table_name,".",field_name,NullS);
24052407
}
24062408
else
24072409
{
24082410
if (table_name[0])
24092411
{
2410-
tmp= (char*) sql_alloc((uint) strlen(table_name) +
2411-
(uint) strlen(field_name) + 2);
2412+
THD *thd= current_thd;
2413+
tmp= (char*) thd->alloc((uint) strlen(table_name) +
2414+
(uint) strlen(field_name) + 2);
24122415
strxmov(tmp, table_name, ".", field_name, NullS);
24132416
}
24142417
else
@@ -6276,10 +6279,11 @@ inline uint char_val(char X)
62766279
}
62776280

62786281

6279-
void Item_hex_constant::hex_string_init(const char *str, uint str_length)
6282+
void Item_hex_constant::hex_string_init(THD *thd, const char *str,
6283+
uint str_length)
62806284
{
62816285
max_length=(str_length+1)/2;
6282-
char *ptr=(char*) sql_alloc(max_length+1);
6286+
char *ptr=(char*) thd->alloc(max_length+1);
62836287
if (!ptr)
62846288
{
62856289
str_value.set("", 0, &my_charset_bin);
@@ -6372,12 +6376,12 @@ Item_bin_string::Item_bin_string(THD *thd, const char *str, uint str_length):
63726376
Item_hex_hybrid(thd)
63736377
{
63746378
const char *end= str + str_length - 1;
6379+
char *ptr;
63756380
uchar bits= 0;
63766381
uint power= 1;
63776382

63786383
max_length= (str_length + 7) >> 3;
6379-
char *ptr= (char*) sql_alloc(max_length + 1);
6380-
if (!ptr)
6384+
if (!(ptr= (char*) thd->alloc(max_length + 1)))
63816385
return;
63826386
str_value.set(ptr, max_length, &my_charset_bin);
63836387

@@ -8260,9 +8264,10 @@ bool Item_default_value::fix_fields(THD *thd, Item **items)
82608264
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
82618265
goto error;
82628266
}
8263-
if (!(def_field= (Field*) sql_alloc(field_arg->field->size_of())))
8267+
if (!(def_field= (Field*) thd->alloc(field_arg->field->size_of())))
82648268
goto error;
8265-
memcpy((void *)def_field, (void *)field_arg->field, field_arg->field->size_of());
8269+
memcpy((void *)def_field, (void *)field_arg->field,
8270+
field_arg->field->size_of());
82668271
def_field->move_field_offset((my_ptrdiff_t)
82678272
(def_field->table->s->default_values -
82688273
def_field->table->record[0]));
@@ -8402,10 +8407,11 @@ bool Item_insert_value::fix_fields(THD *thd, Item **items)
84028407

84038408
if (field_arg->field->table->insert_values)
84048409
{
8405-
Field *def_field= (Field*) sql_alloc(field_arg->field->size_of());
8410+
Field *def_field= (Field*) thd->alloc(field_arg->field->size_of());
84068411
if (!def_field)
84078412
return TRUE;
8408-
memcpy((void *)def_field, (void *)field_arg->field, field_arg->field->size_of());
8413+
memcpy((void *)def_field, (void *)field_arg->field,
8414+
field_arg->field->size_of());
84098415
def_field->move_field_offset((my_ptrdiff_t)
84108416
(def_field->table->insert_values -
84118417
def_field->table->record[0]));

sql/item.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,16 +3172,16 @@ class Item_return_int :public Item_int
31723172
class Item_hex_constant: public Item_basic_constant
31733173
{
31743174
private:
3175-
void hex_string_init(const char *str, uint str_length);
3175+
void hex_string_init(THD *thd, const char *str, uint str_length);
31763176
public:
31773177
Item_hex_constant(THD *thd): Item_basic_constant(thd)
31783178
{
3179-
hex_string_init("", 0);
3179+
hex_string_init(thd, "", 0);
31803180
}
31813181
Item_hex_constant(THD *thd, const char *str, uint str_length):
31823182
Item_basic_constant(thd)
31833183
{
3184-
hex_string_init(str, str_length);
3184+
hex_string_init(thd, str, str_length);
31853185
}
31863186
enum Type type() const { return VARBIN_ITEM; }
31873187
enum Item_result result_type () const { return STRING_RESULT; }
@@ -3409,7 +3409,7 @@ class Item_args
34093409
{
34103410
protected:
34113411
Item **args, *tmp_arg[2];
3412-
void set_arguments(List<Item> &list);
3412+
void set_arguments(THD *thd, List<Item> &list);
34133413
bool walk_args(Item_processor processor, bool walk_subquery, uchar *arg)
34143414
{
34153415
for (uint i= 0; i < arg_count; i++)
@@ -3463,9 +3463,9 @@ class Item_args
34633463
args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
34643464
}
34653465
}
3466-
Item_args(List<Item> &list)
3466+
Item_args(THD *thd, List<Item> &list)
34673467
{
3468-
set_arguments(list);
3468+
set_arguments(thd, list);
34693469
}
34703470
Item_args(THD *thd, const Item_args *other);
34713471
inline Item **arguments() const { return args; }
@@ -3560,7 +3560,7 @@ class Item_func_or_sum: public Item_result_field, public Item_args
35603560
Item_func_or_sum(THD *thd, Item_func_or_sum *item):
35613561
Item_result_field(thd, item), Item_args(thd, item) { }
35623562
Item_func_or_sum(THD *thd, List<Item> &list):
3563-
Item_result_field(thd), Item_args(list) { }
3563+
Item_result_field(thd), Item_args(thd, list) { }
35643564
bool walk(Item_processor processor, bool walk_subquery, uchar *arg)
35653565
{
35663566
if (walk_args(processor, walk_subquery, arg))

sql/item_cmpfunc.cc

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2790,7 +2790,7 @@ Item_func_case::Item_func_case(THD *thd, List<Item> &list,
27902790
else_expr_num= list.elements;
27912791
list.push_back(else_expr_arg, thd->mem_root);
27922792
}
2793-
set_arguments(list);
2793+
set_arguments(thd, list);
27942794
bzero(&cmp_items, sizeof(cmp_items));
27952795
}
27962796

@@ -2949,6 +2949,10 @@ bool Item_func_case::fix_fields(THD *thd, Item **ref)
29492949
Item_func_case::val_int() -> Item_func_case::find_item()
29502950
*/
29512951
uchar buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(longlong)*2];
2952+
2953+
if (!(arg_buffer= (Item**) thd->alloc(sizeof(Item*)*(ncases+1))))
2954+
return TRUE;
2955+
29522956
bool res= Item_func::fix_fields(thd, ref);
29532957
/*
29542958
Call check_stack_overrun after fix_fields to be sure that stack variable
@@ -3002,14 +3006,11 @@ static void change_item_tree_if_needed(THD *thd,
30023006

30033007
void Item_func_case::fix_length_and_dec()
30043008
{
3005-
Item **agg;
3009+
Item **agg= arg_buffer;
30063010
uint nagg;
30073011
uint found_types= 0;
30083012
THD *thd= current_thd;
30093013

3010-
if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
3011-
return;
3012-
30133014
if (else_expr_num == -1 || args[else_expr_num]->maybe_null)
30143015
maybe_null= 1;
30153016

@@ -3052,8 +3053,9 @@ void Item_func_case::fix_length_and_dec()
30523053
if (else_expr_num != -1)
30533054
agg_num_lengths(args[else_expr_num]);
30543055
max_length= my_decimal_precision_to_length_no_truncation(max_length +
3055-
decimals, decimals,
3056-
unsigned_flag);
3056+
decimals,
3057+
decimals,
3058+
unsigned_flag);
30573059
}
30583060

30593061
/*
@@ -3501,9 +3503,9 @@ Item *in_string::create_item(THD *thd)
35013503
}
35023504

35033505

3504-
in_row::in_row(uint elements, Item * item)
3506+
in_row::in_row(THD *thd, uint elements, Item * item)
35053507
{
3506-
base= (char*) new cmp_item_row[count= elements];
3508+
base= (char*) new (thd->mem_root) cmp_item_row[count= elements];
35073509
size= sizeof(cmp_item_row);
35083510
compare= (qsort2_cmp) cmp_row;
35093511
/*
@@ -3532,7 +3534,7 @@ void in_row::set(uint pos, Item *item)
35323534
{
35333535
DBUG_ENTER("in_row::set");
35343536
DBUG_PRINT("enter", ("pos: %u item: 0x%lx", pos, (ulong) item));
3535-
((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3537+
((cmp_item_row*) base)[pos].store_value_by_template(current_thd, &tmp, item);
35363538
DBUG_VOID_RETURN;
35373539
}
35383540

@@ -3745,7 +3747,7 @@ void cmp_item_row::store_value(Item *item)
37453747
}
37463748

37473749

3748-
void cmp_item_row::store_value_by_template(cmp_item *t, Item *item)
3750+
void cmp_item_row::store_value_by_template(THD *thd, cmp_item *t, Item *item)
37493751
{
37503752
cmp_item_row *tmpl= (cmp_item_row*) t;
37513753
if (tmpl->n != item->cols())
@@ -3754,15 +3756,15 @@ void cmp_item_row::store_value_by_template(cmp_item *t, Item *item)
37543756
return;
37553757
}
37563758
n= tmpl->n;
3757-
if ((comparators= (cmp_item **) sql_alloc(sizeof(cmp_item *)*n)))
3759+
if ((comparators= (cmp_item **) thd->alloc(sizeof(cmp_item *)*n)))
37583760
{
37593761
item->bring_value();
37603762
item->null_value= 0;
37613763
for (uint i=0; i < n; i++)
37623764
{
37633765
if (!(comparators[i]= tmpl->comparators[i]->make_same()))
37643766
break; // new failed
3765-
comparators[i]->store_value_by_template(tmpl->comparators[i],
3767+
comparators[i]->store_value_by_template(thd, tmpl->comparators[i],
37663768
item->element_index(i));
37673769
item->null_value|= item->element_index(i)->null_value;
37683770
}
@@ -4006,12 +4008,12 @@ void Item_func_in::fix_length_and_dec()
40064008

40074009
if (const_itm && !nulls_in_row())
40084010
{
4009-
array= new in_row(arg_count-1, 0);
4011+
array= new (thd->mem_root) in_row(thd, arg_count-1, 0);
40104012
cmp= &((in_row*)array)->tmp;
40114013
}
40124014
else
40134015
{
4014-
if (!(cmp= new cmp_item_row))
4016+
if (!(cmp= new (thd->mem_root) cmp_item_row))
40154017
return;
40164018
cmp_items[ROW_RESULT]= cmp;
40174019
}
@@ -4028,7 +4030,7 @@ void Item_func_in::fix_length_and_dec()
40284030
cmp= ((in_row*)array)->tmp.comparators + col;
40294031
else
40304032
cmp= ((cmp_item_row*)cmp_items[ROW_RESULT])->comparators + col;
4031-
*cmp= new cmp_item_datetime(date_arg);
4033+
*cmp= new (thd->mem_root) cmp_item_datetime(date_arg);
40324034
}
40334035
}
40344036
}
@@ -4067,14 +4069,14 @@ void Item_func_in::fix_length_and_dec()
40674069
}
40684070
switch (cmp_type) {
40694071
case STRING_RESULT:
4070-
array=new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
4071-
cmp_collation.collation);
4072+
array=new (thd->mem_root) in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
4073+
cmp_collation.collation);
40724074
break;
40734075
case INT_RESULT:
4074-
array= new in_longlong(arg_count-1);
4076+
array= new (thd->mem_root) in_longlong(arg_count-1);
40754077
break;
40764078
case REAL_RESULT:
4077-
array= new in_double(arg_count-1);
4079+
array= new (thd->mem_root) in_double(arg_count-1);
40784080
break;
40794081
case ROW_RESULT:
40804082
/*
@@ -4085,11 +4087,11 @@ void Item_func_in::fix_length_and_dec()
40854087
((in_row*)array)->tmp.store_value(args[0]);
40864088
break;
40874089
case DECIMAL_RESULT:
4088-
array= new in_decimal(arg_count - 1);
4090+
array= new (thd->mem_root) in_decimal(arg_count - 1);
40894091
break;
40904092
case TIME_RESULT:
40914093
date_arg= find_date_time_item(args, arg_count, 0);
4092-
array= new in_datetime(date_arg, arg_count - 1);
4094+
array= new (thd->mem_root) in_datetime(date_arg, arg_count - 1);
40934095
break;
40944096
case IMPOSSIBLE_RESULT:
40954097
DBUG_ASSERT(0);
@@ -5063,16 +5065,18 @@ bool Item_func_like::find_selective_predicates_list_processor(uchar *arg)
50635065
(find_selective_predicates_list_processor_data *) arg;
50645066
if (use_sampling && used_tables() == data->table->map)
50655067
{
5066-
COND_STATISTIC *stat= (COND_STATISTIC *)sql_alloc(sizeof(COND_STATISTIC));
5067-
if (!stat)
5068+
THD *thd= data->table->in_use;
5069+
COND_STATISTIC *stat;
5070+
Item *arg0;
5071+
if (!(stat= (COND_STATISTIC *) thd->alloc(sizeof(COND_STATISTIC))))
50685072
return TRUE;
50695073
stat->cond= this;
5070-
Item *arg0= args[0]->real_item();
5074+
arg0= args[0]->real_item();
50715075
if (args[1]->const_item() && arg0->type() == FIELD_ITEM)
50725076
stat->field_arg= ((Item_field *)arg0)->field;
50735077
else
50745078
stat->field_arg= NULL;
5075-
data->list.push_back(stat);
5079+
data->list.push_back(stat, thd->mem_root);
50765080
}
50775081
return FALSE;
50785082
}

sql/item_cmpfunc.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ class cmp_item :public Sql_alloc
11051105
static cmp_item* get_comparator(Item_result type, Item * warn_item,
11061106
CHARSET_INFO *cs);
11071107
virtual cmp_item *make_same()= 0;
1108-
virtual void store_value_by_template(cmp_item *tmpl, Item *item)
1108+
virtual void store_value_by_template(THD *thd, cmp_item *tmpl, Item *item)
11091109
{
11101110
store_value(item);
11111111
}
@@ -1300,6 +1300,7 @@ class Item_func_case :public Item_func_hybrid_field_type
13001300
DTCollation cmp_collation;
13011301
cmp_item *cmp_items[6]; /* For all result types */
13021302
cmp_item *case_item;
1303+
Item **arg_buffer;
13031304
public:
13041305
Item_func_case(THD *thd, List<Item> &list, Item *first_expr_arg,
13051306
Item *else_expr_arg);
@@ -1405,7 +1406,7 @@ class cmp_item_row :public cmp_item
14051406
int cmp(Item *arg);
14061407
int compare(cmp_item *arg);
14071408
cmp_item *make_same();
1408-
void store_value_by_template(cmp_item *tmpl, Item *);
1409+
void store_value_by_template(THD *thd, cmp_item *tmpl, Item *);
14091410
friend void Item_func_in::fix_length_and_dec();
14101411
};
14111412

@@ -1414,7 +1415,7 @@ class in_row :public in_vector
14141415
{
14151416
cmp_item_row tmp;
14161417
public:
1417-
in_row(uint elements, Item *);
1418+
in_row(THD *thd, uint elements, Item *);
14181419
~in_row();
14191420
void set(uint pos,Item *item);
14201421
uchar *get_value(Item *item);

0 commit comments

Comments
 (0)