Home | History | Annotate | Line # | Download | only in liblmdb
midl.h revision 1.1.1.1.10.1
      1 /*	$NetBSD: midl.h,v 1.1.1.1.10.1 2017/03/20 06:56:14 pgoyette Exp $	*/
      2 
      3 /**	@file midl.h
      4  *	@brief LMDB ID List header file.
      5  *
      6  *	This file was originally part of back-bdb but has been
      7  *	modified for use in libmdb. Most of the macros defined
      8  *	in this file are unused, just left over from the original.
      9  *
     10  *	This file is only used internally in libmdb and its definitions
     11  *	are not exposed publicly.
     12  */
     13 /* $OpenLDAP$ */
     14 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
     15  *
     16  * Copyright 2000-2016 The OpenLDAP Foundation.
     17  * All rights reserved.
     18  *
     19  * Redistribution and use in source and binary forms, with or without
     20  * modification, are permitted only as authorized by the OpenLDAP
     21  * Public License.
     22  *
     23  * A copy of this license is available in the file LICENSE in the
     24  * top-level directory of the distribution or, alternatively, at
     25  * <http://www.OpenLDAP.org/license.html>.
     26  */
     27 
     28 #ifndef _MDB_MIDL_H_
     29 #define _MDB_MIDL_H_
     30 
     31 #include <stddef.h>
     32 
     33 #ifdef __cplusplus
     34 extern "C" {
     35 #endif
     36 
     37 /** @defgroup internal	LMDB Internals
     38  *	@{
     39  */
     40 
     41 /** @defgroup idls	ID List Management
     42  *	@{
     43  */
     44 	/** A generic unsigned ID number. These were entryIDs in back-bdb.
     45 	 *	Preferably it should have the same size as a pointer.
     46 	 */
     47 typedef size_t MDB_ID;
     48 
     49 	/** An IDL is an ID List, a sorted array of IDs. The first
     50 	 * element of the array is a counter for how many actual
     51 	 * IDs are in the list. In the original back-bdb code, IDLs are
     52 	 * sorted in ascending order. For libmdb IDLs are sorted in
     53 	 * descending order.
     54 	 */
     55 typedef MDB_ID *MDB_IDL;
     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 
     64 #define MDB_IDL_DB_MAX		(MDB_IDL_DB_SIZE-1)
     65 #define MDB_IDL_UM_MAX		(MDB_IDL_UM_SIZE-1)
     66 
     67 #define MDB_IDL_SIZEOF(ids)		(((ids)[0]+1) * sizeof(MDB_ID))
     68 #define MDB_IDL_IS_ZERO(ids) ( (ids)[0] == 0 )
     69 #define MDB_IDL_CPY( dst, src ) (memcpy( dst, src, MDB_IDL_SIZEOF( src ) ))
     70 #define MDB_IDL_FIRST( ids )	( (ids)[1] )
     71 #define MDB_IDL_LAST( ids )		( (ids)[(ids)[0]] )
     72 
     73 	/** Current max length of an #mdb_midl_alloc()ed IDL */
     74 #define MDB_IDL_ALLOCLEN( ids )	( (ids)[-1] )
     75 
     76 	/** Append ID to IDL. The IDL must be big enough. */
     77 #define mdb_midl_xappend(idl, id) do { \
     78 		MDB_ID *xidl = (idl), xlen = ++(xidl[0]); \
     79 		xidl[xlen] = (id); \
     80 	} while (0)
     81 
     82 	/** Search for an ID in an IDL.
     83 	 * @param[in] ids	The IDL to search.
     84 	 * @param[in] id	The ID to search for.
     85 	 * @return	The index of the first ID greater than or equal to \b id.
     86 	 */
     87 unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id );
     88 
     89 	/** Allocate an IDL.
     90 	 * Allocates memory for an IDL of the given size.
     91 	 * @return	IDL on success, NULL on failure.
     92 	 */
     93 MDB_IDL mdb_midl_alloc(int num);
     94 
     95 	/** Free an IDL.
     96 	 * @param[in] ids	The IDL to free.
     97 	 */
     98 void mdb_midl_free(MDB_IDL ids);
     99 
    100 	/** Shrink an IDL.
    101 	 * Return the IDL to the default size if it has grown larger.
    102 	 * @param[in,out] idp	Address of the IDL to shrink.
    103 	 */
    104 void mdb_midl_shrink(MDB_IDL *idp);
    105 
    106 	/** Make room for num additional elements in an IDL.
    107 	 * @param[in,out] idp	Address of the IDL.
    108 	 * @param[in] num	Number of elements to make room for.
    109 	 * @return	0 on success, ENOMEM on failure.
    110 	 */
    111 int mdb_midl_need(MDB_IDL *idp, unsigned num);
    112 
    113 	/** Append an ID onto an IDL.
    114 	 * @param[in,out] idp	Address of the IDL to append to.
    115 	 * @param[in] id	The ID to append.
    116 	 * @return	0 on success, ENOMEM if the IDL is too large.
    117 	 */
    118 int mdb_midl_append( MDB_IDL *idp, MDB_ID id );
    119 
    120 	/** Append an IDL onto an IDL.
    121 	 * @param[in,out] idp	Address of the IDL to append to.
    122 	 * @param[in] app	The IDL to append.
    123 	 * @return	0 on success, ENOMEM if the IDL is too large.
    124 	 */
    125 int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app );
    126 
    127 	/** Append an ID range onto an IDL.
    128 	 * @param[in,out] idp	Address of the IDL to append to.
    129 	 * @param[in] id	The lowest ID to append.
    130 	 * @param[in] n		Number of IDs to append.
    131 	 * @return	0 on success, ENOMEM if the IDL is too large.
    132 	 */
    133 int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n );
    134 
    135 	/** Merge an IDL onto an IDL. The destination IDL must be big enough.
    136 	 * @param[in] idl	The IDL to merge into.
    137 	 * @param[in] merge	The IDL to merge.
    138 	 */
    139 void mdb_midl_xmerge( MDB_IDL idl, MDB_IDL merge );
    140 
    141 	/** Sort an IDL.
    142 	 * @param[in,out] ids	The IDL to sort.
    143 	 */
    144 void mdb_midl_sort( MDB_IDL ids );
    145 
    146 	/** An ID2 is an ID/pointer pair.
    147 	 */
    148 typedef struct MDB_ID2 {
    149 	MDB_ID mid;		/**< The ID */
    150 	void *mptr;		/**< The pointer */
    151 } MDB_ID2;
    152 
    153 	/** An ID2L is an ID2 List, a sorted array of ID2s.
    154 	 * The first element's \b mid member is a count of how many actual
    155 	 * elements are in the array. The \b mptr member of the first element is unused.
    156 	 * The array is sorted in ascending order by \b mid.
    157 	 */
    158 typedef MDB_ID2 *MDB_ID2L;
    159 
    160 	/** Search for an ID in an ID2L.
    161 	 * @param[in] ids	The ID2L to search.
    162 	 * @param[in] id	The ID to search for.
    163 	 * @return	The index of the first ID2 whose \b mid member is greater than or equal to \b id.
    164 	 */
    165 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id );
    166 
    167 
    168 	/** Insert an ID2 into a ID2L.
    169 	 * @param[in,out] ids	The ID2L to insert into.
    170 	 * @param[in] id	The ID2 to insert.
    171 	 * @return	0 on success, -1 if the ID was already present in the ID2L.
    172 	 */
    173 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id );
    174 
    175 	/** Append an ID2 into a ID2L.
    176 	 * @param[in,out] ids	The ID2L to append into.
    177 	 * @param[in] id	The ID2 to append.
    178 	 * @return	0 on success, -2 if the ID2L is too big.
    179 	 */
    180 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id );
    181 
    182 /** @} */
    183 /** @} */
    184 #ifdef __cplusplus
    185 }
    186 #endif
    187 #endif	/* _MDB_MIDL_H_ */
    188