Skip to content

Commit

Permalink
MDEV-6152: Remove calls to current_thd while creating Item
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
montywi committed Aug 27, 2015
1 parent 3cb578c commit 3bca8db
Show file tree
Hide file tree
Showing 22 changed files with 296 additions and 253 deletions.
7 changes: 7 additions & 0 deletions include/my_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,13 @@ extern "C" int madvise(void *addr, size_t len, int behav);
#define UNINIT_VAR(x) x
#endif

/* This is only to be used when reseting variables in a class constructor */
#if defined(_lint) || defined(FORCE_INIT_OF_VARS)
#define LINT_INIT(x) x= 0
#else
#define LINT_INIT(x)
#endif

#if !defined(HAVE_UINT)
#undef HAVE_UINT
#define HAVE_UINT
Expand Down
9 changes: 5 additions & 4 deletions sql/ha_partition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1693,8 +1693,8 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info,
} while (++i < num_parts);
}
if (m_reorged_parts &&
!(m_reorged_file= (handler**)sql_calloc(sizeof(handler*)*
(m_reorged_parts + 1))))
!(m_reorged_file= (handler**) thd->calloc(sizeof(handler*)*
(m_reorged_parts + 1))))
{
mem_alloc_error(sizeof(handler*)*(m_reorged_parts+1));
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
Expand Down Expand Up @@ -1725,8 +1725,9 @@ int ha_partition::change_partitions(HA_CREATE_INFO *create_info,
}
} while (++i < num_parts);
}
if (!(new_file_array= (handler**)sql_calloc(sizeof(handler*)*
(2*(num_remain_partitions + 1)))))
if (!(new_file_array= ((handler**)
thd->calloc(sizeof(handler*)*
(2*(num_remain_partitions + 1))))))
{
mem_alloc_error(sizeof(handler*)*2*(num_remain_partitions+1));
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
Expand Down
30 changes: 18 additions & 12 deletions sql/item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2397,18 +2397,21 @@ const char *Item_ident::full_name() const
char *tmp;
if (!table_name || !field_name)
return field_name ? field_name : name ? name : "tmp_field";

if (db_name && db_name[0])
{
tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
(uint) strlen(field_name)+3);
THD *thd= current_thd;
tmp=(char*) thd->alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
(uint) strlen(field_name)+3);
strxmov(tmp,db_name,".",table_name,".",field_name,NullS);
}
else
{
if (table_name[0])
{
tmp= (char*) sql_alloc((uint) strlen(table_name) +
(uint) strlen(field_name) + 2);
THD *thd= current_thd;
tmp= (char*) thd->alloc((uint) strlen(table_name) +
(uint) strlen(field_name) + 2);
strxmov(tmp, table_name, ".", field_name, NullS);
}
else
Expand Down Expand Up @@ -6276,10 +6279,11 @@ inline uint char_val(char X)
}


