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