1 /* Copyright (c) 2015, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef SQL_PLUGIN_REF_INCLUDED
24 #define SQL_PLUGIN_REF_INCLUDED
25 
26 #include "my_alloc.h"
27 #include "mysql/mysql_lex_string.h"
28 #include "prealloced_array.h"
29 
30 typedef struct st_mysql_lex_string LEX_STRING;
31 
32 class sys_var;
33 struct st_mysql_plugin;
34 struct st_plugin_dl;
35 
36 enum enum_plugin_load_option {
37   PLUGIN_OFF,
38   PLUGIN_ON,
39   PLUGIN_FORCE,
40   PLUGIN_FORCE_PLUS_PERMANENT
41 };
42 
43 /* A handle of a plugin */
44 
45 struct st_plugin_int
46 {
47   LEX_STRING name;
48   st_mysql_plugin *plugin;
49   st_plugin_dl *plugin_dl;
50   uint state;
51   uint ref_count;               /* number of threads using the plugin */
52   void *data;                   /* plugin type specific, e.g. handlerton */
53   MEM_ROOT mem_root;            /* memory for dynamic plugin structures */
54   sys_var *system_vars;         /* server variables for this plugin */
55   enum_plugin_load_option load_option; /* OFF, ON, FORCE, F+PERMANENT */
56 };
57 
58 /*
59   See intern_plugin_lock() for the explanation for the
60   conditionally defined plugin_ref type
61 */
62 
63 #ifdef NDEBUG
64 typedef struct st_plugin_int *plugin_ref;
65 
plugin_decl(st_plugin_int * ref)66 inline st_mysql_plugin *plugin_decl(st_plugin_int *ref)
67 {
68   return ref->plugin;
69 }
plugin_dlib(st_plugin_int * ref)70 inline st_plugin_dl *plugin_dlib(st_plugin_int *ref)
71 {
72   return ref->plugin_dl;
73 }
74 template<typename T>
plugin_data(st_plugin_int * ref)75 inline T plugin_data(st_plugin_int *ref)
76 {
77   return static_cast<T>(ref->data);
78 }
plugin_name(st_plugin_int * ref)79 inline LEX_STRING *plugin_name(st_plugin_int *ref)
80 {
81   return &(ref->name);
82 }
plugin_state(st_plugin_int * ref)83 inline uint plugin_state(st_plugin_int *ref)
84 {
85   return ref->state;
86 }
plugin_load_option(st_plugin_int * ref)87 inline enum_plugin_load_option plugin_load_option(st_plugin_int *ref)
88 {
89   return ref->load_option;
90 }
plugin_equals(st_plugin_int * ref1,st_plugin_int * ref2)91 inline bool plugin_equals(st_plugin_int *ref1, st_plugin_int *ref2)
92 {
93   return ref1 == ref2;
94 }
95 
96 #else
97 
98 typedef struct st_plugin_int **plugin_ref;
99 
plugin_decl(st_plugin_int ** ref)100 inline st_mysql_plugin *plugin_decl(st_plugin_int **ref)
101 {
102   return ref[0]->plugin;
103 }
plugin_dlib(st_plugin_int ** ref)104 inline st_plugin_dl *plugin_dlib(st_plugin_int **ref)
105 {
106   return ref[0]->plugin_dl;
107 }
108 template<typename T>
plugin_data(st_plugin_int ** ref)109 inline T plugin_data(st_plugin_int **ref)
110 {
111   return static_cast<T>(ref[0]->data);
112 }
plugin_name(st_plugin_int ** ref)113 inline LEX_STRING *plugin_name(st_plugin_int **ref)
114 {
115   return &(ref[0]->name);
116 }
plugin_state(st_plugin_int ** ref)117 inline uint plugin_state(st_plugin_int **ref)
118 {
119   return ref[0]->state;
120 }
plugin_load_option(st_plugin_int ** ref)121 inline enum_plugin_load_option plugin_load_option(st_plugin_int **ref)
122 {
123   return ref[0]->load_option;
124 }
plugin_equals(st_plugin_int ** ref1,st_plugin_int ** ref2)125 inline bool plugin_equals(st_plugin_int **ref1, st_plugin_int **ref2)
126 {
127   return ref1 && ref2 && (ref1[0] == ref2[0]);
128 }
129 #endif
130 
131 /**
132   @class Plugin_array
133 
134   @brief Plugin array helper class.
135 */
136 class Plugin_array : public Prealloced_array<plugin_ref, 2>
137 {
138 public:
139   /**
140     Class construction.
141 
142     @param psi_key PSI key.
143   */
Plugin_array(PSI_memory_key psi_key)144   explicit Plugin_array(PSI_memory_key psi_key) :
145     Prealloced_array<plugin_ref, 2>(psi_key)
146   {
147   }
148 
149   /**
150     Check, whether the plugin specified by the plugin argument has been
151     already added into the array.
152 
153     @param plugin Plugin to check.
154 
155     @retval true  Plugin has been already added.
156     @retval false There is no plugin in the array.
157   */
exists(plugin_ref plugin)158   bool exists(plugin_ref plugin)
159   {
160     Plugin_array::iterator i;
161 
162     for (i= begin(); i != end(); ++i)
163       if (plugin_equals(*i, plugin))
164         return true;
165 
166     return false;
167   }
168 };
169 
170 #endif  // SQL_PLUGIN_REF_INCLUDED
171