Skip to content

Commit 8edef48

Browse files
committed
Changed some malloc() calls to my_malloc()
- hostnames in hostname_cache added - Some Galera (WSREP) allocations - Table caches
1 parent c4a5bd1 commit 8edef48

File tree

10 files changed

+61
-24
lines changed

10 files changed

+61
-24
lines changed

sql/hostname.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ bool hostname_cache_init()
151151

152152
if (!(hostname_cache= new Hash_filo<Host_entry>(key_memory_host_cache_hostname,
153153
host_cache_size, key_offset, HOST_ENTRY_KEY_SIZE,
154-
NULL, (my_hash_free_key) free, &my_charset_bin)))
154+
NULL, (my_hash_free_key) my_free, &my_charset_bin)))
155155
return 1;
156156

157157
hostname_cache->clear();
@@ -204,7 +204,8 @@ static void add_hostname_impl(const char *ip_key, const char *hostname,
204204

205205
if (likely(entry == NULL))
206206
{
207-
entry= (Host_entry *) malloc(sizeof (Host_entry));
207+
entry= (Host_entry *) my_malloc(key_memory_host_cache_hostname,
208+
sizeof (Host_entry), 0);
208209
if (entry == NULL)
209210
return;
210211

sql/mysqld.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9304,8 +9304,8 @@ PSI_memory_key key_memory_thd_transactions;
93049304
PSI_memory_key key_memory_user_conn;
93059305
PSI_memory_key key_memory_user_var_entry;
93069306
PSI_memory_key key_memory_user_var_entry_value;
9307-
93089307
PSI_memory_key key_memory_String_value;
9308+
PSI_memory_key key_memory_WSREP;
93099309

93109310
#ifdef HAVE_PSI_INTERFACE
93119311

@@ -9593,6 +9593,7 @@ static PSI_memory_info all_server_memory[]=
95939593
// { &key_memory_get_all_tables, "get_all_tables", 0},
95949594
// { &key_memory_fill_schema_schemata, "fill_schema_schemata", 0},
95959595
{ &key_memory_native_functions, "native_functions", PSI_FLAG_GLOBAL},
9596+
{ &key_memory_WSREP, "wsrep", 0 }
95969597
};
95979598

95989599
/**

sql/mysqld.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ extern PSI_memory_key key_memory_get_all_tables;
537537
extern PSI_memory_key key_memory_fill_schema_schemata;
538538
extern PSI_memory_key key_memory_native_functions;
539539
extern PSI_memory_key key_memory_JSON;
540+
extern PSI_memory_key key_memory_WSREP;
540541

541542
/*
542543
MAINTAINER: Please keep this list in order, to limit merge collisions.

sql/table_cache.cc

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
ulong tdc_size; /**< Table definition cache threshold for LRU eviction. */
5858
ulong tc_size; /**< Table cache threshold for LRU eviction. */
5959
uint32 tc_instances;
60+
static size_t tc_allocated_size;
6061
static std::atomic<uint32_t> tc_active_instances(1);
6162
static std::atomic<bool> tc_contention_warning_reported;
6263

@@ -148,8 +149,20 @@ struct Table_cache_instance
148149
}
149150

150151
static void *operator new[](size_t size)
151-
{ return aligned_malloc(size, CPU_LEVEL1_DCACHE_LINESIZE); }
152+
{
153+
void *res= aligned_malloc(size, CPU_LEVEL1_DCACHE_LINESIZE);
154+
if (res)
155+
{
156+
tc_allocated_size= size;
157+
update_malloc_size(size, 0);
158+
}
159+
return res;
160+
}
152161
static void operator delete[](void *ptr) { aligned_free(ptr); }
162+
static void mark_memory_freed()
163+
{
164+
update_malloc_size(-(longlong) tc_allocated_size, 0);
165+
}
153166

