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