~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to plugin/innodb_memcached/innodb_memcache/include/innodb_utility.h

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***********************************************************************
 
2
 
 
3
Copyright (c) 2012, Oracle and/or its affiliates. All Rights Reserved.
 
4
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
 
 
18
***********************************************************************/
 
19
/**************************************************//**
 
20
@file innodb_list.h
 
21
 
 
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
*******************************************************/
 
26
 
 
27
#ifndef INNODB_UTILITY_H
 
28
#define INNODB_UTILITY_H
 
29
 
 
30
#include "config.h"
 
31
#include "api0api.h"
 
32
 
 
33
 
 
34
#define UT_LIST_NODE_T(TYPE)                                            \
 
35
struct {                                                                \
 
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 */\
 
39
}                                                                       \
 
40
 
 
41
#define UT_LIST_BASE_NODE_T(TYPE)                                       \
 
42
struct {                                                                \
 
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 */      \
 
46
}
 
47
 
 
48
/** Some Macros to manipulate the list, extracted from "ut0lst.h" */
 
49
#define UT_LIST_INIT(BASE)                                              \
 
50
{                                                                       \
 
51
        (BASE).count = 0;                                               \
 
52
        (BASE).start = NULL;                                            \
 
53
        (BASE).end   = NULL;                                            \
 
54
}                                                                       \
 
55
 
 
56
#define UT_LIST_ADD_LAST(NAME, BASE, N)                                 \
 
57
{                                                                       \
 
58
        ((BASE).count)++;                                               \
 
59
        ((N)->NAME).prev = (BASE).end;                                  \
 
60
        ((N)->NAME).next = NULL;                                        \
 
61
        if ((BASE).end != NULL) {                                       \
 
62
                (((BASE).end)->NAME).next = (N);                        \
 
63
        }                                                               \
 
64
        (BASE).end = (N);                                               \
 
65
        if ((BASE).start == NULL) {                                     \
 
66
                (BASE).start = (N);                                     \
 
67
        }                                                               \
 
68
}                                                                       \
 
69
 
 
70
#define UT_LIST_ADD_FIRST(NAME, BASE, N)                                \
 
71
{                                                                       \
 
72
        ((BASE).count)++;                                               \
 
73
        ((N)->NAME).next = (BASE).start;                                \
 
74
        ((N)->NAME).prev = NULL;                                        \
 
75
        if (UNIV_LIKELY((BASE).start != NULL)) {                        \
 
76
                (((BASE).start)->NAME).prev = (N);                      \
 
77
        }                                                               \
 
78
        (BASE).start = (N);                                             \
 
79
        if (UNIV_UNLIKELY((BASE).end == NULL)) {                        \
 
80
                (BASE).end = (N);                                       \
 
81
        }                                                               \
 
82
}                                                                       \
 
83
 
 
84
# define UT_LIST_REMOVE_CLEAR(NAME, N)                                  \
 
85
        ((N)->NAME.prev = (N)->NAME.next = (void*) -1)
 
86
 
 
87
/** Removes a node from a linked list. */
 
88
#define UT_LIST_REMOVE(NAME, BASE, N)                                   \
 
89
do {                                                                    \
 
90
        ((BASE).count)--;                                               \
 
91
        if (((N)->NAME).next != NULL) {                                 \
 
92
                ((((N)->NAME).next)->NAME).prev = ((N)->NAME).prev;     \
 
93
        } else {                                                        \
 
94
                (BASE).end = ((N)->NAME).prev;                          \
 
95
        }                                                               \
 
96
        if (((N)->NAME).prev != NULL) {                                 \
 
97
                ((((N)->NAME).prev)->NAME).next = ((N)->NAME).next;     \
 
98
        } else {                                                        \
 
99
                (BASE).start = ((N)->NAME).next;                        \
 
100
        }                                                               \
 
101
        UT_LIST_REMOVE_CLEAR(NAME, N);                                  \
 
102
} while (0)
 
103
 
 
104
#define UT_LIST_GET_NEXT(NAME, N)       (((N)->NAME).next)
 
105
 
 
106
#define UT_LIST_GET_LEN(BASE)   (BASE).count
 
107
 
 
108
#define UT_LIST_GET_FIRST(BASE) (BASE).start
 
109
 
 
110
/*************************************************************//**
 
111
Folds a character string ending in the null character.
 
112
Extracted from ut0rnd.h and ut0rnd.ic.
 
113
@return folded value */
 
114
 
 
115
ib_ulint_t
 
116
ut_fold_string(
 
117
/*===========*/
 
118
        const char*     str);   /*!< in: null-terminated string */
 
