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