From d481ba55c02407124c499c7800ea556786137bc5 Mon Sep 17 00:00:00 2001 From: jbj Date: Wed, 14 Aug 2002 16:31:49 +0000 Subject: Initial revision CVS patchset: 5630 CVS date: 2002/08/14 16:31:49 --- db/common/db_idspace.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ db/common/util_cache.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 db/common/db_idspace.c create mode 100644 db/common/util_cache.c (limited to 'db/common') diff --git a/db/common/db_idspace.c b/db/common/db_idspace.c new file mode 100644 index 000000000..7defde82b --- /dev/null +++ b/db/common/db_idspace.c @@ -0,0 +1,93 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2001-2002 + * Sleepycat Software. All rights reserved. + */ + +#include "db_config.h" + +#ifndef lint +static const char revid[] = "Id: db_idspace.c,v 1.5 2002/02/01 18:15:29 bostic Exp "; +#endif /* not lint */ + +#ifndef NO_SYSTEM_INCLUDES +#include + +#include +#endif + +#include "db_int.h" + +static int __db_idcmp __P((const void *, const void *)); + +static int +__db_idcmp(a, b) + const void *a; + const void *b; +{ + u_int32_t i, j; + + i = *(u_int32_t *)a; + j = *(u_int32_t *)b; + + if (i < j) + return (-1); + else if (i > j) + return (1); + else + return (0); +} + +/* + * __db_idspace -- + * + * On input, minp and maxp contain the minimum and maximum valid values for + * the name space and on return, they contain the minimum and maximum ids + * available (by finding the biggest gap). + * + * PUBLIC: void __db_idspace __P((u_int32_t *, int, u_int32_t *, u_int32_t *)); + */ +void +__db_idspace(inuse, n, minp, maxp) + u_int32_t *inuse; + int n; + u_int32_t *minp, *maxp; +{ + int i, low; + u_int32_t gap, t; + + /* A single locker ID is a special case. */ + if (n == 1) { + /* + * If the single item in use is the last one in the range, + * then we've got to perform wrap which means that we set + * the min to the minimum ID, which is what we came in with, + * so we don't do anything. + */ + if (inuse[0] != *maxp) + *minp = inuse[0]; + *maxp = inuse[0] - 1; + return; + } + + gap = 0; + low = 0; + qsort(inuse, n, sizeof(u_int32_t), __db_idcmp); + for (i = 0; i < n - 1; i++) + if ((t = (inuse[i + 1] - inuse[i])) > gap) { + gap = t; + low = i; + } + + /* Check for largest gap at the end of the space. */ + if ((*maxp - inuse[n - 1]) + (inuse[0] - *minp) > gap) { + /* Do same check as we do in the n == 1 case. */ + if (inuse[n - 1] != *maxp) + *minp = inuse[n - 1]; + *maxp = inuse[0]; + } else { + *minp = inuse[low]; + *maxp = inuse[low + 1]; + } +} diff --git a/db/common/util_cache.c b/db/common/util_cache.c new file mode 100644 index 000000000..c8af63062 --- /dev/null +++ b/db/common/util_cache.c @@ -0,0 +1,92 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2000-2002 + * Sleepycat Software. All rights reserved. + */ + +#include "db_config.h" + +#ifndef lint +static const char revid[] = "Id: util_cache.c,v 1.3 2002/04/04 18:50:10 bostic Exp "; +#endif /* not lint */ + +#ifndef NO_SYSTEM_INCLUDES +#include + +#include + +#include +#include +#endif + +#include "db_int.h" + +/* + * __db_util_cache -- + * Compute if we have enough cache. + * + * PUBLIC: int __db_util_cache __P((DB_ENV *, DB *, u_int32_t *, int *)); + */ +int +__db_util_cache(dbenv, dbp, cachep, resizep) + DB_ENV *dbenv; + DB *dbp; + u_int32_t *cachep; + int *resizep; +{ + DBTYPE type; + DB_BTREE_STAT *bsp; + DB_HASH_STAT *hsp; + DB_QUEUE_STAT *qsp; + u_int32_t pgsize; + int ret; + void *sp; + + /* + * The current cache size is in cachep. If it's insufficient, set the + * the memory referenced by resizep to 1 and set cachep to the minimum + * size needed. + */ + if ((ret = dbp->get_type(dbp, &type)) != 0) { + dbenv->err(dbenv, ret, "DB->get_type"); + return (ret); + } + + if ((ret = dbp->stat(dbp, &sp, DB_FAST_STAT)) != 0) { + dbenv->err(dbenv, ret, "DB->stat"); + return (ret); + } + + switch (type) { + case DB_QUEUE: + qsp = (DB_QUEUE_STAT *)sp; + pgsize = qsp->qs_pagesize; + break; + case DB_HASH: + hsp = (DB_HASH_STAT *)sp; + pgsize = hsp->hash_pagesize; + break; + case DB_BTREE: + case DB_RECNO: + bsp = (DB_BTREE_STAT *)sp; + pgsize = bsp->bt_pagesize; + break; + default: + dbenv->err(dbenv, ret, "unknown database type: %d", type); + return (EINVAL); + } + free(sp); + + /* + * Make sure our current cache is big enough. We want at least + * DB_MINPAGECACHE pages in the cache. + */ + if ((*cachep / pgsize) < DB_MINPAGECACHE) { + *resizep = 1; + *cachep = pgsize * DB_MINPAGECACHE; + } else + *resizep = 0; + + return (0); +} -- cgit v1.2.3