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