1
/*****************************************************************************
3
Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
*****************************************************************************/
19
/******************************************************************//**
21
Data dictionary memory object creation
23
Created 1/8/1996 Heikki Tuuri
24
***********************************************************************/
29
#include "dict0mem.ic"
33
#include "data0type.h"
34
#include "mach0data.h"
35
#include "dict0dict.h"
36
#ifndef UNIV_HOTBACKUP
37
# include "lock0lock.h"
38
#endif /* !UNIV_HOTBACKUP */
40
#define DICT_HEAP_SIZE 100 /*!< initial memory heap size when
41
creating a table or index object */
43
/**********************************************************************//**
44
Creates a table memory object.
45
@return own: table object */
48
dict_mem_table_create(
49
/*==================*/
50
const char* name, /*!< in: table name */
51
ulint space, /*!< in: space where the clustered index of
52
the table is placed; this parameter is
53
ignored if the table is made a member of
55
ulint n_cols, /*!< in: number of columns */
56
ulint flags) /*!< in: table flags */
62
ut_a(!(flags & (~0 << DICT_TF_BITS)));
64
heap = mem_heap_create(DICT_HEAP_SIZE);
66
table = mem_heap_zalloc(heap, sizeof(dict_table_t));
70
table->flags = (unsigned int) flags;
71
table->name = mem_heap_strdup(heap, name);
72
table->space = (unsigned int) space;
73
table->n_cols = (unsigned int) (n_cols + DATA_N_SYS_COLS);
75
table->cols = mem_heap_alloc(heap, (n_cols + DATA_N_SYS_COLS)
76
* sizeof(dict_col_t));
78
#ifndef UNIV_HOTBACKUP
79
table->autoinc_lock = mem_heap_alloc(heap, lock_get_size());
81
mutex_create(&table->autoinc_mutex, SYNC_DICT_AUTOINC_MUTEX);
85
/* The number of transactions that are either waiting on the
86
AUTOINC lock or have been granted the lock. */
87
table->n_waiting_or_granted_auto_inc_locks = 0;
88
#endif /* !UNIV_HOTBACKUP */
90
ut_d(table->magic_n = DICT_TABLE_MAGIC_N);
94
/****************************************************************//**
95
Free a table memory object. */
100
dict_table_t* table) /*!< in: table */
103
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
104
ut_d(table->cached = FALSE);
106
#ifndef UNIV_HOTBACKUP
107
mutex_free(&(table->autoinc_mutex));
108
#endif /* UNIV_HOTBACKUP */
109
mem_heap_free(table->heap);
112
/****************************************************************//**
113
Append 'name' to 'col_names'. @see dict_table_t::col_names
114
@return new column names array */
119
const char* col_names, /*!< in: existing column names, or
121
ulint cols, /*!< in: number of existing columns */
122
const char* name, /*!< in: new column name */
123
mem_heap_t* heap) /*!< in: heap */
130
ut_ad(!cols == !col_names);
132
/* Find out length of existing array. */
134
const char* s = col_names;
137
for (i = 0; i < cols; i++) {
141
old_len = s - col_names;
146
new_len = strlen(name) + 1;
147
total_len = old_len + new_len;
149
res = mem_heap_alloc(heap, total_len);
152
memcpy(res, col_names, old_len);
155
memcpy(res + old_len, name, new_len);
160
/**********************************************************************//**
161
Adds a column definition to a table. */
164
dict_mem_table_add_col(
165
/*===================*/
166
dict_table_t* table, /*!< in: table */
167
mem_heap_t* heap, /*!< in: temporary memory heap, or NULL */
168
const char* name, /*!< in: column name, or NULL */
169
ulint mtype, /*!< in: main datatype */
170
ulint prtype, /*!< in: precise type */
171
ulint len) /*!< in: precision */
174
#ifndef UNIV_HOTBACKUP
177
#endif /* !UNIV_HOTBACKUP */
181
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
182
ut_ad(!heap == !name);
187
if (UNIV_UNLIKELY(table->n_def == table->n_cols)) {
190
if (UNIV_LIKELY(i) && UNIV_UNLIKELY(!table->col_names)) {
191
/* All preceding column names are empty. */
192
char* s = mem_heap_zalloc(heap, table->n_def);
193
table->col_names = s;
196
table->col_names = dict_add_col_name(table->col_names,
200
col = dict_table_get_nth_col(table, i);
202
col->ind = (unsigned int) i;
205
col->mtype = (unsigned int) mtype;
206
col->prtype = (unsigned int) prtype;
207
col->len = (unsigned int) len;
209
#ifndef UNIV_HOTBACKUP
210
dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
212
col->mbminlen = (unsigned int) mbminlen;
213
col->mbmaxlen = (unsigned int) mbmaxlen;
214
#endif /* !UNIV_HOTBACKUP */
217
/**********************************************************************//**
218
Creates an index memory object.
219
@return own: index object */
222
dict_mem_index_create(
223
/*==================*/
224
const char* table_name, /*!< in: table name */
225
const char* index_name, /*!< in: index name */
226
ulint space, /*!< in: space where the index tree is
227
placed, ignored if the index is of
228
the clustered type */
229
ulint type, /*!< in: DICT_UNIQUE,
230
DICT_CLUSTERED, ... ORed */
231
ulint n_fields) /*!< in: number of fields */
236
ut_ad(table_name && index_name);
238
heap = mem_heap_create(DICT_HEAP_SIZE);
239
index = mem_heap_zalloc(heap, sizeof(dict_index_t));
244
#ifndef UNIV_HOTBACKUP
245
index->space = (unsigned int) space;
246
#endif /* !UNIV_HOTBACKUP */
247
index->name = mem_heap_strdup(heap, index_name);
248
index->table_name = table_name;
249
index->n_fields = (unsigned int) n_fields;
250
index->fields = mem_heap_alloc(heap, 1 + n_fields
251
* sizeof(dict_field_t));
252
/* The '1 +' above prevents allocation
253
of an empty mem block */
255
index->magic_n = DICT_INDEX_MAGIC_N;
256
#endif /* UNIV_DEBUG */
260
/**********************************************************************//**
261
Creates and initializes a foreign constraint memory object.
262
@return own: foreign constraint struct */
265
dict_mem_foreign_create(void)
266
/*=========================*/
268
dict_foreign_t* foreign;
271
heap = mem_heap_create(100);
273
foreign = mem_heap_zalloc(heap, sizeof(dict_foreign_t));
275
foreign->heap = heap;
280
/**********************************************************************//**
281
Adds a field definition to an index. NOTE: does not take a copy
282
of the column name if the field is a column. The memory occupied
283
by the column name may be released only after publishing the index. */
286
dict_mem_index_add_field(
287
/*=====================*/
288
dict_index_t* index, /*!< in: index */
289
const char* name, /*!< in: column name */
290
ulint prefix_len) /*!< in: 0 or the column prefix length
291
in a MySQL index like
292
INDEX (textcol(25)) */
297
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
301
field = dict_index_get_nth_field(index, index->n_def - 1);
304
field->prefix_len = (unsigned int) prefix_len;
307
/**********************************************************************//**
308
Frees an index memory object. */
313
dict_index_t* index) /*!< in: index */
316
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
318
mem_heap_free(index->heap);