midl.h revision 1.1 1 /* $NetBSD: midl.h,v 1.1 2014/05/28 09:58:42 tron Exp $ */
2
3 /** @file midl.h
4 * @brief mdb ID List header file.
5 *
6 * This file was originally part of back-bdb but has been
7 * modified for use in libmdb. Most of the macros defined
8 * in this file are unused, just left over from the original.
9 *
10 * This file is only used internally in libmdb and its definitions
11 * are not exposed publicly.
12 */
13 /* $OpenLDAP$ */
14 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
15 *
16 * Copyright 2000-2014 The OpenLDAP Foundation.
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted only as authorized by the OpenLDAP
21 * Public License.
22 *
23 * A copy of this license is available in the file LICENSE in the
24 * top-level directory of the distribution or, alternatively, at
25 * <http://www.OpenLDAP.org/license.html>.
26 */
27
28 #ifndef _MDB_MIDL_H_
29 #define _MDB_MIDL_H_
30
31 #include <stddef.h>
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /** @defgroup internal MDB Internals
38 * @{
39 */
40
41 /** @defgroup idls ID List Management
42 * @{
43 */
44 /** A generic unsigned ID number. These were entryIDs in back-bdb.
45 * Preferably it should have the same size as a pointer.
46 */
47 typedef size_t MDB_ID;
48
49 /** An IDL is an ID List, a sorted array of IDs. The first
50 * element of the array is a counter for how many actual
51 * IDs are in the list. In the original back-bdb code, IDLs are
52 * sorted in ascending order. For libmdb IDLs are sorted in
53 * descending order.
54 */
55 typedef MDB_ID *MDB_IDL;
56
57 /* IDL sizes - likely should be even bigger
58 * limiting factors: sizeof(ID), thread stack size
59 */
60 #define MDB_IDL_LOGN 16 /* DB_SIZE is 2^16, UM_SIZE is 2^17 */
61 #define MDB_IDL_DB_SIZE (1<<MDB_IDL_LOGN)
62 #define MDB_IDL_UM_SIZE (1<<(MDB_IDL_LOGN+1))
63
64 #define MDB_IDL_DB_MAX (MDB_IDL_DB_SIZE-1)
65 #define MDB_IDL_UM_MAX (MDB_IDL_UM_SIZE-1)
66
67 #define MDB_IDL_SIZEOF(ids) (((ids)[0]+1) * sizeof(MDB_ID))
68 #define MDB_IDL_IS_ZERO(ids) ( (ids)[0] == 0 )
69 #define MDB_IDL_CPY( dst, src ) (memcpy( dst, src, MDB_IDL_SIZEOF( src ) ))
70 #define MDB_IDL_FIRST( ids ) ( (ids)[1] )
71 #define MDB_IDL_LAST( ids ) ( (ids)[(ids)[0]] )
72
73 /** Append ID to IDL. The IDL must be big enough. */
74 #define mdb_midl_xappend(idl, id) do { \
75 MDB_ID *xidl = (idl), xlen = ++(xidl[0]); \
76 xidl[xlen] = (id); \
77 } while (0)
78
79 /** Search for an ID in an IDL.
80 * @param[in] ids The IDL to search.
81 * @param[in] id The ID to search for.
82 * @return The index of the first ID greater than or equal to \b id.
83 */
84 unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id );
85
86 /** Allocate an IDL.
87 * Allocates memory for an IDL of the given size.
88 * @return IDL on success, NULL on failure.
89 */
90 MDB_IDL mdb_midl_alloc(int num);
91
92 /** Free an IDL.
93 * @param[in] ids The IDL to free.
94 */
95 void mdb_midl_free(MDB_IDL ids);
96
97 /** Shrink an IDL.
98 * Return the IDL to the default size if it has grown larger.
99 * @param[in,out] idp Address of the IDL to shrink.
100 * @return 0 on no change, non-zero if shrunk.
101 */
102 int mdb_midl_shrink(MDB_IDL *idp);
103
104 /** Make room for num additional elements in an IDL.
105 * @param[in,out] idp Address of the IDL.
106 * @param[in] num Number of elements to make room for.
107 * @return 0 on success, ENOMEM on failure.
108 */
109 int mdb_midl_need(MDB_IDL *idp, unsigned num);
110
111 /** Append an ID onto an IDL.
112 * @param[in,out] idp Address of the IDL to append to.
113 * @param[in] id The ID to append.
114 * @return 0 on success, ENOMEM if the IDL is too large.
115 */
116 int mdb_midl_append( MDB_IDL *idp, MDB_ID id );
117
118 /** Append an IDL onto an IDL.
119 * @param[in,out] idp Address of the IDL to append to.
120 * @param[in] app The IDL to append.
121 * @return 0 on success, ENOMEM if the IDL is too large.
122 */
123 int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app );
124
125 /** Append an ID range onto an IDL.
126 * @param[in,out] idp Address of the IDL to append to.
127 * @param[in] id The lowest ID to append.
128 * @param[in] n Number of IDs to append.
129 * @return 0 on success, ENOMEM if the IDL is too large.
130 */
131 int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n );
132
133 /** Sort an IDL.
134 * @param[in,out] ids The IDL to sort.
135 */
136 void mdb_midl_sort( MDB_IDL ids );
137
138 /** An ID2 is an ID/pointer pair.
139 */
140 typedef struct MDB_ID2 {
141 MDB_ID mid; /**< The ID */
142 void *mptr; /**< The pointer */
143 } MDB_ID2;
144
145 /** An ID2L is an ID2 List, a sorted array of ID2s.
146 * The first element's \b mid member is a count of how many actual
147 * elements are in the array. The \b mptr member of the first element is unused.
148 * The array is sorted in ascending order by \b mid.
149 */
150 typedef MDB_ID2 *MDB_ID2L;
151
152 /** Search for an ID in an ID2L.
153 * @param[in] ids The ID2L to search.
154 * @param[in] id The ID to search for.
155 * @return The index of the first ID2 whose \b mid member is greater than or equal to \b id.
156 */
157 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id );
158
159
160 /** Insert an ID2 into a ID2L.
161 * @param[in,out] ids The ID2L to insert into.
162 * @param[in] id The ID2 to insert.
163 * @return 0 on success, -1 if the ID was already present in the ID2L.
164 */
165 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id );
166
167 /** Append an ID2 into a ID2L.
168 * @param[in,out] ids The ID2L to append into.
169 * @param[in] id The ID2 to append.
170 * @return 0 on success, -2 if the ID2L is too big.
171 */
172 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id );
173
174 /** @} */
175 /** @} */
176 #ifdef __cplusplus
177 }
178 #endif
179 #endif /* _MDB_MIDL_H_ */
180