drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
dict0mem.cc
1/*****************************************************************************
2
3Copyright (C) 1996, 2010, Innobase Oy. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free Software
7Foundation; version 2 of the License.
8
9This program is distributed in the hope that it will be useful, but WITHOUT
10ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License along with
14this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15St, Fifth Floor, Boston, MA 02110-1301 USA
16
17*****************************************************************************/
18
19/******************************************************************/
25
26#include "dict0mem.h"
27
28#ifdef UNIV_NONINL
29#include "dict0mem.ic"
30#endif
31
32#include "rem0rec.h"
33#include "data0type.h"
34#include "mach0data.h"
35#include "dict0dict.h"
36#include "srv0srv.h" /* srv_lower_case_table_names */
37#include "ha_prototypes.h" /* innobase_casedn_str()*/
38#ifndef UNIV_HOTBACKUP
39# include "lock0lock.h"
40#endif /* !UNIV_HOTBACKUP */
41#ifdef UNIV_BLOB_DEBUG
42# include "ut0rbt.h"
43#endif /* UNIV_BLOB_DEBUG */
44
45#define DICT_HEAP_SIZE 100
47
48#ifdef UNIV_PFS_MUTEX
49/* Key to register autoinc_mutex with performance schema */
50UNIV_INTERN mysql_pfs_key_t autoinc_mutex_key;
51#endif /* UNIV_PFS_MUTEX */
52
53/**********************************************************************/
56UNIV_INTERN
57dict_table_t*
58dict_mem_table_create(
59/*==================*/
60 const char* name,
61 ulint space,
65 ulint n_cols,
66 ulint flags)
67{
68 dict_table_t* table;
69 mem_heap_t* heap;
70
71 ut_ad(name);
72 ut_a(!(flags & (SIZE_MAX << DICT_TF2_BITS)));
73
74 heap = mem_heap_create(DICT_HEAP_SIZE);
75
76 table = static_cast<dict_table_struct *>(mem_heap_zalloc(heap, sizeof(dict_table_t)));
77
78 table->heap = heap;
79
80 table->flags = (unsigned int) flags;
81 table->name = static_cast<char *>(ut_malloc(strlen(name) + 1));
82 memcpy(table->name, name, strlen(name) + 1);
83 table->space = (unsigned int) space;
84 table->n_cols = (unsigned int) (n_cols + DATA_N_SYS_COLS);
85
86 table->cols = static_cast<dict_col_t *>(mem_heap_alloc(heap, (n_cols + DATA_N_SYS_COLS)
87 * sizeof(dict_col_t)));
88
89#ifndef UNIV_HOTBACKUP
90 table->autoinc_lock = static_cast<lock_t *>(mem_heap_alloc(heap, lock_get_size()));
91
92 mutex_create(autoinc_mutex_key,
93 &table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
94
95 table->autoinc = 0;
96
97 /* The number of transactions that are either waiting on the
98 AUTOINC lock or have been granted the lock. */
99 table->n_waiting_or_granted_auto_inc_locks = 0;
100#endif /* !UNIV_HOTBACKUP */
101
102 ut_d(table->magic_n = DICT_TABLE_MAGIC_N);
103 return(table);
104}
105
106/****************************************************************/
108UNIV_INTERN
109void
110dict_mem_table_free(
111/*================*/
112 dict_table_t* table)
113{
114 ut_ad(table);
115 ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
116 ut_d(table->cached = FALSE);
117
118#ifndef UNIV_HOTBACKUP
119 mutex_free(&(table->autoinc_mutex));
120#endif /* UNIV_HOTBACKUP */
121 ut_free(table->name);
122 mem_heap_free(table->heap);
123}
124
125/****************************************************************/
128static
129const char*
130dict_add_col_name(
131/*==============*/
132 const char* col_names,
134 ulint cols,
135 const char* name,
136 mem_heap_t* heap)
137{
138 ulint old_len;
139 ulint new_len;
140 ulint total_len;
141 char* res;
142
143 ut_ad(!cols == !col_names);
144
145 /* Find out length of existing array. */
146 if (col_names) {
147 const char* s = col_names;
148 ulint i;
149
150 for (i = 0; i < cols; i++) {
151 s += strlen(s) + 1;
152 }
153
154 old_len = s - col_names;
155 } else {
156 old_len = 0;
157 }
158
159 new_len = strlen(name) + 1;
160 total_len = old_len + new_len;
161
162 res = static_cast<char *>(mem_heap_alloc(heap, total_len));
163
164 if (old_len > 0) {
165 memcpy(res, col_names, old_len);
166 }
167
168 memcpy(res + old_len, name, new_len);
169
170 return(res);
171}
172
173/**********************************************************************/
175UNIV_INTERN
176void
177dict_mem_table_add_col(
178/*===================*/
179 dict_table_t* table,
180 mem_heap_t* heap,
181 const char* name,
182 ulint mtype,
183 ulint prtype,
184 ulint len)
185{
186 dict_col_t* col;
187 ulint i;
188
189 ut_ad(table);
190 ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
191 ut_ad(!heap == !name);
192
193 i = table->n_def++;
194
195 if (name) {
196 if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
197 heap = table->heap;
198 }
199 if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
200 /* All preceding column names are empty. */
201 char* s = static_cast<char *>(mem_heap_zalloc(heap, table->n_def));
202 table->col_names = s;
203 }
204
205 table->col_names = dict_add_col_name(table->col_names,
206 i, name, heap);
207 }
208
209 col = dict_table_get_nth_col(table, i);
210
211 dict_mem_fill_column_struct(col, i, mtype, prtype, len);
212}
213
214
215/**********************************************************************/
218UNIV_INTERN
219void
220dict_mem_fill_column_struct(
221/*========================*/
222 dict_col_t* column,
224 ulint col_pos,
225 ulint mtype,
226 ulint prtype,
227 ulint col_len)
228{
229#ifndef UNIV_HOTBACKUP
230 ulint mbminlen;
231 ulint mbmaxlen;
232#endif /* !UNIV_HOTBACKUP */
233
234 column->ind = (unsigned int) col_pos;
235 column->ord_part = 0;
236 column->mtype = (unsigned int) mtype;
237 column->prtype = (unsigned int) prtype;
238 column->len = (unsigned int) col_len;
239#ifndef UNIV_HOTBACKUP
240 dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
241 dict_col_set_mbminmaxlen(column, mbminlen, mbmaxlen);
242#endif /* !UNIV_HOTBACKUP */
243}
244
245/**********************************************************************/
248UNIV_INTERN
249dict_index_t*
250dict_mem_index_create(
251/*==================*/
252 const char* table_name,
253 const char* index_name,
254 ulint space,
257 ulint type,
259 ulint n_fields)
260{
261 dict_index_t* index;
262 mem_heap_t* heap;
263
264 ut_ad(table_name && index_name);
265
266 heap = mem_heap_create(DICT_HEAP_SIZE);
267 index = static_cast<dict_index_t *>(mem_heap_zalloc(heap, sizeof(dict_index_t)));
268
269 dict_mem_fill_index_struct(index, heap, table_name, index_name,
270 space, type, n_fields);
271
272 return(index);
273}
274
275/**********************************************************************/
278UNIV_INTERN
279dict_foreign_t*
281/*=========================*/
282{
283 dict_foreign_t* foreign;
284 mem_heap_t* heap;
285
286 heap = mem_heap_create(100);
287
288 foreign = static_cast<dict_foreign_t *>(mem_heap_zalloc(heap, sizeof(dict_foreign_t)));
289
290 foreign->heap = heap;
291
292 return(foreign);
293}
294
295/**********************************************************************/
300UNIV_INTERN
301void
302dict_mem_foreign_table_name_lookup_set(
303/*===================================*/
304 dict_foreign_t* foreign,
305 ibool do_alloc)
306{
307 if (srv_lower_case_table_names == 2) {
308 if (do_alloc) {
309 foreign->foreign_table_name_lookup = static_cast<char *>(mem_heap_alloc(
310 foreign->heap,
311 strlen(foreign->foreign_table_name) + 1));
312 }
313 strcpy(foreign->foreign_table_name_lookup,
314 foreign->foreign_table_name);
316 } else {
318 = foreign->foreign_table_name;
319 }
320}
321
322/**********************************************************************/
327UNIV_INTERN
328void
329dict_mem_referenced_table_name_lookup_set(
330/*======================================*/
331 dict_foreign_t* foreign,
332 ibool do_alloc)
333{
334 if (srv_lower_case_table_names == 2) {
335 if (do_alloc) {
336 foreign->referenced_table_name_lookup = static_cast<char *>(mem_heap_alloc(
337 foreign->heap,
338 strlen(foreign->referenced_table_name) + 1));
339 }
340 strcpy(foreign->referenced_table_name_lookup,
341 foreign->referenced_table_name);
343 } else {
345 = foreign->referenced_table_name;
346 }
347}
348
349/**********************************************************************/
353UNIV_INTERN
354void
355dict_mem_index_add_field(
356/*=====================*/
357 dict_index_t* index,
358 const char* name,
359 ulint prefix_len)
362{
363 dict_field_t* field;
364
365 ut_ad(index);
366 ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
367
368 index->n_def++;
369
370 field = dict_index_get_nth_field(index, index->n_def - 1);
371
372 field->name = name;
373 field->prefix_len = (unsigned int) prefix_len;
374}
375
376/**********************************************************************/
378UNIV_INTERN
379void
380dict_mem_index_free(
381/*================*/
382 dict_index_t* index)
383{
384 ut_ad(index);
385 ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
386#ifdef UNIV_BLOB_DEBUG
387 if (index->blobs) {
388 mutex_free(&index->blobs_mutex);
389 rbt_free(index->blobs);
390 }
391#endif /* UNIV_BLOB_DEBUG */
392
393 mem_heap_free(index->heap);
394}
UNIV_INLINE void dtype_get_mblen(ulint mtype, ulint prtype, ulint *mbminlen, ulint *mbmaxlen)
UNIV_INLINE void dict_col_set_mbminmaxlen(dict_col_t *col, ulint mbminlen, ulint mbmaxlen)
UNIV_INLINE void dict_mem_fill_index_struct(dict_index_t *index, mem_heap_t *heap, const char *table_name, const char *index_name, ulint space, ulint type, ulint n_fields)
UNIV_INTERN dict_foreign_t * dict_mem_foreign_create(void)
Definition dict0mem.cc:280
#define DICT_TF2_BITS
Definition dict0mem.h:111
UNIV_INTERN void innobase_casedn_str(char *a)
UNIV_INTERN ulint lock_get_size(void)
Definition lock0lock.cc:601
#define mem_heap_create(N)
Definition mem0mem.h:91
UNIV_INLINE void * mem_heap_alloc(mem_heap_t *heap, ulint n)
#define mem_heap_free(heap)
Definition mem0mem.h:111
UNIV_INLINE void * mem_heap_zalloc(mem_heap_t *heap, ulint n)
unsigned ind
Definition dict0mem.h:293
unsigned ord_part
Definition dict0mem.h:295
unsigned prtype
Definition dict0mem.h:265
unsigned len
Definition dict0mem.h:275
unsigned mtype
Definition dict0mem.h:264
const char * name
Definition dict0mem.h:313
unsigned prefix_len
Definition dict0mem.h:314
char * referenced_table_name
Definition dict0mem.h:433
char * foreign_table_name_lookup
Definition dict0mem.h:428
char * foreign_table_name
Definition dict0mem.h:427
mem_heap_t * heap
Definition dict0mem.h:415
char * referenced_table_name_lookup
Definition dict0mem.h:434
mem_heap_t * heap
Definition dict0mem.h:470
#define ut_ad(EXPR)
Definition ut0dbg.h:127
#define ut_d(EXPR)
Definition ut0dbg.h:129
#define ut_a(EXPR)
Definition ut0dbg.h:105
UNIV_INTERN void ut_free(void *ptr)
Definition ut0mem.cc:294
UNIV_INTERN void * ut_malloc(ulint n)
Definition ut0mem.cc:235
UNIV_INTERN void rbt_free(ib_rbt_t *tree)
Definition ut0rbt.cc:743