]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/midl.h
Rename libmdb to liblmdb
[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-2012 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 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 #define MDB_NOID        (~(MDB_ID)0)
56
57 /* IDL sizes - likely should be even bigger
58  *   limiting factors: sizeof(ID), thread stack size
59  */
60 #define MDB_IDL_LOGN    16      /* DB_SIZE is 2^16, UM_SIZE is 2^17 */
61 #define MDB_IDL_DB_SIZE         (1<<MDB_IDL_LOGN)
62 #define MDB_IDL_UM_SIZE         (1<<(MDB_IDL_LOGN+1))
63 #define MDB_IDL_UM_SIZEOF       (MDB_IDL_UM_SIZE * sizeof(MDB_ID))
64
65 #define MDB_IDL_DB_MAX          (MDB_IDL_DB_SIZE-1)
66
67 #define MDB_IDL_UM_MAX          (MDB_IDL_UM_SIZE-1)
68
69 #define MDB_IDL_IS_RANGE(ids)   ((ids)[0] == MDB_NOID)
70 #define MDB_IDL_RANGE_SIZE              (3)
71 #define MDB_IDL_RANGE_SIZEOF    (MDB_IDL_RANGE_SIZE * sizeof(MDB_ID))
72 #define MDB_IDL_SIZEOF(ids)             ((MDB_IDL_IS_RANGE(ids) \
73         ? MDB_IDL_RANGE_SIZE : ((ids)[0]+1)) * sizeof(MDB_ID))
74
75 #define MDB_IDL_RANGE_FIRST(ids)        ((ids)[1])
76 #define MDB_IDL_RANGE_LAST(ids)         ((ids)[2])
77
78 #define MDB_IDL_RANGE( ids, f, l ) \
79         do { \
80                 (ids)[0] = MDB_NOID; \
81                 (ids)[1] = (f);  \
82                 (ids)[2] = (l);  \
83         } while(0)
84
85 #define MDB_IDL_ZERO(ids) \
86         do { \
87                 (ids)[0] = 0; \
88                 (ids)[1] = 0; \
89                 (ids)[2] = 0; \
90         } while(0)
91
92 #define MDB_IDL_IS_ZERO(ids) ( (ids)[0] == 0 )
93 #define MDB_IDL_IS_ALL( range, ids ) ( (ids)[0] == MDB_NOID \
94         && (ids)[1] <= (range)[1] && (range)[2] <= (ids)[2] )
95
96 #define MDB_IDL_CPY( dst, src ) (memcpy( dst, src, MDB_IDL_SIZEOF( src ) ))
97
98 #define MDB_IDL_ID( bdb, ids, id ) MDB_IDL_RANGE( ids, id, ((bdb)->bi_lastid) )
99 #define MDB_IDL_ALL( bdb, ids ) MDB_IDL_RANGE( ids, 1, ((bdb)->bi_lastid) )
100
101 #define MDB_IDL_FIRST( ids )    ( (ids)[1] )
102 #define MDB_IDL_LAST( ids )             ( MDB_IDL_IS_RANGE(ids) \
103         ? (ids)[2] : (ids)[(ids)[0]] )
104
105 #define MDB_IDL_N( ids )                ( MDB_IDL_IS_RANGE(ids) \
106         ? ((ids)[2]-(ids)[1])+1 : (ids)[0] )
107
108 #if 0   /* superseded by append/sort */
109         /** Insert an ID into an IDL.
110          * @param[in,out] ids   The IDL to insert into.
111          * @param[in] id        The ID to insert.
112          * @return      0 on success, -1 if the ID was already present in the IDL.
113          */
114 int mdb_midl_insert( MDB_IDL ids, MDB_ID id );
115 #endif
116
117         /** Allocate an IDL.
118          * Allocates memory for an IDL of a default size.
119          * @return      IDL on success, NULL on failure.
120          */
121 MDB_IDL mdb_midl_alloc();
122
123         /** Free an IDL.
124          * @param[in] ids       The IDL to free.
125          */
126 void mdb_midl_free(MDB_IDL ids);
127
128         /** Shrink an IDL.
129          * Return the IDL to the default size if it has grown larger.
130          * @param[in,out] idp   Address of the IDL to shrink.
131          * @return      0 on no change, non-zero if shrunk.
132          */
133 int mdb_midl_shrink(MDB_IDL *idp);
134
135         /** Append an ID onto an IDL.
136          * @param[in,out] idp   Address of the IDL to append to.
137          * @param[in] id        The ID to append.
138          * @return      0 on success, -1 if the IDL is too large.
139          */
140 int mdb_midl_append( MDB_IDL *idp, MDB_ID id );
141
142         /** Append an IDL onto an IDL.
143          * @param[in,out] idp   Address of the IDL to append to.
144          * @param[in] app       The IDL to append.
145          * @return      0 on success, -1 if the IDL is too large.
146          */
147 int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app );
148
149         /** Sort an IDL.
150          * @param[in,out] ids   The IDL to sort.
151          */
152 void mdb_midl_sort( MDB_IDL ids );
153
154         /** An ID2 is an ID/pointer pair.
155          */
156 typedef struct MDB_ID2 {
157         MDB_ID mid;             /**< The ID */
158         void *mptr;             /**< The pointer */
159 } MDB_ID2;
160
161         /** An ID2L is an ID2 List, a sorted array of ID2s.
162          * The first element's \b mid member is a count of how many actual
163          * elements are in the array. The \b mptr member of the first element is unused.
164          * The array is sorted in ascending order by \b mid.
165          */
166 typedef MDB_ID2 *MDB_ID2L;
167
168         /** Search for an ID in an ID2L.
169          * @param[in] ids       The ID2L to search.
170          * @param[in] id        The ID to search for.
171          * @return      The index of the first ID2 whose \b mid member is greater than or equal to \b id.
172          */
173 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id );
174
175
176         /** Insert an ID2 into a ID2L.
177          * @param[in,out] ids   The ID2L to insert into.
178          * @param[in] id        The ID2 to insert.
179          * @return      0 on success, -1 if the ID was already present in the ID2L.
180          */
181 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id );
182
183         /** Append an ID2 into a ID2L.
184          * @param[in,out] ids   The ID2L to append into.
185          * @param[in] id        The ID2 to append.
186          * @return      0 on success, -2 if the ID2L is too big.
187          */
188 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id );
189
190 /** @} */
191 /** @} */
192 #ifdef __cplusplus
193 }
194 #endif
195 #endif  /* _MDB_MIDL_H_ */