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