Coverage Report

Created: 2026-11-22 11:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5SL.c
Line
Count
Source
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 * Copyright by The HDF Group.                                               *
3
 * All rights reserved.                                                      *
4
 *                                                                           *
5
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
6
 * terms governing use, modification, and redistribution, is contained in    *
7
 * the LICENSE file, which can be found at the root of the source code       *
8
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
9
 * If you do not have access to either file, you may request a copy from     *
10
 * help@hdfgroup.org.                                                        *
11
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13
/*
14
 * Purpose: Provides a skip list abstract data type.
15
 *
16
 *              (See "Deterministic Skip Lists" by Munro, Papadakis & Sedgewick)
17
 *
18
 *              (Implementation changed to a deterministic skip list from a
19
 *               probabilistic one.  This implementation uses a 1-2-3 skip list
20
 *               using arrays, as described by Munro, Papadakis & Sedgewick.
21
 *
22
 *               Arrays are allocated using a free list factory for each size
23
 *               that is a power of two.  Factories are created as soon as they
24
 *               are needed, and are never destroyed until the package is shut
25
 *               down.  There is no longer a maximum level or "p" value.
26
 *               -NAF 2008/11/05)
27
 *
28
 *              (See "Skip Lists: A Probabilistic Alternative to Balanced Trees"
29
 *               by William Pugh for additional information)
30
 *
31
 *              (This implementation has the optimization for reducing key
32
 *               key comparisons mentioned in section 3.5 of "A Skip List
33
 *               Cookbook" by William Pugh
34
 *              -Removed as our implementation of this was useless for a 1-2-3
35
 *               skip list.  The implementation in that document hurts
36
 *               performance, at least for integer keys. -NAF)
37
 *
38
 *              (Note: This implementation does not have the information for
39
 *               implementing the "Linear List Operations" (like insert/delete/
40
 *               search by position) in section 3.4 of "A Skip List Cookbook",
41
 *               but they shouldn't be that hard to add, if necessary)
42
 *
43
 *              (This implementation has an additional backward pointer, which
44
 *               allows the list to be iterated in reverse)
45
 *
46
 *              (There's also an article on "Alternating Skip Lists", which
47
 *              are similar to deterministic skip lists, in the August 2000
48
 *              issue of Dr. Dobb's Journal)
49
 *
50
 */
51
52
#include "H5SLmodule.h" /* This source code file is part of the H5SL module */
53
54
/* Private headers needed */
55
#include "H5private.h"   /* Generic Functions     */
56
#include "H5Eprivate.h"  /* Error handling        */
57
#include "H5FLprivate.h" /* Free Lists                           */
58
#include "H5MMprivate.h" /* Memory management                    */
59
#include "H5SLprivate.h" /* Skip list routines      */
60
61
/* Local Macros */
62
63
/* Define the code template for searches for the "OP" in the H5SL_LOCATE macro */
64
#define H5SL_LOCATE_SEARCH_FOUND(SLIST, X, I)                                                                \
65
618k
    {                                                                                                        \
66
618k
        HGOTO_DONE(X->item);                                                                                 \
67
618k
    }
68
69
/* Define the code template for finds for the "OP" in the H5SL_LOCATE macro */
70
#define H5SL_LOCATE_FIND_FOUND(SLIST, X, I)                                                                  \
71
0
    {                                                                                                        \
72
0
        HGOTO_DONE(X);                                                                                       \
73
0
    }
74
75
/* Define a code template for comparing scalar keys for the "CMP" in the H5SL_LOCATE macro */
76
199k
#define H5SL_LOCATE_SCALAR_CMP(SLIST, TYPE, PNODE, PKEY, HASHVAL) (*(TYPE *)((PNODE)->key) < *(TYPE *)PKEY)
77
78
/* Define a code template for comparing string keys for the "CMP" in the H5SL_LOCATE macro */
79
#define H5SL_LOCATE_STRING_CMP(SLIST, TYPE, PNODE, PKEY, HASHVAL)                                            \
80
7.10M
    (((PNODE)->hashval == HASHVAL) ? (strcmp((const char *)(PNODE)->key, (const char *)PKEY) < 0)            \
81
7.10M
                                   : ((PNODE)->hashval < HASHVAL))
82
83
/* Define a code template for comparing H5_obj_t keys for the "CMP" in the H5SL_LOCATE macro */
84
#define H5SL_LOCATE_OBJ_CMP(SLIST, TYPE, PNODE, PKEY, HASHVAL)                                               \
85
0
    ((((TYPE *)((PNODE)->key))->fileno == ((TYPE *)PKEY)->fileno)                                            \
86
0
         ? (((TYPE *)((PNODE)->key))->addr < ((TYPE *)PKEY)->addr)                                           \
87
0
         : (((TYPE *)((PNODE)->key))->fileno < ((TYPE *)PKEY)->fileno))
88
89
/* Define a code template for comparing generic keys for the "CMP" in the H5SL_LOCATE macro */
90
#define H5SL_LOCATE_GENERIC_CMP(SLIST, TYPE, PNODE, PKEY, HASHVAL)                                           \
91
0
    ((SLIST)->cmp((TYPE *)((PNODE)->key), (TYPE *)PKEY) < 0)
92
93
/* Define a code template for comparing scalar keys for the "EQ" in the H5SL_LOCATE macro */
94
80.6k
#define H5SL_LOCATE_SCALAR_EQ(SLIST, TYPE, PNODE, PKEY, HASHVAL) (*(TYPE *)((PNODE)->key) == *(TYPE *)PKEY)
95
96
/* Define a code template for comparing string keys for the "EQ" in the H5SL_LOCATE macro */
97
#define H5SL_LOCATE_STRING_EQ(SLIST, TYPE, PNODE, PKEY, HASHVAL)                                             \
98
2.71M
    (((PNODE)->hashval == HASHVAL) && (strcmp((const char *)(PNODE)->key, (const char *)PKEY) == 0))
99
100
/* Define a code template for comparing H5_obj_t keys for the "EQ" in the H5SL_LOCATE macro */
101
#define H5SL_LOCATE_OBJ_EQ(SLIST, TYPE, PNODE, PKEY, HASHVAL)                                                \
102
0
    ((((TYPE *)((PNODE)->key))->fileno == ((TYPE *)PKEY)->fileno) &&                                         \
103
0
     (((TYPE *)((PNODE)->key))->addr == ((TYPE *)PKEY)->addr))
104
105
/* Define a code template for comparing generic keys for the "EQ" in the H5SL_LOCATE macro */
106
#define H5SL_LOCATE_GENERIC_EQ(SLIST, TYPE, PNODE, PKEY, HASHVAL)                                            \
107
0
    ((SLIST)->cmp((TYPE *)((PNODE)->key), (TYPE *)PKEY) == 0)
108
109
/* Define a code template for initializing the hash value for scalar keys for the "HASHINIT" in the
110
 * H5SL_LOCATE macro */
111
#define H5SL_LOCATE_SCALAR_HASHINIT(KEY, HASHVAL)
112
113
/* Define a code template for initializing the hash value for string keys for the "HASHINIT" in the
114
 * H5SL_LOCATE macro */
115
2.54M
#define H5SL_LOCATE_STRING_HASHINIT(KEY, HASHVAL) HASHVAL = H5_hash_string((const char *)KEY);
116
117
/* Define a code template for initializing the hash value for H5_obj_t keys for the "HASHINIT" in the
118
 * H5SL_LOCATE macro */
119
#define H5SL_LOCATE_OBJ_HASHINIT(KEY, HASHVAL)
120
121
/* Define a code template for initializing the hash value for generic keys for the "HASHINIT" in the
122
 * H5SL_LOCATE macro */
123
#define H5SL_LOCATE_GENERIC_HASHINIT(KEY, HASHVAL)
124
125
/* Macro used to find node for operation, if all keys are valid */
126
#define H5SL_LOCATE_OPT(OP, CMP, SLIST, X, TYPE, KEY, HASHVAL)                                               \
127
1.45M
    {                                                                                                        \
128
1.45M
        int      _i;     /* Local index variable */                                                          \
129
1.45M
        unsigned _count; /* Num nodes searched at this height */                                             \
130
1.45M
                                                                                                             \
131
1.45M
        H5_GLUE3(H5SL_LOCATE_, CMP, _HASHINIT)                                                               \
132
3.90M
        (KEY, HASHVAL) for (_i = (int)SLIST->curr_level; _i >= 0; _i--)                                      \
133
2.45M
        {                                                                                                    \
134
2.45M
            _count = 0;                                                                                      \
135
5.03M
            while (_count < 3 && X->forward[_i] &&                                                           \
136
5.03M
                   H5_GLUE3(H5SL_LOCATE_, CMP, _CMP)(SLIST, TYPE, X->forward[_i], KEY, HASHVAL)) {           \
137
2.58M
                X = X->forward[_i];                                                                          \
138
2.58M
                _count++;                                                                                    \
139
2.58M
            }                                                                                                \
140
2.45M
        }                                                                                                    \
141
1.45M
        X = X->forward[0];                                                                                   \
142
1.45M
        if (X != NULL && H5_GLUE3(H5SL_LOCATE_, CMP, _EQ)(SLIST, TYPE, X, KEY, HASHVAL)) {                   \
143
618k
            /* What to do when a node is found */                                                            \
144
618k
            H5_GLUE3(H5SL_LOCATE_, OP, _FOUND)(SLIST, X, _i)                                                 \
145
618k
        }                                                                                                    \
146
1.45M
    }
147
148
/* Macro used to find node for operation */
149
#define H5SL_LOCATE(OP, CMP, SLIST, X, TYPE, KEY, HASHVAL)                                                   \
150
1.45M
    {                                                                                                        \
151
1.45M
        H5SL_LOCATE_OPT(OP, CMP, SLIST, X, TYPE, KEY, HASHVAL)                                               \
152
1.45M
    }
153
154
/* Macro used to grow a node by 1.  Does not update pointers. LVL is the current
155
 * level of X.  Does not update LVL but does update X->lvl. */
156
#define H5SL_GROW(X, LVL, ERR)                                                                               \
157
673k
    {                                                                                                        \
158
673k
        /* Check if we need to increase allocation of forward pointers */                                    \
159
673k
        if (LVL + 1 >= ((size_t)1) << X->log_nalloc) {                                                       \
160
672k
            H5SL_node_t **_tmp;                                                                              \
161
672k
            assert(LVL + 1 == ((size_t)1) << X->log_nalloc);                                                 \
162
672k
            /* Double the amount of allocated space */                                                       \
163
672k
            X->log_nalloc++;                                                                                 \
164
672k
                                                                                                             \
165
672k
            /* Check if we need to create a new factory */                                                   \
166
672k
            if (X->log_nalloc >= H5SL_fac_nused_g) {                                                         \
167
3
                assert(X->log_nalloc == H5SL_fac_nused_g);                                                   \
168
3
                                                                                                             \
169
3
                /* Check if we need to allocate space for the factory pointer*/                              \
170
3
                if (H5SL_fac_nused_g >= H5SL_fac_nalloc_g) {                                                 \
171
2
                    assert(H5SL_fac_nused_g == H5SL_fac_nalloc_g);                                           \
172
2
                    /* Double the size of the array of factory pointers */                                   \
173
2
                    H5SL_fac_nalloc_g *= 2;                                                                  \
174
2
                    if (NULL == (H5SL_fac_g = (H5FL_fac_head_t **)H5MM_realloc(                              \
175
2
                                     (void *)H5SL_fac_g, H5SL_fac_nalloc_g * sizeof(H5FL_fac_head_t *))))    \
176
2
                        HGOTO_ERROR(H5E_SLIST, H5E_CANTALLOC, ERR, "memory allocation failed");              \
177
2
                }                                                                                            \
178
3
                                                                                                             \
179
3
                /* Create the new factory */                                                                 \
180
3
                H5SL_fac_g[H5SL_fac_nused_g] =                                                               \
181
3
                    H5FL_fac_init((((size_t)1) << H5SL_fac_nused_g) * sizeof(H5SL_node_t *));                \
182
3
                H5SL_fac_nused_g++;                                                                          \
183
3
            }                                                                                                \
184
672k
                                                                                                             \
185
672k
            /* Allocate space for new forward pointers */                                                    \
186
672k
            if (NULL == (_tmp = (H5SL_node_t **)H5FL_FAC_MALLOC(H5SL_fac_g[X->log_nalloc])))                 \
187
672k
                HGOTO_ERROR(H5E_SLIST, H5E_CANTALLOC, ERR, "memory allocation failed");                      \
188
672k
            H5MM_memcpy((void *)_tmp, (const void *)X->forward, (LVL + 1) * sizeof(H5SL_node_t *));          \
189
672k
            X->forward = (H5SL_node_t **)H5FL_FAC_FREE(H5SL_fac_g[X->log_nalloc - 1], (void *)X->forward);   \
190
672k
            X->forward = _tmp;                                                                               \
191
672k
        }                                                                                                    \
192
673k
                                                                                                             \
193
673k
        X->level++;                                                                                          \
194
673k
    }
195
196
/* Macro used to shrink a node by 1.  Does not update pointers.  LVL is the
197
 * current level of X.  Does not update LVL but does update X->level. */
