midl.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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-2018 The OpenLDAP Foundation.
  15. * Portions Copyright 2001-2018 Howard Chu, Symas Corp.
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted only as authorized by the OpenLDAP
  20. * Public License.
  21. *
  22. * A copy of this license is available in the file LICENSE in the
  23. * top-level directory of the distribution or, alternatively, at
  24. * <http://www.OpenLDAP.org/license.html>.
  25. */
  26. #ifndef _MDB_MIDL_H_
  27. #define _MDB_MIDL_H_
  28. #include <stddef.h>
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /** @defgroup internal LMDB Internals
  33. * @{
  34. */
  35. /** @defgroup idls ID List Management
  36. * @{
  37. */
  38. /** A generic unsigned ID number. These were entryIDs in back-bdb.
  39. * Preferably it should have the same size as a pointer.
  40. */
  41. typedef size_t MDB_ID;
  42. /** An IDL is an ID List, a sorted array of IDs. The first
  43. * element of the array is a counter for how many actual
  44. * IDs are in the list. In the original back-bdb code, IDLs are
  45. * sorted in ascending order. For libmdb IDLs are sorted in
  46. * descending order.
  47. */
  48. typedef MDB_ID *MDB_IDL;
  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_DB_MAX (MDB_IDL_DB_SIZE-1)
  56. #define MDB_IDL_UM_MAX (MDB_IDL_UM_SIZE-1)
  57. #define MDB_IDL_SIZEOF(ids) (((ids)[0]+1) * sizeof(MDB_ID))
  58. #define MDB_IDL_IS_ZERO(ids) ( (ids)[0] == 0 )
  59. #define MDB_IDL_CPY( dst, src ) (memcpy( dst, src, MDB_IDL_SIZEOF( src ) ))
  60. #define MDB_IDL_FIRST( ids ) ( (ids)[1] )
  61. #define MDB_IDL_LAST( ids ) ( (ids)[(ids)[0]] )
  62. /** Current max length of an #mdb_midl_alloc()ed IDL */
  63. #define MDB_IDL_ALLOCLEN( ids ) ( (ids)[-1] )
  64. /** Append ID to IDL. The IDL must be big enough. */
  65. #define mdb_midl_xappend(idl, id) do { \
  66. MDB_ID *xidl = (idl), xlen = ++(xidl[0]); \
  67. xidl[xlen] = (id); \
  68. } while (0)
  69. /** Search for an ID in an IDL.
  70. * @param[in] ids The IDL to search.
  71. * @param[in] id The ID to search for.
  72. * @return The index of the first ID greater than or equal to \b id.
  73. */
  74. unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id );
  75. /** Allocate an IDL.
  76. * Allocates memory for an IDL of the given size.
  77. * @return IDL on success, NULL on failure.
  78. */
  79. MDB_IDL mdb_midl_alloc(int num);
  80. /** Free an IDL.
  81. * @param[in] ids The IDL to free.
  82. */
  83. void mdb_midl_free(MDB_IDL ids);
  84. /** Shrink an IDL.
  85. * Return the IDL to the default size if it has grown larger.
  86. * @param[in,out] idp Address of the IDL to shrink.
  87. */
  88. void mdb_midl_shrink(MDB_IDL *idp);
  89. /** Make room for num additional elements in an IDL.
  90. * @param[in,out] idp Address of the IDL.
  91. * @param[in] num Number of elements to make room for.
  92. * @return 0 on success, ENOMEM on failure.
  93. */
  94. int mdb_midl_need(MDB_IDL *idp, unsigned num);
  95. /** Append an ID onto an IDL.
  96. * @param[in,out] idp Address of the IDL to append to.
  97. * @param[in] id The ID to append.
  98. * @return 0 on success, ENOMEM if the IDL is too large.
  99. */
  100. int mdb_midl_append( MDB_IDL *idp, MDB_ID id );
  101. /** Append an IDL onto an IDL.
  102. * @param[in,out] idp Address of the IDL to append to.
  103. * @param[in] app The IDL to append.
  104. * @return 0 on success, ENOMEM if the IDL is too large.
  105. */
  106. int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app );
  107. /** Append an ID range onto an IDL.
  108. * @param[in,out] idp Address of the IDL to append to.
  109. * @param[in] id The lowest ID to append.
  110. * @param[in] n Number of IDs to append.
  111. * @return 0 on success, ENOMEM if the IDL is too large.
  112. */
  113. int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n );
  114. /** Merge an IDL onto an IDL. The destination IDL must be big enough.
  115. * @param[in] idl The IDL to merge into.
  116. * @param[in] merge The IDL to merge.
  117. */
  118. void mdb_midl_xmerge( MDB_IDL idl, MDB_IDL merge );
  119. /** Sort an IDL.
  120. * @param[in,out] ids The IDL to sort.
  121. */
  122. void mdb_midl_sort( MDB_IDL ids );
  123. /** An ID2 is an ID/pointer pair.
  124. */
  125. typedef struct MDB_ID2 {
  126. MDB_ID mid; /**< The ID */
  127. void *mptr; /**< The pointer */
  128. } MDB_ID2;
  129. /** An ID2L is an ID2 List, a sorted array of ID2s.
  130. * The first element's \b mid member is a count of how many actual
  131. * elements are in the array. The \b mptr member of the first element is unused.
  132. * The array is sorted in ascending order by \b mid.
  133. */
  134. typedef MDB_ID2 *MDB_ID2L;
  135. /** Search for an ID in an ID2L.
  136. * @param[in] ids The ID2L to search.
  137. * @param[in] id The ID to search for.
  138. * @return The index of the first ID2 whose \b mid member is greater than or equal to \b id.
  139. */
  140. unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id );
  141. /** Insert an ID2 into a ID2L.
  142. * @param[in,out] ids The ID2L to insert into.
  143. * @param[in] id The ID2 to insert.
  144. * @return 0 on success, -1 if the ID was already present in the ID2L.
  145. */
  146. int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id );
  147. /** Append an ID2 into a ID2L.
  148. * @param[in,out] ids The ID2L to append into.
  149. * @param[in] id The ID2 to append.
  150. * @return 0 on success, -2 if the ID2L is too big.
  151. */
  152. int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id );
  153. /** @} */
  154. /** @} */
  155. #ifdef __cplusplus
  156. }
  157. #endif
  158. #endif /* _MDB_MIDL_H_ */