Import upstream 2.3.14
[packages/xinetd.git] / libs / src / pset / ops.c
1 /*
2  * (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
3  * All rights reserved.  The file named COPYRIGHT specifies the terms 
4  * and conditions for redistribution.
5  */
6
7
8 #include "config.h"
9 #include "pset.h"
10 #include <stdlib.h>
11
12 #define POINTER                         __pset_pointer
13
14
15 /*
16  * Remove all NULL pointers from a pset
17  */
18 void pset_compact( register pset_h pset )
19 {
20         register unsigned u ;
21
22         for ( u = 0 ; u < pset_count( pset ) ; )
23         {
24                 POINTER ptr = pset_pointer( pset, u );
25                 if ( ptr != NULL )
26                         u++ ;
27                 else
28                         pset_delete( pset, ptr ) ;
29         }
30
31         /* See if we can reclaim some memory, make sure we are 2 below for some hysteresis */
32         if ((int)( pset->max - pset->alloc_step - 2) > (int)pset_count( pset ))
33         {       /* This rounds up to the next unit of steps */
34                 POINTER *new_ptrs ;
35                 unsigned new_max = ((pset_count( pset ) / pset->alloc_step) + 1)*pset->alloc_step;
36
37                 new_ptrs = (POINTER *) realloc(
38                                                                 (char *)pset->ptrs, new_max * sizeof( POINTER ) ) ;
39                 if ( new_ptrs == NULL )
40                         return;
41                 pset->max = new_max ;
42                 pset->ptrs = new_ptrs ;
43         }
44 }
45
46
47 /*
48  * Apply a function to all pointers of a pset
49  */
50 void pset_apply( register pset_h pset, void (*func)(), register void *arg )
51 {
52         register unsigned u ;
53
54         for ( u = 0 ; u < pset_count( pset ) ; u++ )
55                 if ( arg )
56                         (*func)( arg, pset_pointer( pset, u ) ) ;
57                 else
58                         (*func)( pset_pointer( pset, u ) ) ;
59 }
60