154167
/**
155168
Lock table cache mutex and check contention.
@@ -654,7 +667,12 @@ void tdc_deinit(void)
654667
tdc_inited= false;
655668
lf_hash_destroy(&tdc_hash);
656669
mysql_mutex_destroy(&LOCK_unused_shares);
657-
delete [] tc;
670+
if (tc)
671+
{
672+
tc->mark_memory_freed();
673+
delete [] tc;
674+
tc= 0;
675+
}
658676
}
659677
DBUG_VOID_RETURN;
660678
}

sql/wsrep_binlog.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@ void wsrep_dump_rbr_buf(THD *thd, const void* rbr_buf, size_t buf_len)
198198
to alloc and pass as an argument to snprintf.
199199
*/
200200

201-
char *filename= (char *)malloc(len+1);
201+
char *filename= (char *) my_malloc(key_memory_WSREP, len+1, 0);
202202
int len1= snprintf(filename, len+1, "%s/GRA_%lld_%lld.log",
203203
wsrep_data_home_dir, (longlong) thd->thread_id,
204204
(long long)wsrep_thd_trx_seqno(thd));
205205

206206
if (len > len1)
207207
{
208208
WSREP_ERROR("RBR dump path truncated: %d, skipping dump.", len);
209-
free(filename);
209+
my_free(filename);
210210
return;
211211
}
212212

@@ -225,7 +225,7 @@ void wsrep_dump_rbr_buf(THD *thd, const void* rbr_buf, size_t buf_len)
225225
WSREP_ERROR("Failed to open file '%s': %d (%s)",
226226
filename, errno, strerror(errno));
227227
}
228-
free(filename);
228+
my_free(filename);
229229
}
230230

231231
/* Dump replication buffer along with header to a file. */
@@ -248,7 +248,7 @@ void wsrep_dump_rbr_buf_with_header(THD *thd, const void *rbr_buf,
248248
to alloc and pass as an argument to snprintf.
249249
*/
250250
char *filename;
251-
if (len < 0 || !(filename= (char*)malloc(len+1)))
251+
if (len < 0 || !(filename= (char*) my_malloc(key_memory_WSREP, len+1, 0)))
252252
{
253253
WSREP_ERROR("snprintf error: %d, skipping dump.", len);
254254
DBUG_VOID_RETURN;
@@ -261,7 +261,7 @@ void wsrep_dump_rbr_buf_with_header(THD *thd, const void *rbr_buf,
261261
if (len > len1)
262262
{
263263
WSREP_ERROR("RBR dump path truncated: %d, skipping dump.", len);
264-
free(filename);
264+
my_free(filename);
265265
DBUG_VOID_RETURN;
266266
}
267267

@@ -301,7 +301,7 @@ void wsrep_dump_rbr_buf_with_header(THD *thd, const void *rbr_buf,
301301
end_io_cache(&cache);
302302

303303
cleanup1:
304-
free(filename);
304+
my_free(filename);
305305
mysql_file_close(file, MYF(MY_WME));
306306

307307
if (!thd->wsrep_applier) delete ev;

sql/wsrep_mysqld.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ void wsrep_deinit_server()
847847
{
848848
wsrep_deinit_schema();
849849
Wsrep_server_state::destroy();
850+
wsrep_free_status_vars();
850851
}
851852

852853
int wsrep_init()

sql/wsrep_sst.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,9 +1656,9 @@ static int sst_flush_tables(THD* thd)
16561656

16571657
const char base_name[]= "tables_flushed";
16581658
ssize_t const full_len= strlen(mysql_real_data_home) + strlen(base_name)+2;
1659-
char *real_name= (char*) malloc(full_len);
1659+
char *real_name= (char*) my_malloc(key_memory_WSREP, full_len, 0);
16601660
sprintf(real_name, "%s/%s", mysql_real_data_home, base_name);
1661-
char *tmp_name= (char*) malloc(full_len + 4);
1661+
char *tmp_name= (char*) my_malloc(key_memory_WSREP, full_len + 4, 0);
16621662
sprintf(tmp_name, "%s.tmp", real_name);
16631663

16641664
FILE* file= fopen(tmp_name, "w+");
@@ -1686,8 +1686,8 @@ static int sst_flush_tables(THD* thd)
16861686
tmp_name, real_name, err,strerror(err));
16871687
}
16881688
}
1689-
free(real_name);
1690-
free(tmp_name);
1689+
my_free(real_name);
1690+
my_free(tmp_name);
16911691
if (err)
16921692
ha_disable_internal_writes(false);
16931693
}

