MySQL 8.0.30
Source Code Documentation
sys_vars_resource_mgr.h
Go to the documentation of this file.
1#ifndef SYS_VARS_RESOURCE_MGR_INCLUDED
2#define SYS_VARS_RESOURCE_MGR_INCLUDED
3
4/* Copyright (c) 2014, 2022, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26/**
27 Session_sysvar_resource_manager
28 -------------------------------
29 When a session (THD) gets initialized, it receives a shallow copy of all
30 global system variables.
31 thd->variables= global_system_variables; (see plugin_thdvar_init())
32
33 In case of Sys_var_charptr variables, we need to maintain a separate copy for
34 each session though so that global and session variables can be altered
35 independently.
36
37 This class is responsible for alloc|dealloc-ating memory for Sys_var_charptr
38 variables for every session. It works in three steps :
39
40 (1) init :
41 Creates a copy (memdup()) of global Sys_var_charptr system variable for
42 the respective session variable (passed as a parameter) & inserts it
43 into sysvar_string_alloc_hash (containing the allocated address) to infer
44 that memory has been allocated for the session. init() is called during
45 the initialization of session system variables. (plugin_thdvar_init())
46 (2) update :
47 When the session variable is updated, the old memory is freed and new
48 memory is allocated to hold the new value. The corresponding member in
49 sysvar_string_alloc_hash is also updated to hold the new allocated memory
50 address. (Sys_var_charptr::session_update())
51 (3) deinit :
52 Its a one-shot operation to free all the session Sys_var_charptr system
53 variables. It basically traverses down the sysvar_string_alloc_hash
54 hash and calls free() for all the addresses that it holds.
55
56 Note, there should always be at most one node per Sys_var_charptr session
57 system variable.
58
59*/
60
61#include <stddef.h>
62#include <memory>
63
64#include "map_helpers.h"
65#include "sql/psi_memory_key.h"
66
68 private:
69 // The value always contains the string that the key points to.
73
74 public:
75 /**
76 Allocates memory for Sys_var_charptr session variable during session
77 initialization.
78 */
79 bool init(char **var);
80
81 /**
82 Frees the old allocated memory, memdup()'s the given val to a new memory
83 address & updates the session variable pointer.
84 */
85 bool update(char **var, char *val, size_t val_len);
86
87 void claim_memory_ownership(bool claim);
88
89 /**
90 Frees the memory allocated for Sys_var_charptr session variables.
91 */
92 void deinit();
93};
94
95#endif /* SYS_VARS_RESOURCE_MGR_INCLUDED */
Definition: sys_vars_resource_mgr.h:67
bool init(char **var)
Allocates memory for Sys_var_charptr session variable during session initialization.
Definition: sys_vars_resource_mgr.cc:46
void deinit()
Frees the memory allocated for Sys_var_charptr session variables.
Definition: sys_vars_resource_mgr.cc:110
malloc_unordered_map< char **, unique_ptr_my_free< char > > m_sysvar_string_alloc_hash
Definition: sys_vars_resource_mgr.h:71
bool update(char **var, char *val, size_t val_len)
Frees the old allocated memory, memdup()'s the given val to a new memory address & updates the sessio...
Definition: sys_vars_resource_mgr.cc:72
void claim_memory_ownership(bool claim)
Definition: sys_vars_resource_mgr.cc:99
std::unordered_map, but with my_malloc, so that you can track the memory used using PSI memory keys.
Definition: map_helpers.h:147
PSI_memory_key key_memory_THD_Session_sysvar_resource_manager
Definition: psi_memory_key.cc:84