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