| 1634 | */ |
| 1635 | |
| 1636 | bool THD::set_db(const LEX_CSTRING *new_db) |
| 1637 | { |
| 1638 | bool result= 0; |
| 1639 | /* |
| 1640 | Acquiring mutex LOCK_thd_data as we either free the memory allocated |
| 1641 | for the database and reallocating the memory for the new db or memcpy |
| 1642 | the new_db to the db. |
| 1643 | */ |
| 1644 | /* Do not reallocate memory if current chunk is big enough. */ |
| 1645 | if (db.str && new_db->str && db.length >= new_db->length) |
| 1646 | { |
| 1647 | mysql_mutex_lock(&LOCK_thd_data); |
| 1648 | db.length= new_db->length; |
| 1649 | memcpy((char*) db.str, new_db->str, new_db->length+1); |
| 1650 | mysql_mutex_unlock(&LOCK_thd_data); |
| 1651 | } |
| 1652 | else |
| 1653 | { |
| 1654 | const char *org_db= db.str; |
| 1655 | const char *tmp= NULL; |
| 1656 | if (new_db->str) |
| 1657 | { |
| 1658 | if (!(tmp= my_strndup(key_memory_THD_db, new_db->str, new_db->length, |
| 1659 | MYF(MY_WME | ME_FATAL)))) |
| 1660 | result= 1; |
| 1661 | } |
| 1662 | |
| 1663 | mysql_mutex_lock(&LOCK_thd_data); |
| 1664 | db.str= tmp; |
| 1665 | db.length= tmp ? new_db->length : 0; |
| 1666 | mysql_mutex_unlock(&LOCK_thd_data); |
| 1667 | my_free((char*) org_db); |
| 1668 | } |
| 1669 | PSI_CALL_set_thread_db(db.str, (int) db.length); |
| 1670 | return result; |
| 1671 | } |
| 1672 | |
| 1673 | |
| 1674 | /** |
no test coverage detected