1 /* idl.c - ldap id list handling routines */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
12 #include <ac/socket.h>
14 #include "ldap_defaults.h"
16 #include "back-ldbm.h"
18 static ID_BLOCK* idl_dup( ID_BLOCK *idl );
20 /* Allocate an ID_BLOCK with room for nids ids */
22 idl_alloc( unsigned int nids )
26 /* nmax + nids + space for the ids */
27 new = (ID_BLOCK *) ch_calloc( (ID_BLOCK_IDS_OFFSET + nids), sizeof(ID) );
28 ID_BLOCK_NMAX(new) = nids;
29 ID_BLOCK_NIDS(new) = 0;
35 /* Allocate an empty ALLIDS ID_BLOCK */
37 idl_allids( Backend *be )
42 ID_BLOCK_NMAX(idl) = ID_BLOCK_ALLIDS_VALUE;
43 ID_BLOCK_NIDS(idl) = next_id_get( be );
49 /* Free an ID_BLOCK */
51 idl_free( ID_BLOCK *idl )
54 Debug( LDAP_DEBUG_TRACE,
55 "idl_free: called with NULL pointer\n",
64 /* Fetch an single ID_BLOCK from the cache */
75 ldbm_datum_init( data );
77 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
79 data = ldbm_cache_fetch( db, key );
81 if( data.dptr == NULL ) {
85 idl = idl_dup( (ID_BLOCK *) data.dptr);
86 ldbm_datum_free( db->dbc_db, data );
92 /* Fetch a set of ID_BLOCKs from the cache
94 * if block return is an ALLIDS block,
95 * return an new ALLIDS block
98 * construct super block from all blocks referenced by INDIRECT block
114 idl = idl_fetch_one( be, db, key );
120 if ( ID_BLOCK_ALLIDS(idl) ) {
122 /* make sure we have the current value of highest id */
124 idl = idl_allids( be );
129 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
135 * this is an indirect block which points to other blocks.
136 * we need to read in all the blocks it points to and construct
137 * a big id list containing all the ids, which we will return.
140 /* count the number of blocks & allocate space for pointers to them */
141 for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ )
143 tmp = (ID_BLOCK **) ch_malloc( (i + 1) * sizeof(ID_BLOCK *) );
145 /* read in all the blocks */
146 kstr = (char *) ch_malloc( key.dsize + 20 );
148 for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
149 ldbm_datum_init( data );
151 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, ID_BLOCK_ID(idl, i) );
153 data.dsize = strlen( kstr ) + 1;
155 if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
156 Debug( LDAP_DEBUG_ANY,
157 "idl_fetch of (%s) returns NULL\n", data.dptr, 0, 0 );
161 nids += ID_BLOCK_NIDS(tmp[i]);
167 /* allocate space for the big block */
168 idl = idl_alloc( nids );
169 ID_BLOCK_NIDS(idl) = nids;
172 /* copy in all the ids from the component blocks */
173 for ( i = 0; tmp[i] != NULL; i++ ) {
174 if ( tmp[i] == NULL ) {
179 (char *) &ID_BLOCK_ID(idl, nids),
180 (char *) &ID_BLOCK_ID(tmp[i], 0),
181 ID_BLOCK_NIDS(tmp[i]) * sizeof(ID) );
182 nids += ID_BLOCK_NIDS(tmp[i]);
186 free( (char *) tmp );
188 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %ld ids (%ld max)\n",
189 ID_BLOCK_NIDS(idl), ID_BLOCK_NMAX(idl), 0 );
194 /* store a single block */
205 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
207 ldbm_datum_init( data );
209 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
211 data.dptr = (char *) idl;
212 data.dsize = (ID_BLOCK_IDS_OFFSET + ID_BLOCK_NMAX(idl)) * sizeof(ID);
215 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
219 flags = LDBM_REPLACE;
220 if( li->li_dbcachewsync ) flags |= LDBM_SYNC;
221 rc = ldbm_cache_store( db, key, data, flags );
223 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
227 /* split the block at id
228 * locate ID greater than or equal to id.
240 /* find where to split the block *//* XXX linear search XXX */
241 for ( nr = 0; nr < ID_BLOCK_NIDS(b) && id > ID_BLOCK_ID(b, nr); nr++ )
244 nl = ID_BLOCK_NIDS(b) - nr;
246 *right = idl_alloc( nr == 0 ? 1 : nr );
247 *left = idl_alloc( nl + (nr == 0 ? 0 : 1));
250 * everything before the id being inserted in the first block
251 * unless there is nothing, in which case the id being inserted
255 ID_BLOCK_NIDS(*right) = 1;
256 ID_BLOCK_ID(*right, 0) = id;
259 (char *) &ID_BLOCK_ID(*right, 0),
260 (char *) &ID_BLOCK_ID(b, 0),
262 ID_BLOCK_NIDS(*right) = nr;
263 ID_BLOCK_ID(*left, 0) = id;
266 /* the id being inserted & everything after in the second block */
268 (char *) &ID_BLOCK_ID(*left, (nr == 0 ? 0 : 1)),
269 (char *) &ID_BLOCK_ID(b, nr),
271 ID_BLOCK_NIDS(*left) = nl + (nr == 0 ? 0 : 1);
276 * idl_change_first - called when an indirect block's first key has
277 * changed, meaning it needs to be stored under a new key, and the
278 * header block pointing to it needs updating.
284 Datum hkey, /* header block key */
285 ID_BLOCK *h, /* header block */
286 int pos, /* pos in h to update */
287 Datum bkey, /* data block key */
288 ID_BLOCK *b /* data block */
293 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
295 /* delete old key block */
296 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
297 Debug( LDAP_DEBUG_ANY,
298 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
303 /* write block with new key */
304 sprintf( bkey.dptr, "%c%s%ld", CONT_PREFIX, hkey.dptr, ID_BLOCK_ID(b, 0) );
305 bkey.dsize = strlen( bkey.dptr ) + 1;
306 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
307 Debug( LDAP_DEBUG_ANY,
308 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
312 /* update + write indirect header block */
313 ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
314 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
315 Debug( LDAP_DEBUG_ANY,
316 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
333 ID_BLOCK *idl, *tmp, *tmp2, *tmp3;
337 ldbm_datum_init( k2 );
339 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
341 Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
345 idl = idl_alloc( 1 );
346 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
347 rc = idl_store( be, db, key, idl );
353 if ( ID_BLOCK_ALLIDS( idl ) ) {
359 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
361 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
362 case 0: /* id inserted - store the updated block */
364 rc = idl_store( be, db, key, idl );
367 case 2: /* id already there - nothing to do */
371 case 3: /* id not inserted - block must be split */
372 /* check threshold for marking this an all-id block */
373 if ( db->dbc_maxindirect < 2 ) {
375 idl = idl_allids( be );
376 rc = idl_store( be, db, key, idl );
380 idl_split_block( idl, id, &tmp, &tmp2 );
383 /* create the header indirect block */
384 idl = idl_alloc( 3 );
385 ID_BLOCK_NMAX(idl) = 3;
386 ID_BLOCK_NIDS(idl) = ID_BLOCK_INDIRECT_VALUE;
387 ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
388 ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
389 ID_BLOCK_ID(idl, 2) = NOID;
392 rc = idl_store( be, db, key, idl );
394 /* store the first id block */
395 kstr = (char *) ch_malloc( key.dsize + 20 );
396 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
397 ID_BLOCK_ID(tmp, 0) );
399 k2.dsize = strlen( kstr ) + 1;
400 rc = idl_store( be, db, k2, tmp );
402 /* store the second id block */
403 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
404 ID_BLOCK_ID(tmp2, 0) );
406 k2.dsize = strlen( kstr ) + 1;
407 rc = idl_store( be, db, k2, tmp2 );
420 * this is an indirect block which points to other blocks.
421 * we need to read in the block into which the id should be
422 * inserted, then insert the id and store the block. we might
423 * have to split the block if it is full, which means we also
424 * need to write a new "header" block.
427 /* select the block to try inserting into *//* XXX linear search XXX */
428 for ( i = 0; !ID_BLOCK_NOID(idl, i) && id > ID_BLOCK_ID(idl, i); i++ )
438 kstr = (char *) ch_malloc( key.dsize + 20 );
439 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, ID_BLOCK_ID(idl, i) );
441 k2.dsize = strlen( kstr ) + 1;
442 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
443 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
451 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
452 case 0: /* id inserted ok */
453 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
454 Debug( LDAP_DEBUG_ANY,
455 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
459 case 1: /* id inserted - first id in block has changed */
461 * key for this block has changed, so we have to
462 * write the block under the new key, delete the
463 * old key block + update and write the indirect
467 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
470 case 2: /* id not inserted - already there */
473 case 3: /* id not inserted - block is full */
475 * first, see if it will fit in the next block,
476 * without splitting, unless we're trying to insert
477 * into the beginning of the first block.
480 /* is there a next block? */
481 if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
483 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
484 ID_BLOCK_ID(idl, i + 1) );
486 k2.dsize = strlen( kstr ) + 1;
487 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
488 Debug( LDAP_DEBUG_ANY,
489 "idl_fetch_one (%s) returns NULL\n",
494 switch ( (rc = idl_insert( &tmp2, id,
495 db->dbc_maxids )) ) {
496 case 1: /* id inserted first in block */
497 rc = idl_change_first( be, db, key, idl,
501 case 2: /* id already there - how? */
502 case 0: /* id inserted */
504 Debug( LDAP_DEBUG_ANY,
505 "id %ld already in next block\n",
514 case 3: /* split the original block */
521 * must split the block, write both new blocks + update
522 * and write the indirect header block.
525 /* count how many indirect blocks *//* XXX linear count XXX */
526 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
529 /* check it against all-id thresholed */
530 if ( j + 1 > db->dbc_maxindirect ) {
532 * we've passed the all-id threshold, meaning
533 * that this set of blocks should be replaced
534 * by a single "all-id" block. our job: delete
535 * all the indirect blocks, and replace the header
536 * block by an all-id block.
539 /* delete all indirect blocks */
540 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
541 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
542 ID_BLOCK_ID(idl, j) );
544 k2.dsize = strlen( kstr ) + 1;
546 rc = ldbm_cache_delete( db, k2 );
549 /* store allid block in place of header block */
551 idl = idl_allids( be );
552 rc = idl_store( be, db, key, idl );
560 idl_split_block( tmp, id, &tmp2, &tmp3 );
563 /* create a new updated indirect header block */
564 tmp = idl_alloc( ID_BLOCK_NMAX(idl) + 1 );
565 ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
566 /* everything up to the split block */
568 (char *) &ID_BLOCK_ID(tmp, 0),
569 (char *) &ID_BLOCK_ID(idl, 0),
571 /* the two new blocks */
572 ID_BLOCK_ID(tmp, i) = ID_BLOCK_ID(tmp2, 0);
573 ID_BLOCK_ID(tmp, i + 1) = ID_BLOCK_ID(tmp3, 0);
574 /* everything after the split block */
576 (char *) &ID_BLOCK_ID(tmp, i + 2),
577 (char *) &ID_BLOCK_ID(idl, i + 1),
578 (ID_BLOCK_NMAX(idl) - i - 1) * sizeof(ID) );
580 /* store the header block */
581 rc = idl_store( be, db, key, tmp );
583 /* store the first id block */
584 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
585 ID_BLOCK_ID(tmp2, 0) );
587 k2.dsize = strlen( kstr ) + 1;
588 rc = idl_store( be, db, k2, tmp2 );
590 /* store the second id block */
591 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr,
592 ID_BLOCK_ID(tmp3, 0) );
594 k2.dsize = strlen( kstr ) + 1;
595 rc = idl_store( be, db, k2, tmp3 );
610 * idl_insert - insert an id into an id list.
614 * 1 id inserted, first id in block has changed
615 * 2 id not inserted, already there
616 * 3 id not inserted, block must be split
619 idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
623 if ( ID_BLOCK_ALLIDS( *idl ) ) {
624 return( 2 ); /* already there */
627 /* is it already there? *//* XXX linear search XXX */
628 for ( i = 0; i < ID_BLOCK_NIDS(*idl) && id > ID_BLOCK_ID(*idl, i); i++ ) {
631 if ( i < ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) == id ) {
632 return( 2 ); /* already there */
635 /* do we need to make room for it? */
636 if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAX(*idl) ) {
637 /* make room or indicate block needs splitting */
638 if ( ID_BLOCK_NMAX(*idl) >= maxids ) {
639 return( 3 ); /* block needs splitting */
642 ID_BLOCK_NMAX(*idl) *= 2;
643 if ( ID_BLOCK_NMAX(*idl) > maxids ) {
644 ID_BLOCK_NMAX(*idl) = maxids;
646 *idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
647 (ID_BLOCK_NMAX(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
650 /* make a slot for the new id *//* XXX bubble move XXX */
653 for ( j = ID_BLOCK_NIDS(*idl); j != i; j-- ) {
654 ID_BLOCK_ID(*idl, j) = ID_BLOCK_ID(*idl, j-1);
657 SAFEMEMCPY( &ID_BLOCK_ID(*idl, i), &ID_BLOCK_ID(*idl, i+1),
658 ID_BLOCK_NIDS(*idl) - i );
661 ID_BLOCK_ID(*idl, i) = id;
662 ID_BLOCK_NIDS(*idl)++;
664 (char *) &ID_BLOCK_ID((*idl), ID_BLOCK_NIDS(*idl)),
666 (ID_BLOCK_NMAX(*idl) - ID_BLOCK_NIDS(*idl)) * sizeof(ID) );
668 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
686 if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
688 /* It wasn't found. Hmm... */
692 if ( ID_BLOCK_ALLIDS( idl ) ) {
697 if ( ! ID_BLOCK_INDIRECT( idl ) ) {
698 for ( i=0; i < ID_BLOCK_NIDS(idl); i++ ) {
699 if ( ID_BLOCK_ID(idl, i) == id ) {
700 if( --ID_BLOCK_NIDS(idl) == 0 ) {
701 ldbm_cache_delete( db, key );
705 &ID_BLOCK_ID(idl, i),
706 &ID_BLOCK_ID(idl, i+1),
707 (ID_BLOCK_NIDS(idl)-i) * sizeof(ID) );
709 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)) = NOID;
711 idl_store( be, db, key, idl );
717 /* We didn't find the ID. Hmmm... */
723 /* We have to go through an indirect block and find the ID
726 for ( nids = 0; !ID_BLOCK_NOID(idl, nids); nids++ )
728 kstr = (char *) ch_malloc( key.dsize + 20 );
730 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
732 ldbm_datum_init( data );
733 sprintf( kstr, "%c%s%ld", CONT_PREFIX, key.dptr, ID_BLOCK_ID(idl, j) );
735 data.dsize = strlen( kstr ) + 1;
737 if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
738 Debug( LDAP_DEBUG_ANY,
739 "idl_fetch of (%s) returns NULL\n", data.dptr, 0, 0 );
743 Now try to find the ID in tmp
745 for ( i=0; i < ID_BLOCK_NIDS(tmp); i++ )
747 if ( ID_BLOCK_ID(tmp, i) == id )
750 &ID_BLOCK_ID(tmp, i),
751 &ID_BLOCK_ID(tmp, i+1),
752 (ID_BLOCK_NIDS(tmp)-(i+1)) * sizeof(ID));
753 ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp)-1 ) = NOID;
754 ID_BLOCK_NIDS(tmp)--;
756 if ( ID_BLOCK_NIDS(tmp) ) {
757 idl_store ( be, db, data, tmp );
760 ldbm_cache_delete( db, data );
762 &ID_BLOCK_ID(idl, j),
763 &ID_BLOCK_ID(idl, j+1),
764 (nids-(j+1)) * sizeof(ID));
765 ID_BLOCK_ID(idl, nids-1) = NOID;
768 ldbm_cache_delete( db, key );
770 idl_store( be, db, key, idl );
786 /* return a duplicate of a single ID_BLOCK */
788 idl_dup( ID_BLOCK *idl )
796 new = idl_alloc( ID_BLOCK_NMAX(idl) );
801 (ID_BLOCK_NMAX(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
807 /* return the smaller ID_BLOCK */
809 idl_min( ID_BLOCK *a, ID_BLOCK *b )
811 return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
816 * idl_intersection - return a intersection b
825 unsigned int ai, bi, ni;
828 if ( a == NULL || b == NULL ) {
831 if ( ID_BLOCK_ALLIDS( a ) ) {
832 return( idl_dup( b ) );
834 if ( ID_BLOCK_ALLIDS( b ) ) {
835 return( idl_dup( a ) );
838 n = idl_dup( idl_min( a, b ) );
840 for ( ni = 0, ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
842 bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
848 if ( bi == ID_BLOCK_NIDS(b) ) {
852 if ( ID_BLOCK_ID(b, bi) == ID_BLOCK_ID(a, ai) ) {
853 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
861 ID_BLOCK_NIDS(n) = ni;
868 * idl_union - return a union b
877 unsigned int ai, bi, ni;
881 return( idl_dup( b ) );
884 return( idl_dup( a ) );
886 if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
887 return( idl_allids( be ) );
890 if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
896 n = idl_alloc( ID_BLOCK_NIDS(a) + ID_BLOCK_NIDS(b) );
898 for ( ni = 0, ai = 0, bi = 0;
899 ai < ID_BLOCK_NIDS(a) && bi < ID_BLOCK_NIDS(b);
902 if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
903 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai++);
905 } else if ( ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai) ) {
906 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi++);
909 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
914 for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
915 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
917 for ( ; bi < ID_BLOCK_NIDS(b); bi++ ) {
918 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi);
920 ID_BLOCK_NIDS(n) = ni;
927 * idl_notin - return a intersection ~b (or a minus b)
936 unsigned int ni, ai, bi;
942 if ( b == NULL || ID_BLOCK_ALLIDS( b )) {
943 return( idl_dup( a ) );
946 if ( ID_BLOCK_ALLIDS( a ) ) {
947 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
950 for ( ai = 1, bi = 0;
951 ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n) && bi < ID_BLOCK_NMAX(b);
954 if ( ID_BLOCK_ID(b, bi) == ai ) {
957 ID_BLOCK_ID(n, ni++) = ai;
961 for ( ; ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n); ai++ ) {
962 ID_BLOCK_ID(n, ni++) = ai;
965 if ( ni == ID_BLOCK_NMAX(n) ) {
967 return( idl_allids( be ) );
969 ID_BLOCK_NIDS(n) = ni;
977 for ( ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
979 bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
985 if ( bi == ID_BLOCK_NIDS(b) ) {
989 if ( ID_BLOCK_ID(b, bi) != ID_BLOCK_ID(a, ai) ) {
990 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
994 for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
995 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
997 ID_BLOCK_NIDS(n) = ni;
1002 /* return the first ID in the block
1005 * otherwise return NOID
1006 * otherwise return first ID
1009 idl_firstid( ID_BLOCK *idl )
1011 if ( idl == NULL || ID_BLOCK_NIDS(idl) == 0 ) {
1015 if ( ID_BLOCK_ALLIDS( idl ) ) {
1016 return( ID_BLOCK_NIDS(idl) > 1 ? 1 : NOID );
1019 return( ID_BLOCK_ID(idl, 0) );
1022 /* return next ID after id
1023 * if ALLIDS block, increment id.
1024 * if id < NIDS return id
1026 * otherwise SEARCH for next id (ugh!)
1029 idl_nextid( ID_BLOCK *idl, ID id )
1033 if ( ID_BLOCK_ALLIDS( idl ) ) {
1034 return( ++id < ID_BLOCK_NIDS(idl) ? id : NOID );
1037 for ( i = 0; i < ID_BLOCK_NIDS(idl) && ID_BLOCK_ID(idl, i) <= id; i++ ) {
1041 if ( i >= ID_BLOCK_NIDS(idl) ) {
1044 return( ID_BLOCK_ID(idl, i) );