]> git.sur5r.net Git - openldap/blob - libraries/libmdb/midl.h
Cleanup midl, start documenting internals
[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-2011 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 /** @defgroup internal  MDB Internals
30  *      @{
31  */
32 /** @defgroup idls      ID List Management
33  *      @{
34  */
35         /** An ID should be the largest integer type supported on a machine.
36          */
37 #define ID      unsigned long
38
39         /** An IDL is an ID List, a sorted array of IDs. The first
40          * element of the array is a counter for how many actual
41          * IDs are in the list. In the original back-bdb code, IDLs are
42          * sorted in ascending order. For libmdb IDLs are sorted in
43          * descending order.
44          */
45 typedef ID *IDL;
46
47 #define NOID    (~(ID)0)
48
49 /* IDL sizes - likely should be even bigger
50  *   limiting factors: sizeof(ID), thread stack size
51  */
52 #define MDB_IDL_LOGN    16      /* DB_SIZE is 2^16, UM_SIZE is 2^17 */
53 #define MDB_IDL_DB_SIZE         (1<<MDB_IDL_LOGN)
54 #define MDB_IDL_UM_SIZE         (1<<(MDB_IDL_LOGN+1))
55 #define MDB_IDL_UM_SIZEOF       (MDB_IDL_UM_SIZE * sizeof(ID))
56
57 #define MDB_IDL_DB_MAX          (MDB_IDL_DB_SIZE-1)
58
59 #define MDB_IDL_UM_MAX          (MDB_IDL_UM_SIZE-1)
60
61 #define MDB_IDL_IS_RANGE(ids)   ((ids)[0] == NOID)
62 #define MDB_IDL_RANGE_SIZE              (3)
63 #define MDB_IDL_RANGE_SIZEOF    (MDB_IDL_RANGE_SIZE * sizeof(ID))
64 #define MDB_IDL_SIZEOF(ids)             ((MDB_IDL_IS_RANGE(ids) \
65         ? MDB_IDL_RANGE_SIZE : ((ids)[0]+1)) * sizeof(ID))
66
67 #define MDB_IDL_RANGE_FIRST(ids)        ((ids)[1])
68 #define MDB_IDL_RANGE_LAST(ids)         ((ids)[2])
69
70 #define MDB_IDL_RANGE( ids, f, l ) \
71         do { \
72                 (ids)[0] = NOID; \
73                 (ids)[1] = (f);  \
74                 (ids)[2] = (l);  \
75         } while(0)
76
77 #define MDB_IDL_ZERO(ids) \
78         do { \
79                 (ids)[0] = 0; \
80                 (ids)[1] = 0; \
81                 (ids)[2] = 0; \
82         } while(0)
83
84 #define MDB_IDL_IS_ZERO(ids) ( (ids)[0] == 0 )
85 #define MDB_IDL_IS_ALL( range, ids ) ( (ids)[0] == NOID \
86         && (ids)[1] <= (range)[1] && (range)[2] <= (ids)[2] )
87
88 #define MDB_IDL_CPY( dst, src ) (memcpy( dst, src, MDB_IDL_SIZEOF( src ) ))
89
90 #define MDB_IDL_ID( bdb, ids, id ) MDB_IDL_RANGE( ids, id, ((bdb)->bi_lastid) )
91 #define MDB_IDL_ALL( bdb, ids ) MDB_IDL_RANGE( ids, 1, ((bdb)->bi_lastid) )
92
93 #define MDB_IDL_FIRST( ids )    ( (ids)[1] )
94 #define MDB_IDL_LAST( ids )             ( MDB_IDL_IS_RANGE(ids) \
95         ? (ids)[2] : (ids)[(ids)[0]] )
96
97 #define MDB_IDL_N( ids )                ( MDB_IDL_IS_RANGE(ids) \
98         ? ((ids)[2]-(ids)[1])+1 : (ids)[0] )
99
100         /** Insert an ID into an IDL.
101          * @param[in,out] ids   The IDL to insert into.
102          * @param[in] id        The ID to insert.
103          * @return      0 on success, -1 if the ID was already present in the IDL.
104          */
105 int mdb_midl_insert( IDL ids, ID id );
106
107         /** An ID2 is an ID/pointer pair.
108          */
109 typedef struct ID2 {
110         ID mid;                 /**< The ID */
111         void *mptr;             /**< The pointer */
112 } ID2;
113
114         /** An ID2L is an ID2 List, a sorted array of ID2s.
115          * The first element's \b mid member is a count of how many actual
116          * elements are in the array. The \b mptr member of the first element is unused.
117          * The array is sorted in ascending order by \b mid.
118          */
119 typedef ID2 *ID2L;
120
121         /** Search for an ID in an ID2L.
122          * @param[in] ids       The ID2L to search.
123          * @param[in] id        The ID to search for.
124          * @return      The index of the first ID2 whose \b mid member is greater than or equal to \b id.
125          */
126 unsigned mdb_mid2l_search( ID2L ids, ID id );
127
128
129         /** Insert an ID2 into a ID2L.
130          * @param[in,out] ids   The ID2L to insert into.
131          * @param[in] id        The ID2 to insert.
132          * @return      0 on success, -1 if the ID was already present in the MIDL2.
133          */
134 int mdb_mid2l_insert( ID2L ids, ID2 *id );
135
136 /** @} */
137 /** @} */
138 #endif  /* _MDB_MIDL_H_ */