2 Copyright (c) 2003, Keir Fraser All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution. Neither the name of the Keir Fraser
14 * nor the names of its contributors may be used to endorse or
15 * promote products derived from this software without specific
16 * prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 typedef unsigned long setkey_t
;
36 typedef void *setval_t
;
39 #ifdef __SET_IMPLEMENTATION__
41 /*************************************
42 * INTERNAL DEFINITIONS
45 /* Fine for 2^NUM_LEVELS nodes. */
49 /* Internal key values with special meanings. */
50 #define INVALID_FIELD (0) /* Uninitialised field value. */
51 #define SENTINEL_KEYMIN ( 1UL) /* Key value of first dummy node. */
52 #define SENTINEL_KEYMAX (~0UL) /* Key value of last dummy node. */
56 * Used internally be set access functions, so that callers can use
57 * key values 0 and 1, without knowing these have special meanings.
59 #define CALLER_TO_INTERNAL_KEY(_k) ((_k) + 2)
63 * SUPPORT FOR WEAK ORDERING OF MEMORY ACCESSES
68 /* Read field @_f into variable @_x. */
69 #define READ_FIELD(_x,_f) \
72 if ( (_x) == INVALID_FIELD ) { RMB(); (_x) = (_f); } \
73 assert((_x) != INVALID_FIELD); \
78 /* Read field @_f into variable @_x. */
79 #define READ_FIELD(_x,_f) ((_x) = (_f))
86 /*************************************
91 * Key range accepted by set functions.
92 * We lose three values (conveniently at top end of key space).
93 * - Known invalid value to which all fields are initialised.
94 * - Sentinel key values for up to two dummy nodes.
97 #define KEY_MAX ((~0U) - 3)
99 typedef void set_t
; /* opaque */
101 void _init_set_subsystem(void);
104 * Allocate an empty set.
106 set_t
*set_alloc(void);
109 * Add mapping (@k -> @v) into set @s. Return previous mapped value if
110 * one existed, or NULL if no previous mapping for @k existed.
112 * If @overwrite is FALSE, then if a mapping already exists it is not
113 * modified, and the existing value is returned unchanged. It is possible
114 * to see if the value was changed by observing if the return value is NULL.
116 setval_t
set_update(set_t
*s
, setkey_t k
, setval_t v
, int overwrite
);
119 * Remove mapping for key @k from set @s. Return value associated with
120 * removed mapping, or NULL is there was no mapping to delete.
122 setval_t
set_remove(set_t
*s
, setkey_t k
);
125 * Look up mapping for key @k in set @s. Return value if found, else NULL.
127 setval_t
set_lookup(set_t
*s
, setkey_t k
);
129 #endif /* __SET_IMPLEMENTATION__ */
132 #endif /* __SET_H__ */