Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

db_idspace.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2001-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: db_idspace.c,v 12.1 2005/06/16 20:20:53 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #include <stdlib.h>
00016 #endif
00017 
00018 #include "db_int.h"
00019 
00020 static int __db_idcmp __P((const void *, const void *));
00021 
00022 static int
00023 __db_idcmp(a, b)
00024         const void *a;
00025         const void *b;
00026 {
00027         u_int32_t i, j;
00028 
00029         i = *(u_int32_t *)a;
00030         j = *(u_int32_t *)b;
00031 
00032         if (i < j)
00033                 return (-1);
00034         else if (i > j)
00035                 return (1);
00036         else
00037                 return (0);
00038 }
00039 
00040 /*
00041  * __db_idspace --
00042  *
00043  * On input, minp and maxp contain the minimum and maximum valid values for
00044  * the name space and on return, they contain the minimum and maximum ids
00045  * available (by finding the biggest gap).  The minimum can be an inuse
00046  * value, but the maximum cannot be.
00047  *
00048  * PUBLIC: void __db_idspace __P((u_int32_t *, int, u_int32_t *, u_int32_t *));
00049  */
00050 void
00051 __db_idspace(inuse, n, minp, maxp)
00052         u_int32_t *inuse;
00053         int n;
00054         u_int32_t *minp, *maxp;
00055 {
00056         int i, low;
00057         u_int32_t gap, t;
00058 
00059         /* A single locker ID is a special case. */
00060         if (n == 1) {
00061                 /*
00062                  * If the single item in use is the last one in the range,
00063                  * then we've got to perform wrap which means that we set
00064                  * the min to the minimum ID, which is what we came in with,
00065                  * so we don't do anything.
00066                  */
00067                 if (inuse[0] != *maxp)
00068                         *minp = inuse[0];
00069                 *maxp = inuse[0] - 1;
00070                 return;
00071         }
00072 
00073         gap = 0;
00074         low = 0;
00075         qsort(inuse, (size_t)n, sizeof(u_int32_t), __db_idcmp);
00076         for (i = 0; i < n - 1; i++)
00077                 if ((t = (inuse[i + 1] - inuse[i])) > gap) {
00078                         gap = t;
00079                         low = i;
00080                 }
00081 
00082         /* Check for largest gap at the end of the space. */
00083         if ((*maxp - inuse[n - 1]) + (inuse[0] - *minp) > gap) {
00084                 /* Do same check as we do in the n == 1 case. */
00085                 if (inuse[n - 1] != *maxp)
00086                         *minp = inuse[n - 1];
00087                 *maxp = inuse[0] - 1;
00088         } else {
00089                 *minp = inuse[low];
00090                 *maxp = inuse[low + 1] - 1;
00091         }
00092 }

Generated on Sun Dec 25 12:14:17 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2