void Item_hex_constant::hex_string_init(const char *str, uint str_length)
void Item_hex_constant::hex_string_init(THD *thd, const char *str,
uint str_length)
{
max_length=(str_length+1)/2;
char *ptr=(char*) sql_alloc(max_length+1);
char *ptr=(char*) thd->alloc(max_length+1);
if (!ptr)
{
str_value.set("", 0, &my_charset_bin);
Expand Down Expand Up @@ -6372,12 +6376,12 @@ Item_bin_string::Item_bin_string(THD *thd, const char *str, uint str_length):
Item_hex_hybrid(thd)
{
const char *end= str + str_length - 1;
char *ptr;
uchar bits= 0;
uint power= 1;

max_length= (str_length + 7) >> 3;
char *ptr= (char*) sql_alloc(max_length + 1);
if (!ptr)
if (!(ptr= (char*) thd->alloc(max_length + 1)))
return;
str_value.set(ptr, max_length, &my_charset_bin);

Expand Down Expand Up @@ -8260,9 +8264,10 @@ bool Item_default_value::fix_fields(THD *thd, Item **items)
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
goto error;
}
if (!(def_field= (Field*) sql_alloc(field_arg->field->size_of())))
if (!(def_field= (Field*) thd->alloc(field_arg->field->size_of())))
goto error;
memcpy((void *)def_field, (void *)field_arg->field, field_arg->field->size_of());
memcpy((void *)def_field, (void *)field_arg->field,
field_arg->field->size_of());
def_field->move_field_offset((my_ptrdiff_t)
(def_field->table->s->default_values -
def_field->table->record[0]));
Expand Down Expand Up @@ -8402,10 +8407,11 @@ bool Item_insert_value::fix_fields(THD *thd, Item **items)

if (field_arg->field->table->insert_values)
{
Field *def_field= (Field*) sql_alloc(field_arg->field->size_of());
Field *def_field= (Field*) thd->alloc(field_arg->field->size_of());
if (!def_field)
return TRUE;
memcpy((void *)def_field, (void *)field_arg->field, field_arg->field->size_of());
memcpy((void *)def_field, (void *)field_arg->field,
field_arg->field->size_of());
def_field->move_field_offset((my_ptrdiff_t)
(def_field->table->insert_values -
def_field->table->record[0]));
Expand Down
14 changes: 7 additions & 7 deletions sql/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -3172,16 +3172,16 @@ class Item_return_int :public Item_int
class Item_hex_constant: public Item_basic_constant
{
private:
void hex_string_init(const char *str, uint str_length);
void hex_string_init(THD *thd, const char *str, uint str_length);
public:
Item_hex_constant(THD *thd): Item_basic_constant(thd)
{
hex_string_init("", 0);
hex_string_init(thd, "", 0);
}
Item_hex_constant(THD *thd, const char *str, uint str_length):
Item_basic_constant(thd)
{
hex_string_init(str, str_length);
hex_string_init(thd, str, str_length);
}
enum Type type() const { return VARBIN_ITEM; }
enum Item_result result_type () const { return STRING_RESULT; }
Expand Down Expand Up @@ -3409,7 +3409,7 @@ class Item_args
{
protected:
Item **args, *tmp_arg[2];
void set_arguments(List<Item> &list);
void set_arguments(THD *thd, List<Item> &list);
bool walk_args(Item_processor processor, bool walk_subquery, uchar *arg)
{
for (uint i= 0; i < arg_count; i++)
Expand Down Expand Up @@ -3463,9 +3463,9 @@ class Item_args
args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
}
}
Item_args(List<Item> &list)
Item_args(THD *thd, List<Item> &list)
{
set_arguments(list);
set_arguments(thd, list);
}
Item_args(THD *thd, const Item_args *other);
inline Item **arguments() const { return args; }
Expand Down Expand Up @@ -3560,7 +3560,7 @@ class Item_func_or_sum: public Item_result_field, public Item_args
Item_func_or_sum(THD *thd, Item_func_or_sum *item):
Item_result_field(thd, item), Item_args(thd, item) { }
Item_func_or_sum(THD *thd, List<Item> &list):
Item_result_field(thd), Item_args(list) { }
Item_result_field(thd), Item_args(thd, list) { }
bool walk(Item_processor processor, bool walk_subquery, uchar *arg)
{
if (walk_args(processor, walk_subquery, arg))
Expand Down
56 changes: 30 additions & 26 deletions sql/item_cmpfunc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2790,7 +2790,7 @@ Item_func_case::Item_func_case(THD *thd, List<Item> &list,
else_expr_num= list.elements;
list.push_back(else_expr_arg, thd->mem_root);
}
set_arguments(list);
set_arguments(thd, list);
bzero(&cmp_items, sizeof(cmp_items));
}

Expand Down Expand Up @@ -2949,6 +2949,10 @@ bool Item_func_case::fix_fields(THD *thd, Item **ref)
Item_func_case::val_int() -> Item_func_case::find_item()
*/
uchar buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(longlong)*2];

if (!(arg_buffer= (Item**) thd->alloc(sizeof(Item*)*(ncases+1))))
return TRUE;

bool res= Item_func::fix_fields(thd, ref);
/*
Call check_stack_overrun after fix_fields to be sure that stack variable
Expand Down Expand Up @@ -3002,14 +3006,11 @@ static void change_item_tree_if_needed(THD *thd,

void Item_func_case::fix_length_and_dec()
{
Item **agg;
Item **agg= arg_buffer;
uint nagg;
uint found_types= 0;
THD *thd= current_thd;

if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
return;

if (else_expr_num == -1 || args[else_expr_num]->maybe_null)
maybe_null= 1;

Expand Down Expand Up @@ -3052,8 +3053,9 @@ void Item_func_case::fix_length_and_dec()
if (else_expr_num != -1)
agg_num_lengths(args[else_expr_num]);
max_length= my_decimal_precision_to_length_no_truncation(max_length +
decimals, decimals,
unsigned_flag);
decimals,
decimals,
unsigned_flag);
}

/*
Expand Down Expand Up @@ -3501,9 +3503,9 @@ Item *in_string::create_item(THD *thd)
}


in_row::in_row(uint elements, Item * item)
in_row::in_row(THD *thd, uint elements, Item * item)
{
base= (char*) new cmp_item_row[count= elements];
base= (char*) new (thd->mem_root) cmp_item_row[count= elements];
size= sizeof(cmp_item_row);
compare= (qsort2_cmp) cmp_row;
/*
Expand Down Expand Up @@ -3532,7 +3534,7 @@ void in_row::set(uint pos, Item *item)
{
DBUG_ENTER("in_row::set");
DBUG_PRINT("enter", ("pos: %u item: 0x%lx", pos, (ulong) item));
((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
((cmp_item_row*) base)[pos].store_value_by_template(current_thd, &tmp, item);
DBUG_VOID_RETURN;
}

Expand Down Expand Up @@ -3745,7 +3747,7 @@ void cmp_item_row::store_value(Item *item)
}


void cmp_item_row::store_value_by_template(cmp_item *t, Item *item)
void cmp_item_row::store_value_by_template(THD *thd, cmp_item *t, Item *item)
{
cmp_item_row *tmpl= (cmp_item_row*) t;
if (tmpl->n != item->cols())
Expand All @@ -3754,15 +3756,15 @@ void cmp_item_row::store_value_by_template(cmp_item *t, Item *item)
return;
}
n= tmpl->n;
if ((comparators= (cmp_item **) sql_alloc(sizeof(cmp_item *)*n)))
if ((comparators= (cmp_item **) thd->alloc(sizeof(cmp_item *)*n)))
{
item->bring_value();
item->null_value= 0;
for (uint i=0; i < n; i++)
{
if (!(comparators[i]= tmpl->comparators[i]->make_same()))
break; // new failed
comparators[i]->store_value_by_template(tmpl->comparators[i],
comparators[i]->store_value_by_template(thd, tmpl->comparators[i],
item->element_index(i));
item->null_value|= item->element_index(i)->null_value;
}
Expand Down Expand Up @@ -4006,12 +4008,12 @@ void Item_func_in::fix_length_and_dec()

if (const_itm && !nulls_in_row())
{
array= new in_row(arg_count-1, 0);
array= new (thd->mem_root) in_row(thd, arg_count-1, 0);
cmp= &((in_row*)array)->tmp;
}
else
{
if (!(cmp= new cmp_item_row))
if (!(cmp= new (thd->mem_root) cmp_item_row))
return;
cmp_items[ROW_RESULT]= cmp;
}
Expand All @@ -4028,7 +4030,7 @@ void Item_func_in::fix_length_and_dec()
cmp= ((in_row*)array)->tmp.comparators + col;
else
cmp= ((cmp_item_row*)cmp_items[ROW_RESULT])->comparators + col;
*cmp= new cmp_item_datetime(date_arg);
*cmp= new (thd->mem_root) cmp_item_datetime(date_arg);
}
}
}
Expand Down Expand Up @@ -4067,14 +4069,14 @@ void Item_func_in::fix_length_and_dec()
}
switch (cmp_type) {
case STRING_RESULT:
array=new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
cmp_collation.collation);
array=new (thd->mem_root) in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
cmp_collation.collation);
break;
case INT_RESULT:
array= new in_longlong(arg_count-1);
array= new (thd->mem_root) in_longlong(arg_count-1);
break;
case REAL_RESULT:
array= new in_double(arg_count-1);
array= new (thd->mem_root) in_double(arg_count-1);
break;
case ROW_RESULT:
/*
Expand All @@ -4085,11 +4087,11 @@ void Item_func_in::fix_length_and_dec()
((in_row*)array)->tmp.store_value(args[0]);
break;
case DECIMAL_RESULT:
array= new in_decimal(arg_count - 1);
array= new (thd->mem_root) in_decimal(arg_count - 1);
break;
case TIME_RESULT:
date_arg= find_date_time_item(args, arg_count, 0);
array= new in_datetime(date_arg, arg_count - 1);
array= new (thd->mem_root) in_datetime(date_arg, arg_count - 1);
break;
case IMPOSSIBLE_RESULT:
DBUG_ASSERT(0);
Expand Down Expand Up @@ -5063,16 +5065,18 @@ bool Item_func_like::find_selective_predicates_list_processor(uchar *arg)
(find_selective_predicates_list_processor_data *) arg;
if (use_sampling && used_tables() == data->table->map)
{
COND_STATISTIC *stat= (COND_STATISTIC *)sql_alloc(sizeof(COND_STATISTIC));
if (!stat)
THD *thd= data->table->in_use;
COND_STATISTIC *stat;
Item *arg0;
if (!(stat= (COND_STATISTIC *) thd->alloc(sizeof(COND_STATISTIC))))
return TRUE;
stat->cond= this;
Item *arg0= args[0]->real_item();
arg0= args[0]->real_item();
if (args[1]->const_item() && arg0->type() == FIELD_ITEM)
stat->field_arg= ((Item_field *)arg0)->field;
else
stat->field_arg= NULL;
data->list.push_back(stat);
data->list.push_back(stat, thd->mem_root);
}
return FALSE;
}
Expand Down
7 changes: 4 additions & 3 deletions sql/item_cmpfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ class cmp_item :public Sql_alloc
static cmp_item* get_comparator(Item_result type, Item * warn_item,
CHARSET_INFO *cs);
virtual cmp_item *make_same()= 0;
virtual void store_value_by_template(cmp_item *tmpl, Item *item)
virtual void store_value_by_template(THD *thd, cmp_item *tmpl, Item *item)
{
store_value(item);
}
Expand Down Expand Up @@ -1300,6 +1300,7 @@ class Item_func_case :public Item_func_hybrid_field_type
DTCollation cmp_collation;
cmp_item *cmp_items[6]; /* For all result types */
cmp_item *case_item;
Item **arg_buffer;
public:
Item_func_case(THD *thd, List<Item> &list, Item *first_expr_arg,
Item *else_expr_arg);
Expand Down Expand Up @@ -1405,7 +1406,7 @@ class cmp_item_row :public cmp_item
int cmp(Item *arg);
int compare(cmp_item *arg);
cmp_item *make_same();
void store_value_by_template(cmp_item *tmpl, Item *);
void store_value_by_template(THD *thd, cmp_item *tmpl, Item *);
friend void Item_func_in::fix_length_and_dec();
};

Expand All @@ -1414,7 +1415,7 @@ class in_row :public in_vector
{
cmp_item_row tmp;
public:
in_row(uint elements, Item *);
in_row(THD *thd, uint elements, Item *);
~in_row();
void set(uint pos,Item *item);
uchar *get_value(Item *item);
Expand Down
Loading

0 comments on commit 3bca8db

Please sign in to comment.