]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/midl.h
Add mdb_dump, update copyrights
[openldap] / libraries / liblmdb / midl.h
1 /**     @file midl.h
2  *      @brief mdb 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-2014 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
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /** @defgroup internal  MDB Internals
36  *      @{
37  */
38
39 /** @defgroup idls      ID List Management
40  *      @{
41  */
42         /** A generic unsigned ID number. These were entryIDs in back-bdb.
43          *      Preferably it should have the same size as a pointer.
44          */
45 typedef size_t MDB_ID;
46
47         /** An IDL is an ID List, a sorted array of IDs. The first
48          * element of the array is a counter for how many actual
49          * IDs are in the list. In the original back-bdb code, IDLs are
50          * sorted in ascending order. For libmdb IDLs are sorted in
51          * descending order.
52          */
53 typedef MDB_ID *MDB_IDL;
54
55 /* IDL sizes - likely should be even bigger
56  *   limiting factors: sizeof(ID), thread stack size
57  */
58 #define MDB_IDL_LOGN    16      /* DB_SIZE is 2^16, UM_SIZE is 2^17 */
59 #define MDB_IDL_DB_SIZE         (1<<MDB_IDL_LOGN)
60 #define MDB_IDL_UM_SIZE         (1<<(MDB_IDL_LOGN+1))
61
62 #define MDB_IDL_DB_MAX          (MDB_IDL_DB_SIZE-1)
63 #define MDB_IDL_UM_MAX          (MDB_IDL_UM_SIZE-1)
64
65 #define MDB_IDL_SIZEOF(ids)             (((ids)[0]+1) * sizeof(MDB_ID))
66 #define MDB_IDL_IS_ZERO(ids) ( (ids)[0] == 0 )
67 #define MDB_IDL_CPY( dst, src ) (memcpy( dst, src, MDB_IDL_SIZEOF( src ) ))
68 #define MDB_IDL_FIRST( ids )    ( (ids)[1] )
69 #define MDB_IDL_LAST( ids )             ( (ids)[(ids)[0]] )
70
71         /** Append ID to IDL. The IDL must be big enough. */
72 #define mdb_midl_xappend(idl, id) do { \
73                 MDB_ID *xidl = (idl), xlen = ++(xidl[0]); \
74                 xidl[xlen] = (id); \
75         } while (0)
76
77         /** Search for an ID in an IDL.
78          * @param[in] ids       The IDL to search.
79          * @param[in] id        The ID to search for.
80          * @return      The index of the first ID greater than or equal to \b id.
81          */
82 unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id );
83
84         /** Allocate an IDL.
85          * Allocates memory for an IDL of the given size.
86          * @return      IDL on success, NULL on failure.
87          */
88 MDB_IDL mdb_midl_alloc(int num);
89
90         /** Free an IDL.
91          * @param[in] ids       The IDL to free.
92          */
93 void mdb_midl_free(MDB_IDL ids);
94
95         /** Shrink an IDL.
96          * Return the IDL to the default size if it has grown larger.
97          * @param[in,out] idp   Address of the IDL to shrink.
98          * @return      0 on no change, non-zero if shrunk.
99          */
100 int mdb_midl_shrink(MDB_IDL *idp);
101
102         /** Make room for num additional elements in an IDL.
103          * @param[in,out] idp   Address of the IDL.
104          * @param[in] num       Number of elements to make room for.
105          * @return      0 on success, ENOMEM on failure.
106          */
107 int mdb_midl_need(MDB_IDL *idp, unsigned num);
108
109         /** Append an ID onto an IDL.
110          * @param[in,out] idp   Address of the IDL to append to.
111          * @param[in] id        The ID to append.
112          * @return      0 on success, ENOMEM if the IDL is too large.
113          */
114 int mdb_midl_append( MDB_IDL *idp, MDB_ID id );
115
116         /** Append an IDL onto an IDL.
117          * @param[in,out] idp   Address of the IDL to append to.
118          * @param[in] app       The IDL to append.
119          * @return      0 on success, ENOMEM if the IDL is too large.
120          */
121 int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app );
122
123         /** Append an ID range onto an IDL.
124          * @param[in,out] idp   Address of the IDL to append to.
125          * @param[in] id        The lowest ID to append.
126          * @param[in] n         Number of IDs to append.
127          * @return      0 on success, ENOMEM if the IDL is too large.
128          */
129 int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n );
130
131         /** Sort an IDL.
132          * @param[in,out] ids   The IDL to sort.
133          */
134 void mdb_midl_sort( MDB_IDL ids );
135
136         /** An ID2 is an ID/pointer pair.
137          */
138 typedef struct MDB_ID2 {
139         MDB_ID mid;             /**< The ID */
140         void *mptr;             /**< The pointer */
141 } MDB_ID2;
142
143         /** An ID2L is an ID2 List, a sorted array of ID2s.
144          * The first element's \b mid member is a count of how many actual
145          * elements are in the array. The \b mptr member of the first element is unused.
146          * The array is sorted in ascending order by \b mid.
147          */
148 typedef MDB_ID2 *MDB_ID2L;
149
150         /** Search for an ID in an ID2L.
151          * @param[in] ids       The ID2L to search.
152          * @param[in] id        The ID to search for.
153          * @return      The index of the first ID2 whose \b mid member is greater than or equal to \b id.
154          */
155 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id );
156
157
158         /** Insert an ID2 into a ID2L.
159          * @param[in,out] ids   The ID2L to insert into.
160          * @param[in] id        The ID2 to insert.
161          * @return      0 on success, -1 if the ID was already present in the ID2L.
162          */
163 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id );
164
165         /** Append an ID2 into a ID2L.
166          * @param[in,out] ids   The ID2L to append into.
167          * @param[in] id        The ID2 to append.
168          * @return      0 on success, -2 if the ID2L is too big.
169          */
170 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id );
171
172 /** @} */
173 /** @} */
174 #ifdef __cplusplus
175 }
176 #endif
177 #endif  /* _MDB_MIDL_H_ */