[Commits] 232d9c1: MDEV-8715 - Obsolete sql_alloc() in favor of THD::alloc() and thd_alloc()

Sergey Vojtovich svoj at mariadb.org
Thu Nov 19 14:53:22 EET 2015


revision-id: 232d9c1e5325d04f8c71095b5bf2d570de86939b (mariadb-10.1.8-56-g232d9c1)
parent(s): 0782e19c5879403f5259a09acaa7b5623c3e0867
committer: Sergey Vojtovich
timestamp: 2015-11-19 16:50:09 +0400
message:

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

---
 client/mysqlbinlog.cc             | 10 ----------
 sql/field.h                       |  2 +-
 sql/ha_partition.cc               |  6 +++---
 sql/item.cc                       |  6 +++---
 sql/item.h                        | 20 ++++++++++----------
 sql/item_cmpfunc.cc               | 11 +++++------
 sql/item_func.cc                  |  2 +-
 sql/item_strfunc.cc               | 31 ++++++++++++++-----------------
 sql/item_strfunc.h                |  2 +-
 sql/item_subselect.cc             |  3 ++-
 sql/mysqld.cc                     |  6 +-----
 sql/mysqld.h                      |  6 ------
 sql/sp_head.cc                    |  6 +++---
 sql/sp_head.h                     |  2 +-
 sql/sql_acl.cc                    | 13 +++++--------
 sql/sql_analyse.cc                |  2 +-
 sql/sql_class.cc                  | 28 +++++++++++++---------------
 sql/sql_class.h                   |  1 +
 sql/sql_handler.cc                |  2 +-
 sql/sql_lex.h                     |  4 ----
 sql/sql_list.h                    |  7 ++++---
 sql/sql_load.cc                   |  8 ++++----
 sql/sql_parse.cc                  |  8 ++++----
 sql/sql_partition.cc              |  4 ++--
 sql/sql_partition.h               |  2 +-
 sql/sql_select.cc                 |  8 ++++----
 sql/sql_show.cc                   |  2 +-
 sql/sql_statistics.cc             |  2 +-
 sql/sql_table.cc                  |  6 +++---
 sql/sql_yacc.yy                   | 18 +++++++++---------
 sql/table.cc                      | 10 ++++------
 sql/thr_malloc.cc                 | 13 ++-----------
 sql/thr_malloc.h                  |  3 +--
 sql/threadpool_common.cc          |  1 -
 sql/tztime.cc                     |  2 --
 sql/unireg.cc                     |  4 +---
 storage/maria/ha_maria.cc         |  4 ++--
 storage/myisam/ha_myisam.cc       |  4 ++--
 storage/myisammrg/ha_myisammrg.cc |  2 +-
 storage/spider/spd_table.cc       |  2 +-
 40 files changed, 113 insertions(+), 160 deletions(-)

diff --git a/client/mysqlbinlog.cc b/client/mysqlbinlog.cc
index 587e538..069fe29 100644
--- a/client/mysqlbinlog.cc
+++ b/client/mysqlbinlog.cc
@@ -2431,9 +2431,6 @@ static Exit_status dump_local_log_entries(PRINT_EVENT_INFO *print_event_info,
   return retval;
 }
 
-/* Used in sql_alloc(). Inited and freed in main() */
-MEM_ROOT s_mem_root;
-
 int main(int argc, char** argv)
 {
   char **defaults_argv;
@@ -2446,7 +2443,6 @@ int main(int argc, char** argv)
   my_init_time(); // for time functions
   tzset(); // set tzname
 
-  init_alloc_root(&s_mem_root, 16384, 0, MYF(0));
   if (load_defaults("my", load_groups, &argc, &argv))
     exit(1);
 
@@ -2557,7 +2553,6 @@ int main(int argc, char** argv)
     my_fclose(result_file, MYF(0));
   cleanup();
   free_annotate_event();
-  free_root(&s_mem_root, MYF(0));
   free_defaults(defaults_argv);
   my_free_open_file_info();
   load_processor.destroy();
@@ -2570,11 +2565,6 @@ int main(int argc, char** argv)
 }
 
 
