1
/**********************************************************************
2
Data dictionary memory object creation
6
Created 1/8/1996 Heikki Tuuri
7
***********************************************************************/
12
#include "dict0mem.ic"
16
#include "data0type.h"
17
#include "mach0data.h"
18
#include "dict0dict.h"
20
#include "pars0pars.h"
21
#include "lock0lock.h"
23
#define DICT_HEAP_SIZE 100 /* initial memory heap size when
24
creating a table or index object */
26
/**************************************************************************
27
Creates a table memory object. */
30
dict_mem_table_create(
31
/*==================*/
32
/* out, own: table object */
33
const char* name, /* in: table name */
34
ulint space, /* in: space where the clustered index of
35
the table is placed; this parameter is
36
ignored if the table is made a member of
38
ulint n_cols, /* in: number of columns */
39
ulint flags) /* in: table flags */
45
ut_ad(!(flags & ~DICT_TF_COMPACT));
47
heap = mem_heap_create(DICT_HEAP_SIZE);
49
table = mem_heap_alloc(heap, sizeof(dict_table_t));
53
table->flags = (unsigned int) flags;
54
table->name = mem_heap_strdup(heap, name);
55
table->dir_path_of_temp_table = NULL;
56
table->space = (unsigned int) space;
57
table->ibd_file_missing = FALSE;
58
table->tablespace_discarded = FALSE;
60
table->n_cols = (unsigned int) (n_cols + DATA_N_SYS_COLS);
62
table->n_mysql_handles_opened = 0;
63
table->n_foreign_key_checks_running = 0;
65
table->cached = FALSE;
67
table->cols = mem_heap_alloc(heap, (n_cols + DATA_N_SYS_COLS)
68
* sizeof(dict_col_t));
69
table->col_names = NULL;
70
UT_LIST_INIT(table->indexes);
72
table->auto_inc_lock = mem_heap_alloc(heap, lock_get_size());
74
table->query_cache_inv_trx_id = ut_dulint_zero;
76
UT_LIST_INIT(table->locks);
77
UT_LIST_INIT(table->foreign_list);
78
UT_LIST_INIT(table->referenced_list);
81
table->does_not_fit_in_memory = FALSE;
82
#endif /* UNIV_DEBUG */
84
table->stat_initialized = FALSE;
86
table->stat_modified_counter = 0;
90
mutex_create(&table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
92
table->autoinc_inited = FALSE;
94
/* The actual increment value will be set by MySQL, we simply
96
table->autoinc_increment = 1;
98
/* The number of transactions that are either waiting on the
99
AUTOINC lock or have been granted the lock. */
100
table->n_waiting_or_granted_auto_inc_locks = 0;
103
table->magic_n = DICT_TABLE_MAGIC_N;
104
#endif /* UNIV_DEBUG */
108
/********************************************************************
109
Free a table memory object. */
114
dict_table_t* table) /* in: table */
117
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
119
mutex_free(&(table->autoinc_mutex));
120
mem_heap_free(table->heap);
123
/********************************************************************
124
Append 'name' to 'col_names' (@see dict_table_t::col_names). */
129
/* out: new column names array */
130
const char* col_names, /* in: existing column names, or
132
ulint cols, /* in: number of existing columns */
133
const char* name, /* in: new column name */
134
mem_heap_t* heap) /* in: heap */
141
ut_ad(!cols == !col_names);
143
/* Find out length of existing array. */
145
const char* s = col_names;
148
for (i = 0; i < cols; i++) {
152
old_len = s - col_names;
157
new_len = strlen(name) + 1;
158
total_len = old_len + new_len;
160
res = mem_heap_alloc(heap, total_len);
163
memcpy(res, col_names, old_len);
166
memcpy(res + old_len, name, new_len);
171
/**************************************************************************
172
Adds a column definition to a table. */
175
dict_mem_table_add_col(
176
/*===================*/
177
dict_table_t* table, /* in: table */
178
mem_heap_t* heap, /* in: temporary memory heap, or NULL */
179
const char* name, /* in: column name, or NULL */
180
ulint mtype, /* in: main datatype */
181
ulint prtype, /* in: precise type */
182
ulint len) /* in: precision */
190
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
191
ut_ad(!heap == !name);
196
if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
199
if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
200
/* All preceding column names are empty. */
201
char* s = mem_heap_alloc(heap, table->n_def);
202
memset(s, 0, table->n_def);
203
table->col_names = s;
206
table->col_names = dict_add_col_name(table->col_names,
210
col = (dict_col_t*) dict_table_get_nth_col(table, i);
212
col->ind = (unsigned int) i;
215
col->mtype = (unsigned int) mtype;
216
col->prtype = (unsigned int) prtype;
217
col->len = (unsigned int) len;
219
dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
221
col->mbminlen = (unsigned int) mbminlen;
222
col->mbmaxlen = (unsigned int) mbmaxlen;
225
/**************************************************************************
226
Creates an index memory object. */
229
dict_mem_index_create(
230
/*==================*/
231
/* out, own: index object */
232
const char* table_name, /* in: table name */
233
const char* index_name, /* in: index name */
234
ulint space, /* in: space where the index tree is
235
placed, ignored if the index is of
236
the clustered type */
237
ulint type, /* in: DICT_UNIQUE,
238
DICT_CLUSTERED, ... ORed */
239
ulint n_fields) /* in: number of fields */
244
ut_ad(table_name && index_name);
246
heap = mem_heap_create(DICT_HEAP_SIZE);
247
index = mem_heap_alloc(heap, sizeof(dict_index_t));
252
index->space = (unsigned int) space;
254
index->name = mem_heap_strdup(heap, index_name);
255
index->table_name = table_name;
257
index->n_def = index->n_nullable = 0;
258
index->n_fields = (unsigned int) n_fields;
259
index->fields = mem_heap_alloc(heap, 1 + n_fields
260
* sizeof(dict_field_t));
261
/* The '1 +' above prevents allocation
262
of an empty mem block */
263
index->stat_n_diff_key_vals = NULL;
265
index->cached = FALSE;
266
memset(&index->lock, 0, sizeof index->lock);
268
index->magic_n = DICT_INDEX_MAGIC_N;
269
#endif /* UNIV_DEBUG */
273
/**************************************************************************
274
Creates and initializes a foreign constraint memory object. */
277
dict_mem_foreign_create(void)
278
/*=========================*/
279
/* out, own: foreign constraint struct */
281
dict_foreign_t* foreign;
284
heap = mem_heap_create(100);
286
foreign = mem_heap_alloc(heap, sizeof(dict_foreign_t));
288
foreign->heap = heap;
293
foreign->foreign_table_name = NULL;
294
foreign->foreign_table = NULL;
295
foreign->foreign_col_names = NULL;
297
foreign->referenced_table_name = NULL;
298
foreign->referenced_table = NULL;
299
foreign->referenced_col_names = NULL;
301
foreign->n_fields = 0;
303
foreign->foreign_index = NULL;
304
foreign->referenced_index = NULL;
309
/**************************************************************************
310
Adds a field definition to an index. NOTE: does not take a copy
311
of the column name if the field is a column. The memory occupied
312
by the column name may be released only after publishing the index. */
315
dict_mem_index_add_field(
316
/*=====================*/
317
dict_index_t* index, /* in: index */
318
const char* name, /* in: column name */
319
ulint prefix_len) /* in: 0 or the column prefix length
320
in a MySQL index like
321
INDEX (textcol(25)) */
326
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
330
field = dict_index_get_nth_field(index, index->n_def - 1);
333
field->prefix_len = (unsigned int) prefix_len;
336
/**************************************************************************
337
Frees an index memory object. */
342
dict_index_t* index) /* in: index */
345
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
347
mem_heap_free(index->heap);