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);
94
/* The number of transactions that are either waiting on the
95
AUTOINC lock or have been granted the lock. */
96
table->n_waiting_or_granted_auto_inc_locks = 0;
99
table->magic_n = DICT_TABLE_MAGIC_N;
100
#endif /* UNIV_DEBUG */
104
/********************************************************************
105
Free a table memory object. */
110
dict_table_t* table) /* in: table */
113
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
115
mutex_free(&(table->autoinc_mutex));
116
mem_heap_free(table->heap);
119
/********************************************************************
120
Append 'name' to 'col_names' (@see dict_table_t::col_names). */
125
/* out: new column names array */
126
const char* col_names, /* in: existing column names, or
128
ulint cols, /* in: number of existing columns */
129
const char* name, /* in: new column name */
130
mem_heap_t* heap) /* in: heap */
137
ut_ad(!cols == !col_names);
139
/* Find out length of existing array. */
141
const char* s = col_names;
144
for (i = 0; i < cols; i++) {
148
old_len = s - col_names;
153
new_len = strlen(name) + 1;
154
total_len = old_len + new_len;
156
res = mem_heap_alloc(heap, total_len);
159
memcpy(res, col_names, old_len);
162
memcpy(res + old_len, name, new_len);
167
/**************************************************************************
168
Adds a column definition to a table. */
171
dict_mem_table_add_col(
172
/*===================*/
173
dict_table_t* table, /* in: table */
174
mem_heap_t* heap, /* in: temporary memory heap, or NULL */
175
const char* name, /* in: column name, or NULL */
176
ulint mtype, /* in: main datatype */
177
ulint prtype, /* in: precise type */
178
ulint len) /* in: precision */
186
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
187
ut_ad(!heap == !name);
192
if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
195
if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
196
/* All preceding column names are empty. */
197
char* s = mem_heap_alloc(heap, table->n_def);
198
memset(s, 0, table->n_def);
199
table->col_names = s;
202
table->col_names = dict_add_col_name(table->col_names,
206
col = (dict_col_t*) dict_table_get_nth_col(table, i);
208
col->ind = (unsigned int) i;
211
col->mtype = (unsigned int) mtype;
212
col->prtype = (unsigned int) prtype;
213
col->len = (unsigned int) len;
215
dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
217
col->mbminlen = (unsigned int) mbminlen;
218
col->mbmaxlen = (unsigned int) mbmaxlen;
221
/**************************************************************************
222
Creates an index memory object. */
225
dict_mem_index_create(
226
/*==================*/
227
/* out, own: index object */
228
const char* table_name, /* in: table name */
229
const char* index_name, /* in: index name */
230
ulint space, /* in: space where the index tree is
231
placed, ignored if the index is of
232
the clustered type */
233
ulint type, /* in: DICT_UNIQUE,
234
DICT_CLUSTERED, ... ORed */
235
ulint n_fields) /* in: number of fields */
240
ut_ad(table_name && index_name);
242
heap = mem_heap_create(DICT_HEAP_SIZE);
243
index = mem_heap_alloc(heap, sizeof(dict_index_t));
248
index->space = (unsigned int) space;
250
index->name = mem_heap_strdup(heap, index_name);
251
index->table_name = table_name;
253
index->n_def = index->n_nullable = 0;
254
index->n_fields = (unsigned int) n_fields;
255
index->fields = mem_heap_alloc(heap, 1 + n_fields
256
* sizeof(dict_field_t));
257
/* The '1 +' above prevents allocation
258
of an empty mem block */
259
index->stat_n_diff_key_vals = NULL;
261
index->cached = FALSE;
262
memset(&index->lock, 0, sizeof index->lock);
264
index->magic_n = DICT_INDEX_MAGIC_N;
265
#endif /* UNIV_DEBUG */
269
/**************************************************************************
270
Creates and initializes a foreign constraint memory object. */
273
dict_mem_foreign_create(void)
274
/*=========================*/
275
/* out, own: foreign constraint struct */
277
dict_foreign_t* foreign;
280
heap = mem_heap_create(100);
282
foreign = mem_heap_alloc(heap, sizeof(dict_foreign_t));
284
foreign->heap = heap;
289
foreign->foreign_table_name = NULL;
290
foreign->foreign_table = NULL;
291
foreign->foreign_col_names = NULL;
293
foreign->referenced_table_name = NULL;
294
foreign->referenced_table = NULL;
295
foreign->referenced_col_names = NULL;
297
foreign->n_fields = 0;
299
foreign->foreign_index = NULL;
300
foreign->referenced_index = NULL;
305
/**************************************************************************
306
Adds a field definition to an index. NOTE: does not take a copy
307
of the column name if the field is a column. The memory occupied
308
by the column name may be released only after publishing the index. */
311
dict_mem_index_add_field(
312
/*=====================*/
313
dict_index_t* index, /* in: index */
314
const char* name, /* in: column name */
315
ulint prefix_len) /* in: 0 or the column prefix length
316
in a MySQL index like
317
INDEX (textcol(25)) */
322
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
326
field = dict_index_get_nth_field(index, index->n_def - 1);
329
field->prefix_len = (unsigned int) prefix_len;
332
/**************************************************************************
333
Frees an index memory object. */
338
dict_index_t* index) /* in: index */
341
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
343
mem_heap_free(index->heap);