119
 
 
120
typedef struct hash_cell_struct{
 
121
        void*           node;   /*!< hash chain node, NULL if none */
 
122
} hash_cell_t;
 
123
 
 
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 */
 
127
} hash_table_t;
 
128
 
 
129
#define HASH_INSERT(TYPE, NAME, TABLE, FOLD, DATA)      \
 
130
do {                                                    \
 
131
        hash_cell_t*    cell3333;                       \
 
132
        TYPE*           struct3333;                     \
 
133
                                                        \
 
134
                                                        \
 
135
        (DATA)->NAME = NULL;                            \
 
136
                                                        \
 
137
        cell3333 = hash_get_nth_cell(TABLE, hash_calc_hash(FOLD, TABLE));\
 
138
                                                        \
 
139
        if (cell3333->node == NULL) {                   \
 
140
                cell3333->node = DATA;                  \
 
141
        } else {                                        \
 
142
                struct3333 = (TYPE*) cell3333->node;    \
 
143
                                                        \
 
144
                while (struct3333->NAME != NULL) {      \
 
145
                                                        \
 
146
                        struct3333 = (TYPE*) struct3333->NAME;\
 
147
                }                                       \
 
148
                                                        \
 
149
                struct3333->NAME = DATA;                \
 
150
        }                                               \
 
151
} while (0)
 
152
 
 
153
/*******************************************************************//**
 
154
Gets the first struct in a hash chain, NULL if none. */
 
155
 
 
156
#define HASH_GET_FIRST(TABLE, HASH_VAL)                 \
 
157
        (hash_get_nth_cell(TABLE, HASH_VAL)->node)
 
158
 
 
159
/*******************************************************************//**
 
160
Gets the next struct in a hash chain, NULL if none. */
 
161
 
 
162
#define HASH_GET_NEXT(NAME, DATA)       ((DATA)->NAME)
 
163
 
 
164
/********************************************************************//**
 
165
Looks for a struct in a hash table. */
 
166
#define HASH_SEARCH(NAME, TABLE, FOLD, TYPE, DATA, TEST)                \
 
167
{                                                                       \
 
168
        (DATA) = (TYPE) HASH_GET_FIRST(TABLE, hash_calc_hash(FOLD, TABLE));\
 
169
                                                                        \
 
170
        while ((DATA) != NULL) {                                        \
 
171
                if (TEST) {                                             \
 
172
                        break;                                          \
 
173
                } else {                                                \
 
174
                        (DATA) = (TYPE) HASH_GET_NEXT(NAME, DATA);      \
 
175
                }                                                       \
 
176
        }                                                               \
 
177
}
 
178
 
 
179
 
 
180
/********************************************************************//**
 
181
Cleanup items in a hash table */
 
182
#define HASH_CLEANUP(TABLE, TYPE)                                       \
 
183
do {                                                                    \
 
184
        ib_ulint_t      i;                                              \
 
185
        TYPE            data;                                           \
 
186
                                                                        \
 
187
        for (i = 0; i < TABLE->n_cells; i++) {                          \
 
188
                data = (TYPE) HASH_GET_FIRST(TABLE, i);                 \
 
189
                                                                        \
 
190
                while (data) {                                          \
 
191
                        TYPE    next_data;                              \
 
192
                        next_data = HASH_GET_NEXT(name_hash, data);     \
 
193
                        innodb_config_free(data);                       \
 
194
                        free(data);                                     \
 
195
                        data = next_data;                               \
 
196
                }                                                       \
 
197
        }                                                               \
 
198
                                                                        \
 
199
        free(TABLE->array);                                             \
 
200
        free(TABLE);                                                    \
 
201
} while(0)
 
202
 
 
203
/************************************************************//**
 
204
Gets the nth cell in a hash table.
 
205
@return pointer to cell */
 
206
hash_cell_t*
 
207
hash_get_nth_cell(
 
208
/*==============*/
 
209
        hash_table_t*   table,  /*!< in: hash table */
 
210
        ib_ulint_t      n);     /*!< in: cell index */
 
211
 
 
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 */
 
216
hash_table_t*
 
217
hash_create(
 
218
/*========*/
 
219
        ib_ulint_t      n);     /*!< in: number of array cells */
 
220
 
 
221
/**************************************************************//**
 
222
Calculates the hash value from a folded value.
 
223
@return hashed value */
 
224
ib_ulint_t
 
225
hash_calc_hash(
 
226
/*===========*/
 
227
        ib_ulint_t      fold,   /*!< in: folded value */
 
228
        hash_table_t*   table); /*!< in: hash table */
 
229
 
 
230
#endif /* INNODB_UTILITY_H */