1
/***********************************************************************
3
Copyright (c) 2012, Oracle and/or its affiliates. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it
6
under the terms of the GNU General Public License as published by the
7
Free Software Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but
10
WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12
Public License for more details.
14
You should have received a copy of the GNU General Public License along
15
with this program; if not, write to the Free Software Foundation, Inc.,
16
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
18
***********************************************************************/
19
/**************************************************//**
22
Created 03/15/2011 Jimmy Yang (most macros and defines are adopted
23
from utility files in the InnoDB. Namely
24
ut/ut0lst.h, ut0rnd.h and hash0hash.h etc.)
25
*******************************************************/
27
#ifndef INNODB_UTILITY_H
28
#define INNODB_UTILITY_H
34
#define UT_LIST_NODE_T(TYPE) \
36
TYPE* prev; /*!< pointer to the previous node, \
37
NULL if start of list */ \
38
TYPE* next; /*!< pointer to next node, NULL if end of list */\
41
#define UT_LIST_BASE_NODE_T(TYPE) \
43
int count; /*!< count of nodes in list */ \
44
TYPE * start; /*!< pointer to list start, NULL if empty */ \
45
TYPE * end; /*!< pointer to list end, NULL if empty */ \
48
/** Some Macros to manipulate the list, extracted from "ut0lst.h" */
49
#define UT_LIST_INIT(BASE) \
52
(BASE).start = NULL; \
56
#define UT_LIST_ADD_LAST(NAME, BASE, N) \
59
((N)->NAME).prev = (BASE).end; \
60
((N)->NAME).next = NULL; \
61
if ((BASE).end != NULL) { \
62
(((BASE).end)->NAME).next = (N); \
65
if ((BASE).start == NULL) { \
70
#define UT_LIST_ADD_FIRST(NAME, BASE, N) \
73
((N)->NAME).next = (BASE).start; \
74
((N)->NAME).prev = NULL; \
75
if (UNIV_LIKELY((BASE).start != NULL)) { \
76
(((BASE).start)->NAME).prev = (N); \
79
if (UNIV_UNLIKELY((BASE).end == NULL)) { \
84
# define UT_LIST_REMOVE_CLEAR(NAME, N) \
85
((N)->NAME.prev = (N)->NAME.next = (void*) -1)
87
/** Removes a node from a linked list. */
88
#define UT_LIST_REMOVE(NAME, BASE, N) \
91
if (((N)->NAME).next != NULL) { \
92
((((N)->NAME).next)->NAME).prev = ((N)->NAME).prev; \
94
(BASE).end = ((N)->NAME).prev; \
96
if (((N)->NAME).prev != NULL) { \
97
((((N)->NAME).prev)->NAME).next = ((N)->NAME).next; \
99
(BASE).start = ((N)->NAME).next; \
101
UT_LIST_REMOVE_CLEAR(NAME, N); \
104
#define UT_LIST_GET_NEXT(NAME, N) (((N)->NAME).next)
106
#define UT_LIST_GET_LEN(BASE) (BASE).count
108
#define UT_LIST_GET_FIRST(BASE) (BASE).start
110
/*************************************************************//**
111
Folds a character string ending in the null character.
112
Extracted from ut0rnd.h and ut0rnd.ic.
113
@return folded value */
118
const char* str); /*!< in: null-terminated string */
120
typedef struct hash_cell_struct{
121
void* node; /*!< hash chain node, NULL if none */
124
typedef struct hash_table_struct {
125
ib_ulint_t n_cells;/* number of cells in the hash table */
126
hash_cell_t* array; /*!< pointer to cell array */
129
#define HASH_INSERT(TYPE, NAME, TABLE, FOLD, DATA) \
131
hash_cell_t* cell3333; \
135
(DATA)->NAME = NULL; \
137
cell3333 = hash_get_nth_cell(TABLE, hash_calc_hash(FOLD, TABLE));\
139
if (cell3333->node == NULL) { \
140
cell3333->node = DATA; \
142
struct3333 = (TYPE*) cell3333->node; \
144
while (struct3333->NAME != NULL) { \
146
struct3333 = (TYPE*) struct3333->NAME;\
149
struct3333->NAME = DATA; \
153
/*******************************************************************//**
154
Gets the first struct in a hash chain, NULL if none. */
156
#define HASH_GET_FIRST(TABLE, HASH_VAL) \
157
(hash_get_nth_cell(TABLE, HASH_VAL)->node)
159
/*******************************************************************//**
160
Gets the next struct in a hash chain, NULL if none. */
162
#define HASH_GET_NEXT(NAME, DATA) ((DATA)->NAME)
164
/********************************************************************//**
165
Looks for a struct in a hash table. */
166
#define HASH_SEARCH(NAME, TABLE, FOLD, TYPE, DATA, TEST) \
168
(DATA) = (TYPE) HASH_GET_FIRST(TABLE, hash_calc_hash(FOLD, TABLE));\
170
while ((DATA) != NULL) { \
174
(DATA) = (TYPE) HASH_GET_NEXT(NAME, DATA); \
180
/********************************************************************//**
181
Cleanup items in a hash table */
182
#define HASH_CLEANUP(TABLE, TYPE) \
187
for (i = 0; i < TABLE->n_cells; i++) { \
188
data = (TYPE) HASH_GET_FIRST(TABLE, i); \
192
next_data = HASH_GET_NEXT(name_hash, data); \
193
innodb_config_free(data); \
199
free(TABLE->array); \
203
/************************************************************//**
204
Gets the nth cell in a hash table.
205
@return pointer to cell */
209
hash_table_t* table, /*!< in: hash table */
210
ib_ulint_t n); /*!< in: cell index */
212
/*************************************************************//**
213
Creates a hash table with >= n array cells. The actual number
214
of cells is chosen to be a prime number slightly bigger than n.
215
@return own: created table */
219
ib_ulint_t n); /*!< in: number of array cells */
221
/**************************************************************//**
222
Calculates the hash value from a folded value.
223
@return hashed value */
227
ib_ulint_t fold, /*!< in: folded value */
228
hash_table_t* table); /*!< in: hash table */
230
#endif /* INNODB_UTILITY_H */