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