-void *sql_alloc(size_t size)
-{
-  return alloc_root(&s_mem_root, size);
-}
-
 struct encryption_service_st encryption_handler=
 {
   0, 0, 0, 0, 0, 0, 0
diff --git a/sql/field.h b/sql/field.h
index cb7f94b..9a958b0 100644
--- a/sql/field.h
+++ b/sql/field.h
@@ -610,7 +610,7 @@ class Field: public Value_source
   static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
   { return alloc_root(mem_root, size); }
   static void *operator new(size_t size) throw ()
-  { return sql_alloc(size); }
+  { return thd_alloc(current_thd, size); }
   static void operator delete(void *ptr_arg, size_t size) { TRASH(ptr_arg, size); }
   static void operator delete(void *ptr, MEM_ROOT *mem_root)
   { DBUG_ASSERT(0); }
diff --git a/sql/ha_partition.cc b/sql/ha_partition.cc
index 043f7fb..cf14321 100644
--- a/sql/ha_partition.cc
+++ b/sql/ha_partition.cc
@@ -1281,8 +1281,8 @@ static bool print_admin_msg(THD* thd, uint len,
   length=(uint) (strxmov(name, db_name, ".", table_name.c_ptr_safe(), NullS) - name);
   /*
      TODO: switch from protocol to push_warning here. The main reason we didn't
-     it yet is parallel repair. Due to following trace:
-     mi_check_print_msg/push_warning/sql_alloc/my_pthread_getspecific_ptr.
+     it yet is parallel repair, which threads have no THD object accessible via
+     current_thd.
 
      Also we likely need to lock mutex here (in both cases with protocol and
      push_warning).
@@ -9057,7 +9057,7 @@ int ha_partition::check_for_upgrade(HA_CHECK_OPT *check_opt)
           }
           m_part_info->key_algorithm= partition_info::KEY_ALGORITHM_51;
           if (skip_generation ||
-              !(part_buf= generate_partition_syntax(m_part_info,
+              !(part_buf= generate_partition_syntax(thd, m_part_info,
                                                     &part_buf_len,
                                                     true,
                                                     true,
diff --git a/sql/item.cc b/sql/item.cc
index 346dc1a..5ce6140 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -1019,7 +1019,7 @@ void Item::set_name(THD *thd, const char *str, uint length, CHARSET_INFO *cs)
   if (!my_charset_same(cs, system_charset_info))
   {
     size_t res_length;
-    name= sql_strmake_with_convert(str, length, cs,
+    name= sql_strmake_with_convert(thd, str, length, cs,
 				   MAX_ALIAS_NAME, system_charset_info,
 				   &res_length);
     name_length= res_length;
@@ -1035,7 +1035,7 @@ void Item::set_name_no_truncate(THD *thd, const char *str, uint length,
   if (!my_charset_same(cs, system_charset_info))
   {
     size_t res_length;
-    name= sql_strmake_with_convert(str, length, cs,
+    name= sql_strmake_with_convert(thd, str, length, cs,
 				   UINT_MAX, system_charset_info,
 				   &res_length);
     name_length= res_length;
@@ -8467,7 +8467,7 @@ void resolve_const_item(THD *thd, Item **ref, Item *comp_item)
 
   Item *new_item= NULL;
   Item_result res_type= item_cmp_type(comp_item, item);
-  char *name=item->name;			// Alloced by sql_alloc
+  char *name= item->name;                       // Alloced on THD::mem_root
   MEM_ROOT *mem_root= thd->mem_root;
 
   switch (res_type) {
diff --git a/sql/item.h b/sql/item.h
index 8d238ba..76987c2 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -697,7 +697,7 @@ class Item: public Value_source,
   bool with_subselect;                  /* If this item is a subselect or some
                                            of its arguments is or contains a
                                            subselect */
-  // alloc & destruct is done as start of select using sql_alloc
+  // alloc & destruct is done as start of select on THD::mem_root
   Item(THD *thd);
   /*
      Constructor used by Item_field, Item_ref & aggregate (sum) functions.
@@ -3561,28 +3561,28 @@ class Item_args
   {
     args[0]= a; args[1]= b;
   }
-  Item_args(Item *a, Item *b, Item *c)
+  Item_args(THD *thd, Item *a, Item *b, Item *c)
   {
     arg_count= 0;
-    if ((args= (Item**) sql_alloc(sizeof(Item*) * 3)))
+    if ((args= (Item**) thd_alloc(thd, sizeof(Item*) * 3)))
     {
       arg_count= 3;
       args[0]= a; args[1]= b; args[2]= c;
     }
   }
-  Item_args(Item *a, Item *b, Item *c, Item *d)
+  Item_args(THD *thd, Item *a, Item *b, Item *c, Item *d)
   {
     arg_count= 0;
-    if ((args= (Item**) sql_alloc(sizeof(Item*) * 4)))
+    if ((args= (Item**) thd_alloc(thd, sizeof(Item*) * 4)))
     {
       arg_count= 4;
       args[0]= a; args[1]= b; args[2]= c; args[3]= d;
     }
   }
-  Item_args(Item *a, Item *b, Item *c, Item *d, Item* e)
+  Item_args(THD *thd, Item *a, Item *b, Item *c, Item *d, Item* e)
   {
     arg_count= 5;
-    if ((args= (Item**) sql_alloc(sizeof(Item*) * 5)))
+    if ((args= (Item**) thd_alloc(thd, sizeof(Item*) * 5)))
     {
       arg_count= 5;
       args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
@@ -3792,11 +3792,11 @@ class Item_func_or_sum: public Item_result_field,
   Item_func_or_sum(THD *thd, Item *a, Item *b):
     Item_result_field(thd), Item_args(a, b) { }
   Item_func_or_sum(THD *thd, Item *a, Item *b, Item *c):
-    Item_result_field(thd), Item_args(a, b, c) { }
+    Item_result_field(thd), Item_args(thd, a, b, c) { }
   Item_func_or_sum(THD *thd, Item *a, Item *b, Item *c, Item *d):
-    Item_result_field(thd), Item_args(a, b, c, d) { }
+    Item_result_field(thd), Item_args(thd, a, b, c, d) { }
   Item_func_or_sum(THD *thd, Item *a, Item *b, Item *c, Item *d, Item *e):
-    Item_result_field(thd), Item_args(a, b, c, d, e) { }
+    Item_result_field(thd), Item_args(thd, a, b, c, d, e) { }
   Item_func_or_sum(THD *thd, Item_func_or_sum *item):
     Item_result_field(thd, item), Item_args(thd, item),
     Used_tables_and_const_cache(item) { }
diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc
index 76b8b70..0c73aad 100644
--- a/sql/item_cmpfunc.cc
+++ b/sql/item_cmpfunc.cc
@@ -1846,8 +1846,8 @@ void Item_func_interval::fix_length_and_dec()
     }
 
     if (not_null_consts &&
-        (intervals=
-          (interval_range*) sql_alloc(sizeof(interval_range) * (rows - 1))))
+        (intervals= (interval_range*) current_thd->alloc(sizeof(interval_range) *
+                                                         (rows - 1))))
     {
       if (use_decimal_comparison)
       {
@@ -3462,7 +3462,7 @@ in_string::~in_string()
 {
   if (base)
   {
-    // base was allocated with help of sql_alloc => following is OK
+    // base was allocated on THD::mem_root => following is OK
     for (uint i=0 ; i < count ; i++)
       ((String*) base)[i].free();
   }
@@ -6567,10 +6567,9 @@ longlong Item_func_dyncol_exists::val_int()
     }
     else
     {
-      uint strlen;
+      uint strlen= nm->length() * my_charset_utf8_general_ci.mbmaxlen + 1;
       uint dummy_errors;
-      buf.str= (char *)sql_alloc((strlen= nm->length() *
-                                     my_charset_utf8_general_ci.mbmaxlen + 1));
+      buf.str= (char *) current_thd->alloc(strlen);
       if (buf.str)
       {
         buf.length=
diff --git a/sql/item_func.cc b/sql/item_func.cc
index e4f6eaf..a58e64f 100644
--- a/sql/item_func.cc
+++ b/sql/item_func.cc
@@ -3553,7 +3553,7 @@ udf_handler::fix_fields(THD *thd, Item_func_or_sum *func,
       func->used_tables_and_const_cache_join(item);
       f_args.arg_type[i]=item->result_type();
     }
-    //TODO: why all following memory is not allocated with 1 call of sql_alloc?
+    //TODO: why all following memory is not allocated with 1 thd->alloc() call?
     if (!(buffers=new String[arg_count]) ||
 	!(f_args.args= (char**) thd->alloc(arg_count * sizeof(char *))) ||
 	!(f_args.lengths= (ulong*) thd->alloc(arg_count * sizeof(long))) ||
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc
index 203fc4a..9dba262 100644
--- a/sql/item_strfunc.cc
+++ b/sql/item_strfunc.cc
@@ -4436,11 +4436,9 @@ bool Item_func_dyncol_create::prepare_arguments(THD *thd, bool force_names_arg)
         }
         else
         {
-          uint strlen;
+          uint strlen= res->length() * my_charset_utf8_general_ci.mbmaxlen + 1;
           uint dummy_errors;
-          char *str=
-            (char *)sql_alloc((strlen= res->length() *
-                               my_charset_utf8_general_ci.mbmaxlen + 1));
+          char *str= (char *) thd->alloc(strlen);
           if (str)
           {
             keys_str[i].length=
@@ -4742,7 +4740,8 @@ void Item_func_dyncol_add::print(String *str,
   This function ensures that null_value is set correctly
 */
 
-bool Item_dyncol_get::get_dyn_value(DYNAMIC_COLUMN_VALUE *val, String *tmp)
+bool Item_dyncol_get::get_dyn_value(THD *thd, DYNAMIC_COLUMN_VALUE *val,
+                                    String *tmp)
 {
   DYNAMIC_COLUMN dyn_str;
   String *res;
@@ -4770,10 +4769,9 @@ bool Item_dyncol_get::get_dyn_value(DYNAMIC_COLUMN_VALUE *val, String *tmp)
     }
     else
     {
-      uint strlen;
+      uint strlen= nm->length() * my_charset_utf8_general_ci.mbmaxlen + 1;
       uint dummy_errors;
-      buf.str= (char *)sql_alloc((strlen= nm->length() *
-                                     my_charset_utf8_general_ci.mbmaxlen + 1));
+      buf.str= (char *) thd->alloc(strlen);
       if (buf.str)
       {
         buf.length=
@@ -4823,7 +4821,7 @@ String *Item_dyncol_get::val_str(String *str_result)
   char buff[STRING_BUFFER_USUAL_SIZE];
   String tmp(buff, sizeof(buff), &my_charset_bin);
 
-  if (get_dyn_value(&val, &tmp))
+  if (get_dyn_value(current_thd, &val, &tmp))
     return NULL;
 
   switch (val.type) {
@@ -4905,11 +4903,12 @@ String *Item_dyncol_get::val_str(String *str_result)
 
 longlong Item_dyncol_get::val_int()
 {
+  THD *thd= current_thd;
   DYNAMIC_COLUMN_VALUE val;
   char buff[STRING_BUFFER_USUAL_SIZE];
   String tmp(buff, sizeof(buff), &my_charset_bin);
 
-  if (get_dyn_value(&val, &tmp))
+  if (get_dyn_value(thd, &val, &tmp))
     return 0;
 
   switch (val.type) {
@@ -4930,7 +4929,6 @@ longlong Item_dyncol_get::val_int()
     num= double_to_longlong(val.x.double_value, unsigned_flag, &error);
     if (error)
     {
-      THD *thd= current_thd;
       char buff[30];
       sprintf(buff, "%lg", val.x.double_value);
       push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
@@ -4950,7 +4948,6 @@ longlong Item_dyncol_get::val_int()
     num= my_strtoll10(val.x.string.value.str, &end, &error);
     if (end != org_end || error > 0)
     {
-      THD *thd= current_thd;
       push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                           ER_BAD_DATA,
                           ER_THD(thd, ER_BAD_DATA),
@@ -4987,11 +4984,12 @@ longlong Item_dyncol_get::val_int()
 
 double Item_dyncol_get::val_real()
 {
+  THD *thd= current_thd;
   DYNAMIC_COLUMN_VALUE val;
   char buff[STRING_BUFFER_USUAL_SIZE];
   String tmp(buff, sizeof(buff), &my_charset_bin);
 
-  if (get_dyn_value(&val, &tmp))
+  if (get_dyn_value(thd, &val, &tmp))
     return 0.0;
 
   switch (val.type) {
@@ -5014,7 +5012,6 @@ double Item_dyncol_get::val_real()
     if (end != (char*) val.x.string.value.str + val.x.string.value.length ||
         error)
     {
-      THD *thd= current_thd;
       push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                           ER_BAD_DATA,
                           ER_THD(thd, ER_BAD_DATA),
@@ -5046,11 +5043,12 @@ double Item_dyncol_get::val_real()
 
 my_decimal *Item_dyncol_get::val_decimal(my_decimal *decimal_value)
 {
+  THD *thd= current_thd;
   DYNAMIC_COLUMN_VALUE val;
   char buff[STRING_BUFFER_USUAL_SIZE];
   String tmp(buff, sizeof(buff), &my_charset_bin);
 
-  if (get_dyn_value(&val, &tmp))
+  if (get_dyn_value(thd, &val, &tmp))
     return NULL;
 
   switch (val.type) {
@@ -5075,7 +5073,6 @@ my_decimal *Item_dyncol_get::val_decimal(my_decimal *decimal_value)
     if (rc != E_DEC_OK ||
         end != val.x.string.value.str + val.x.string.value.length)
     {
-      THD *thd= current_thd;
       push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                           ER_BAD_DATA,
                           ER_THD(thd, ER_BAD_DATA),
@@ -5110,7 +5107,7 @@ bool Item_dyncol_get::get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
   String tmp(buff, sizeof(buff), &my_charset_bin);
   bool signed_value= 0;
 
-  if (get_dyn_value(&val, &tmp))
+  if (get_dyn_value(current_thd, &val, &tmp))
     return 1;                                   // Error
 
   switch (val.type) {
diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h
index 2f3fe0b..d2a83b2 100644
--- a/sql/item_strfunc.h
+++ b/sql/item_strfunc.h
@@ -1253,7 +1253,7 @@ class Item_dyncol_get: public Item_str_func
   longlong val_int();
   double val_real();
   my_decimal *val_decimal(my_decimal *);
-  bool get_dyn_value(DYNAMIC_COLUMN_VALUE *val, String *tmp);
+  bool get_dyn_value(THD *thd, DYNAMIC_COLUMN_VALUE *val, String *tmp);
   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
   void print(String *str, enum_query_type query_type);
 };
diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc
index 3224362..f232d77 100644
--- a/sql/item_subselect.cc
+++ b/sql/item_subselect.cc
@@ -1149,7 +1149,8 @@ void Item_singlerow_subselect::fix_length_and_dec()
   }
   else
   {
-    if (!(row= (Item_cache**) sql_alloc(sizeof(Item_cache*)*max_columns)))
+    if (!(row= (Item_cache**) current_thd->alloc(sizeof(Item_cache*) *
+                                                 max_columns)))
       return;
     engine->fix_length_and_dec(row);
     value= *row;
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index 932bb0c..bc622f2 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -711,7 +711,6 @@ SHOW_COMP_OPTION have_openssl;
 
 /* Thread specific variables */
 
-pthread_key(MEM_ROOT**,THR_MALLOC);
 pthread_key(THD*, THR_THD);
 mysql_mutex_t LOCK_thread_count, LOCK_thread_cache;
 mysql_mutex_t
@@ -2051,8 +2050,6 @@ static void cleanup_tls()
 {
   if (THR_THD)
     (void)pthread_key_delete(THR_THD);
-  if (THR_MALLOC)
-    (void)pthread_key_delete(THR_MALLOC);
 }
 
 
@@ -4082,8 +4079,7 @@ static int init_common_variables()
   connection_errors_peer_addr= 0;
   my_decimal_set_zero(&decimal_zero); // set decimal_zero constant;
 
-  if (pthread_key_create(&THR_THD,NULL) ||
-      pthread_key_create(&THR_MALLOC,NULL))
+  if (pthread_key_create(&THR_THD, NULL))
   {
     sql_print_error("Can't create thread-keys");
     return 1;
diff --git a/sql/mysqld.h b/sql/mysqld.h
index 29164c7..2a9d1ff 100644
--- a/sql/mysqld.h
+++ b/sql/mysqld.h
@@ -262,12 +262,6 @@ extern my_bool encrypt_tmp_disk_tables, encrypt_tmp_files;
 extern ulong encryption_algorithm;
 extern const char *encryption_algorithm_names[];
 
-/*
-  THR_MALLOC is a key which will be used to set/get MEM_ROOT** for a thread,
-  using my_pthread_setspecific_ptr()/my_thread_getspecific_ptr().
-*/
-extern pthread_key(MEM_ROOT**,THR_MALLOC);
-
 #ifdef HAVE_PSI_INTERFACE
 #ifdef HAVE_MMAP
 extern PSI_mutex_key key_PAGE_lock, key_LOCK_sync, key_LOCK_active,
diff --git a/sql/sp_head.cc b/sql/sp_head.cc
index 375a77d..c709eef 100644
--- a/sql/sp_head.cc
+++ b/sql/sp_head.cc
@@ -1185,7 +1185,7 @@ sp_head::execute(THD *thd, bool merge_da_on_success)
 
   /*
     Switch query context. This has to be done early as this is sometimes
-    allocated trough sql_alloc
+    allocated on THD::mem_root
   */
   if (m_creation_ctx)
     saved_creation_ctx= m_creation_ctx->set_n_backup(thd);
@@ -2292,9 +2292,9 @@ sp_head::restore_lex(THD *thd)
   Put the instruction on the backpatch list, associated with the label.
 */
 int
-sp_head::push_backpatch(sp_instr *i, sp_label *lab)
+sp_head::push_backpatch(THD *thd, sp_instr *i, sp_label *lab)
 {
-  bp_t *bp= (bp_t *)sql_alloc(sizeof(bp_t));
+  bp_t *bp= (bp_t *) thd->alloc(sizeof(bp_t));
 
   if (!bp)
     return 1;
diff --git a/sql/sp_head.h b/sql/sp_head.h
index dbdb957..a2ac658 100644
--- a/sql/sp_head.h
+++ b/sql/sp_head.h
@@ -386,7 +386,7 @@ class sp_head :private Query_arena
 
   /// Put the instruction on the backpatch list, associated with the label.
   int
-  push_backpatch(sp_instr *, sp_label *);
+  push_backpatch(THD *thd, sp_instr *, sp_label *);
 
   /// Update all instruction with this label in the backpatch list to
   /// the current position.
diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc
index 679bd15..5fd7e87 100644
--- a/sql/sql_acl.cc
+++ b/sql/sql_acl.cc
@@ -6583,12 +6583,10 @@ bool grant_init()
 
 static bool grant_load(THD *thd, TABLE_LIST *tables)
 {
-  MEM_ROOT *memex_ptr;
   bool return_val= 1;
   TABLE *t_table, *c_table, *p_table;
   bool check_no_resolve= specialflag & SPECIAL_NO_RESOLVE;
-  MEM_ROOT **save_mem_root_ptr= my_pthread_getspecific_ptr(MEM_ROOT**,
-                                                           THR_MALLOC);
+  MEM_ROOT *save_mem_root= thd->mem_root;
   ulonglong old_sql_mode= thd->variables.sql_mode;
   DBUG_ENTER("grant_load");
 
@@ -6613,15 +6611,14 @@ static bool grant_load(THD *thd, TABLE_LIST *tables)
   t_table->use_all_columns();
   c_table->use_all_columns();
 
-  memex_ptr= &grant_memroot;
-  my_pthread_setspecific_ptr(THR_MALLOC, &memex_ptr);
+  thd->mem_root= &grant_memroot;
 
   if (!t_table->file->ha_index_first(t_table->record[0]))
   {
     do
     {
       GRANT_TABLE *mem_check;
-      if (!(mem_check=new (memex_ptr) GRANT_TABLE(t_table,c_table)))
+      if (!(mem_check= new (&grant_memroot) GRANT_TABLE(t_table, c_table)))
       {
 	/* This could only happen if we are out memory */
 	goto end_unlock;
@@ -6666,7 +6663,7 @@ static bool grant_load(THD *thd, TABLE_LIST *tables)
       {
         GRANT_NAME *mem_check;
         HASH *hash;
-        if (!(mem_check=new (memex_ptr) GRANT_NAME(p_table, TRUE)))
+        if (!(mem_check= new (&grant_memroot) GRANT_NAME(p_table, TRUE)))
         {
           /* This could only happen if we are out memory */
           goto end_unlock_p;
@@ -6719,7 +6716,7 @@ static bool grant_load(THD *thd, TABLE_LIST *tables)
     p_table->file->ha_index_end();
 end_unlock:
   t_table->file->ha_index_end();
-  my_pthread_setspecific_ptr(THR_MALLOC, save_mem_root_ptr);
+  thd->mem_root= save_mem_root;
 end_index_init:
   thd->variables.sql_mode= old_sql_mode;
   DBUG_RETURN(return_val);
diff --git a/sql/sql_analyse.cc b/sql/sql_analyse.cc
index 1f801a3..b2bc9fc 100644
--- a/sql/sql_analyse.cc
+++ b/sql/sql_analyse.cc
@@ -134,7 +134,7 @@ proc_analyse_init(THD *thd, ORDER *param, select_result *result,
   }
 
   if (!(pc->f_info=
-        (field_info**)sql_alloc(sizeof(field_info*)*field_list.elements)))
+        (field_info**) thd->alloc(sizeof(field_info*) * field_list.elements)))
     goto err;
   pc->f_end = pc->f_info + field_list.elements;
   pc->fields = field_list;
diff --git a/sql/sql_class.cc b/sql/sql_class.cc
index 1bf4892..e0f87a1 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -371,16 +371,6 @@ void thd_close_connection(THD *thd)
 }
 
 /**
-  Get current THD object from thread local data
-
-  @retval     The THD object for the thread, NULL if not connection thread
-*/
-THD *thd_get_current_thd()
-{
-  return current_thd;
-}
-
-/**
   Lock data that needs protection in THD object
 
   @param thd                   THD object
@@ -470,6 +460,16 @@ my_socket thd_get_fd(THD *thd)
 #endif
 
 /**
+  Get current THD object from thread local data
+
+  @retval     The THD object for the thread, NULL if not connection thread
+*/
+THD *thd_get_current_thd()
+{
+  return current_thd;
+}
+
+/**
   Get thread attributes for connection threads
 
   @retval      Reference to thread attribute for connection threads
@@ -2044,7 +2044,7 @@ int killed_errno(killed_state killed)
 
 /*
   Remember the location of thread info, the structure needed for
-  sql_alloc() and the structure for the net buffer
+  the structure for the net buffer
 */
 
 bool THD::store_globals()
@@ -2055,8 +2055,7 @@ bool THD::store_globals()
   */
   DBUG_ASSERT(thread_stack);
 
-  if (set_current_thd(this) ||
-      my_pthread_setspecific_ptr(THR_MALLOC, &mem_root))
+  if (set_current_thd(this))
     return 1;
   /*
     mysys_var is concurrently readable by a killer thread.
@@ -2109,7 +2108,6 @@ void THD::reset_globals()
 
   /* Undocking the thread specific data. */
   set_current_thd(0);
-  my_pthread_setspecific_ptr(THR_MALLOC, NULL);
   net.thd= 0;
 }
 
@@ -3548,7 +3546,7 @@ void Query_arena::free_items()
 {
   Item *next;
   DBUG_ENTER("Query_arena::free_items");
-  /* This works because items are allocated with sql_alloc() */
+  /* This works because items are allocated on THD::mem_root */
   for (; free_list; free_list= next)
   {
     next= free_list->next;
diff --git a/sql/sql_class.h b/sql/sql_class.h
index 2b62c46..2edcaf2 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -38,6 +38,7 @@
 #include "violite.h"        /* vio_is_connected */
 #include "thr_lock.h"       /* thr_lock_type, THR_LOCK_DATA, THR_LOCK_INFO */
 #include "thr_timer.h"
+#include "thr_malloc.h"
 
 #include "sql_digest_stream.h"            // sql_digest_state
 
diff --git a/sql/sql_handler.cc b/sql/sql_handler.cc
index 05e333e..db5ec5d 100644
--- a/sql/sql_handler.cc
+++ b/sql/sql_handler.cc
@@ -31,7 +31,7 @@
   then do { handler_items=concat(handler_items, free_list); free_list=0; }
 
   But !!! do_command calls free_root at the end of every query and frees up
-  all the sql_alloc'ed memory. It's harder to work around...
+  all the memory allocated on THD::mem_root. It's harder to work around...
 */
 
 /*
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index d39da81..58cddf8 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -496,10 +496,6 @@ class st_select_lex_node {
   enum sub_select_type linkage;
   bool no_table_names_allowed; /* used for global order by */
 
-  static void *operator new(size_t size) throw ()
-  {
-    return sql_alloc(size);
-  }
   static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
   { return (void*) alloc_root(mem_root, (uint) size); }
   static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
diff --git a/sql/sql_list.h b/sql/sql_list.h
index 6f01b64..718306c 100644
--- a/sql/sql_list.h
+++ b/sql/sql_list.h
@@ -22,7 +22,8 @@
 #include "my_sys.h"                    /* alloc_root, TRASH, MY_WME,
                                           MY_FAE, MY_ALLOW_ZERO_PTR */
 #include "m_string.h"                           /* bfill */
-#include "thr_malloc.h"                         /* sql_alloc */
+
+THD *thd_get_current_thd();
 
 /* mysql standard class memory allocator */
 
@@ -31,11 +32,11 @@ class Sql_alloc
 public:
   static void *operator new(size_t size) throw ()
   {
-    return sql_alloc(size);
+    return thd_alloc(thd_get_current_thd(), size);
   }
   static void *operator new[](size_t size) throw ()
   {
-    return sql_alloc(size);
+    return thd_alloc(thd_get_current_thd(), size);
   }
   static void *operator new[](size_t size, MEM_ROOT *mem_root) throw ()
   { return alloc_root(mem_root, size); }
diff --git a/sql/sql_load.cc b/sql/sql_load.cc
index 9cf82c1..2c2c0df 100644
--- a/sql/sql_load.cc
+++ b/sql/sql_load.cc
@@ -86,7 +86,7 @@ class READ_INFO {
   CHARSET_INFO *read_charset;
   LOAD_FILE_IO_CACHE cache;
 
-  READ_INFO(File file,uint tot_length,CHARSET_INFO *cs,
+  READ_INFO(THD *thd, File file, uint tot_length, CHARSET_INFO *cs,
 	    String &field_term,String &line_start,String &line_term,
 	    String &enclosed,int escape,bool get_it_from_net, bool is_fifo);
   ~READ_INFO();
@@ -437,7 +437,7 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list,
                     !(thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)))
                     ? (*escaped)[0] : INT_MAX;
 
-  READ_INFO read_info(file,tot_length,
+  READ_INFO read_info(thd, file, tot_length,
                       ex->cs ? ex->cs : thd->variables.collation_database,
 		      *field_term,*ex->line_start, *ex->line_term, *enclosed,
 		      info.escape_char, read_file_from_client, is_fifo);
@@ -1330,7 +1330,7 @@ READ_INFO::unescape(char chr)
 */
 
 
-READ_INFO::READ_INFO(File file_par, uint tot_length, CHARSET_INFO *cs,
+READ_INFO::READ_INFO(THD *thd, File file_par, uint tot_length, CHARSET_INFO *cs,
 		     String &field_term, String &line_start, String &line_term,
 		     String &enclosed_par, int escape, bool get_it_from_net,
 		     bool is_fifo)
@@ -1377,7 +1377,7 @@ READ_INFO::READ_INFO(File file_par, uint tot_length, CHARSET_INFO *cs,
   /* Set of a stack for unget if long terminators */
   uint length= MY_MAX(cs->mbmaxlen, MY_MAX(field_term_length, line_term_length)) + 1;
   set_if_bigger(length,line_start.length());
-  stack=stack_pos=(int*) sql_alloc(sizeof(int)*length);
+  stack= stack_pos= (int*) thd->alloc(sizeof(int) * length);
 
   if (!(buffer=(uchar*) my_malloc(buff_length+1,MYF(MY_THREAD_SPECIFIC))))
     error=1; /* purecov: inspected */
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index 9dca938..f297f7a 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -859,7 +859,7 @@ void do_handle_bootstrap(THD *thd)
 }
 
 
-/* This works because items are allocated with sql_alloc() */
+/* This works because items are allocated on THD::mem_root */
 
 void free_items(Item *item)
 {
@@ -874,7 +874,7 @@ void free_items(Item *item)
 }
 
 /**
-   This works because items are allocated with sql_alloc().
+   This works because items are allocated on THD::mem_root.
    @note The function also handles null pointers (empty list).
 */
 void cleanup_items(Item *item)
@@ -2100,7 +2100,7 @@ int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
       {
         DBUG_RETURN(1);
       }
-      schema_select_lex= new SELECT_LEX();
+      schema_select_lex= new (thd->mem_root) SELECT_LEX();
       db.str= schema_select_lex->db= lex->select_lex.db;
       schema_select_lex->table_list.first= NULL;
       db.length= strlen(db.str);
@@ -2123,7 +2123,7 @@ int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
 #else
     DBUG_ASSERT(table_ident);
     TABLE_LIST **query_tables_last= lex->query_tables_last;
-    schema_select_lex= new SELECT_LEX();
+    schema_select_lex= new (thd->mem_root) SELECT_LEX();
     /* 'parent_lex' is used in init_query() so it must be before it. */
     schema_select_lex->parent_lex= lex;
     schema_select_lex->init_query();
diff --git a/sql/sql_partition.cc b/sql/sql_partition.cc
index 411420d..c978e10 100644
--- a/sql/sql_partition.cc
+++ b/sql/sql_partition.cc
@@ -2469,7 +2469,7 @@ static int add_key_with_algorithm(File fptr, partition_info *part_info,
   common queries.
 */
 
-char *generate_partition_syntax(partition_info *part_info,
+char *generate_partition_syntax(THD *thd, partition_info *part_info,
                                 uint *buf_length,
                                 bool use_sql_alloc,
                                 bool show_partition_options,
@@ -2643,7 +2643,7 @@ char *generate_partition_syntax(partition_info *part_info,
     goto close_file;
   *buf_length= (uint)buffer_length;
   if (use_sql_alloc)
-    buf= (char*) sql_alloc(*buf_length+1);
+    buf= (char*) thd->alloc(*buf_length + 1);
   else
     buf= (char*) my_malloc(*buf_length+1, MYF(MY_WME));
   if (!buf)
diff --git a/sql/sql_partition.h b/sql/sql_partition.h
index b560c53..dd352b6 100644
--- a/sql/sql_partition.h
+++ b/sql/sql_partition.h
@@ -262,7 +262,7 @@ uint prep_alter_part_table(THD *thd, TABLE *table, Alter_info *alter_info,
                            Alter_table_ctx *alter_ctx,
                            bool *partition_changed,
                            bool *fast_alter_table);
-char *generate_partition_syntax(partition_info *part_info,
+char *generate_partition_syntax(THD *thd, partition_info *part_info,
                                 uint *buf_length, bool use_sql_alloc,
                                 bool show_partition_options,
                                 HA_CREATE_INFO *create_info,
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 617b88a..e49d06e 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -12493,9 +12493,9 @@ static void clear_tables(JOIN *join)
 
 class COND_CMP :public ilink {
 public:
-  static void *operator new(size_t size)
+  static void *operator new(size_t size, MEM_ROOT *mem_root)
   {
-    return (void*) sql_alloc((uint) size);
+    return alloc_root(mem_root, size);
   }
   static void operator delete(void *ptr __attribute__((unused)),
                               size_t size __attribute__((unused)))
@@ -14115,7 +14115,7 @@ change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
       {
 	cond->marker=1;
 	COND_CMP *tmp2;
-	if ((tmp2=new COND_CMP(and_father,func)))
+        if ((tmp2= new (thd->mem_root) COND_CMP(and_father, func)))
 	  save_list->push_back(tmp2);
       }
       /*
@@ -14147,7 +14147,7 @@ change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
         thd->change_item_tree(args + 1, value);
 	cond->marker=1;
 	COND_CMP *tmp2;
-	if ((tmp2=new COND_CMP(and_father,func)))
+        if ((tmp2=new (thd->mem_root) COND_CMP(and_father, func)))
 	  save_list->push_back(tmp2);
       }
       if (functype != Item_func::LIKE_FUNC)
diff --git a/sql/sql_show.cc b/sql/sql_show.cc
index edde3cb..d2310ae 100644
--- a/sql/sql_show.cc
+++ b/sql/sql_show.cc
@@ -2123,7 +2123,7 @@ int show_create_table(THD *thd, TABLE_LIST *table_list, String *packet,
       char *part_syntax;
       String comment_start;
       table->part_info->set_show_version_string(&comment_start);
-      if ((part_syntax= generate_partition_syntax(table->part_info,
+      if ((part_syntax= generate_partition_syntax(thd, table->part_info,
                                                   &part_syntax_len,
                                                   FALSE,
                                                   show_table_options,
diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc
index b570642..69ca395 100644
--- a/sql/sql_statistics.cc
+++ b/sql/sql_statistics.cc
@@ -1564,7 +1564,7 @@ class Index_prefix_calc: public Sql_alloc
     }
         
     if ((calc_state=
-         (Prefix_calc_state *) sql_alloc(sizeof(Prefix_calc_state)*key_parts)))
+         (Prefix_calc_state *) thd->alloc(sizeof(Prefix_calc_state)*key_parts)))
     {
       uint keyno= key_info-table->key_info;
       for (i= 0, state= calc_state; i < key_parts; i++, state++)
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index 5e97809..80ceb07 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -1814,7 +1814,7 @@ bool mysql_write_frm(ALTER_PARTITION_PARAM_TYPE *lpt, uint flags)
       partition_info *part_info= lpt->table->part_info;
       if (part_info)
       {
-        if (!(part_syntax_buf= generate_partition_syntax(part_info,
+        if (!(part_syntax_buf= generate_partition_syntax(lpt->thd, part_info,
                                                          &syntax_len,
                                                          TRUE, TRUE,
                                                          lpt->create_info,
@@ -1897,7 +1897,7 @@ bool mysql_write_frm(ALTER_PARTITION_PARAM_TYPE *lpt, uint flags)
     {
       TABLE_SHARE *share= lpt->table->s;
       char *tmp_part_syntax_str;
-      if (!(part_syntax_buf= generate_partition_syntax(part_info,
+      if (!(part_syntax_buf= generate_partition_syntax(lpt->thd, part_info,
                                                        &syntax_len,
                                                        TRUE, TRUE,
                                                        lpt->create_info,
@@ -4478,7 +4478,7 @@ handler *mysql_create_frm_image(THD *thd,
       We reverse the partitioning parser and generate a standard format
       for syntax stored in frm file.
     */
-    if (!(part_syntax_buf= generate_partition_syntax(part_info,
+    if (!(part_syntax_buf= generate_partition_syntax(thd, part_info,
                                                      &syntax_len,
                                                      TRUE, TRUE,
                                                      create_info,
diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index 9d67642..67f7bc7 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -341,7 +341,7 @@ int case_stmt_action_when(LEX *lex, Item *when, bool simple)
   */
 
   return !MY_TEST(i) ||
-         sp->push_backpatch(i, ctx->push_label(thd, empty_lex_str, 0)) ||
+         sp->push_backpatch(thd, i, ctx->push_label(thd, empty_lex_str, 0)) ||
          sp->add_cont_backpatch(i) ||
          sp->add_instr(i);
 }
@@ -375,7 +375,7 @@ int case_stmt_action_then(LEX *lex)
     (jump from instruction 4 to 12, 7 to 12 ... in the example)
   */
 
-  return sp->push_backpatch(i, ctx->last_label());
+  return sp->push_backpatch(lex->thd, i, ctx->last_label());
 }
 
 static bool
@@ -3141,10 +3141,10 @@ sp_decl:
 
             /* For continue handlers, mark end of handler scope. */
             if ($2 == sp_handler::CONTINUE &&
-                sp->push_backpatch(i, ctx->last_label()))
+                sp->push_backpatch(thd, i, ctx->last_label()))
               MYSQL_YYABORT;
 
-            if (sp->push_backpatch(i, ctx->push_label(thd, empty_lex_str, 0)))
+            if (sp->push_backpatch(thd, i, ctx->push_label(thd, empty_lex_str, 0)))
               MYSQL_YYABORT;
           }
           sp_hcond_list sp_proc_stmt
@@ -3169,7 +3169,7 @@ sp_decl:
                  sp_instr_hreturn(sp->instructions(), ctx);
               if (i == NULL ||
                   sp->add_instr(i) ||
-                  sp->push_backpatch(i, lex->spcont->last_label())) /* Block end */
+                  sp->push_backpatch(thd, i, lex->spcont->last_label())) /* Block end */
                 MYSQL_YYABORT;
             }
             lex->sphead->backpatch(hlab);
@@ -3855,7 +3855,7 @@ sp_proc_stmt_leave:
               i= new (lex->thd->mem_root) sp_instr_jump(ip, ctx);
               if (i == NULL)
                 MYSQL_YYABORT;
-              sp->push_backpatch(i, lab);  /* Jumping forward */
+              sp->push_backpatch(thd, i, lab);  /* Jumping forward */
               sp->add_instr(i);
             }
           }
@@ -4032,7 +4032,7 @@ sp_if:
             sp_instr_jump_if_not *i= new (lex->thd->mem_root)
               sp_instr_jump_if_not(ip, ctx, $2, lex);
             if (i == NULL ||
-                sp->push_backpatch(i, ctx->push_label(thd, empty_lex_str, 0)) ||
+                sp->push_backpatch(thd, i, ctx->push_label(thd, empty_lex_str, 0)) ||
                 sp->add_cont_backpatch(i) ||
                 sp->add_instr(i))
               MYSQL_YYABORT;
@@ -4049,7 +4049,7 @@ sp_if:
                 sp->add_instr(i))
               MYSQL_YYABORT;
             sp->backpatch(ctx->pop_label());
-            sp->push_backpatch(i, ctx->push_label(thd, empty_lex_str, 0));
+            sp->push_backpatch(thd, i, ctx->push_label(thd, empty_lex_str, 0));
           }
           sp_elseifs
           {
@@ -4375,7 +4375,7 @@ sp_control_content:
               sp_instr_jump_if_not(ip, lex->spcont, $3, lex);
             if (i == NULL ||
                 /* Jumping forward */
-                sp->push_backpatch(i, lex->spcont->last_label()) ||
+                sp->push_backpatch(thd, i, lex->spcont->last_label()) ||
                 sp->new_cont_backpatch(i) ||
                 sp->add_instr(i))
               MYSQL_YYABORT;
diff --git a/sql/table.cc b/sql/table.cc
index 9c90e50..49f8fab 100644
--- a/sql/table.cc
+++ b/sql/table.cc
@@ -949,12 +949,10 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write,
   plugin_ref se_plugin= 0;
   keyinfo= &first_keyinfo;
   share->ext_key_parts= 0;
-  MEM_ROOT **root_ptr, *old_root;
+  MEM_ROOT *old_root= thd->mem_root;
   DBUG_ENTER("TABLE_SHARE::init_from_binary_frm_image");
 
-  root_ptr= my_pthread_getspecific_ptr(MEM_ROOT**, THR_MALLOC);
-  old_root= *root_ptr;
-  *root_ptr= &share->mem_root;
+  thd->mem_root= &share->mem_root;
 
   if (write && write_frm_image(frm_image, frm_length))
     goto err;
@@ -2085,7 +2083,7 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write,
   share->db_plugin= se_plugin;
   share->error= OPEN_FRM_OK;
   thd->status_var.opened_shares++;
-  *root_ptr= old_root;
+  thd->mem_root= old_root;
   DBUG_RETURN(0);
 
  err:
@@ -2098,7 +2096,7 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write,
   if (!thd->is_error())
     open_table_error(share, OPEN_FRM_CORRUPTED, share->open_errno);
 
-  *root_ptr= old_root;
+  thd->mem_root= old_root;
   DBUG_RETURN(HA_ERR_NOT_A_TABLE);
 }
 
diff --git a/sql/thr_malloc.cc b/sql/thr_malloc.cc
index e722b5a..cbed769 100644
--- a/sql/thr_malloc.cc
+++ b/sql/thr_malloc.cc
@@ -66,16 +66,7 @@ void init_sql_alloc(MEM_ROOT *mem_root, uint block_size, uint pre_alloc,
 }
 
 
-#ifndef MYSQL_CLIENT
-void *sql_alloc(size_t Size)
-{
-  MEM_ROOT *root= *my_pthread_getspecific_ptr(MEM_ROOT**,THR_MALLOC);
-  return alloc_root(root,Size);
-}
-#endif
-
-
-char *sql_strmake_with_convert(const char *str, size_t arg_length,
+char *sql_strmake_with_convert(THD *thd, const char *str, size_t arg_length,
 			       CHARSET_INFO *from_cs,
 			       size_t max_res_length,
 			       CHARSET_INFO *to_cs, size_t *result_length)
@@ -85,7 +76,7 @@ char *sql_strmake_with_convert(const char *str, size_t arg_length,
   max_res_length--;				// Reserve place for end null
 
   set_if_smaller(new_length, max_res_length);
-  if (!(pos= (char*) sql_alloc(new_length+1)))
+  if (!(pos= (char*) thd->alloc(new_length + 1)))
     return pos;					// Error
 
   if ((from_cs == &my_charset_bin) || (to_cs == &my_charset_bin))
diff --git a/sql/thr_malloc.h b/sql/thr_malloc.h
index d35207c..fc23c87 100644
--- a/sql/thr_malloc.h
+++ b/sql/thr_malloc.h
@@ -22,8 +22,7 @@ typedef struct st_mem_root MEM_ROOT;
 
 void init_sql_alloc(MEM_ROOT *root, uint block_size, uint pre_alloc_size,
                     myf my_flags);
-void *sql_alloc(size_t);
-char *sql_strmake_with_convert(const char *str, size_t arg_length,
+char *sql_strmake_with_convert(THD *thd, const char *str, size_t arg_length,
 			       CHARSET_INFO *from_cs,
 			       size_t max_res_length,
 			       CHARSET_INFO *to_cs, size_t *result_length);
diff --git a/sql/threadpool_common.cc b/sql/threadpool_common.cc
index 9e0cb07..97352e4 100644
--- a/sql/threadpool_common.cc
+++ b/sql/threadpool_common.cc
@@ -87,7 +87,6 @@ struct Worker_thread_context
 #endif
     pthread_setspecific(THR_KEY_mysys,mysys_var);
     pthread_setspecific(THR_THD, 0);
-    pthread_setspecific(THR_MALLOC, 0);
   }
 };
 
diff --git a/sql/tztime.cc b/sql/tztime.cc
index 5e5f34f..f94e10c 100644
--- a/sql/tztime.cc
+++ b/sql/tztime.cc
@@ -1800,8 +1800,6 @@ my_tz_init(THD *org_thd, const char *default_tzname, my_bool bootstrap)
   delete thd;
   if (org_thd)
     org_thd->store_globals();			/* purecov: inspected */
-  else
-    my_pthread_setspecific_ptr(THR_MALLOC,  0);
   
   default_tz= default_tz_name ? global_system_variables.time_zone
                               : my_tz_SYSTEM;
diff --git a/sql/unireg.cc b/sql/unireg.cc
index 66959f4..919a533 100644
--- a/sql/unireg.cc
+++ b/sql/unireg.cc
@@ -921,9 +921,7 @@ static bool make_empty_rec(THD *thd, uchar *buff, uint table_options,
   thd->count_cuted_fields= CHECK_FIELD_WARN;    // To find wrong default values
   while ((field=it++))
   {
-    /*
-      regfield don't have to be deleted as it's allocated with sql_alloc()
-    */
+    /* regfield don't have to be deleted as it's allocated on THD::mem_root */
     Field *regfield= make_field(&share, thd->mem_root,
                                 buff+field->offset + data_offset,
                                 field->length,
diff --git a/storage/maria/ha_maria.cc b/storage/maria/ha_maria.cc
index 5fdd2a9..2e2aa71 100644
--- a/storage/maria/ha_maria.cc
+++ b/storage/maria/ha_maria.cc
@@ -435,8 +435,8 @@ static void _ma_check_print_msg(HA_CHECK *param, const char *msg_type,
                           NullS) - name);
   /*
     TODO: switch from protocol to push_warning here. The main reason we didn't
-    it yet is parallel repair. Due to following trace:
-    ma_check_print_msg/push_warning/sql_alloc/my_pthread_getspecific_ptr.
+    it yet is parallel repair, which threads have no THD object accessible via
+    current_thd.
 
     Also we likely need to lock mutex here (in both cases with protocol and
     push_warning).
diff --git a/storage/myisam/ha_myisam.cc b/storage/myisam/ha_myisam.cc
index 3029909..0d040b3 100644
--- a/storage/myisam/ha_myisam.cc
+++ b/storage/myisam/ha_myisam.cc
@@ -166,8 +166,8 @@ static void mi_check_print_msg(HA_CHECK *param,	const char* msg_type,
 		 name);
   /*
     TODO: switch from protocol to push_warning here. The main reason we didn't
-    it yet is parallel repair. Due to following trace:
-    mi_check_print_msg/push_warning/sql_alloc/my_pthread_getspecific_ptr.
+    it yet is parallel repair, which threads have no THD object accessible via
+    current_thd.
 
     Also we likely need to lock mutex here (in both cases with protocol and
     push_warning).
diff --git a/storage/myisammrg/ha_myisammrg.cc b/storage/myisammrg/ha_myisammrg.cc
index 05d5ef1..418e095 100644
--- a/storage/myisammrg/ha_myisammrg.cc
+++ b/storage/myisammrg/ha_myisammrg.cc
@@ -100,7 +100,7 @@
 #include "../myisam/ha_myisam.h"
 #include "ha_myisammrg.h"
 #include "myrg_def.h"
-#include "thr_malloc.h"                         // int_sql_alloc
+#include "thr_malloc.h"                         // init_sql_alloc
 #include "sql_class.h"                          // THD
 #include "debug_sync.h"
 
diff --git a/storage/spider/spd_table.cc b/storage/spider/spd_table.cc
index bb3cb13..34f3768 100644
--- a/storage/spider/spd_table.cc
+++ b/storage/spider/spd_table.cc
@@ -8504,7 +8504,7 @@ int spider_discover_table_structure(
       DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
     }
 #ifdef SPIDER_HAS_DISCOVER_TABLE_STRUCTURE_COMMENT
-    if (!(part_syntax = generate_partition_syntax(part_info, &part_syntax_len,
+    if (!(part_syntax = generate_partition_syntax(thd, part_info, &part_syntax_len,
       FALSE, TRUE, info, NULL, NULL)))
 #else
     if (!(part_syntax = generate_partition_syntax(part_info, &part_syntax_len,



More information about the commits mailing list