sql/wsrep_utils.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,16 @@ namespace wsp
9696
bool
9797
env::ctor_common(char** e)
9898
{
99-
env_= static_cast<char**>(malloc((len_ + 1) * sizeof(char*)));
99+
env_= static_cast<char**>(my_malloc(key_memory_WSREP,
100+
(len_ + 1) * sizeof(char*),
101+
0));
100102

101103
if (env_)
102104
{
103105
for (size_t i(0); i < len_; ++i)
104106
{
105107
assert(e[i]); // caller should make sure about len_
106-
env_[i]= strdup(e[i]);
108+
env_[i]= my_strdup(key_memory_WSREP, e[i], MYF(0));
107109
if (!env_[i])
108110
{
109111
errno_= errno;
@@ -129,8 +131,8 @@ env::dtor()
129131
if (env_)
130132
{
131133
/* don't need to go beyond the first NULL */
132-
for (size_t i(0); env_[i] != NULL; ++i) { free(env_[i]); }
133-
free(env_);
134+
for (size_t i(0); env_[i] != NULL; ++i) { my_free(env_[i]); }
135+
my_free(env_);
134136
env_= NULL;
135137
}
136138
len_= 0;
@@ -157,12 +159,13 @@ env::~env() { dtor(); }
157159
int
158160
env::append(const char* val)
159161
{
160-
char** tmp= static_cast<char**>(realloc(env_, (len_ + 2)*sizeof(char*)));
161-
162+
char** tmp= static_cast<char**>(my_realloc(key_memory_WSREP,
163+
env_, (len_ + 2)*sizeof(char*),
164+
0));
162165
if (tmp)
163166
{
164167
env_= tmp;
165-
env_[len_]= strdup(val);
168+
env_[len_]= my_strdup(key_memory_WSREP, val, 0);
166169

167170
if (env_[len_])
168171
{

sql/wsrep_var.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,8 +1057,10 @@ static void export_wsrep_status_to_mysql(THD* thd)
10571057

10581058
#if DYNAMIC
10591059
if (wsrep_status_len != mysql_status_len) {
1060-
void* tmp= realloc (mysql_status_vars,
1061-
(wsrep_status_len + 1) * sizeof(SHOW_VAR));
1060+
void* tmp= my_realloc(key_memory_WSREP,
1061+
mysql_status_vars,
1062+
(wsrep_status_len + 1) * sizeof(SHOW_VAR),
1063+
MYF(MY_ALLOW_ZERO_PTR));
10621064
if (!tmp) {
10631065

10641066
sql_print_error ("Out of memory for wsrep status variables."
@@ -1110,6 +1112,15 @@ void wsrep_free_status (THD* thd)
11101112
thd->wsrep_status_vars.clear();
11111113
}
11121114

1115+
void wsrep_free_status_vars()
1116+
{
1117+
#if DYNAMIC
1118+
my_free(mysql_status_vars);
1119+
mysql_status_vars= NULL;
1120+
mysql_status_len= 0;
1121+
#endif
1122+
}
1123+
11131124
bool wsrep_gtid_domain_id_update(sys_var* self, THD *thd, enum_var_type)
11141125
{
11151126
WSREP_DEBUG("wsrep_gtid_domain_id_update: %llu",

sql/wsrep_var.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class THD;
3737

3838
int wsrep_init_vars();
3939
void wsrep_set_wsrep_on(THD *thd);
40+
void wsrep_free_status_vars();
4041

4142
#define CHECK_ARGS (sys_var *self, THD* thd, set_var *var)
4243
#define UPDATE_ARGS (sys_var *self, THD* thd, enum_var_type type)

0 commit comments

Comments
 (0)