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_a(!(flags & (~0 << DICT_TF_BITS)));
47
heap = mem_heap_create(DICT_HEAP_SIZE);
49
table = mem_heap_zalloc(heap, sizeof(dict_table_t));
53
table->flags = (unsigned int) flags;
54
table->name = mem_heap_strdup(heap, name);
55
table->space = (unsigned int) space;
56
table->n_cols = (unsigned int) (n_cols + DATA_N_SYS_COLS);
58
table->cols = mem_heap_alloc(heap, (n_cols + DATA_N_SYS_COLS)
59
* sizeof(dict_col_t));
61
table->auto_inc_lock = mem_heap_alloc(heap, lock_get_size());
63
mutex_create(&table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
65
/* The actual increment value will be set by MySQL, we simply
67
table->autoinc_increment = 1;
70
table->magic_n = DICT_TABLE_MAGIC_N;
71
#endif /* UNIV_DEBUG */
75
/********************************************************************
76
Free a table memory object. */
81
dict_table_t* table) /* in: table */
84
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
86
mutex_free(&(table->autoinc_mutex));
87
mem_heap_free(table->heap);
90
/********************************************************************
91
Append 'name' to 'col_names' (@see dict_table_t::col_names). */
96
/* out: new column names array */
97
const char* col_names, /* in: existing column names, or
99
ulint cols, /* in: number of existing columns */
100
const char* name, /* in: new column name */
101
mem_heap_t* heap) /* in: heap */
108
ut_ad(!cols == !col_names);
110
/* Find out length of existing array. */
112
const char* s = col_names;
115
for (i = 0; i < cols; i++) {
119
old_len = s - col_names;
124
new_len = strlen(name) + 1;
125
total_len = old_len + new_len;
127
res = mem_heap_alloc(heap, total_len);
130
memcpy(res, col_names, old_len);
133
memcpy(res + old_len, name, new_len);
138
/**************************************************************************
139
Adds a column definition to a table. */
142
dict_mem_table_add_col(
143
/*===================*/
144
dict_table_t* table, /* in: table */
145
mem_heap_t* heap, /* in: temporary memory heap, or NULL */
146
const char* name, /* in: column name, or NULL */
147
ulint mtype, /* in: main datatype */
148
ulint prtype, /* in: precise type */
149
ulint len) /* in: precision */
157
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
158
ut_ad(!heap == !name);
163
if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
166
if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
167
/* All preceding column names are empty. */
168
char* s = mem_heap_zalloc(heap, table->n_def);
169
table->col_names = s;
172
table->col_names = dict_add_col_name(table->col_names,
176
col = dict_table_get_nth_col(table, i);
178
col->ind = (unsigned int) i;
181
col->mtype = (unsigned int) mtype;
182
col->prtype = (unsigned int) prtype;
183
col->len = (unsigned int) len;
185
dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
187
col->mbminlen = (unsigned int) mbminlen;
188
col->mbmaxlen = (unsigned int) mbmaxlen;
191
/**************************************************************************
192
Creates an index memory object. */
195
dict_mem_index_create(
196
/*==================*/
197
/* out, own: index object */
198
const char* table_name, /* in: table name */
199
const char* index_name, /* in: index name */
200
ulint space, /* in: space where the index tree is
201
placed, ignored if the index is of
202
the clustered type */
203
ulint type, /* in: DICT_UNIQUE,
204
DICT_CLUSTERED, ... ORed */
205
ulint n_fields) /* in: number of fields */
210
ut_ad(table_name && index_name);
212
heap = mem_heap_create(DICT_HEAP_SIZE);
213
index = mem_heap_zalloc(heap, sizeof(dict_index_t));
218
index->space = (unsigned int) space;
219
index->name = mem_heap_strdup(heap, index_name);
220
index->table_name = table_name;
221
index->n_fields = (unsigned int) n_fields;
222
index->fields = mem_heap_alloc(heap, 1 + n_fields
223
* sizeof(dict_field_t));
224
/* The '1 +' above prevents allocation
225
of an empty mem block */
227
index->magic_n = DICT_INDEX_MAGIC_N;
228
#endif /* UNIV_DEBUG */
232
/**************************************************************************
233
Creates and initializes a foreign constraint memory object. */
236
dict_mem_foreign_create(void)
237
/*=========================*/
238
/* out, own: foreign constraint struct */
240
dict_foreign_t* foreign;
243
heap = mem_heap_create(100);
245
foreign = mem_heap_zalloc(heap, sizeof(dict_foreign_t));
247
foreign->heap = heap;
252
/**************************************************************************
253
Adds a field definition to an index. NOTE: does not take a copy
254
of the column name if the field is a column. The memory occupied
255
by the column name may be released only after publishing the index. */
258
dict_mem_index_add_field(
259
/*=====================*/
260
dict_index_t* index, /* in: index */
261
const char* name, /* in: column name */
262
ulint prefix_len) /* in: 0 or the column prefix length
263
in a MySQL index like
264
INDEX (textcol(25)) */
269
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
273
field = dict_index_get_nth_field(index, index->n_def - 1);
276
field->prefix_len = (unsigned int) prefix_len;
279
/**************************************************************************
280
Frees an index memory object. */
285
dict_index_t* index) /* in: index */
288
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
290
mem_heap_free(index->heap);