1 /* idl.c - ldap id list handling routines */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
16 #include "back-ldbm.h"
18 static ID_BLOCK* idl_dup( ID_BLOCK *idl );
20 static void cont_alloc( Datum *cont, Datum *key )
22 ldbm_datum_init( *cont );
23 cont->dsize = 1 + sizeof(ID) + key->dsize;
24 cont->dptr = ch_malloc( cont->dsize );
26 * (unsigned char *) cont->dptr = SLAP_INDEX_CONT_PREFIX;
28 memcpy( &((unsigned char *)cont->dptr)[1 + sizeof(ID)],
29 key->dptr, key->dsize );
32 static void cont_id( Datum *cont, ID id )
36 for( i=1; i <= sizeof(id); i++) {
37 ((unsigned char *)cont->dptr)[i] = (unsigned char)(id & 0xFF);
43 static void cont_free( Datum *cont )
45 ch_free( cont->dptr );
48 /* Allocate an ID_BLOCK with room for nids ids */
50 idl_alloc( unsigned int nids )
54 /* nmax + nids + space for the ids */
55 new = (ID_BLOCK *) ch_calloc( (ID_BLOCK_IDS_OFFSET + nids), sizeof(ID) );
56 ID_BLOCK_NMAX(new) = nids;
57 ID_BLOCK_NIDS(new) = 0;
63 /* Allocate an empty ALLIDS ID_BLOCK */
65 idl_allids( Backend *be )
70 ID_BLOCK_NMAX(idl) = ID_BLOCK_ALLIDS_VALUE;
71 ID_BLOCK_NIDS(idl) = next_id_get( be );
76 /* Free an ID_BLOCK */
78 idl_free( ID_BLOCK *idl )
81 Debug( LDAP_DEBUG_TRACE,
82 "idl_free: called with NULL pointer\n",
91 /* Fetch an single ID_BLOCK from the cache */
102 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
104 data = ldbm_cache_fetch( db, key );
106 if( data.dptr == NULL ) {
110 idl = idl_dup((ID_BLOCK *) data.dptr);
112 ldbm_datum_free( db->dbc_db, data );
118 /* Fetch a set of ID_BLOCKs from the cache
120 * if block return is an ALLIDS block,
121 * return an new ALLIDS block
124 * construct super block from all blocks referenced by INDIRECT block
139 idl = idl_fetch_one( be, db, key );
145 if ( ID_BLOCK_ALLIDS(idl) ) {
147 /* make sure we have the current value of highest id */
149 idl = idl_allids( be );
154 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
160 * this is an indirect block which points to other blocks.
161 * we need to read in all the blocks it points to and construct
162 * a big id list containing all the ids, which we will return.
165 /* count the number of blocks & allocate space for pointers to them */
166 for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ )
168 tmp = (ID_BLOCK **) ch_malloc( (i + 1) * sizeof(ID_BLOCK *) );
170 /* read in all the blocks */
171 cont_alloc( &data, &key );
173 for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
174 cont_id( &data, ID_BLOCK_ID(idl, i) );
176 if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
177 Debug( LDAP_DEBUG_ANY,
178 "idl_fetch: one returned NULL\n", 0, 0, 0 );
182 nids += ID_BLOCK_NIDS(tmp[i]);
188 /* allocate space for the big block */
189 idl = idl_alloc( nids );
190 ID_BLOCK_NIDS(idl) = nids;
193 /* copy in all the ids from the component blocks */
194 for ( i = 0; tmp[i] != NULL; i++ ) {
195 if ( tmp[i] == NULL ) {
200 (char *) &ID_BLOCK_ID(idl, nids),
201 (char *) &ID_BLOCK_ID(tmp[i], 0),
202 ID_BLOCK_NIDS(tmp[i]) * sizeof(ID) );
203 nids += ID_BLOCK_NIDS(tmp[i]);
207 free( (char *) tmp );
209 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %ld ids (%ld max)\n",
210 ID_BLOCK_NIDS(idl), ID_BLOCK_NMAX(idl), 0 );
215 /* store a single block */
226 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
228 ldbm_datum_init( data );
230 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
232 data.dptr = (char *) idl;
233 data.dsize = (ID_BLOCK_IDS_OFFSET + ID_BLOCK_NMAX(idl)) * sizeof(ID);
236 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
240 flags = LDBM_REPLACE;
241 rc = ldbm_cache_store( db, key, data, flags );
243 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
247 /* split the block at id
248 * locate ID greater than or equal to id.
260 /* find where to split the block *//* XXX linear search XXX */
261 for ( nr = 0; nr < ID_BLOCK_NIDS(b) && id > ID_BLOCK_ID(b, nr); nr++ )
264 nl = ID_BLOCK_NIDS(b) - nr;
266 *right = idl_alloc( nr == 0 ? 1 : nr );
267 *left = idl_alloc( nl + (nr == 0 ? 0 : 1));
270 * everything before the id being inserted in the first block
271 * unless there is nothing, in which case the id being inserted
275 ID_BLOCK_NIDS(*right) = 1;
276 ID_BLOCK_ID(*right, 0) = id;
279 (char *) &ID_BLOCK_ID(*right, 0),
280 (char *) &ID_BLOCK_ID(b, 0),
282 ID_BLOCK_NIDS(*right) = nr;
283 ID_BLOCK_ID(*left, 0) = id;
286 /* the id being inserted & everything after in the second block */
288 (char *) &ID_BLOCK_ID(*left, (nr == 0 ? 0 : 1)),
289 (char *) &ID_BLOCK_ID(b, nr),
291 ID_BLOCK_NIDS(*left) = nl + (nr == 0 ? 0 : 1);
296 * idl_change_first - called when an indirect block's first key has
297 * changed, meaning it needs to be stored under a new key, and the
298 * header block pointing to it needs updating.
304 Datum hkey, /* header block key */
305 ID_BLOCK *h, /* header block */
306 int pos, /* pos in h to update */
307 Datum bkey, /* data block key */
308 ID_BLOCK *b /* data block */
313 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
315 /* delete old key block */
316 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
317 Debug( LDAP_DEBUG_ANY,
318 "idl_change_first: ldbm_cache_delete returned %d\n",
323 /* write block with new key */
324 cont_id( &bkey, ID_BLOCK_ID(b, 0) );
326 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
327 Debug( LDAP_DEBUG_ANY,
328 "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
332 /* update + write indirect header block */
333 ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
334 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
335 Debug( LDAP_DEBUG_ANY,
336 "idl_change_first: idl_store returned %d\n", rc, 0, 0 );
353 ID_BLOCK *idl, *tmp, *tmp2, *tmp3;
356 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
357 idl = idl_alloc( 1 );
358 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
359 rc = idl_store( be, db, key, idl );
365 if ( ID_BLOCK_ALLIDS( idl ) ) {
371 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
373 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
374 case 0: /* id inserted - store the updated block */
376 rc = idl_store( be, db, key, idl );
379 case 2: /* id already there - nothing to do */
383 case 3: /* id not inserted - block must be split */
384 /* check threshold for marking this an all-id block */
385 if ( db->dbc_maxindirect < 2 ) {
387 idl = idl_allids( be );
388 rc = idl_store( be, db, key, idl );
392 idl_split_block( idl, id, &tmp, &tmp2 );
395 /* create the header indirect block */
396 idl = idl_alloc( 3 );
397 ID_BLOCK_NMAX(idl) = 3;
398 ID_BLOCK_NIDS(idl) = ID_BLOCK_INDIRECT_VALUE;
399 ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
400 ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
401 ID_BLOCK_ID(idl, 2) = NOID;
404 rc = idl_store( be, db, key, idl );
406 cont_alloc( &k2, &key );
407 cont_id( &k2, ID_BLOCK_ID(tmp, 0) );
409 rc = idl_store( be, db, k2, tmp );
411 cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
412 rc = idl_store( be, db, k2, tmp2 );
426 * this is an indirect block which points to other blocks.
427 * we need to read in the block into which the id should be
428 * inserted, then insert the id and store the block. we might
429 * have to split the block if it is full, which means we also
430 * need to write a new "header" block.
433 /* select the block to try inserting into *//* XXX linear search XXX */
434 for ( i = 0; !ID_BLOCK_NOID(idl, i) && id > ID_BLOCK_ID(idl, i); i++ )
445 cont_alloc( &k2, &key );
446 cont_id( &k2, ID_BLOCK_ID(idl, i) );
448 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
449 Debug( LDAP_DEBUG_ANY, "idl_insert_key: nonexistent continuation block\n",
457 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
458 case 0: /* id inserted ok */
459 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
460 Debug( LDAP_DEBUG_ANY,
461 "idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
465 case 1: /* id inserted - first id in block has changed */
467 * key for this block has changed, so we have to
468 * write the block under the new key, delete the
469 * old key block + update and write the indirect
473 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
476 case 2: /* id not inserted - already there, do nothing */
480 case 3: /* id not inserted - block is full */
482 * first, see if it will fit in the next block,
483 * without splitting, unless we're trying to insert
484 * into the beginning of the first block.
487 /* is there a next block? */
488 if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
490 cont_alloc( &k2, &key );
491 cont_id( &k2, ID_BLOCK_ID(idl, i) );
492 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
493 Debug( LDAP_DEBUG_ANY,
494 "idl_insert_key: idl_fetch_one returned NULL\n",
496 /* split the original block */
501 /* If the new id is less than the last id in the
502 * current block, it must not be put into the next
503 * block. Push the last id of the current block
504 * into the next block instead.
506 if (id < ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1)) {
507 ID id2 = ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1);
510 ldbm_datum_init( k3 );
512 --ID_BLOCK_NIDS(tmp);
513 /* This must succeed since we just popped one
514 * ID off the end of it.
516 rc = idl_insert( &tmp, id, db->dbc_maxids );
518 k3.dptr = ch_malloc(k2.dsize);
520 memcpy(k3.dptr, k2.dptr, k3.dsize);
521 if ( (rc = idl_store( be, db, k3, tmp )) != 0 ) {
522 Debug( LDAP_DEBUG_ANY,
523 "idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
529 /* This new id will necessarily be inserted
530 * as the first id of the next block by the
531 * following switch() statement.
535 switch ( (rc = idl_insert( &tmp2, id,
536 db->dbc_maxids )) ) {
537 case 1: /* id inserted first in block */
538 rc = idl_change_first( be, db, key, idl,
542 case 2: /* id already there - how? */
543 case 0: /* id inserted: this can never be
544 * the result of idl_insert, because
545 * we guaranteed that idl_change_first
546 * will always be called.
549 Debug( LDAP_DEBUG_ANY,
550 "idl_insert_key: id %ld already in next block\n",
554 assert( 0 ); /* not yet implemented */
560 case 3: /* split the original block */
569 * must split the block, write both new blocks + update
570 * and write the indirect header block.
573 rc = 0; /* optimistic */
576 /* count how many indirect blocks *//* XXX linear count XXX */
577 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
580 /* check it against all-id thresholed */
581 if ( j + 1 > db->dbc_maxindirect ) {
583 * we've passed the all-id threshold, meaning
584 * that this set of blocks should be replaced
585 * by a single "all-id" block. our job: delete
586 * all the indirect blocks, and replace the header
587 * block by an all-id block.
590 /* delete all indirect blocks */
591 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
592 cont_id( &k2, ID_BLOCK_ID(idl, j) );
594 rc = ldbm_cache_delete( db, k2 );
597 /* store allid block in place of header block */
599 idl = idl_allids( be );
600 rc = idl_store( be, db, key, idl );
608 idl_split_block( tmp, id, &tmp2, &tmp3 );
611 /* create a new updated indirect header block */
612 tmp = idl_alloc( ID_BLOCK_NMAX(idl) + 1 );
613 ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
614 /* everything up to the split block */
616 (char *) &ID_BLOCK_ID(tmp, 0),
617 (char *) &ID_BLOCK_ID(idl, 0),
619 /* the two new blocks */
620 ID_BLOCK_ID(tmp, i) = ID_BLOCK_ID(tmp2, 0);
621 ID_BLOCK_ID(tmp, i + 1) = ID_BLOCK_ID(tmp3, 0);
622 /* everything after the split block */
624 (char *) &ID_BLOCK_ID(tmp, i + 2),
625 (char *) &ID_BLOCK_ID(idl, i + 1),
626 (ID_BLOCK_NMAX(idl) - i - 1) * sizeof(ID) );
628 /* store the header block */
629 rc = idl_store( be, db, key, tmp );
631 /* store the first id block */
632 cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
633 rc = idl_store( be, db, k2, tmp2 );
635 /* store the second id block */
636 cont_id( &k2, ID_BLOCK_ID(tmp3, 0) );
637 rc = idl_store( be, db, k2, tmp3 );
652 * idl_insert - insert an id into an id list.
656 * 1 id inserted, first id in block has changed
657 * 2 id not inserted, already there
658 * 3 id not inserted, block must be split
661 idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
665 if ( ID_BLOCK_ALLIDS( *idl ) ) {
666 return( 2 ); /* already there */
669 /* is it already there? *//* XXX linear search XXX */
670 for ( i = 0; i < ID_BLOCK_NIDS(*idl) && id > ID_BLOCK_ID(*idl, i); i++ ) {
673 if ( i < ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) == id ) {
674 return( 2 ); /* already there */
677 /* do we need to make room for it? */
678 if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAX(*idl) ) {
679 /* make room or indicate block needs splitting */
680 if ( ID_BLOCK_NMAX(*idl) >= maxids ) {
681 return( 3 ); /* block needs splitting */
684 ID_BLOCK_NMAX(*idl) *= 2;
685 if ( ID_BLOCK_NMAX(*idl) > maxids ) {
686 ID_BLOCK_NMAX(*idl) = maxids;
688 *idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
689 (ID_BLOCK_NMAX(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
692 /* make a slot for the new id */
693 SAFEMEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
694 (ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );
696 ID_BLOCK_ID(*idl, i) = id;
697 ID_BLOCK_NIDS(*idl)++;
699 (char *) &ID_BLOCK_ID((*idl), ID_BLOCK_NIDS(*idl)),
701 (ID_BLOCK_NMAX(*idl) - ID_BLOCK_NIDS(*idl)) * sizeof(ID) );
703 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
720 if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
722 /* It wasn't found. Hmm... */
726 if ( ID_BLOCK_ALLIDS( idl ) ) {
731 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
732 for ( i=0; i < ID_BLOCK_NIDS(idl); i++ ) {
733 if ( ID_BLOCK_ID(idl, i) == id ) {
734 if( --ID_BLOCK_NIDS(idl) == 0 ) {
735 ldbm_cache_delete( db, key );
739 &ID_BLOCK_ID(idl, i),
740 &ID_BLOCK_ID(idl, i+1),
741 (ID_BLOCK_NIDS(idl)-i) * sizeof(ID) );
743 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)) = NOID;
745 idl_store( be, db, key, idl );
751 /* We didn't find the ID. Hmmm... */
757 /* We have to go through an indirect block and find the ID
760 for ( nids = 0; !ID_BLOCK_NOID(idl, nids); nids++ )
763 cont_alloc( &data, &key );
765 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
768 cont_id( &data, ID_BLOCK_ID(idl, j) );
770 if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
771 Debug( LDAP_DEBUG_ANY,
772 "idl_delete_key: idl_fetch of returned NULL\n", 0, 0, 0 );
776 Now try to find the ID in tmp
778 for ( i=0; i < ID_BLOCK_NIDS(tmp); i++ )
780 if ( ID_BLOCK_ID(tmp, i) == id )
783 &ID_BLOCK_ID(tmp, i),
784 &ID_BLOCK_ID(tmp, i+1),
785 (ID_BLOCK_NIDS(tmp)-(i+1)) * sizeof(ID));
786 ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp)-1 ) = NOID;
787 ID_BLOCK_NIDS(tmp)--;
789 if ( ID_BLOCK_NIDS(tmp) ) {
790 idl_store ( be, db, data, tmp );
793 ldbm_cache_delete( db, data );
795 &ID_BLOCK_ID(idl, j),
796 &ID_BLOCK_ID(idl, j+1),
797 (nids-(j+1)) * sizeof(ID));
798 ID_BLOCK_ID(idl, nids-1) = NOID;
801 ldbm_cache_delete( db, key );
803 idl_store( be, db, key, idl );
820 /* return a duplicate of a single ID_BLOCK */
822 idl_dup( ID_BLOCK *idl )
830 new = idl_alloc( ID_BLOCK_NMAX(idl) );
835 (ID_BLOCK_NMAX(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
841 /* return the smaller ID_BLOCK */
843 idl_min( ID_BLOCK *a, ID_BLOCK *b )
845 return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
850 * idl_intersection - return a intersection b
859 unsigned int ai, bi, ni;
862 if ( a == NULL || b == NULL ) {
865 if ( ID_BLOCK_ALLIDS( a ) ) {
866 return( idl_dup( b ) );
868 if ( ID_BLOCK_ALLIDS( b ) ) {
869 return( idl_dup( a ) );
872 n = idl_dup( idl_min( a, b ) );
874 for ( ni = 0, ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
876 bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
882 if ( bi == ID_BLOCK_NIDS(b) ) {
886 if ( ID_BLOCK_ID(b, bi) == ID_BLOCK_ID(a, ai) ) {
887 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
895 ID_BLOCK_NIDS(n) = ni;
902 * idl_union - return a union b
911 unsigned int ai, bi, ni;
915 return( idl_dup( b ) );
918 return( idl_dup( a ) );
920 if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
921 return( idl_allids( be ) );
924 if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
930 n = idl_alloc( ID_BLOCK_NIDS(a) + ID_BLOCK_NIDS(b) );
932 for ( ni = 0, ai = 0, bi = 0;
933 ai < ID_BLOCK_NIDS(a) && bi < ID_BLOCK_NIDS(b);
936 if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
937 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai++);
939 } else if ( ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai) ) {
940 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi++);
943 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
948 for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
949 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
951 for ( ; bi < ID_BLOCK_NIDS(b); bi++ ) {
952 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi);
954 ID_BLOCK_NIDS(n) = ni;
961 * idl_notin - return a intersection ~b (or a minus b)
970 unsigned int ni, ai, bi;
976 if ( b == NULL || ID_BLOCK_ALLIDS( b )) {
977 return( idl_dup( a ) );
980 if ( ID_BLOCK_ALLIDS( a ) ) {
981 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
984 for ( ai = 1, bi = 0;
985 ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n) && bi < ID_BLOCK_NMAX(b);
988 if ( ID_BLOCK_ID(b, bi) == ai ) {
991 ID_BLOCK_ID(n, ni++) = ai;
995 for ( ; ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n); ai++ ) {
996 ID_BLOCK_ID(n, ni++) = ai;
999 if ( ni == ID_BLOCK_NMAX(n) ) {
1001 return( idl_allids( be ) );
1003 ID_BLOCK_NIDS(n) = ni;
1011 for ( ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
1013 bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
1019 if ( bi == ID_BLOCK_NIDS(b) ) {
1023 if ( ID_BLOCK_ID(b, bi) != ID_BLOCK_ID(a, ai) ) {
1024 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1028 for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
1029 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1031 ID_BLOCK_NIDS(n) = ni;
1036 /* return the first ID in the block
1039 * otherwise return NOID
1040 * otherwise return first ID
1042 * cursor is set to 1
1045 idl_firstid( ID_BLOCK *idl, ID *cursor )
1049 if ( idl == NULL || ID_BLOCK_NIDS(idl) == 0 ) {
1053 if ( ID_BLOCK_ALLIDS( idl ) ) {
1054 return( ID_BLOCK_NIDS(idl) > 1 ? 1 : NOID );
1057 return( ID_BLOCK_ID(idl, 0) );
1061 * if ALLIDS block, cursor is id.
1063 * if id < NIDS return id
1065 * otherwise cursor is index into block
1067 * return id at index then increment
1070 idl_nextid( ID_BLOCK *idl, ID *cursor )
1072 if ( ID_BLOCK_ALLIDS( idl ) ) {
1073 if( ++(*cursor) < ID_BLOCK_NIDS(idl) ) {
1080 if ( *cursor < ID_BLOCK_NIDS(idl) ) {
1081 return( ID_BLOCK_ID(idl, (*cursor)++) );