ia64/xen-unstable

changeset 8460:4937d9f496ab

Add a little more internal abstraction to the rangeset
library, avoiding direct interaction with the linked-list
structure.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Thu Dec 29 18:16:01 2005 +0100 (2005-12-29)
parents fe039c5fd127
children 65430a8f80d7
files xen/common/rangeset.c
line diff
     1.1 --- a/xen/common/rangeset.c	Thu Dec 29 17:53:22 2005 +0100
     1.2 +++ b/xen/common/rangeset.c	Thu Dec 29 18:16:01 2005 +0100
     1.3 @@ -28,7 +28,7 @@ struct rangeset {
     1.4      /* Pretty-printing name. */
     1.5      char             name[32];
     1.6  
     1.7 -    /* RANGESETF_??? */
     1.8 +    /* RANGESETF flags. */
     1.9      unsigned int     flags;
    1.10  };
    1.11  
    1.12 @@ -48,6 +48,24 @@ static struct range *find_range(
    1.13      return x;
    1.14  }
    1.15  
    1.16 +/* Return the lowest range in the set r, or NULL if r is empty. */
    1.17 +static struct range *first_range(
    1.18 +    struct rangeset *r)
    1.19 +{
    1.20 +    if ( list_empty(&r->range_list) )
    1.21 +        return NULL;
    1.22 +    return list_entry(r->range_list.next, struct range, list);
    1.23 +}
    1.24 +
    1.25 +/* Return range following x in ascending order, or NULL if x is the highest. */
    1.26 +static struct range *next_range(
    1.27 +    struct rangeset *r, struct range *x)
    1.28 +{
    1.29 +    if ( x->list.next == &r->range_list )
    1.30 +        return NULL;
    1.31 +    return list_entry(x->list.next, struct range, list);
    1.32 +}
    1.33 +
    1.34  /* Remove a range from its list and free it. */
    1.35  static void destroy_range(
    1.36      struct range *x)
    1.37 @@ -91,12 +109,12 @@ int rangeset_add_range(
    1.38      {
    1.39          if ( x == NULL )
    1.40          {
    1.41 -            x = list_entry(r->range_list.next, struct range, list);
    1.42 +            x = first_range(r);
    1.43              x->s = s;
    1.44          }
    1.45          else if ( (x->e < s) && ((x->e + 1) != s) )
    1.46          {
    1.47 -            x = list_entry(x->list.next, struct range, list);
    1.48 +            x = next_range(r, x);
    1.49              x->s = s;
    1.50          }
    1.51          
    1.52 @@ -104,15 +122,15 @@ int rangeset_add_range(
    1.53  
    1.54          for ( ; ; )
    1.55          {
    1.56 -            y = list_entry(x->list.next, struct range, list);
    1.57 -            if ( (x->list.next == &r->range_list) || (y->e > x->e) )
    1.58 +            y = next_range(r, x);
    1.59 +            if ( (y == NULL) || (y->e > x->e) )
    1.60                  break;
    1.61              destroy_range(y);
    1.62          }
    1.63      }
    1.64  
    1.65 -    y = list_entry(x->list.next, struct range, list);
    1.66 -    if ( (x->list.next != &r->range_list) && ((x->e + 1) == y->s) )
    1.67 +    y = next_range(r, x);
    1.68 +    if ( (y != NULL) && ((x->e + 1) == y->s) )
    1.69      {
    1.70          x->e = y->e;
    1.71          destroy_range(y);
    1.72 @@ -162,18 +180,18 @@ int rangeset_remove_range(
    1.73      else
    1.74      {
    1.75          if ( x == NULL )
    1.76 -            x = list_entry(r->range_list.next, struct range, list);
    1.77 +            x = first_range(r);
    1.78  
    1.79          if ( x->s < s )
    1.80          {
    1.81              x->e = s - 1;
    1.82 -            x = list_entry(x->list.next, struct range, list);
    1.83 +            x = next_range(r, x);
    1.84          }
    1.85  
    1.86          while ( x != y )
    1.87          {
    1.88              t = x;
    1.89 -            x = list_entry(x->list.next, struct range, list);
    1.90 +            x = next_range(r, x);
    1.91              destroy_range(t);
    1.92          }
    1.93  
    1.94 @@ -257,6 +275,8 @@ struct rangeset *rangeset_new(
    1.95  void rangeset_destroy(
    1.96      struct rangeset *r)
    1.97  {
    1.98 +    struct range *x;
    1.99 +
   1.100      if ( r == NULL )
   1.101          return;
   1.102  
   1.103 @@ -267,11 +287,8 @@ void rangeset_destroy(
   1.104          spin_unlock(&r->domain->rangesets_lock);
   1.105      }
   1.106  
   1.107 -    while ( !list_empty(&r->range_list) )
   1.108 -    {
   1.109 -        struct range *x = list_entry(r->range_list.next, struct range, list);
   1.110 +    while ( (x = first_range(r)) != NULL )
   1.111          destroy_range(x);
   1.112 -    }
   1.113  
   1.114      xfree(r);
   1.115  }