198
#define H5SL_SHRINK(X, LVL)                                                                                  \
199
8.74k
    {                                                                                                        \
200
8.74k
        /* Check if we can reduce the allocation of forward pointers */                                      \
201
8.74k
        if (LVL <= ((size_t)1) << (X->log_nalloc - 1)) {                                                     \
202
8.06k
            H5SL_node_t **_tmp;                                                                              \
203
8.06k
            assert(LVL == ((size_t)1) << (X->log_nalloc - 1));                                               \
204
8.06k
            X->log_nalloc--;                                                                                 \
205
8.06k
                                                                                                             \
206
8.06k
            /* Allocate space for new forward pointers */                                                    \
207
8.06k
            if (NULL == (_tmp = (H5SL_node_t **)H5FL_FAC_MALLOC(H5SL_fac_g[X->log_nalloc])))                 \
208
8.06k
                HGOTO_ERROR(H5E_SLIST, H5E_NOSPACE, NULL, "memory allocation failed");                       \
209
8.06k
            H5MM_memcpy((void *)_tmp, (const void *)X->forward, (LVL) * sizeof(H5SL_node_t *));              \
210
8.06k
            X->forward = (H5SL_node_t **)H5FL_FAC_FREE(H5SL_fac_g[X->log_nalloc + 1], (void *)X->forward);   \
211
8.06k
            X->forward = _tmp;                                                                               \
212
8.06k
        }                                                                                                    \
213
8.74k
                                                                                                             \
214
8.74k
        X->level--;                                                                                          \
215
8.74k
    }
216
217
/* Macro used to grow the level of a node by 1, with appropriate changes to the
218
 * head node if necessary.  PREV is the previous node of the height that X is to
219
 * grow to. */
220
#define H5SL_PROMOTE(SLIST, X, PREV, ERR)                                                                    \
221
445k
    do {                                                                                                     \
222
445k
        size_t _lvl = X->level;                                                                              \
223
445k
                                                                                                             \
224
445k
        H5SL_GROW(X, _lvl, ERR);                                                                             \
225
445k
                                                                                                             \
226
445k
        if (_lvl == (size_t)SLIST->curr_level) {                                                             \
227
228k
            assert(PREV == SLIST->header);                                                                   \
228
228k
            /* Grow the head */                                                                              \
229
228k
            H5SL_GROW(PREV, _lvl, ERR)                                                                       \
230
228k
            SLIST->curr_level++;                                                                             \
231
228k
            X->forward[_lvl + 1] = NULL;                                                                     \
232
228k
        }                                                                                                    \
233
445k
        else {                                                                                               \
234
216k
            assert(_lvl < (size_t)SLIST->curr_level);                                                        \
235
216k
            X->forward[_lvl + 1] = PREV->forward[_lvl + 1];                                                  \
236
216k
        }                                                                                                    \
237
445k
        PREV->forward[_lvl + 1] = X;                                                                         \
238
445k
    } while (0)
239
240
/* Macro used to reduce the level of a node by 1.  Does not update the head node
241
 * "current level".  PREV is the previous node of the current height of X. */
242
#define H5SL_DEMOTE(X, PREV)                                                                                 \
243
7.11k
    {                                                                                                        \
244
7.11k
        size_t _lvl = X->level;                                                                              \
245
7.11k
                                                                                                             \
246
7.11k
        assert(PREV->forward[_lvl] == X);                                                                    \
247
7.11k
        PREV->forward[_lvl] = X->forward[_lvl];                                                              \
248
7.11k
        H5SL_SHRINK(X, _lvl);                                                                                \
249
7.11k
    }
250
251
/* Macro used to insert node.  Does not actually insert the node.  After running
252
 * this macro, X will contain the node before where the new node should be
253
 * inserted (at level 0). */
254
#define H5SL_INSERT(CMP, SLIST, X, TYPE, KEY, HASHVAL)                                                       \
255
1.15M
    {                                                                                                        \
256
1.15M
        H5SL_node_t *_last = X;    /* Lowest node in the current gap */                                      \
257
1.15M
        H5SL_node_t *_next = NULL; /* Highest node in the current gap */                                     \
258
1.15M
        H5SL_node_t *_drop;        /* Low node of the gap to drop into */                                    \
259
1.15M
        int          _count;       /* Number of nodes in the current gap */                                  \
260
1.15M
        int          _i;                                                                                     \
261
1.15M
                                                                                                             \
262
1.15M
        H5_GLUE3(H5SL_LOCATE_, CMP, _HASHINIT)                                                               \
263
2.86M
        (KEY, HASHVAL) for (_i = (int)SLIST->curr_level; _i >= 0; _i--)                                      \
264
1.70M
        {                                                                                                    \
265
1.70M
            /* Search for the node to drop into, also count the number of nodes */                           \
266
1.70M
            /* of height _i in this gap */                                                                   \
267
1.70M
            _drop = NULL;                                                                                    \
268
4.49M
            for (_count = 0;; _count++) {                                                                    \
269
4.49M
                /* Terminate if this is the last node in the gap */                                          \
270
4.49M
                if (X->forward[_i] == _next) {                                                               \
271
1.26M
                    if (!_drop)                                                                              \
272
1.26M
                        _drop = X;                                                                           \
273
1.26M
                    break;                                                                                   \
274
1.26M
                }                                                                                            \
275
4.49M
                                                                                                             \
276
4.49M
                /* Check if this node is the start of the next gap */                                        \
277
4.49M
                if (!_drop && !H5_GLUE3(H5SL_LOCATE_, CMP, _CMP)(SLIST, TYPE, X->forward[_i], KEY, HASHVAL)) \
278
3.23M
                    _drop = X;                                                                               \
279
3.23M
                                                                                                             \
280
3.23M
                /* No need to check the last node in the gap if there are 3, as */                           \
281
3.23M
                /* there cannot be a fourth */                                                               \
282
3.23M
                if (_count == 2) {                                                                           \
283
442k
                    if (!_drop)                                                                              \
284
442k
                        _drop = X->forward[_i];                                                              \
285
442k
                    _count = 3;                                                                              \
286
442k
                    break;                                                                                   \
287
442k
                }                                                                                            \
288
3.23M
                X = X->forward[_i];                                                                          \
289
2.78M
            }                                                                                                \
290
1.70M
            assert(!_drop->forward[_i] ||                                                                    \
291
1.70M
                   !H5_GLUE3(H5SL_LOCATE_, CMP, _CMP)(SLIST, TYPE, _drop->forward[_i], KEY, HASHVAL));       \
292
1.70M
                                                                                                             \
293
1.70M
            /* Promote the middle node if necessary */                                                       \
294
1.70M
            if (_count == 3) {                                                                               \
295
442k
                assert(X == _last->forward[_i]->forward[_i]);                                                \
296
442k
                H5SL_PROMOTE(SLIST, X, _last, NULL);                                                         \
297
442k
            }                                                                                                \
298
1.70M
                                                                                                             \
299
1.70M
            /* Prepare to drop down */                                                                       \
300
1.70M
            X = _last = _drop;                                                                               \
301
1.70M
            _next     = _drop->forward[_i];                                                                  \
302
1.70M
        }                                                                                                    \
303
1.15M
                                                                                                             \
304
1.15M
        if (_next && H5_GLUE3(H5SL_LOCATE_, CMP, _EQ)(SLIST, TYPE, _next, KEY, HASHVAL))                     \
305
1.15M
            HGOTO_ERROR(H5E_SLIST, H5E_CANTINSERT, NULL, "can't insert duplicate key");                      \
306
1.15M
    }
307
308
/* Macro used to remove node */
309
#define H5SL_REMOVE(CMP, SLIST, X, TYPE, KEY, HASHVAL)                                                       \
310
26.4k
    {                                                                                                        \
311
26.4k
        H5SL_node_t *_last  = X;             /* Lowest node in the current gap */                            \
312
26.4k
        H5SL_node_t *_llast = X;             /* Lowest node in the previous gap */                           \
313
26.4k
        H5SL_node_t *_next  = NULL;          /* Highest node in the current gap */                           \
314
26.4k
        H5SL_node_t *_drop  = NULL;          /* Low node of the gap to drop into */                          \
315
26.4k
        H5SL_node_t *_ldrop = NULL;          /* Low node of gap before the one to drop into */               \
316
26.4k
        H5SL_node_t *_head  = SLIST->header; /* Head of the skip list */                                     \
317
26.4k
        int          _count;                 /* Number of nodes in the current gap */                        \
318
26.4k
        int          _i = (int)SLIST->curr_level;                                                            \
319
26.4k
                                                                                                             \
320
26.4k
        if (_i < 0)                                                                                          \
321
26.4k
            HGOTO_DONE(NULL);                                                                                \
322
26.4k
                                                                                                             \
323
26.4k
        H5_GLUE3(H5SL_LOCATE_, CMP, _HASHINIT)                                                               \
324
0
        (KEY, HASHVAL)                                                                                       \
325
0
                                                                                                             \
326
0
            /* Find the gap to drop in to at the highest level */                                            \
327
63.7k
            while (X && (!X->key || H5_GLUE3(H5SL_LOCATE_, CMP, _CMP)(SLIST, TYPE, X, KEY, HASHVAL)))        \
328
37.3k
        {                                                                                                    \
329
37.3k
            _llast = _last;                                                                                  \
330
37.3k
            _last  = X;                                                                                      \
331
37.3k
            X      = X->forward[_i];                                                                         \
332
37.3k
        }                                                                                                    \
333
26.4k
        _next = X;                                                                                           \
334
26.4k
                                                                                                             \
335
26.4k
        /* Main loop */                                                                                      \
336
41.6k
        for (_i--; _i >= 0; _i--) {                                                                          \
337
15.2k
            /* Search for the node to drop into, also count the number of */                                 \
338
15.2k
            /* nodes of height _i in this gap and keep track of of the node */                               \
339
15.2k
            /* before the one to drop into (_ldrop will become _llast, */                                    \
340
15.2k
            /* _drop will become _last). */                                                                  \
341
15.2k
            X = _ldrop = _last;                                                                              \
342
15.2k
            _drop      = NULL;                                                                               \
343
35.3k
            for (_count = 0;; _count++) {                                                                    \
344
35.3k
                /* Terminate if this is the last node in the gap */                                          \
345
35.3k
                if (X->forward[_i] == _next) {                                                               \
346
10.2k
                    if (!_drop)                                                                              \
347
10.2k
                        _drop = X;                                                                           \
348
10.2k
                    break;                                                                                   \
349
10.2k
                }                                                                                            \
350
35.3k
                                                                                                             \
351
35.3k
                /* If we have already found the node to drop into and there */                               \
352
35.3k
                /* is more than one node in this gap, we can stop searching */                               \
353
35.3k
                if (_drop) {                                                                                 \
354
988
                    assert(_count >= 1);                                                                     \
355
988
                    _count = 2;                                                                              \
356
988
                    break;                                                                                   \
357
988
                }                                                                                            \
358
25.1k
                else { /* !_drop */                                                                          \
359
24.1k
                    /* Check if this node is the start of the next gap */                                    \
360
24.1k
                    if (!H5_GLUE3(H5SL_LOCATE_, CMP, _CMP)(SLIST, TYPE, X->forward[_i], KEY, HASHVAL)) {     \
361
8.86k
                        _drop = X;                                                                           \
362
8.86k
                        /* Again check if we can stop searching */                                           \
363
8.86k
                        if (_count) {                                                                        \
364
3.98k
                            _count = 2;                                                                      \
365
3.98k
                            break;                                                                           \
366
3.98k
                        }                                                                                    \
367
8.86k
                    }                                                                                        \
368
24.1k
                    else                                                                                     \
369
24.1k
                        _ldrop = X;                                                                          \
370
24.1k
                }                                                                                            \
371
25.1k
                                                                                                             \
372
25.1k
                /* No need to check the last node in the gap if there are */                                 \
373
25.1k
                /* 3, as there cannot be a fourth */                                                         \
374
25.1k
                if (_count == 2) {                                                                           \
375
6
                    if (!_drop)                                                                              \
376
6
                        _drop = X->forward[_i];                                                              \
377
6
                    break;                                                                                   \
378
6
                }                                                                                            \
379
20.1k
                X = X->forward[_i];                                                                          \
380
20.1k
            }                                                                                                \
381
15.2k
            assert(_count >= 1 && _count <= 3);                                                              \
382
15.2k
            assert(!_drop->forward[_i] ||                                                                    \
383
15.2k
                   !H5_GLUE3(H5SL_LOCATE_, CMP, _CMP)(SLIST, TYPE, _drop->forward[_i], KEY, HASHVAL));       \
384
15.2k
                                                                                                             \
385
15.2k
            /* Check if we need to adjust node heights */                                                    \
386
15.2k
            if (_count == 1) {                                                                               \
387
7.11k
                /* Check if we are in the first gap */                                                       \
388
7.11k
                if (_llast == _last) {                                                                       \
389
2.64k
                    /* We are in the first gap, count the number of nodes */                                 \
390
2.64k
                    /* of height _i in the next gap.  We need only check */                                  \
391
2.64k
                    /* onenode to see if we should promote the first node */                                 \
392
2.64k
                    /* in the next gap */                                                                    \
393
2.64k
                    _llast = _next->forward[_i + 1];                                                         \
394
2.64k
                                                                                                             \
395
2.64k
                    /* Demote the separator node */                                                          \
396
2.64k
                    H5SL_DEMOTE(_next, _last)                                                                \
397
2.64k
                                                                                                             \
398
2.64k
                    /* If there are 2 or more nodes, promote the first */                                    \
399
2.64k
                    if (_next->forward[_i]->forward[_i] != _llast) {                                         \
400
866
                        X = _next->forward[_i];                                                              \
401
866
                        H5SL_PROMOTE(SLIST, X, _last, NULL);                                                 \
402
866
                    }                                                                                        \
403
2.64k
                    else if (!_head->forward[_i + 1]) {                                                      \
404
834
                        /* shrink the header */                                                              \
405
834
                        assert(_i == SLIST->curr_level - 1);                                                 \
406
834
                        assert((size_t)SLIST->curr_level == _head->level);                                   \
407
834
                                                                                                             \
408
834
                        H5SL_SHRINK(_head, (size_t)(_i + 1))                                                 \
409
834
                        SLIST->curr_level--;                                                                 \
410
834
                    }                                                                                        \
411
2.64k
                }                                                                                            \
412
7.11k
                else {                                                                                       \
413
4.46k
                    /* We are not in the first gap, count the number of */                                   \
414
4.46k
                    /* nodes of height _i in the previous gap.  Note we */                                   \
415
4.46k
                    /* "look ahead" in this loop so X has the value of the */                                \
416
4.46k
                    /* last node in the previous gap. */                                                     \
417
4.46k
                    X = _llast->forward[_i];                                                                 \
418
5.76k
                    for (_count = 1; _count < 3 && X->forward[_i] != _last; _count++)                        \
419
4.46k
                        X = X->forward[_i];                                                                  \
420
4.46k
                    assert(X->forward[_i] == _last);                                                         \
421
4.46k
                                                                                                             \
422
4.46k
                    /* Demote the separator node */                                                          \
423
4.46k
                    H5SL_DEMOTE(_last, _llast)                                                               \
424
4.46k
                                                                                                             \
425
4.46k
                    /* If there are 2 or more nodes, promote the last */                                     \
426
4.46k
                    if (_count >= 2)                                                                         \
427
4.46k
                        H5SL_PROMOTE(SLIST, X, _llast, NULL);                                                \
428
4.46k
                    else if (!_head->forward[_i + 1]) {                                                      \
429
794
                        /* shrink the header */                                                              \
430
794
                        assert(_i == SLIST->curr_level - 1);                                                 \
431
794
                        assert((size_t)SLIST->curr_level == _head->level);                                   \
432
794
                                                                                                             \
433
794
                        H5SL_SHRINK(_head, (size_t)(_i + 1))                                                 \
434
794
                        SLIST->curr_level--;                                                                 \
435
794
                    }                                                                                        \
436
4.46k
                }                                                                                            \
437
7.11k
            }                                                                                                \
438
15.2k
                                                                                                             \
439
15.2k
            /* Prepare to drop down */                                                                       \
440
15.2k
            _llast = _ldrop;                                                                                 \
441
15.2k
            _last  = _drop;                                                                                  \
442
15.2k
            _next  = _drop->forward[_i];                                                                     \
443
15.2k
        }                                                                                                    \
444
26.4k
                                                                                                             \
445
26.4k
        /* Check if we've found the node */                                                                  \
446
26.4k
        if (_next && H5_GLUE3(H5SL_LOCATE_, CMP, _EQ)(SLIST, TYPE, _next, KEY, HASHVAL)) {                   \
447
26.4k
            void *tmp = _next->item;                                                                         \
448
26.4k
            X         = _next;                                                                               \
449
26.4k
                                                                                                             \
450
26.4k
            /* If the node has a height > 0, swap it with its (lower) */                                     \
451
26.4k
            /* neighbor */                                                                                   \
452
26.4k
            if (X->level) {                                                                                  \
453
2.26k
                X              = X->backward;                                                                \
454
2.26k
                _next->key     = X->key;                                                                     \
455
2.26k
                _next->item    = X->item;                                                                    \
456
2.26k
                _next->hashval = X->hashval;                                                                 \
457
2.26k
            }                                                                                                \
458
26.4k
            assert(!X->level);                                                                               \
459
26.4k
                                                                                                             \
460
26.4k
            /* Remove the node */                                                                            \
461
26.4k
            X->backward->forward[0] = X->forward[0];                                                         \
462
26.4k
            if (SLIST->last == X)                                                                            \
463
26.4k
                SLIST->last = X->backward;                                                                   \
464
26.4k
            else                                                                                             \
465
26.4k
                X->forward[0]->backward = X->backward;                                                       \
466
26.4k
            SLIST->nobjs--;                                                                                  \
467
26.4k
            X->forward = (H5SL_node_t **)H5FL_FAC_FREE(H5SL_fac_g[0], X->forward);                           \
468
26.4k
            X          = H5FL_FREE(H5SL_node_t, X);                                                          \
469
26.4k
                                                                                                             \
470
26.4k
            HGOTO_DONE(tmp);                                                                                 \
471
26.4k
        }                                                                                                    \
472
26.4k
    }
