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