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-2016 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 "lmdb.h" 30 31 #ifdef __cplusplus 32 extern "C" { 33 #endif 34 35 /** @defgroup internal LMDB 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 mdb_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 #ifdef MDB_VL32 59 #define MDB_IDL_LOGN 14 /* DB_SIZE is 2^14, UM_SIZE is 2^15 */ 60 #else 61 #define MDB_IDL_LOGN 16 /* DB_SIZE is 2^16, UM_SIZE is 2^17 */ 62 #endif 63 #define MDB_IDL_DB_SIZE (1<<MDB_IDL_LOGN) 64 #define MDB_IDL_UM_SIZE (1<<(MDB_IDL_LOGN+1)) 65 66 #define MDB_IDL_DB_MAX (MDB_IDL_DB_SIZE-1) 67 #define MDB_IDL_UM_MAX (MDB_IDL_UM_SIZE-1) 68 69 #define MDB_IDL_SIZEOF(ids) (((ids)[0]+1) * sizeof(MDB_ID)) 70 #define MDB_IDL_IS_ZERO(ids) ( (ids)[0] == 0 ) 71 #define MDB_IDL_CPY( dst, src ) (memcpy( dst, src, MDB_IDL_SIZEOF( src ) )) 72 #define MDB_IDL_FIRST( ids ) ( (ids)[1] ) 73 #define MDB_IDL_LAST( ids ) ( (ids)[(ids)[0]] ) 74 75 /** Current max length of an #mdb_midl_alloc()ed IDL */ 76 #define MDB_IDL_ALLOCLEN( ids ) ( (ids)[-1] ) 77 78 /** Append ID to IDL. The IDL must be big enough. */ 79 #define mdb_midl_xappend(idl, id) do { \ 80 MDB_ID *xidl = (idl), xlen = ++(xidl[0]); \ 81 xidl[xlen] = (id); \ 82 } while (0) 83 84 /** Search for an ID in an IDL. 85 * @param[in] ids The IDL to search. 86 * @param[in] id The ID to search for. 87 * @return The index of the first ID greater than or equal to \b id. 88 */ 89 unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id ); 90 91 /** Allocate an IDL. 92 * Allocates memory for an IDL of the given size. 93 * @return IDL on success, NULL on failure. 94 */ 95 MDB_IDL mdb_midl_alloc(int num); 96 97 /** Free an IDL. 98 * @param[in] ids The IDL to free. 99 */ 100 void mdb_midl_free(MDB_IDL ids); 101 102 /** Shrink an IDL. 103 * Return the IDL to the default size if it has grown larger. 104 * @param[in,out] idp Address of the IDL to shrink. 105 */ 106 void mdb_midl_shrink(MDB_IDL *idp); 107 108 /** Make room for num additional elements in an IDL. 109 * @param[in,out] idp Address of the IDL. 110 * @param[in] num Number of elements to make room for. 111 * @return 0 on success, ENOMEM on failure. 112 */ 113 int mdb_midl_need(MDB_IDL *idp, unsigned num); 114 115 /** Append an ID onto an IDL. 116 * @param[in,out] idp Address of the IDL to append to. 117 * @param[in] id The ID to append. 118 * @return 0 on success, ENOMEM if the IDL is too large. 119 */ 120 int mdb_midl_append( MDB_IDL *idp, MDB_ID id ); 121 122 /** Append an IDL onto an IDL. 123 * @param[in,out] idp Address of the IDL to append to. 124 * @param[in] app The IDL to append. 125 * @return 0 on success, ENOMEM if the IDL is too large. 126 */ 127 int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app ); 128 129 /** Append an ID range onto an IDL. 130 * @param[in,out] idp Address of the IDL to append to. 131 * @param[in] id The lowest ID to append. 132 * @param[in] n Number of IDs to append. 133 * @return 0 on success, ENOMEM if the IDL is too large. 134 */ 135 int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n ); 136 137 /** Merge an IDL onto an IDL. The destination IDL must be big enough. 138 * @param[in] idl The IDL to merge into. 139 * @param[in] merge The IDL to merge. 140 */ 141 void mdb_midl_xmerge( MDB_IDL idl, MDB_IDL merge ); 142 143 /** Sort an IDL. 144 * @param[in,out] ids The IDL to sort. 145 */ 146 void mdb_midl_sort( MDB_IDL ids ); 147 148 /** An ID2 is an ID/pointer pair. 149 */ 150 typedef struct MDB_ID2 { 151 MDB_ID mid; /**< The ID */ 152 void *mptr; /**< The pointer */ 153 } MDB_ID2; 154 155 /** An ID2L is an ID2 List, a sorted array of ID2s. 156 * The first element's \b mid member is a count of how many actual 157 * elements are in the array. The \b mptr member of the first element is unused. 158 * The array is sorted in ascending order by \b mid. 159 */ 160 typedef MDB_ID2 *MDB_ID2L; 161 162 /** Search for an ID in an ID2L. 163 * @param[in] ids The ID2L to search. 164 * @param[in] id The ID to search for. 165 * @return The index of the first ID2 whose \b mid member is greater than or equal to \b id. 166 */ 167 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id ); 168 169 170 /** Insert an ID2 into a ID2L. 171 * @param[in,out] ids The ID2L to insert into. 172 * @param[in] id The ID2 to insert. 173 * @return 0 on success, -1 if the ID was already present in the ID2L. 174 */ 175 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id ); 176 177 /** Append an ID2 into a ID2L. 178 * @param[in,out] ids The ID2L to append into. 179 * @param[in] id The ID2 to append. 180 * @return 0 on success, -2 if the ID2L is too big. 181 */ 182 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id ); 183 184 #ifdef MDB_VL32 185 typedef struct MDB_ID3 { 186 MDB_ID mid; /**< The ID */ 187 void *mptr; /**< The pointer */ 188 unsigned int mcnt; /**< Number of pages */ 189 unsigned int mref; /**< Refcounter */ 190 } MDB_ID3; 191 192 typedef MDB_ID3 *MDB_ID3L; 193 194 unsigned mdb_mid3l_search( MDB_ID3L ids, MDB_ID id ); 195 int mdb_mid3l_insert( MDB_ID3L ids, MDB_ID3 *id ); 196 197 #endif /* MDB_VL32 */ 198 /** @} */ 199 /** @} */ 200 #ifdef __cplusplus 201 } 202 #endif 203 #endif /* _MDB_MIDL_H_ */ 204