473
474
/* Macro used to search for node */
475
1.45M
#define H5SL_SEARCH(CMP, SLIST, X, TYPE, KEY, HASHVAL) H5SL_LOCATE(SEARCH, CMP, SLIST, X, TYPE, KEY, HASHVAL)
476
477
/* Macro used to find a node */
478
0
#define H5SL_FIND(CMP, SLIST, X, TYPE, KEY, HASHVAL) H5SL_LOCATE(FIND, CMP, SLIST, X, TYPE, KEY, HASHVAL)
479
480
/* Private typedefs & structs */
481
482
/* Skip list node data structure */
483
struct H5SL_node_t {
484
    const void          *key;        /* Pointer to node's key */
485
    void                *item;       /* Pointer to node's item */
486
    size_t               level;      /* The level of this node */
487
    size_t               log_nalloc; /* log2(Number of slots allocated in forward) */
488
    uint32_t             hashval;    /* Hash value for key (only for strings, currently) */
489
    struct H5SL_node_t **forward;    /* Array of forward pointers from this node */
490
    struct H5SL_node_t  *backward;   /* Backward pointer from this node */
491
};
492
493
/* Main skip list data structure */
494
struct H5SL_t {
495
    /* Static values for each list */
496
    H5SL_type_t type; /* Type of skip list */
497
    H5SL_cmp_t  cmp;  /* Comparison callback, if type is H5SL_TYPE_GENERIC */
498
499
    /* Dynamic values for each list */
500
    int          curr_level; /* Current top level used in list */
501
    size_t       nobjs;      /* Number of active objects in skip list */
502
    H5SL_node_t *header;     /* Header for nodes in skip list */
503
    H5SL_node_t *last;       /* Pointer to last node in skip list */
504
};
505
506
/* Static functions */
507
static H5SL_node_t *H5SL__new_node(void *item, const void *key, uint32_t hashval);
508
static H5SL_node_t *H5SL__insert_common(H5SL_t *slist, void *item, const void *key);
509
static herr_t       H5SL__release_common(H5SL_t *slist, H5SL_operator_t op, void *op_data);
510
static herr_t       H5SL__close_common(H5SL_t *slist, H5SL_operator_t op, void *op_data);
511
512
/* Package initialization variable */
513
bool H5_PKG_INIT_VAR = false;
514
515
/* Declare a free list to manage the H5SL_t struct */
516
H5FL_DEFINE_STATIC(H5SL_t);
517
518
/* Declare a free list to manage the H5SL_node_t struct */
519
H5FL_DEFINE_STATIC(H5SL_node_t);
520
521
/* Global variables */
522
static H5FL_fac_head_t **H5SL_fac_g;
523
static size_t            H5SL_fac_nused_g;
524
static size_t            H5SL_fac_nalloc_g;
525
526
/*--------------------------------------------------------------------------
527
 NAME
528
    H5SL__init_package
529
 PURPOSE
530
    Initialize interface-specific information
531
 USAGE
532
    herr_t H5SL__init_package()
533
 RETURNS
534
    Non-negative on success/Negative on failure
535
 DESCRIPTION
536
    Initializes any interface-specific data or routines.
537
 GLOBAL VARIABLES
538
 COMMENTS, BUGS, ASSUMPTIONS
539
 EXAMPLES
540
 REVISION LOG
541
--------------------------------------------------------------------------*/
542
herr_t
543
H5SL__init_package(void)
544
1
{
545
1
    FUNC_ENTER_PACKAGE_NOERR
546
547
    /* Allocate space for array of factories */
548
1
    H5SL_fac_g = (H5FL_fac_head_t **)H5MM_malloc(sizeof(H5FL_fac_head_t *));
549
1
    assert(H5SL_fac_g);
550
1
    H5SL_fac_nalloc_g = 1;
551
552
    /* Initialize first factory */
553
1
    H5SL_fac_g[0] = H5FL_fac_init(sizeof(H5SL_node_t *));
554
1
    assert(H5SL_fac_g[0]);
555
1
    H5SL_fac_nused_g = 1;
556
557
1
    FUNC_LEAVE_NOAPI(SUCCEED)
558
1
} /* end H5SL__init_package() */
559
560
/*--------------------------------------------------------------------------
561
 NAME
562
    H5SL_term_package
563
 PURPOSE
564
    Terminate all the H5FL factories used in this package, and clear memory
565
 USAGE
566
    int H5SL_term_package()
567
 RETURNS
568
    Success:  Positive if any action might have caused a change in some
569
                other interface; zero otherwise.
570
        Failure:  Negative
571
 DESCRIPTION
572
    Release any resources allocated.
573
 GLOBAL VARIABLES
574
 COMMENTS, BUGS, ASSUMPTIONS
575
     Can't report errors...
576
 EXAMPLES
577
 REVISION LOG
578
--------------------------------------------------------------------------*/
579
int
580
H5SL_term_package(void)
581
2
{
582
2
    int n = 0;
583
584
2
    FUNC_ENTER_NOAPI_NOINIT_NOERR
585
586
2
    if (H5_PKG_INIT_VAR) {
587
        /* Terminate all the factories */
588
2
        if (H5SL_fac_nused_g > 0) {
589
1
            size_t                       i;
590
1
            herr_t H5_ATTR_NDEBUG_UNUSED ret;
591
592
5
            for (i = 0; i < H5SL_fac_nused_g; i++) {
593
4
                ret = H5FL_fac_term(H5SL_fac_g[i]);
594
4
                assert(ret >= 0);
595
4
            }
596
1
            H5SL_fac_nused_g = 0;
597
598
1
            n++;
599
1
        }
600
601
        /* Free the list of factories */
602
2
        if (H5SL_fac_g) {
603
1
            H5SL_fac_g        = (H5FL_fac_head_t **)H5MM_xfree((void *)H5SL_fac_g);
604
1
            H5SL_fac_nalloc_g = 0;
605
606
1
            n++;
607
1
        }
608
609
        /* Mark the interface as uninitialized */
610
2
        if (0 == n)
611
1
            H5_PKG_INIT_VAR = false;
612
2
    }
613
614
2
    FUNC_LEAVE_NOAPI(n)
615
2
} /* H5SL_term_package() */
616
617
/*--------------------------------------------------------------------------
618
 NAME
619
    H5SL__new_node
620
 PURPOSE
621
    Create a new skip list node of level 0
622
 USAGE
623
    H5SL_node_t *H5SL__new_node(item,key,hasval)
624
        void *item;             IN: Pointer to item info for node
625
        void *key;              IN: Pointer to key info for node
626
        uint32_t hashval;       IN: Hash value for node
627
628
 RETURNS
629
    Returns a pointer to a skip list node on success, NULL on failure.
630
 DESCRIPTION
631
    Create a new skip list node of the height 0, setting the item
632
    and key values internally.
633
 GLOBAL VARIABLES
634
 COMMENTS, BUGS, ASSUMPTIONS
635
    This routine does _not_ initialize the 'forward' pointers
636
 EXAMPLES
637
 REVISION LOG
638
--------------------------------------------------------------------------*/
639
static H5SL_node_t *
640
H5SL__new_node(void *item, const void *key, uint32_t hashval)
641
1.33M
{
642
1.33M
    H5SL_node_t *ret_value = NULL; /* New skip list node */
643
644
1.33M
    FUNC_ENTER_PACKAGE
645
646
    /* Allocate the node */
647
1.33M
    if (NULL == (ret_value = (H5SL_node_t *)H5FL_MALLOC(H5SL_node_t)))
648
0
        HGOTO_ERROR(H5E_SLIST, H5E_NOSPACE, NULL, "memory allocation failed");
649
650
    /* Initialize node */
651
1.33M
    ret_value->key     = key;
652
1.33M
    ret_value->item    = item;
653
1.33M
    ret_value->level   = 0;
654
1.33M
    ret_value->hashval = hashval;
655
1.33M
    if (NULL == (ret_value->forward = (H5SL_node_t **)H5FL_FAC_MALLOC(H5SL_fac_g[0]))) {
656
0
        ret_value = H5FL_FREE(H5SL_node_t, ret_value);
657
0
        HGOTO_ERROR(H5E_SLIST, H5E_NOSPACE, NULL, "memory allocation failed");
658
0
    }
659
1.33M
    ret_value->log_nalloc = 0;
660
661
1.33M
done:
662
1.33M
    FUNC_LEAVE_NOAPI(ret_value)
663
1.33M
} /* end H5SL__new_node() */
664
665
/*--------------------------------------------------------------------------
666
 NAME
667
    H5SL__insert_common
668
 PURPOSE
669
    Common code for inserting an object into a skip list
670
 USAGE
671
    H5SL_node_t *H5SL__insert_common(slist,item,key)
672
        H5SL_t *slist;          IN/OUT: Pointer to skip list
673
        void *item;             IN: Item to insert
674
        void *key;              IN: Key for item to insert
675
676
 RETURNS
677
    Returns pointer to new node on success, NULL on failure.
678
 DESCRIPTION
679
    Common code for inserting an element into a skip list.
680
 GLOBAL VARIABLES
681
 COMMENTS, BUGS, ASSUMPTIONS
682
    Inserting an item with the same key as an existing object fails.
683
 EXAMPLES
684
 REVISION LOG
685
--------------------------------------------------------------------------*/
686
static H5SL_node_t *
687
H5SL__insert_common(H5SL_t *slist, void *item, const void *key)
688
1.15M
{
689
1.15M
    H5SL_node_t *x;                /* Current node to examine */
690
1.15M
    H5SL_node_t *prev;             /* Node before the new node */
691
1.15M
    uint32_t     hashval   = 0;    /* Hash value for key */
692
1.15M
    H5SL_node_t *ret_value = NULL; /* Return value */
693
694
1.15M
    FUNC_ENTER_PACKAGE
695
696
    /* Check args */
697
1.15M
    assert(slist);
698
1.15M
    assert(key);
699
700
    /* Check internal consistency */
701
    /* (Pre-condition) */
702
703
    /* Insert item into skip list */
704
705
    /* Work through the forward pointers for a node, finding the node at each
706
     * level that is before the location to insert
707
     */
708
1.15M
    prev = slist->header;
709
1.15M
    switch (slist->type) {
710
0
        case H5SL_TYPE_INT:
711
0
            H5SL_INSERT(SCALAR, slist, prev, const int, key, -)
712
0
            break;
713
714
26.4k
        case H5SL_TYPE_HADDR:
715
26.4k
            H5SL_INSERT(SCALAR, slist, prev, const haddr_t, key, -)
716
26.4k
            break;
717
718
1.13M
        case H5SL_TYPE_STR:
719
1.13M
            H5SL_INSERT(STRING, slist, prev, char *, key, hashval)
720
1.13M
            break;
721
722
1.13M
        case H5SL_TYPE_HSIZE:
723
0
            H5SL_INSERT(SCALAR, slist, prev, const hsize_t, key, -)
724
0
            break;
725
726
0
        case H5SL_TYPE_UNSIGNED:
727
0
            H5SL_INSERT(SCALAR, slist, prev, const unsigned, key, -)
728
0
            break;
729
730
0
        case H5SL_TYPE_SIZE:
731
0
            H5SL_INSERT(SCALAR, slist, prev, const size_t, key, -)
732
0
            break;
733
734
0
        case H5SL_TYPE_OBJ:
735
0
            H5SL_INSERT(OBJ, slist, prev, const H5_obj_t, key, -)
736
0
            break;
737
738
0
        case H5SL_TYPE_HID:
739
0
            H5SL_INSERT(SCALAR, slist, prev, const hid_t, key, -)
740
0
            break;
741
742
0
        case H5SL_TYPE_GENERIC:
743
0
            H5SL_INSERT(GENERIC, slist, prev, const void, key, -)
744
0
            break;
745
746
0
        default:
747
0
            assert(0 && "Unknown skiplist type!");
748
1.15M
    }
749
750
    /* 'key' must not have been found in existing list, if we get here */
751
752
1.15M
    if (slist->curr_level < 0)
753
136k
        slist->curr_level = 0;
754
755
    /* Create new node of level 0 */
756
1.15M
    if (NULL == (x = H5SL__new_node(item, key, hashval)))
757
0
        HGOTO_ERROR(H5E_SLIST, H5E_NOSPACE, NULL, "can't create new skip list node");
758
759
    /* Update the links */
760
1.15M
    x->backward      = prev;
761
1.15M
    x->forward[0]    = prev->forward[0];
762
1.15M
    prev->forward[0] = x;
763
1.15M
    if (x->forward[0])
764
346k
        x->forward[0]->backward = x;
765
810k
    else {
766
810k
        assert(slist->last == prev);
767
810k
        slist->last = x;
768
810k
    }
769
770
    /* Increment the number of nodes in the skip list */
771
1.15M
    slist->nobjs++;
772
773
    /* Set return value */
774
1.15M
    ret_value = x;
775
776
1.15M
done:
777
1.15M
    FUNC_LEAVE_NOAPI(ret_value)
778
1.15M
} /* end H5SL__insert_common() */
779
780
/*--------------------------------------------------------------------------
781
 NAME
782
    H5SL__release_common
783
 PURPOSE
784
    Release all nodes from a skip list, optionally calling a 'free' operator
785
 USAGE
786
    herr_t H5SL__release_common(slist,op,opdata)
787
        H5SL_t *slist;            IN/OUT: Pointer to skip list to release nodes
788
        H5SL_operator_t op;     IN: Callback function to free item & key
789
        void *op_data;          IN/OUT: Pointer to application data for callback
790
791
 RETURNS
792
    Returns non-negative on success, negative on failure.
793
 DESCRIPTION
794
    Release all the nodes in a skip list.  The 'op' routine is called for
795
    each node in the list.
796
 GLOBAL VARIABLES
797
 COMMENTS, BUGS, ASSUMPTIONS
798
    The return value from the 'op' routine is ignored.
799
800
    The skip list itself is still valid, it just has all its nodes removed.
801
 EXAMPLES
802
 REVISION LOG
803
--------------------------------------------------------------------------*/
804
static herr_t
805
H5SL__release_common(H5SL_t *slist, H5SL_operator_t op, void *op_data)
806
182k
{
807
182k
    H5SL_node_t *node, *next_node; /* Pointers to skip list nodes */
808
182k
    herr_t       ret_value = SUCCEED;
809
810
182k
    FUNC_ENTER_PACKAGE
811
812
    /* Check args */
813
182k
    assert(slist);
814
815
    /* Check internal consistency */
816
    /* (Pre-condition) */
817
818
    /* Free skip list nodes */
819
182k
    node = slist->header->forward[0];
820
1.31M
    while (node) {
821
1.13M
        next_node = node->forward[0];
822
823
        /* Call callback, if one is given.
824
         *
825
         * Ignoring const here is fine as we only need the value to be const
826
         * with respect to the list code, which should never modify the
827
         * elements. The library code that is making use of the skip list
828
         * container can do what it likes with the elements.
829
         */
830
1.13M
        H5_WARN_CAST_AWAY_CONST_OFF
831
1.13M
        if (op)
832
218k
            (void)(op)(node->item, (void *)node->key, op_data);
833
1.13M
        H5_WARN_CAST_AWAY_CONST_ON
834
835
1.13M
        node->forward = (H5SL_node_t **)H5FL_FAC_FREE(H5SL_fac_g[node->log_nalloc], node->forward);
836
1.13M
        node          = H5FL_FREE(H5SL_node_t, node);
837
1.13M
        node          = next_node;
838
1.13M
    }
839
840
    /* Reset the header pointers */
841
182k
    slist->header->forward =
842
182k
        (H5SL_node_t **)H5FL_FAC_FREE(H5SL_fac_g[slist->header->log_nalloc], slist->header->forward);
843
182k
    if (NULL == (slist->header->forward = (H5SL_node_t **)H5FL_FAC_MALLOC(H5SL_fac_g[0])))
844
0
        HGOTO_ERROR(H5E_SLIST, H5E_NOSPACE, FAIL, "memory allocation failed");
845
182k
    slist->header->forward[0] = NULL;
846
182k
    slist->header->log_nalloc = 0;
847
182k
    slist->header->level      = 0;
848
849
    /* Reset the last pointer */
850
182k
    slist->last = slist->header;
851
852
    /* Reset the dynamic internal fields */
853
182k
    slist->curr_level = -1;
854
182k
    slist->nobjs      = 0;
855
856
182k
done:
857
182k
    FUNC_LEAVE_NOAPI(ret_value)
858
182k
} /* end H5SL__release_common() */
859
860
/*--------------------------------------------------------------------------
861
 NAME
862
    H5SL__close_common
863
 PURPOSE
864
    Close a skip list, deallocating it and potentially freeing all its nodes.
865
 USAGE
866
    herr_t H5SL__close_common(slist,op,opdata)
867
        H5SL_t *slist;          IN/OUT: Pointer to skip list to close
868
        H5SL_operator_t op;     IN: Callback function to free item & key
869
        void *op_data;          IN/OUT: Pointer to application data for callback
870
871
 RETURNS
872
    Returns non-negative on success, negative on failure.
873
 DESCRIPTION
874
    Close a skip list, freeing all internal information.  Any objects left in
875
    the skip list have the 'op' routine called for each.
876
 GLOBAL VARIABLES
877
 COMMENTS, BUGS, ASSUMPTIONS
878
    If the 'op' routine returns non-zero, only the nodes up to that
879
    point in the list are released and the list is still valid.
880
 EXAMPLES
881
 REVISION LOG
882
--------------------------------------------------------------------------*/
883
static herr_t
884
H5SL__close_common(H5SL_t *slist, H5SL_operator_t op, void *op_data)
885
182k
{
886
182k
    herr_t ret_value = SUCCEED;
887
888
182k
    FUNC_ENTER_PACKAGE
889
890
    /* Check args */
891
182k
    assert(slist);
892
893
    /* Check internal consistency */
894
    /* (Pre-condition) */
895
896
    /* Free skip list nodes */
897
182k
    if (H5SL__release_common(slist, op, op_data) < 0)
898
0
        HGOTO_ERROR(H5E_SLIST, H5E_CANTFREE, FAIL, "can't release skip list nodes");
899
900
    /* Release header node */
901
182k
    slist->header->forward =
902
182k
        (H5SL_node_t **)H5FL_FAC_FREE(H5SL_fac_g[slist->header->log_nalloc], slist->header->forward);
903
182k
    slist->header = H5FL_FREE(H5SL_node_t, slist->header);
904
905
    /* Free skip list object */
906
182k
    slist = H5FL_FREE(H5SL_t, slist);
907
908
182k
done:
909
182k
    FUNC_LEAVE_NOAPI(ret_value)
910
182k
} /* end H5SL__close_common() */
911
912
/*--------------------------------------------------------------------------
913
 NAME
914
    H5SL_create
915
 PURPOSE
916
    Create a skip list
917
 USAGE
918
    H5SL_t *H5SL_create(H5SL_type_t type, H5SL_cmp_t cmp)
919
920
 RETURNS
921
    Returns a pointer to a skip list on success, NULL on failure.
922
 DESCRIPTION
923
    Create a skip list.
924
 GLOBAL VARIABLES
925
 COMMENTS, BUGS, ASSUMPTIONS
926
 EXAMPLES
927
 REVISION LOG
928
--------------------------------------------------------------------------*/
929
H5SL_t *
930
H5SL_create(H5SL_type_t type, H5SL_cmp_t cmp)
931
182k
{
932
182k
    H5SL_t      *new_slist = NULL; /* Pointer to new skip list object created */
933
182k
    H5SL_node_t *header;           /* Pointer to skip list header node */
934
182k
    H5SL_t      *ret_value = NULL; /* Return value */
935
936
182k
    FUNC_ENTER_NOAPI(NULL)
937
938
    /* Check args */
939
182k
    assert(type >= H5SL_TYPE_INT && type <= H5SL_TYPE_GENERIC);
940
941
    /* Allocate skip list structure */
942
182k
    if (NULL == (new_slist = H5FL_MALLOC(H5SL_t)))
943
0
        HGOTO_ERROR(H5E_SLIST, H5E_NOSPACE, NULL, "memory allocation failed");
944
945
    /* Set the static internal fields */
946
182k
    new_slist->type = type;
947
182k
    assert((type == H5SL_TYPE_GENERIC) == !!cmp);
948
182k
    new_slist->cmp = cmp;
949
950
    /* Set the dynamic internal fields */
951
182k
    new_slist->curr_level = -1;
952
182k
    new_slist->nobjs      = 0;
953
954
    /* Allocate the header node */
955
182k
    if (NULL == (header = H5SL__new_node(NULL, NULL, (uint32_t)ULONG_MAX)))
956
0
        HGOTO_ERROR(H5E_SLIST, H5E_NOSPACE, NULL, "can't create new skip list node");
957
958
    /* Initialize header node's forward pointer */
959
182k
    header->forward[0] = NULL;
960
961
    /* Initialize header node's backward pointer */
962
182k
    header->backward = NULL;
963
964
    /* Attach the header */
965
182k
    new_slist->header = header;
966
182k
    new_slist->last   = header;
967
968
    /* Set the return value */
969
182k
    ret_value = new_slist;
970
971
182k
done:
972
    /* Error cleanup */
973
182k
    if (ret_value == NULL) {
974
0
        if (new_slist != NULL)
975
0
            new_slist = H5FL_FREE(H5SL_t, new_slist);
976
0
    }
977
978
182k
    FUNC_LEAVE_NOAPI(ret_value)
979
182k
} /* end H5SL_create() */
980
981
/*--------------------------------------------------------------------------
982
 NAME
983
    H5SL_count
984
 PURPOSE
985
    Count the number of objects in a skip list
986
 USAGE
987
    size_t H5SL_count(slist)
988
        H5SL_t *slist;            IN: Pointer to skip list to count
989
990
 RETURNS
991
    Returns number of objects on success, can't fail
992
 DESCRIPTION
993
    Count elements in a skip list.
994
 GLOBAL VARIABLES
995
 COMMENTS, BUGS, ASSUMPTIONS
996
 EXAMPLES
997
 REVISION LOG
998
--------------------------------------------------------------------------*/
999
size_t
1000
H5SL_count(H5SL_t *slist)
1001
182k
{
1002
182k
    FUNC_ENTER_NOAPI_NOINIT_NOERR
1003
1004
    /* Check args */
1005
182k
    assert(slist);
1006
1007
    /* Check internal consistency */
1008
    /* (Pre-condition) */
1009
1010
182k
    FUNC_LEAVE_NOAPI(slist->nobjs)
1011
182k
} /* end H5SL_count() */
1012
1013
/*--------------------------------------------------------------------------
1014
 NAME
1015
    H5SL_insert
1016
 PURPOSE
1017
    Insert an object into a skip list
1018
 USAGE
1019
    herr_t H5SL_insert(slist,item,key)
1020
        H5SL_t *slist;          IN/OUT: Pointer to skip list
1021
        void *item;             IN: Item to insert
1022
        void *key;              IN: Key for item to insert
1023
1024
 RETURNS
1025
    Returns non-negative on success, negative on failure.
1026
 DESCRIPTION
1027
    Insert element into a skip list.
1028
 GLOBAL VARIABLES
1029
 COMMENTS, BUGS, ASSUMPTIONS
1030
    Inserting an item with the same key as an existing object fails.
1031
 EXAMPLES
1032
 REVISION LOG
1033
--------------------------------------------------------------------------*/
1034
herr_t
1035
H5SL_insert(H5SL_t *slist, void *item, const void *key)
1036
1.15M
{
1037
1.15M
    herr_t ret_value = SUCCEED; /* Return value */
1038
1039
1.15M
    FUNC_ENTER_NOAPI_NOINIT
1040
1041
    /* Check args */
1042
1.15M
    assert(slist);
1043
1.15M
    assert(key);
1044
1045
    /* Check internal consistency */
1046
    /* (Pre-condition) */
1047
1048
    /* Insert item into skip list */
1049
1.15M
    if (NULL == H5SL__insert_common(slist, item, key))
1050
0
        HGOTO_ERROR(H5E_SLIST, H5E_CANTINSERT, FAIL, "can't create new skip list node");
1051
1052
1.15M
done:
1053
1.15M
    FUNC_LEAVE_NOAPI(ret_value)
1054
1.15M
} /* end H5SL_insert() */
1055
1056
/*--------------------------------------------------------------------------
1057
 NAME
1058
    H5SL_add
1059
 PURPOSE
1060
    Insert an object into a skip list
1061
 USAGE
1062
    H5SL_node_t *H5SL_add(slist,item,key)
1063
        H5SL_t *slist;          IN/OUT: Pointer to skip list
1064
        void *item;             IN: Item to insert
1065
        void *key;              IN: Key for item to insert
1066
1067
 RETURNS
1068
    Returns pointer to new skip list node on success, NULL on failure.
1069
 DESCRIPTION
1070
    Insert element into a skip list and return the skip list node for the
1071
    new element in the list.
1072
 GLOBAL VARIABLES
1073
 COMMENTS, BUGS, ASSUMPTIONS
1074
    Inserting an item with the same key as an existing object fails.
1075
1076
    This routine is a useful starting point for next/prev calls
1077
 EXAMPLES
1078
 REVISION LOG
1079
--------------------------------------------------------------------------*/
1080
H5SL_node_t *
1081
H5SL_add(H5SL_t *slist, void *item, const void *key)
1082
0
{
1083
0
    H5SL_node_t *ret_value = NULL; /* Return value */
1084
1085
0
    FUNC_ENTER_NOAPI_NOINIT
1086
1087
    /* Check args */
1088
0
    assert(slist);
1089
0
    assert(key);
1090
1091
    /* Check internal consistency */
1092
    /* (Pre-condition) */
1093
1094
    /* Insert item into skip list */
1095
0
    if (NULL == (ret_value = H5SL__insert_common(slist, item, key)))
1096
0
        HGOTO_ERROR(H5E_SLIST, H5E_CANTINSERT, NULL, "can't create new skip list node");
1097
1098
0
done:
1099
0
    FUNC_LEAVE_NOAPI(ret_value)
1100
0
} /* end H5SL_add() */
1101
1102
/*--------------------------------------------------------------------------
1103
 NAME
1104
    H5SL_remove
1105
 PURPOSE
1106
    Removes an object from a skip list
1107
 USAGE
1108
    void *H5SL_remove(slist,key)
1109
        H5SL_t *slist;          IN/OUT: Pointer to skip list
1110
        void *key;              IN: Key for item to remove
1111
1112
 RETURNS
1113
    Returns pointer to item removed on success, NULL on failure.
1114
 DESCRIPTION
1115
    Remove element from a skip list.
1116
 GLOBAL VARIABLES
1117
 COMMENTS, BUGS, ASSUMPTIONS
1118
 EXAMPLES
1119
 REVISION LOG
1120
--------------------------------------------------------------------------*/
1121
void *
1122
H5SL_remove(H5SL_t *slist, const void *key)
1123
26.4k
{
1124
26.4k
    H5SL_node_t *x;                /* Current node to examine */
1125
26.4k
    uint32_t     hashval   = 0;    /* Hash value for key */
1126
26.4k
    void        *ret_value = NULL; /* Return value */
1127
1128
26.4k
    FUNC_ENTER_NOAPI_NOINIT
1129
1130
    /* Check args */
1131
26.4k
    assert(slist);
1132
26.4k
    assert(key);
1133
1134
    /* Check internal consistency */
1135
    /* (Pre-condition) */
1136
1137
    /* Remove item from skip list */
1138
1139
    /* Work through the forward pointers for a node, finding the node at each
1140
     * level that is before the location to remove
1141
     */
1142
26.4k
    x = slist->header;
1143
26.4k
    switch (slist->type) {
1144
0
        case H5SL_TYPE_INT:
1145
0
            H5SL_REMOVE(SCALAR, slist, x, const int, key, -)
1146
0
            break;
1147
1148
26.4k
        case H5SL_TYPE_HADDR:
1149
52.8k
            H5SL_REMOVE(SCALAR, slist, x, const haddr_t, key, -)
1150
0
            break;
1151
1152
0
        case H5SL_TYPE_STR:
1153
0
            H5SL_REMOVE(STRING, slist, x, char *, key, hashval)
1154
0
            break;
1155
1156
0
        case H5SL_TYPE_HSIZE:
1157
0
            H5SL_REMOVE(SCALAR, slist, x, const hsize_t, key, -)
1158
0
            break;
1159
1160
0
        case H5SL_TYPE_UNSIGNED:
1161
0
            H5SL_REMOVE(SCALAR, slist, x, const unsigned, key, -)
1162
0
            break;
1163
1164
0
        case H5SL_TYPE_SIZE:
1165
0
            H5SL_REMOVE(SCALAR, slist, x, const size_t, key, -)
1166
0
            break;
1167
1168
0
        case H5SL_TYPE_OBJ:
1169
0
            H5SL_REMOVE(OBJ, slist, x, const H5_obj_t, key, -)
1170
0
            break;
1171
1172
0
        case H5SL_TYPE_HID:
1173
0
            H5SL_REMOVE(SCALAR, slist, x, const hid_t, key, -)
1174
0
            break;
1175
1176
0
        case H5SL_TYPE_GENERIC:
1177
0
            H5SL_REMOVE(GENERIC, slist, x, const void, key, -)
1178
0
            break;
1179
1180
0
        default:
1181
0
            assert(0 && "Unknown skiplist type!");
1182
26.4k
    }
1183
1184
26.4k
done:
1185
26.4k
    FUNC_LEAVE_NOAPI(ret_value)
1186
26.4k
} /* end H5SL_remove() */
1187
1188
/*--------------------------------------------------------------------------
1189
 NAME
1190
    H5SL_remove_first
1191
 PURPOSE
1192
    Removes the first object from a skip list
1193
 USAGE
1194
    void *H5SL_remove_first(slist)
1195
        H5SL_t *slist;          IN/OUT: Pointer to skip list
1196
1197
 RETURNS
1198
    Returns pointer to item removed on success, NULL on failure.
1199
 DESCRIPTION
1200
    Remove first element from a skip list.
1201
 GLOBAL VARIABLES
1202
 COMMENTS, BUGS, ASSUMPTIONS
1203
 EXAMPLES
1204
 REVISION LOG
1205
--------------------------------------------------------------------------*/
1206
void *
1207
H5SL_remove_first(H5SL_t *slist)
1208
0
{
1209
0
    void        *ret_value = NULL;                      /* Return value             */
1210
0
    H5SL_node_t *head      = slist->header;             /* Skip list header         */
1211
0
    H5SL_node_t *tmp       = slist->header->forward[0]; /* Temporary node pointer   */
1212
0
    H5SL_node_t *next;                                  /* Next node to search for  */
1213
0
    size_t       level;                                 /* Skip list level          */
1214
0
    size_t       i;                                     /* Index                    */
1215
1216
0
    FUNC_ENTER_NOAPI_NOINIT
1217
1218
    /* Check args */
1219
0
    assert(slist);
1220
1221
    /* Assign level */
1222
0
    H5_CHECK_OVERFLOW(slist->curr_level, int, size_t);
1223
0
    level = (size_t)slist->curr_level;
1224
1225
    /* Check internal consistency */
1226
    /* (Pre-condition) */
1227
1228
    /* Remove item from skip list */
1229
1230
    /* Check for empty list */
1231
0
    if (slist->last != slist->header) {
1232
1233
        /* Assign return value */
1234
0
        ret_value = tmp->item;
1235
0
        assert(level == head->level);
1236
0
        assert(0 == tmp->level);
1237
1238
        /* Remove the first node */
1239
0
        head->forward[0] = tmp->forward[0];
1240
0
        if (slist->last == tmp)
1241
0
            slist->last = head;
1242
0
        else
1243
0
            tmp->forward[0]->backward = head;
1244
0
        slist->nobjs--;
1245
        /* Free memory */
1246
0
        tmp->forward = (H5SL_node_t **)H5FL_FAC_FREE(H5SL_fac_g[0], tmp->forward);
1247
0
        tmp          = H5FL_FREE(H5SL_node_t, tmp);
1248
1249
        /* Reshape the skip list as necessary to maintain 1-2-3 condition */
1250
0
        for (i = 0; i < level; i++) {
1251
0
            next = head->forward[i + 1];
1252
0
            assert(next);
1253
1254
            /* Check if  head->forward[i] == head->forward[i+1] (illegal) */
1255
0
            if (head->forward[i] == next) {
1256
0
                tmp  = next;
1257
0
                next = next->forward[i + 1];
1258
1259
0
                assert(tmp->level == i + 1);
1260
1261
                /* Demote head->forward[i] */
1262
0
                H5SL_DEMOTE(tmp, head)
1263
1264
                /* Check if we need to promote the following node to maintain
1265
                 * 1-2-3 condition */
1266
0
                if (tmp->forward[i]->forward[i] != next) {
1267
0
                    assert(tmp->forward[i]->forward[i]->forward[i] == next ||
1268
0
                           tmp->forward[i]->forward[i]->forward[i]->forward[i] == next);
1269
0
                    tmp = tmp->forward[i];
1270
0
                    H5SL_PROMOTE(slist, tmp, head, NULL);
1271
                    /* In this case, since there is a node of height = i+1 here
1272
                     * now (tmp), we know the skip list must be valid and can
1273
                     * break */
1274
0
                    break;
1275
0
                }
1276
0
                else if (!head->forward[i + 1]) {
1277
                    /* We just shrunk the largest node, shrink the header */
1278
0
                    assert(i == level - 1);
1279
1280
0
                    H5SL_SHRINK(head, level)
1281
0
                    slist->curr_level--;
1282
0
                }
1283
0
            }
1284
0
            else
1285
0
                break;
1286
0
        }
1287
0
    }
1288
1289
0
done:
1290
0
    FUNC_LEAVE_NOAPI(ret_value)
1291
0
} /* end H5SL_remove_first() */
1292
1293
/*--------------------------------------------------------------------------
1294
 NAME
1295
    H5SL_search
1296
 PURPOSE
1297
    Search for object in a skip list
1298
 USAGE
1299
    void *H5SL_search(slist,key)
1300
        H5SL_t *slist;          IN/OUT: Pointer to skip list
1301
        void *key;              IN: Key for item to search for
1302
1303
 RETURNS
1304
    Returns pointer to item on success, NULL on failure
1305
 DESCRIPTION
1306
    Search for an object in a skip list, according to it's key
1307
 GLOBAL VARIABLES
1308
 COMMENTS, BUGS, ASSUMPTIONS
1309
 EXAMPLES
1310
 REVISION LOG
1311
--------------------------------------------------------------------------*/
1312
void *
1313
H5SL_search(H5SL_t *slist, const void *key)
1314
1.45M
{
1315
1.45M
    H5SL_node_t *x;                /* Current node to examine */
1316
1.45M
    uint32_t     hashval   = 0;    /* Hash value for key */
1317
1.45M
    void        *ret_value = NULL; /* Return value */
1318
1319
1.45M
    FUNC_ENTER_NOAPI_NOINIT_NOERR
1320
1321
    /* Check args */
1322
1.45M
    assert(slist);
1323
1.45M
    assert(key);
1324
1325
    /* Check internal consistency */
1326
    /* (Pre-condition) */
1327
1328
    /* Insert item into skip list */
1329
1330
    /* Work through the forward pointers for a node, finding the node at each
1331
     * level that is before the location to insert
1332
     */
1333
1.45M
    x = slist->header;
1334
1.45M
    switch (slist->type) {
1335
0
        case H5SL_TYPE_INT:
1336
0
            H5SL_SEARCH(SCALAR, slist, x, const int, key, -)
1337
0
            break;
1338
1339
39.9k
        case H5SL_TYPE_HADDR:
1340
39.9k
            H5SL_SEARCH(SCALAR, slist, x, const haddr_t, key, -)
1341
26.7k
            break;
1342
1343
1.41M
        case H5SL_TYPE_STR:
1344
1.41M
            H5SL_SEARCH(STRING, slist, x, char *, key, hashval)
1345
808k
            break;
1346
1347
808k
        case H5SL_TYPE_HSIZE:
1348
0
            H5SL_SEARCH(SCALAR, slist, x, const hsize_t, key, -)
1349
0
            break;
1350
1351
0
        case H5SL_TYPE_UNSIGNED:
1352
0
            H5SL_SEARCH(SCALAR, slist, x, const unsigned, key, -)
1353
0
            break;
1354
1355
0
        case H5SL_TYPE_SIZE:
1356
0
            H5SL_SEARCH(SCALAR, slist, x, const size_t, key, -)
1357
0
            break;
1358
1359
0
        case H5SL_TYPE_OBJ:
1360
0
            H5SL_SEARCH(OBJ, slist, x, const H5_obj_t, key, -)
1361
0
            break;
1362
1363
0
        case H5SL_TYPE_HID:
1364
0
            H5SL_SEARCH(SCALAR, slist, x, const hid_t, key, -)
1365
0
            break;
1366
1367
0
        case H5SL_TYPE_GENERIC:
1368
0
            H5SL_SEARCH(GENERIC, slist, x, const void, key, -)
1369
0
            break;
1370
1371
0
        default:
1372
0
            assert(0 && "Unknown skiplist type!");
1373
1.45M
    }
1374
1375
    /* 'key' must not have been found in list, if we get here */
1376
835k
    ret_value = NULL;
1377
1378
1.45M
done:
1379
1.45M
    FUNC_LEAVE_NOAPI(ret_value)
1380
1.45M
} /* end H5SL_search() */
1381
1382
/*--------------------------------------------------------------------------
1383
 NAME
1384
    H5SL_less
1385
 PURPOSE
1386
    Search for object in a skip list that is less than or equal to 'key'
1387
 USAGE
1388
    void *H5SL_less(slist,key)
1389
        H5SL_t *slist;          IN/OUT: Pointer to skip list
1390
        void *key;              IN: Key for item to search for
1391
1392
 RETURNS
1393
    Returns pointer to item who key is less than or equal to 'key' on success,
1394
        NULL on failure
1395
 DESCRIPTION
1396
    Search for an object in a skip list, according to it's key, returning the
1397
    object itself (for an exact match), or the object with the next highest
1398
    key that is less than 'key'
1399
 GLOBAL VARIABLES
1400
 COMMENTS, BUGS, ASSUMPTIONS
1401
 EXAMPLES
1402
 REVISION LOG
1403
--------------------------------------------------------------------------*/
1404
void *
1405
H5SL_less(H5SL_t *slist, const void *key)
1406
0
{
1407
0
    H5SL_node_t *x;                /* Current node to examine */
1408
0
    uint32_t     hashval   = 0;    /* Hash value for key */
1409
0
    void        *ret_value = NULL; /* Return value */
1410
1411
0
    FUNC_ENTER_NOAPI_NOINIT_NOERR
1412
1413
    /* Check args */
1414
0
    assert(slist);
1415
0
    assert(key);
1416
1417
    /* Check internal consistency */
1418
    /* (Pre-condition) */
1419
1420
    /* Insert item into skip list */
1421
1422
    /* Work through the forward pointers for a node, finding the node at each
1423
     * level that is before the location to insert
1424
     */
1425
0
    x = slist->header;
1426
0
    switch (slist->type) {
1427
0
        case H5SL_TYPE_INT:
1428
0
            H5SL_SEARCH(SCALAR, slist, x, const int, key, -)
1429
0
            break;
1430
1431
0
        case H5SL_TYPE_HADDR:
1432
0
            H5SL_SEARCH(SCALAR, slist, x, const haddr_t, key, -)
1433
0
            break;
1434
1435
0
        case H5SL_TYPE_STR:
1436
0
            H5SL_SEARCH(STRING, slist, x, char *, key, hashval)
1437
0
            break;
1438
1439
0
        case H5SL_TYPE_HSIZE:
1440
0
            H5SL_SEARCH(SCALAR, slist, x, const hsize_t, key, -)
1441
0
            break;
1442
1443
0
        case H5SL_TYPE_UNSIGNED:
1444
0
            H5SL_SEARCH(SCALAR, slist, x, const unsigned, key, -)
1445
0
            break;
1446
1447
0
        case H5SL_TYPE_SIZE:
1448
0
            H5SL_SEARCH(SCALAR, slist, x, const size_t, key, -)
1449
0
            break;
1450
1451
0
        case H5SL_TYPE_OBJ:
1452
0
            H5SL_SEARCH(OBJ, slist, x, const H5_obj_t, key, -)
1453
0
            break;
1454
1455
0
        case H5SL_TYPE_HID:
1456
0
            H5SL_SEARCH(SCALAR, slist, x, const hid_t, key, -)
1457
0
            break;
1458
1459
0
        case H5SL_TYPE_GENERIC:
1460
0
            H5SL_SEARCH(GENERIC, slist, x, const void, key, -)
1461
0
            break;
1462
1463
0
        default:
1464
0
            assert(0 && "Unknown skiplist type!");
1465
0
    }
1466
1467
    /* An exact match for 'key' must not have been found in list, if we get here */
1468
    /* Check for a node with a key that is less than the given 'key' */
1469
0
    if (x == NULL) {
1470
        /* Check for walking off the list */
1471
0
        if (slist->last != slist->header)
1472
0
            ret_value = slist->last->item;
1473
0
        else
1474
0
            ret_value = NULL;
1475
0
    }
1476
0
    else {
1477
0
        if (x->backward != slist->header)
1478
0
            ret_value = x->backward->item;
1479
0
        else
1480
0
            ret_value = NULL;
1481
0
    }
1482
1483
0
done:
1484
0
    FUNC_LEAVE_NOAPI(ret_value)
1485
0
} /* end H5SL_less() */
1486
1487
/*--------------------------------------------------------------------------
1488
 NAME
1489
    H5SL_greater
1490
 PURPOSE
1491
    Search for object in a skip list that is greater than or equal to 'key'
1492
 USAGE
1493
    void *H5SL_greater(slist, key)
1494
        H5SL_t *slist;          IN/OUT: Pointer to skip list
1495
        void *key;              IN: Key for item to search for
1496
1497
 RETURNS
1498
    Returns pointer to item who key is greater than or equal to 'key' on success,
1499
        NULL on failure
1500
 DESCRIPTION
1501
    Search for an object in a skip list, according to it's key, returning the
1502
    object itself (for an exact match), or the object with the next lowest
1503
    key that is greater than 'key'
1504
 GLOBAL VARIABLES
1505
 COMMENTS, BUGS, ASSUMPTIONS
1506
 EXAMPLES
1507
 REVISION LOG
1508
--------------------------------------------------------------------------*/
1509
void *
1510
H5SL_greater(H5SL_t *slist, const void *key)
1511
0
{
1512
0
    H5SL_node_t *x;                /* Current node to examine */
1513
0
    uint32_t     hashval   = 0;    /* Hash value for key */
1514
0
    void        *ret_value = NULL; /* Return value */
1515
1516
0
    FUNC_ENTER_NOAPI_NOINIT_NOERR
1517
1518
    /* Check args */
1519
0
    assert(slist);
1520
0
    assert(key);
1521
1522
    /* Check internal consistency */
1523
    /* (Pre-condition) */
1524
1525
    /* Insert item into skip list */
1526
1527
    /* Work through the forward pointers for a node, finding the node at each
1528
     * level that is before the location to insert
1529
     */
1530
0
    x = slist->header;
1531
0
    switch (slist->type) {
1532
0
        case H5SL_TYPE_INT:
1533
0
            H5SL_SEARCH(SCALAR, slist, x, const int, key, -)
1534
0
            break;
1535
1536
0
        case H5SL_TYPE_HADDR:
1537
0
            H5SL_SEARCH(SCALAR, slist, x, const haddr_t, key, -)
1538
0
            break;
1539
1540
0
        case H5SL_TYPE_STR:
1541
0
            H5SL_SEARCH(STRING, slist, x, char *, key, hashval)
1542
0
            break;
1543
1544
0
        case H5SL_TYPE_HSIZE:
1545
0
            H5SL_SEARCH(SCALAR, slist, x, const hsize_t, key, -)
1546
0
            break;
1547
1548
0
        case H5SL_TYPE_UNSIGNED:
1549
0
            H5SL_SEARCH(SCALAR, slist, x, const unsigned, key, -)
1550
0
            break;
1551
1552
0
        case H5SL_TYPE_SIZE:
1553
0
            H5SL_SEARCH(SCALAR, slist, x, const size_t, key, -)
1554
0
            break;
1555
1556
0
        case H5SL_TYPE_OBJ:
1557
0
            H5SL_SEARCH(OBJ, slist, x, const H5_obj_t, key, -)
1558
0
            break;
1559
1560
0
        case H5SL_TYPE_HID:
1561
0
            H5SL_SEARCH(SCALAR, slist, x, const hid_t, key, -)
1562
0
            break;
1563
1564
0
        case H5SL_TYPE_GENERIC:
1565
0
            H5SL_SEARCH(GENERIC, slist, x, const void, key, -)
1566
0
            break;
1567
1568
0
        default:
1569
0
            assert(0 && "Unknown skiplist type!");
1570
0
    }
1571
1572
    /* An exact match for 'key' must not have been found in list, if we get here */
1573
    /* ('x' must be the next node with a key greater than the 'key', or NULL) */
1574
0
    if (x)
1575
0
        ret_value = x->item;
1576
0
    else
1577
0
        ret_value = NULL;
1578
1579
0
done:
1580
0
    FUNC_LEAVE_NOAPI(ret_value)
1581
0
} /* end H5SL_greater() */
1582
1583
/*--------------------------------------------------------------------------
1584
 NAME
1585
    H5SL_find
1586
 PURPOSE
1587
    Search for _node_ in a skip list
1588
 USAGE
1589
    H5SL_node_t *H5SL_node(slist,key)
1590
        H5SL_t *slist;          IN/OUT: Pointer to skip list
1591
        void *key;              IN: Key for item to search for
1592
1593
 RETURNS
1594
    Returns pointer to _node_ matching key on success, NULL on failure
1595
 DESCRIPTION
1596
    Search for an object in a skip list, according to it's key and returns
1597
    the node that the object is attached to
1598
 GLOBAL VARIABLES
1599
 COMMENTS, BUGS, ASSUMPTIONS
1600
    This routine is a useful starting point for next/prev calls
1601
 EXAMPLES
1602
 REVISION LOG
1603
--------------------------------------------------------------------------*/
1604
H5SL_node_t *
1605
H5SL_find(H5SL_t *slist, const void *key)
1606
0
{
1607
0
    H5SL_node_t *x;                /* Current node to examine */
1608
0
    uint32_t     hashval   = 0;    /* Hash value for key */
1609
0
    H5SL_node_t *ret_value = NULL; /* Return value */
1610
1611
0
    FUNC_ENTER_NOAPI_NOINIT_NOERR
1612
1613
    /* Check args */
1614
0
    assert(slist);
1615
0
    assert(key);
1616
1617
    /* Check internal consistency */
1618
    /* (Pre-condition) */
1619
1620
    /* Insert item into skip list */
1621
1622
    /* Work through the forward pointers for a node, finding the node at each
1623
     * level that is before the location to insert
1624
     */
1625
0
    x = slist->header;
1626
0
    switch (slist->type) {
1627
0
        case H5SL_TYPE_INT:
1628
0
            H5SL_FIND(SCALAR, slist, x, const int, key, -)
1629
0
            break;
1630
1631
0
        case H5SL_TYPE_HADDR:
1632
0
            H5SL_FIND(SCALAR, slist, x, const haddr_t, key, -)
1633
0
            break;
1634
1635
0
        case H5SL_TYPE_STR:
1636
0
            H5SL_FIND(STRING, slist, x, char *, key, hashval)
1637
0
            break;
1638
1639
0
        case H5SL_TYPE_HSIZE:
1640
0
            H5SL_FIND(SCALAR, slist, x, const hsize_t, key, -)
1641
0
            break;
1642
1643
0
        case H5SL_TYPE_UNSIGNED:
1644
0
            H5SL_FIND(SCALAR, slist, x, const unsigned, key, -)
1645
0
            break;
1646
1647
0
        case H5SL_TYPE_SIZE:
1648
0
            H5SL_FIND(SCALAR, slist, x, const size_t, key, -)
1649
0
            break;
1650
1651
0
        case H5SL_TYPE_OBJ:
1652
0
            H5SL_FIND(OBJ, slist, x, const H5_obj_t, key, -)
1653
0
            break;
1654
1655
0
        case H5SL_TYPE_HID:
1656
0
            H5SL_FIND(SCALAR, slist, x, const hid_t, key, -)
1657
0
            break;
1658
1659
0
        case H5SL_TYPE_GENERIC:
1660
0
            H5SL_FIND(GENERIC, slist, x, const void, key, -)
1661
0
            break;
1662
1663
0
        default:
1664
0
            assert(0 && "Unknown skiplist type!");
1665
0
    }
1666
1667
    /* 'key' must not have been found in list, if we get here */
1668
0
    ret_value = NULL;
1669
1670
0
done:
1671
0
    FUNC_LEAVE_NOAPI(ret_value)
1672
0
} /* end H5SL_find() */
1673
1674
/*--------------------------------------------------------------------------
1675
 NAME
1676
    H5SL_below
1677
 PURPOSE
1678
    Search for _node_ in a skip list whose object is less than or equal to 'key'
1679
 USAGE
1680
    H5SL_node_t *H5SL_below(slist, key)
1681
        H5SL_t *slist;          IN/OUT: Pointer to skip list
1682
        void *key;              IN: Key for item to search for
1683
1684
 RETURNS
1685
    Returns pointer to _node_ who key is less than or equal to 'key' on success,
1686
        NULL on failure
1687
 DESCRIPTION
1688
    Search for a node with an object in a skip list, according to it's key,
1689
    returning the node itself (for an exact match), or the node with the next
1690
    highest key that is less than 'key'
1691
 GLOBAL VARIABLES
1692
 COMMENTS, BUGS, ASSUMPTIONS
1693
 EXAMPLES
1694
 REVISION LOG
1695
--------------------------------------------------------------------------*/
1696
H5SL_node_t *
1697
H5SL_below(H5SL_t *slist, const void *key)
1698
0
{
1699
0
    H5SL_node_t *x;                /* Current node to examine */
1700
0
    uint32_t     hashval   = 0;    /* Hash value for key */
1701
0
    H5SL_node_t *ret_value = NULL; /* Return value */
1702
1703
0
    FUNC_ENTER_NOAPI_NOINIT_NOERR
1704
1705
    /* Check args */
1706
0
    assert(slist);
1707
0
    assert(key);
1708
1709
    /* Check internal consistency */
1710
    /* (Pre-condition) */
1711
1712
    /* Insert item into skip list */
1713
1714
    /* Work through the forward pointers for a node, finding the node at each
1715
     * level that is before the location to insert
1716
     */
1717
0
    x = slist->header;
1718
0
    switch (slist->type) {
1719
0
        case H5SL_TYPE_INT:
1720
0
            H5SL_FIND(SCALAR, slist, x, const int, key, -)
1721
0
            break;
1722
1723
0
        case H5SL_TYPE_HADDR:
1724
0
            H5SL_FIND(SCALAR, slist, x, const haddr_t, key, -)
1725
0
            break;
1726
1727
0
        case H5SL_TYPE_STR:
1728
0
            H5SL_FIND(STRING, slist, x, char *, key, hashval)
1729
0
            break;
1730
1731
0
        case H5SL_TYPE_HSIZE:
1732
0
            H5SL_FIND(SCALAR, slist, x, const hsize_t, key, -)
1733
0
            break;
1734
1735
0
        case H5SL_TYPE_UNSIGNED:
1736
0
            H5SL_FIND(SCALAR, slist, x, const unsigned, key, -)
1737
0
            break;
1738
1739
0
        case H5SL_TYPE_SIZE:
1740
0
            H5SL_FIND(SCALAR, slist, x, const size_t, key, -)
1741
0
            break;
1742
1743
0
        case H5SL_TYPE_OBJ:
1744
0
            H5SL_FIND(OBJ, slist, x, const H5_obj_t, key, -)
1745
0
            break;
1746
1747
0
        case H5SL_TYPE_HID:
1748
0
            H5SL_FIND(SCALAR, slist, x, const hid_t, key, -)
1749
0
            break;
1750
1751
0
        case H5SL_TYPE_GENERIC:
1752
0
            H5SL_FIND(GENERIC, slist, x, const void, key, -)
1753
0
            break;
1754
1755
0
        default:
1756
0
            assert(0 && "Unknown skiplist type!");
1757
0
    }
1758
1759
    /* An exact match for 'key' must not have been found in list, if we get here */
1760
    /* Check for a node with a key that is less than the given 'key' */
1761
0
    if (NULL == x) {
1762
        /* Check for walking off the list */
1763
0
        if (slist->last != slist->header)
1764
0
            ret_value = slist->last;
1765
0
        else
1766
0
            ret_value = NULL;
1767
0
    }
1768
0
    else {
1769
0
        if (x->backward != slist->header)
1770
0
            ret_value = x->backward;
1771
0
        else
1772
0
            ret_value = NULL;
1773
0
    }
1774
1775
0
done:
1776
0
    FUNC_LEAVE_NOAPI(ret_value)
1777
0
} /* end H5SL_below() */
1778
1779
/*--------------------------------------------------------------------------
1780
 NAME
1781
    H5SL_above
1782
 PURPOSE
1783
    Search for _node_ in a skip list whose object is greater than or equal to 'key'
1784
 USAGE
1785
    H5SL_node_t *H5SL_above(slist, key)
1786
        H5SL_t *slist;          IN/OUT: Pointer to skip list
1787
        void *key;              IN: Key for item to search for
1788
1789
 RETURNS
1790
    Returns pointer to _node_ with object that has a key is greater than or
1791
        equal to 'key' on success, NULL on failure
1792
 DESCRIPTION
1793
    Search for a node with an object in a skip list, according to it's key,
1794
    returning the node itself (for an exact match), or the node with the next
1795
    lowest key that is greater than 'key'
1796
 GLOBAL VARIABLES
1797
 COMMENTS, BUGS, ASSUMPTIONS
1798
 EXAMPLES
1799
 REVISION LOG
1800
--------------------------------------------------------------------------*/
1801
H5SL_node_t *
1802
H5SL_above(H5SL_t *slist, const void *key)
1803
0
{
1804
0
    H5SL_node_t *x;                /* Current node to examine */
1805
0
    uint32_t     hashval   = 0;    /* Hash value for key */
1806
0
    H5SL_node_t *ret_value = NULL; /* Return value */
1807
1808
0
    FUNC_ENTER_NOAPI_NOINIT_NOERR
1809
1810
    /* Check args */
1811
0
    assert(slist);
1812
0
    assert(key);
1813
1814
    /* Check internal consistency */
1815
    /* (Pre-condition) */
1816
1817
    /* Insert item into skip list */
1818
1819
    /* Work through the forward pointers for a node, finding the node at each
1820
     * level that is before the location to insert
1821
     */
1822
0
    x = slist->header;
1823
0
    switch (slist->type) {
1824
0
        case H5SL_TYPE_INT:
1825
0
            H5SL_FIND(SCALAR, slist, x, const int, key, -)
1826
0
            break;
1827
1828
0
        case H5SL_TYPE_HADDR:
1829
0
            H5SL_FIND(SCALAR, slist, x, const haddr_t, key, -)
1830
0
            break;
1831
1832
0
        case H5SL_TYPE_STR:
1833
0
            H5SL_FIND(STRING, slist, x, char *, key, hashval)
1834
0
            break;
1835
1836
0
        case H5SL_TYPE_HSIZE:
1837
0
            H5SL_FIND(SCALAR, slist, x, const hsize_t, key, -)
1838
0
            break;
1839
1840
0
        case H5SL_TYPE_UNSIGNED:
1841
0
            H5SL_FIND(SCALAR, slist, x, const unsigned, key, -)
1842
0
            break;
1843
1844
0
        case H5SL_TYPE_SIZE:
1845
0
            H5SL_FIND(SCALAR, slist, x, const size_t, key, -)
1846
0
            break;
1847
1848
0
        case H5SL_TYPE_OBJ:
1849
0
            H5SL_FIND(OBJ, slist, x, const H5_obj_t, key, -)
1850
0
            break;
1851
1852
0
        case H5SL_TYPE_HID:
1853
0
            H5SL_FIND(SCALAR, slist, x, const hid_t, key, -)
1854
0
            break;
1855
1856
0
        case H5SL_TYPE_GENERIC:
1857
0
            H5SL_FIND(GENERIC, slist, x, const void, key, -)
1858
0
            break;
1859
1860
0
        default:
1861
0
            assert(0 && "Unknown skiplist type!");
1862
0
    }
1863
1864
    /* An exact match for 'key' must not have been found in list, if we get here */
1865
    /* ('x' must be the next node with a key greater than the 'key', or NULL) */
1866
0
    if (x)
1867
0
        ret_value = x;
1868
0
    else
1869
0
        ret_value = NULL;
1870
1871
0
done:
1872
0
    FUNC_LEAVE_NOAPI(ret_value)
1873
0
} /* end H5SL_above() */
1874
1875
/*--------------------------------------------------------------------------
1876
 NAME
1877
    H5SL_first
1878
 PURPOSE
1879
    Gets a pointer to the first node in a skip list
1880
 USAGE
1881
    H5SL_node_t *H5SL_first(slist)
1882
        H5SL_t *slist;          IN: Pointer to skip list
1883
1884
 RETURNS
1885
    Returns pointer to first node in skip list on success, NULL on failure.
1886
 DESCRIPTION
1887
    Retrieves a pointer to the first node in a skip list, for iterating over
1888
    the list.
1889
 GLOBAL VARIABLES
1890
 COMMENTS, BUGS, ASSUMPTIONS
1891
 EXAMPLES
1892
 REVISION LOG
1893
--------------------------------------------------------------------------*/
1894
H5SL_node_t *
1895
H5SL_first(H5SL_t *slist)
1896
256k
{
1897
256k
    FUNC_ENTER_NOAPI_NOINIT_NOERR
1898
1899
    /* Check args */
1900
256k
    assert(slist);
1901
1902
    /* Check internal consistency */
1903
    /* (Pre-condition) */
1904
1905
256k
    FUNC_LEAVE_NOAPI(slist->header->forward[0])
1906
256k
} /* end H5SL_first() */
1907
1908
/*--------------------------------------------------------------------------
1909
 NAME
1910
    H5SL_next
1911
 PURPOSE
1912
    Gets a pointer to the next node in a skip list
1913
 USAGE
1914
    H5SL_node_t *H5SL_next(slist_node)
1915
        H5SL_node_t *slist_node;          IN: Pointer to skip list node
1916
1917
 RETURNS
1918
    Returns pointer to node after slist_node in skip list on success, NULL on failure.
1919
 DESCRIPTION
1920
    Retrieves a pointer to the next node in a skip list, for iterating over
1921
    the list.
1922
 GLOBAL VARIABLES
1923
 COMMENTS, BUGS, ASSUMPTIONS
1924
 EXAMPLES
1925
 REVISION LOG
1926
--------------------------------------------------------------------------*/
1927
H5SL_node_t *
1928
H5SL_next(H5SL_node_t *slist_node)
1929
1.26M
{
1930
1.26M
    FUNC_ENTER_NOAPI_NOINIT_NOERR
1931
1932
    /* Check args */
1933
1.26M
    assert(slist_node);
1934
1935
    /* Check internal consistency */
1936
    /* (Pre-condition) */
1937
1938
1.26M
    FUNC_LEAVE_NOAPI(slist_node->forward[0])
1939
1.26M
} /* end H5SL_next() */
1940
1941
/*--------------------------------------------------------------------------
1942
 NAME
1943
    H5SL_prev
1944
 PURPOSE
1945
    Gets a pointer to the previous node in a skip list
1946
 USAGE
1947
    H5SL_node_t *H5SL_prev(slist_node)
1948
        H5SL_node_t *slist_node;          IN: Pointer to skip list node
1949
1950
 RETURNS
1951
    Returns pointer to node before slist_node in skip list on success, NULL on failure.
1952
 DESCRIPTION
1953
    Retrieves a pointer to the previous node in a skip list, for iterating over
1954
    the list.
1955
 GLOBAL VARIABLES
1956
 COMMENTS, BUGS, ASSUMPTIONS
1957
 EXAMPLES
1958
 REVISION LOG
1959
--------------------------------------------------------------------------*/
1960
H5SL_node_t *
1961
H5SL_prev(H5SL_node_t *slist_node)
1962
0
{
1963
0
    FUNC_ENTER_NOAPI_NOINIT_NOERR
1964
1965
    /* Check args */
1966
0
    assert(slist_node);
1967
1968
    /* Check internal consistency */
1969
    /* (Pre-condition) */
1970
1971
    /* Walk backward, detecting the header node (which has it's key set to NULL) */
1972
0
    FUNC_LEAVE_NOAPI(slist_node->backward->key == NULL ? NULL : slist_node->backward)
1973
0
} /* end H5SL_prev() */
1974
1975
/*--------------------------------------------------------------------------
1976
 NAME
1977
    H5SL_last
1978
 PURPOSE
1979
    Gets a pointer to the last node in a skip list
1980
 USAGE
1981
    H5SL_node_t *H5SL_last(slist)
1982
        H5SL_t *slist;          IN: Pointer to skip list
1983
1984
 RETURNS
1985
    Returns pointer to last node in skip list on success, NULL on failure.
1986
 DESCRIPTION
1987
    Retrieves a pointer to the last node in a skip list, for iterating over
1988
    the list.
1989
 GLOBAL VARIABLES
1990
 COMMENTS, BUGS, ASSUMPTIONS
1991
 EXAMPLES
1992
 REVISION LOG
1993
--------------------------------------------------------------------------*/
1994
H5SL_node_t *
1995
H5SL_last(H5SL_t *slist)
1996
0
{
1997
0
    FUNC_ENTER_NOAPI_NOINIT_NOERR
1998
1999
    /* Check args */
2000
0
    assert(slist);
2001
2002
    /* Check internal consistency */
2003
    /* (Pre-condition) */
2004
2005
    /* Find last node, avoiding the header node */
2006
0
    FUNC_LEAVE_NOAPI(slist->last == slist->header ? NULL : slist->last)
2007
0
} /* end H5SL_last() */
2008
2009
/*--------------------------------------------------------------------------
2010
 NAME
2011
    H5SL_item
2012
 PURPOSE
2013
    Gets pointer to the 'item' for a skip list node
2014
 USAGE
2015
    void *H5SL_item(slist_node)
2016
        H5SL_node_t *slist_node;          IN: Pointer to skip list node
2017
2018
 RETURNS
2019
    Returns pointer to node 'item' on success, NULL on failure.
2020
 DESCRIPTION
2021
    Retrieves a node's 'item'
2022
 GLOBAL VARIABLES
2023
 COMMENTS, BUGS, ASSUMPTIONS
2024
 EXAMPLES
2025
 REVISION LOG
2026
--------------------------------------------------------------------------*/
2027
void *
2028
H5SL_item(H5SL_node_t *slist_node)
2029
1.26M
{
2030
1.26M
    FUNC_ENTER_NOAPI_NOINIT_NOERR
2031
2032
    /* Check args */
2033
1.26M
    assert(slist_node);
2034
2035
    /* Check internal consistency */
2036
    /* (Pre-condition) */
2037
2038
1.26M
    FUNC_LEAVE_NOAPI(slist_node->item)
2039
1.26M
} /* end H5SL_item() */
2040
2041
/*--------------------------------------------------------------------------
2042
 NAME
2043
    H5SL_iterate
2044
 PURPOSE
2045
    Iterate over all nodes in a skip list
2046
 USAGE
2047
    herr_t H5SL_iterate(slist, op, op_data)
2048
        H5SL_t *slist;          IN/OUT: Pointer to skip list to iterate over
2049
        H5SL_operator_t op;     IN: Callback function for iteration
2050
        void *op_data;          IN/OUT: Pointer to application data for callback
2051
2052
 RETURNS
2053
    Returns a negative value if something is wrong, the return
2054
        value of the last operator if it was non-zero, or zero if all
2055
        nodes were processed.
2056
 DESCRIPTION
2057
    Iterate over all the nodes in a skip list, calling an application callback
2058
    with the item, key and any operator data.
2059
2060
    The operator callback receives a pointer to the item and key for the list
2061
    being iterated over ('mesg'), and the pointer to the operator data passed
2062
    in to H5SL_iterate ('op_data').  The return values from an operator are:
2063
        A. Zero causes the iterator to continue, returning zero when all
2064
            nodes of that type have been processed.
2065
        B. Positive causes the iterator to immediately return that positive
2066
            value, indicating short-circuit success.
2067
        C. Negative causes the iterator to immediately return that value,
2068
            indicating failure.
2069
 GLOBAL VARIABLES
2070
 COMMENTS, BUGS, ASSUMPTIONS
2071
 EXAMPLES
2072
 REVISION LOG
2073
--------------------------------------------------------------------------*/
2074
herr_t
2075
H5SL_iterate(H5SL_t *slist, H5SL_operator_t op, void *op_data)
2076
0
{
2077
0
    H5SL_node_t *node;          /* Pointer to current skip list node */
2078
0
    H5SL_node_t *next;          /* Pointer to next skip list node */
2079
0
    herr_t       ret_value = 0; /* Return value */
2080
2081
0
    FUNC_ENTER_NOAPI_NOINIT_NOERR
2082
2083
    /* Check args */
2084
0
    assert(slist);
2085
2086
    /* Check internal consistency */
2087
    /* (Pre-condition) */
2088
2089
    /* Free skip list nodes */
2090
0
    node = slist->header->forward[0];
2091
0
    while (node != NULL) {
2092
        /* Protect against the node being deleted by the callback */
2093
0
        next = node->forward[0];
2094
2095
        /* Call the iterator callback
2096
         *
2097
         * Ignoring const here is fine as we only need the value to be const
2098
         * with respect to the list code, which should never modify the
2099
         * elements. The library code that is making use of the skip list
2100
         * container can do what it likes with the elements.
2101
         */
2102
0
        H5_WARN_CAST_AWAY_CONST_OFF
2103
0
        if ((ret_value = (op)(node->item, (void *)node->key, op_data)) != 0)
2104
0
            break;
2105
0
        H5_WARN_CAST_AWAY_CONST_ON
2106
2107
        /* Advance to next node */
2108
0
        node = next;
2109
0
    }
2110
2111
0
    FUNC_LEAVE_NOAPI(ret_value)
2112
0
} /* end H5SL_iterate() */
2113
2114
/*--------------------------------------------------------------------------
2115
 NAME
2116
    H5SL_release
2117
 PURPOSE
2118
    Release all nodes from a skip list
2119
 USAGE
2120
    herr_t H5SL_release(slist)
2121
        H5SL_t *slist;            IN/OUT: Pointer to skip list to release nodes
2122
2123
 RETURNS
2124
    Returns non-negative on success, negative on failure.
2125
 DESCRIPTION
2126
    Release all the nodes in a skip list.  Any objects left in the skip list
2127
    nodes are not deallocated.
2128
 GLOBAL VARIABLES
2129
 COMMENTS, BUGS, ASSUMPTIONS
2130
    The skip list itself is still valid, it just has all its nodes removed.
2131
 EXAMPLES
2132
 REVISION LOG
2133
--------------------------------------------------------------------------*/
2134
herr_t
2135
H5SL_release(H5SL_t *slist)
2136
0
{
2137
0
    herr_t ret_value = SUCCEED;
2138
2139
0
    FUNC_ENTER_NOAPI(FAIL)
2140
2141
    /* Check args */
2142
0
    assert(slist);
2143
2144
    /* Check internal consistency */
2145
    /* (Pre-condition) */
2146
2147
    /* Free skip list nodes */
2148
0
    if (H5SL__release_common(slist, NULL, NULL) < 0)
2149
0
        HGOTO_ERROR(H5E_SLIST, H5E_CANTFREE, FAIL, "can't release skip list nodes");
2150
2151
0
done:
2152
0
    FUNC_LEAVE_NOAPI(ret_value)
2153
0
} /* end H5SL_release() */
2154
2155
/*--------------------------------------------------------------------------
2156
 NAME
2157
    H5SL_free
2158
 PURPOSE
2159
    Release all nodes from a skip list, freeing all nodes
2160
 USAGE
2161
    herr_t H5SL_free(slist,op,op_data)
2162
        H5SL_t *slist;          IN/OUT: Pointer to skip list to release nodes
2163
        H5SL_operator_t op;     IN: Callback function to free item & key
2164
        void *op_data;          IN/OUT: Pointer to application data for callback
2165
2166
 RETURNS
2167
    Returns non-negative on success, negative on failure.
2168
 DESCRIPTION
2169
    Release all the nodes in a skip list.  Any objects left in
2170
    the skip list have the 'op' routine called for each.
2171
 GLOBAL VARIABLES
2172
 COMMENTS, BUGS, ASSUMPTIONS
2173
    The skip list itself is still valid, it just has all its nodes removed.
2174
2175
    The return value from the 'op' routine is ignored.
2176
2177
    This routine is essentially a combination of iterating over all the nodes
2178
    (where the iterator callback is supposed to free the items and/or keys)
2179
    followed by a call to H5SL_release().
2180
 EXAMPLES
2181
 REVISION LOG
2182
--------------------------------------------------------------------------*/
2183
herr_t
2184
H5SL_free(H5SL_t *slist, H5SL_operator_t op, void *op_data)
2185
0
{
2186
0
    herr_t ret_value = SUCCEED;
2187
2188
0
    FUNC_ENTER_NOAPI(FAIL)
2189
2190
    /* Check args */
2191
0
    assert(slist);
2192
2193
    /* Check internal consistency */
2194
    /* (Pre-condition) */
2195
2196
    /* Free skip list nodes */
2197
0
    if (H5SL__release_common(slist, op, op_data) < 0)
2198
0
        HGOTO_ERROR(H5E_SLIST, H5E_CANTFREE, FAIL, "can't release skip list nodes");
2199
2200
0
done:
2201
0
    FUNC_LEAVE_NOAPI(ret_value)
2202
0
} /* end H5SL_free() */
2203
2204
/*--------------------------------------------------------------------------
2205
 NAME
2206
    H5SL_destroy
2207
 PURPOSE
2208
    Close a skip list, deallocating it and freeing all its nodes.
2209
 USAGE
2210
    herr_t H5SL_destroy(slist,op,opdata)
2211
        H5SL_t *slist;          IN/OUT: Pointer to skip list to close
2212
        H5SL_operator_t op;     IN: Callback function to free item & key
2213
        void *op_data;          IN/OUT: Pointer to application data for callback
2214
2215
 RETURNS
2216
    Returns non-negative on success, negative on failure.
2217
 DESCRIPTION
2218
    Close a skip list, freeing all internal information.  Any objects left in
2219
    the skip list have the 'op' routine called for each.
2220
 GLOBAL VARIABLES
2221
 COMMENTS, BUGS, ASSUMPTIONS
2222
    The return value from the 'op' routine is ignored.
2223
2224
    This routine is essentially a combination of iterating over all the nodes
2225
    (where the iterator callback is supposed to free the items and/or keys)
2226
    followed by a call to H5SL_close().
2227
 EXAMPLES
2228
 REVISION LOG
2229
--------------------------------------------------------------------------*/
2230
herr_t
2231
H5SL_destroy(H5SL_t *slist, H5SL_operator_t op, void *op_data)
2232
90.8k
{
2233
90.8k
    herr_t ret_value = SUCCEED; /* Return value */
2234
2235
90.8k
    FUNC_ENTER_NOAPI_NOINIT
2236
2237
    /* Check args */
2238
90.8k
    assert(slist);
2239
2240
    /* Check internal consistency */
2241
    /* (Pre-condition) */
2242
2243
    /* Close skip list */
2244
90.8k
    if (H5SL__close_common(slist, op, op_data) < 0)
2245
0
        HGOTO_ERROR(H5E_SLIST, H5E_CANTCLOSEOBJ, FAIL, "can't close skip list");
2246
2247
90.8k
done:
2248
90.8k
    FUNC_LEAVE_NOAPI(ret_value)
2249
90.8k
} /* end H5SL_destroy() */
2250
2251
/*--------------------------------------------------------------------------
2252
 NAME
2253
    H5SL_close
2254
 PURPOSE
2255
    Close a skip list, deallocating it.
2256
 USAGE
2257
    herr_t H5SL_close(slist)
2258
        H5SL_t *slist;            IN/OUT: Pointer to skip list to close
2259
2260
 RETURNS
2261
    Returns non-negative on success, negative on failure.
2262
 DESCRIPTION
2263
    Close a skip list, freeing all internal information.  Any objects left in
2264
    the skip list are not deallocated.
2265
 GLOBAL VARIABLES
2266
 COMMENTS, BUGS, ASSUMPTIONS
2267
 EXAMPLES
2268
 REVISION LOG
2269
--------------------------------------------------------------------------*/
2270
herr_t
2271
H5SL_close(H5SL_t *slist)
2272
91.7k
{
2273
91.7k
    herr_t ret_value = SUCCEED; /* Return value */
2274
2275
91.7k
    FUNC_ENTER_NOAPI_NOINIT
2276
2277
    /* Check args */
2278
91.7k
    assert(slist);
2279
2280
    /* Check internal consistency */
2281
    /* (Pre-condition) */
2282
2283
    /* Close skip list */
2284
91.7k
    if (H5SL__close_common(slist, NULL, NULL) < 0)
2285
0
        HGOTO_ERROR(H5E_SLIST, H5E_CANTCLOSEOBJ, FAIL, "can't close skip list");
2286
2287
91.7k
done:
2288
91.7k
    FUNC_LEAVE_NOAPI(ret_value)
2289
91.7k
} /* end H5SL_close() */