Commit 5999e78f authored by Tom Lane's avatar Tom Lane

Another round of cleanups for dynahash.c (maybe it's finally clean of

portability issues).  Caller-visible data structures are now allocated
on MAXALIGN boundaries, allowing safe use of datatypes wider than 'long'.
Rejigger hash_create API so that caller specifies size of key and
total size of entry, not size of key and size of rest of entry.
This simplifies life considerably since each number is just a sizeof(),
and padding issues etc. are taken care of automatically.
parent f5817966
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Header: /cvsroot/pgsql/src/backend/access/transam/xlogutils.c,v 1.18 2001/08/25 18:52:41 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/access/transam/xlogutils.c,v 1.19 2001/10/01 05:36:13 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -228,9 +228,9 @@ _xl_init_rel_cache(void) ...@@ -228,9 +228,9 @@ _xl_init_rel_cache(void)
_xlrelarr[0].moreRecently = &(_xlrelarr[0]); _xlrelarr[0].moreRecently = &(_xlrelarr[0]);
_xlrelarr[0].lessRecently = &(_xlrelarr[0]); _xlrelarr[0].lessRecently = &(_xlrelarr[0]);
memset(&ctl, 0, (int) sizeof(ctl)); memset(&ctl, 0, sizeof(ctl));
ctl.keysize = sizeof(RelFileNode); ctl.keysize = sizeof(RelFileNode);
ctl.datasize = sizeof(XLogRelDesc *); ctl.entrysize = sizeof(XLogRelCacheEntry);
ctl.hash = tag_hash; ctl.hash = tag_hash;
_xlrelcache = hash_create(_XLOG_RELCACHESIZE, &ctl, _xlrelcache = hash_create(_XLOG_RELCACHESIZE, &ctl,
...@@ -249,7 +249,7 @@ _xl_remove_hash_entry(XLogRelDesc **edata, Datum dummy) ...@@ -249,7 +249,7 @@ _xl_remove_hash_entry(XLogRelDesc **edata, Datum dummy)
rdesc->moreRecently->lessRecently = rdesc->lessRecently; rdesc->moreRecently->lessRecently = rdesc->lessRecently;
hentry = (XLogRelCacheEntry *) hash_search(_xlrelcache, hentry = (XLogRelCacheEntry *) hash_search(_xlrelcache,
(char *) &(rdesc->reldata.rd_node), HASH_REMOVE, &found); (void *) &(rdesc->reldata.rd_node), HASH_REMOVE, &found);
if (hentry == NULL) if (hentry == NULL)
elog(STOP, "_xl_remove_hash_entry: can't delete from cache"); elog(STOP, "_xl_remove_hash_entry: can't delete from cache");
...@@ -321,7 +321,7 @@ XLogOpenRelation(bool redo, RmgrId rmid, RelFileNode rnode) ...@@ -321,7 +321,7 @@ XLogOpenRelation(bool redo, RmgrId rmid, RelFileNode rnode)
bool found; bool found;
hentry = (XLogRelCacheEntry *) hentry = (XLogRelCacheEntry *)
hash_search(_xlrelcache, (char *) &rnode, HASH_FIND, &found); hash_search(_xlrelcache, (void *) &rnode, HASH_FIND, &found);
if (hentry == NULL) if (hentry == NULL)
elog(STOP, "XLogOpenRelation: error in cache"); elog(STOP, "XLogOpenRelation: error in cache");
...@@ -345,7 +345,7 @@ XLogOpenRelation(bool redo, RmgrId rmid, RelFileNode rnode) ...@@ -345,7 +345,7 @@ XLogOpenRelation(bool redo, RmgrId rmid, RelFileNode rnode)
res->reldata.rd_node = rnode; res->reldata.rd_node = rnode;
hentry = (XLogRelCacheEntry *) hentry = (XLogRelCacheEntry *)
hash_search(_xlrelcache, (char *) &rnode, HASH_ENTER, &found); hash_search(_xlrelcache, (void *) &rnode, HASH_ENTER, &found);
if (hentry == NULL) if (hentry == NULL)
elog(STOP, "XLogOpenRelation: can't insert into cache"); elog(STOP, "XLogOpenRelation: can't insert into cache");
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
* *
* Copyright (c) 2001, PostgreSQL Global Development Group * Copyright (c) 2001, PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.7 2001/08/23 23:06:37 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.8 2001/10/01 05:36:13 tgl Exp $
* ---------- * ----------
*/ */
#include "postgres.h" #include "postgres.h"
...@@ -511,7 +511,8 @@ pgstat_vacuum_tabstat(void) ...@@ -511,7 +511,8 @@ pgstat_vacuum_tabstat(void)
* Lookup our own database entry * Lookup our own database entry
*/ */
dbentry = (PgStat_StatDBEntry *)hash_search(pgStatDBHash, dbentry = (PgStat_StatDBEntry *)hash_search(pgStatDBHash,
(char *)&MyDatabaseId, HASH_FIND, &found); (void *) &MyDatabaseId,
HASH_FIND, &found);
if (!found || dbentry == NULL) if (!found || dbentry == NULL)
return -1; return -1;
...@@ -926,8 +927,9 @@ pgstat_fetch_stat_dbentry(Oid dbid) ...@@ -926,8 +927,9 @@ pgstat_fetch_stat_dbentry(Oid dbid)
/* /*
* Lookup the requested database * Lookup the requested database
*/ */
dbentry = (PgStat_StatDBEntry *)hash_search(pgStatDBHash, dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
(char *)&dbid, HASH_FIND, &found); (void *) &dbid,
HASH_FIND, &found);
if (!found || dbentry == NULL) if (!found || dbentry == NULL)
return NULL; return NULL;
...@@ -966,8 +968,9 @@ pgstat_fetch_stat_tabentry(Oid relid) ...@@ -966,8 +968,9 @@ pgstat_fetch_stat_tabentry(Oid relid)
/* /*
* Lookup our database. * Lookup our database.
*/ */
dbentry = (PgStat_StatDBEntry *)hash_search(pgStatDBHash, dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
(char *)&MyDatabaseId, HASH_FIND, &found); (void *) &MyDatabaseId,
HASH_FIND, &found);
if (!found || dbentry == NULL) if (!found || dbentry == NULL)
return NULL; return NULL;
...@@ -976,8 +979,9 @@ pgstat_fetch_stat_tabentry(Oid relid) ...@@ -976,8 +979,9 @@ pgstat_fetch_stat_tabentry(Oid relid)
*/ */
if (dbentry->tables == NULL) if (dbentry->tables == NULL)
return NULL; return NULL;
tabentry = (PgStat_StatTabEntry *)hash_search(dbentry->tables, tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
(char *)&relid, HASH_FIND, &found); (void *) &relid,
HASH_FIND, &found);
if (!found || tabentry == NULL) if (!found || tabentry == NULL)
return NULL; return NULL;
...@@ -1197,9 +1201,9 @@ pgstat_main(int real_argc, char *real_argv[]) ...@@ -1197,9 +1201,9 @@ pgstat_main(int real_argc, char *real_argv[])
* Create the dead backend hashtable * Create the dead backend hashtable
*/ */
memset(&hash_ctl, 0, sizeof(hash_ctl)); memset(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.keysize = sizeof(int); hash_ctl.keysize = sizeof(int);
hash_ctl.datasize = sizeof(PgStat_StatBeDead); hash_ctl.entrysize = sizeof(PgStat_StatBeDead);
hash_ctl.hash = tag_hash; hash_ctl.hash = tag_hash;
pgStatBeDead = hash_create(PGSTAT_BE_HASH_SIZE, &hash_ctl, pgStatBeDead = hash_create(PGSTAT_BE_HASH_SIZE, &hash_ctl,
HASH_ELEM | HASH_FUNCTION); HASH_ELEM | HASH_FUNCTION);
if (pgStatBeDead == NULL) if (pgStatBeDead == NULL)
...@@ -1720,8 +1724,9 @@ pgstat_add_backend(PgStat_MsgHdr *msg) ...@@ -1720,8 +1724,9 @@ pgstat_add_backend(PgStat_MsgHdr *msg)
* *
* If the backend is known to be dead, we ignore this add. * If the backend is known to be dead, we ignore this add.
*/ */
deadbe = (PgStat_StatBeDead *)hash_search(pgStatBeDead, deadbe = (PgStat_StatBeDead *) hash_search(pgStatBeDead,
(char *)&(msg->m_procpid), HASH_FIND, &found); (void *) &(msg->m_procpid),
HASH_FIND, &found);
if (deadbe == NULL) if (deadbe == NULL)
{ {
fprintf(stderr, "PGSTAT: Dead backend table corrupted - abort\n"); fprintf(stderr, "PGSTAT: Dead backend table corrupted - abort\n");
...@@ -1747,9 +1752,9 @@ pgstat_add_backend(PgStat_MsgHdr *msg) ...@@ -1747,9 +1752,9 @@ pgstat_add_backend(PgStat_MsgHdr *msg)
/* /*
* Lookup or create the database entry for this backends DB. * Lookup or create the database entry for this backends DB.
*/ */
dbentry = (PgStat_StatDBEntry *)hash_search(pgStatDBHash, dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
(char *)&(msg->m_databaseid), HASH_ENTER, (void *) &(msg->m_databaseid),
&found); HASH_ENTER, &found);
if (dbentry == NULL) if (dbentry == NULL)
{ {
fprintf(stderr, "PGSTAT: DB hash table corrupted - abort\n"); fprintf(stderr, "PGSTAT: DB hash table corrupted - abort\n");
...@@ -1772,9 +1777,9 @@ pgstat_add_backend(PgStat_MsgHdr *msg) ...@@ -1772,9 +1777,9 @@ pgstat_add_backend(PgStat_MsgHdr *msg)
dbentry->destroy = 0; dbentry->destroy = 0;
memset(&hash_ctl, 0, sizeof(hash_ctl)); memset(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.keysize = sizeof(Oid); hash_ctl.keysize = sizeof(Oid);
hash_ctl.datasize = sizeof(PgStat_StatTabEntry); hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
hash_ctl.hash = tag_hash; hash_ctl.hash = tag_hash;
dbentry->tables = hash_create(PGSTAT_TAB_HASH_SIZE, &hash_ctl, dbentry->tables = hash_create(PGSTAT_TAB_HASH_SIZE, &hash_ctl,
HASH_ELEM | HASH_FUNCTION); HASH_ELEM | HASH_FUNCTION);
if (dbentry->tables == NULL) if (dbentry->tables == NULL)
...@@ -1827,8 +1832,9 @@ pgstat_sub_backend(int procpid) ...@@ -1827,8 +1832,9 @@ pgstat_sub_backend(int procpid)
* the counting of backends, not the table access stats they * the counting of backends, not the table access stats they
* sent). * sent).
*/ */
deadbe = (PgStat_StatBeDead *)hash_search(pgStatBeDead, deadbe = (PgStat_StatBeDead *) hash_search(pgStatBeDead,
(char *)&procpid, HASH_ENTER, &found); (void *) &procpid,
HASH_ENTER, &found);
if (deadbe == NULL) if (deadbe == NULL)
{ {
fprintf(stderr, "PGSTAT: dead backend hash table corrupted " fprintf(stderr, "PGSTAT: dead backend hash table corrupted "
...@@ -1914,8 +1920,8 @@ pgstat_write_statsfile(void) ...@@ -1914,8 +1920,8 @@ pgstat_write_statsfile(void)
hash_destroy(dbentry->tables); hash_destroy(dbentry->tables);
hentry = hash_search(pgStatDBHash, hentry = hash_search(pgStatDBHash,
(char *)&(dbentry->databaseid), (void *) &(dbentry->databaseid),
HASH_REMOVE, &found); HASH_REMOVE, &found);
if (hentry == NULL) if (hentry == NULL)
{ {
fprintf(stderr, "PGSTAT: database hash table corrupted " fprintf(stderr, "PGSTAT: database hash table corrupted "
...@@ -1959,7 +1965,7 @@ pgstat_write_statsfile(void) ...@@ -1959,7 +1965,7 @@ pgstat_write_statsfile(void)
if (--(tabentry->destroy) == 0) if (--(tabentry->destroy) == 0)
{ {
hentry = hash_search(dbentry->tables, hentry = hash_search(dbentry->tables,
(char *)&(tabentry->tableid), (void *) &(tabentry->tableid),
HASH_REMOVE, &found); HASH_REMOVE, &found);
if (hentry == NULL) if (hentry == NULL)
{ {
...@@ -2047,7 +2053,8 @@ pgstat_write_statsfile(void) ...@@ -2047,7 +2053,8 @@ pgstat_write_statsfile(void)
*/ */
if (--(deadbe->destroy) <= 0) if (--(deadbe->destroy) <= 0)
{ {
hentry = hash_search(pgStatBeDead, (char *)&(deadbe->procpid), hentry = hash_search(pgStatBeDead,
(void *) &(deadbe->procpid),
HASH_REMOVE, &found); HASH_REMOVE, &found);
if (hentry == NULL) if (hentry == NULL)
{ {
...@@ -2107,10 +2114,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2107,10 +2114,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
* Create the DB hashtable * Create the DB hashtable
*/ */
memset(&hash_ctl, 0, sizeof(hash_ctl)); memset(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.keysize = sizeof(Oid); hash_ctl.keysize = sizeof(Oid);
hash_ctl.datasize = sizeof(PgStat_StatDBEntry); hash_ctl.entrysize = sizeof(PgStat_StatDBEntry);
hash_ctl.hash = tag_hash; hash_ctl.hash = tag_hash;
hash_ctl.hcxt = use_mcxt; hash_ctl.hcxt = use_mcxt;
*dbhash = hash_create(PGSTAT_DB_HASH_SIZE, &hash_ctl, *dbhash = hash_create(PGSTAT_DB_HASH_SIZE, &hash_ctl,
HASH_ELEM | HASH_FUNCTION | mcxt_flags); HASH_ELEM | HASH_FUNCTION | mcxt_flags);
if (pgStatDBHash == NULL) if (pgStatDBHash == NULL)
...@@ -2175,8 +2182,8 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2175,8 +2182,8 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
/* /*
* Add to the DB hash * Add to the DB hash
*/ */
dbentry = (PgStat_StatDBEntry *)hash_search(*dbhash, dbentry = (PgStat_StatDBEntry *) hash_search(*dbhash,
(char *)&dbbuf.databaseid, (void *) &dbbuf.databaseid,
HASH_ENTER, &found); HASH_ENTER, &found);
if (dbentry == NULL) if (dbentry == NULL)
{ {
...@@ -2222,10 +2229,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2222,10 +2229,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
memset(&hash_ctl, 0, sizeof(hash_ctl)); memset(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.keysize = sizeof(Oid); hash_ctl.keysize = sizeof(Oid);
hash_ctl.datasize = sizeof(PgStat_StatTabEntry); hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
hash_ctl.hash = tag_hash; hash_ctl.hash = tag_hash;
hash_ctl.hcxt = use_mcxt; hash_ctl.hcxt = use_mcxt;
dbentry->tables = hash_create(PGSTAT_TAB_HASH_SIZE, &hash_ctl, dbentry->tables = hash_create(PGSTAT_TAB_HASH_SIZE, &hash_ctl,
HASH_ELEM | HASH_FUNCTION | mcxt_flags); HASH_ELEM | HASH_FUNCTION | mcxt_flags);
if (dbentry->tables == NULL) if (dbentry->tables == NULL)
...@@ -2286,8 +2293,8 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2286,8 +2293,8 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
if (tabhash == NULL) if (tabhash == NULL)
break; break;
tabentry = (PgStat_StatTabEntry *)hash_search(tabhash, tabentry = (PgStat_StatTabEntry *) hash_search(tabhash,
(char *)&tabbuf.tableid, (void *) &tabbuf.tableid,
HASH_ENTER, &found); HASH_ENTER, &found);
if (tabentry == NULL) if (tabentry == NULL)
{ {
...@@ -2411,7 +2418,7 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2411,7 +2418,7 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
* Count backends per database here. * Count backends per database here.
*/ */
dbentry = (PgStat_StatDBEntry *)hash_search(*dbhash, dbentry = (PgStat_StatDBEntry *)hash_search(*dbhash,
(char *)&((*betab)[havebackends].databaseid), (void *) &((*betab)[havebackends].databaseid),
HASH_FIND, &found); HASH_FIND, &found);
if (found) if (found)
dbentry->n_backends++; dbentry->n_backends++;
...@@ -2525,8 +2532,8 @@ pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len) ...@@ -2525,8 +2532,8 @@ pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
/* /*
* Lookup the database in the hashtable. * Lookup the database in the hashtable.
*/ */
dbentry = (PgStat_StatDBEntry *)hash_search(pgStatDBHash, dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
(char *)&(msg->m_hdr.m_databaseid), (void *) &(msg->m_hdr.m_databaseid),
HASH_FIND, &found); HASH_FIND, &found);
if (dbentry == NULL) if (dbentry == NULL)
{ {
...@@ -2551,8 +2558,8 @@ pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len) ...@@ -2551,8 +2558,8 @@ pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
*/ */
for (i = 0; i < msg->m_nentries; i++) for (i = 0; i < msg->m_nentries; i++)
{ {
tabentry = (PgStat_StatTabEntry *)hash_search(dbentry->tables, tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
(char *)&(tabmsg[i].t_id), (void *) &(tabmsg[i].t_id),
HASH_ENTER, &found); HASH_ENTER, &found);
if (tabentry == NULL) if (tabentry == NULL)
{ {
...@@ -2625,8 +2632,8 @@ pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len) ...@@ -2625,8 +2632,8 @@ pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len)
/* /*
* Lookup the database in the hashtable. * Lookup the database in the hashtable.
*/ */
dbentry = (PgStat_StatDBEntry *)hash_search(pgStatDBHash, dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
(char *)&(msg->m_hdr.m_databaseid), (void *) &(msg->m_hdr.m_databaseid),
HASH_FIND, &found); HASH_FIND, &found);
if (dbentry == NULL) if (dbentry == NULL)
{ {
...@@ -2648,8 +2655,8 @@ pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len) ...@@ -2648,8 +2655,8 @@ pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len)
*/ */
for (i = 0; i < msg->m_nentries; i++) for (i = 0; i < msg->m_nentries; i++)
{ {
tabentry = (PgStat_StatTabEntry *)hash_search(dbentry->tables, tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
(char *)&(msg->m_tableid[i]), (void *) &(msg->m_tableid[i]),
HASH_FIND, &found); HASH_FIND, &found);
if (tabentry == NULL) if (tabentry == NULL)
{ {
...@@ -2685,8 +2692,8 @@ pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len) ...@@ -2685,8 +2692,8 @@ pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len)
/* /*
* Lookup the database in the hashtable. * Lookup the database in the hashtable.
*/ */
dbentry = (PgStat_StatDBEntry *)hash_search(pgStatDBHash, dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
(char *)&(msg->m_databaseid), (void *) &(msg->m_databaseid),
HASH_FIND, &found); HASH_FIND, &found);
if (dbentry == NULL) if (dbentry == NULL)
{ {
...@@ -2725,8 +2732,8 @@ pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len) ...@@ -2725,8 +2732,8 @@ pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
/* /*
* Lookup the database in the hashtable. * Lookup the database in the hashtable.
*/ */
dbentry = (PgStat_StatDBEntry *)hash_search(pgStatDBHash, dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
(char *)&(msg->m_hdr.m_databaseid), (void *) &(msg->m_hdr.m_databaseid),
HASH_FIND, &found); HASH_FIND, &found);
if (dbentry == NULL) if (dbentry == NULL)
{ {
...@@ -2753,7 +2760,7 @@ pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len) ...@@ -2753,7 +2760,7 @@ pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
memset(&hash_ctl, 0, sizeof(hash_ctl)); memset(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.keysize = sizeof(Oid); hash_ctl.keysize = sizeof(Oid);
hash_ctl.datasize = sizeof(PgStat_StatTabEntry); hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
hash_ctl.hash = tag_hash; hash_ctl.hash = tag_hash;
dbentry->tables = hash_create(PGSTAT_TAB_HASH_SIZE, &hash_ctl, dbentry->tables = hash_create(PGSTAT_TAB_HASH_SIZE, &hash_ctl,
HASH_ELEM | HASH_FUNCTION); HASH_ELEM | HASH_FUNCTION);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.44 2001/09/29 04:02:22 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.45 2001/10/01 05:36:13 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -280,9 +280,7 @@ BufferShmemSize(void) ...@@ -280,9 +280,7 @@ BufferShmemSize(void)
int size = 0; int size = 0;
/* size of shmem index hash table */ /* size of shmem index hash table */
size += hash_estimate_size(SHMEM_INDEX_SIZE, size += hash_estimate_size(SHMEM_INDEX_SIZE, sizeof(ShmemIndexEnt));
SHMEM_INDEX_KEYSIZE,
SHMEM_INDEX_DATASIZE);
/* size of buffer descriptors */ /* size of buffer descriptors */
size += MAXALIGN((NBuffers + 1) * sizeof(BufferDesc)); size += MAXALIGN((NBuffers + 1) * sizeof(BufferDesc));
...@@ -291,9 +289,7 @@ BufferShmemSize(void) ...@@ -291,9 +289,7 @@ BufferShmemSize(void)
size += NBuffers * MAXALIGN(BLCKSZ); size += NBuffers * MAXALIGN(BLCKSZ);
/* size of buffer hash table */ /* size of buffer hash table */
size += hash_estimate_size(NBuffers, size += hash_estimate_size(NBuffers, sizeof(BufferLookupEnt));
sizeof(BufferTag),
sizeof(Buffer));
#ifdef BMTRACE #ifdef BMTRACE
size += (BMT_LIMIT * sizeof(bmtrace)) + sizeof(long); size += (BMT_LIMIT * sizeof(bmtrace)) + sizeof(long);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_table.c,v 1.22 2001/09/29 04:02:22 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_table.c,v 1.23 2001/10/01 05:36:13 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -33,54 +33,42 @@ ...@@ -33,54 +33,42 @@
static HTAB *SharedBufHash; static HTAB *SharedBufHash;
typedef struct lookup
{
BufferTag key;
Buffer id;
} LookupEnt;
/* /*
* Initialize shmem hash table for mapping buffers * Initialize shmem hash table for mapping buffers
*/ */
void void
InitBufTable() InitBufTable(void)
{ {
HASHCTL info; HASHCTL info;
int hash_flags;
/* assume lock is held */ /* assume lock is held */
/* BufferTag maps to Buffer */ /* BufferTag maps to Buffer */
info.keysize = sizeof(BufferTag); info.keysize = sizeof(BufferTag);
info.datasize = sizeof(Buffer); info.entrysize = sizeof(BufferLookupEnt);
info.hash = tag_hash; info.hash = tag_hash;
hash_flags = (HASH_ELEM | HASH_FUNCTION); SharedBufHash = ShmemInitHash("Shared Buffer Lookup Table",
NBuffers, NBuffers,
&info,
SharedBufHash = (HTAB *) ShmemInitHash("Shared Buffer Lookup Table", HASH_ELEM | HASH_FUNCTION);
NBuffers, NBuffers,
&info, hash_flags);
if (!SharedBufHash) if (!SharedBufHash)
{
elog(FATAL, "couldn't initialize shared buffer pool Hash Tbl"); elog(FATAL, "couldn't initialize shared buffer pool Hash Tbl");
exit(1);
}
} }
BufferDesc * BufferDesc *
BufTableLookup(BufferTag *tagPtr) BufTableLookup(BufferTag *tagPtr)
{ {
LookupEnt *result; BufferLookupEnt *result;
bool found; bool found;
if (tagPtr->blockNum == P_NEW) if (tagPtr->blockNum == P_NEW)
return NULL; return NULL;
result = (LookupEnt *) result = (BufferLookupEnt *)
hash_search(SharedBufHash, (char *) tagPtr, HASH_FIND, &found); hash_search(SharedBufHash, (void *) tagPtr, HASH_FIND, &found);
if (!result) if (!result)
{ {
...@@ -98,7 +86,7 @@ BufTableLookup(BufferTag *tagPtr) ...@@ -98,7 +86,7 @@ BufTableLookup(BufferTag *tagPtr)
bool bool
BufTableDelete(BufferDesc *buf) BufTableDelete(BufferDesc *buf)
{ {
LookupEnt *result; BufferLookupEnt *result;
bool found; bool found;
/* /*
...@@ -110,8 +98,8 @@ BufTableDelete(BufferDesc *buf) ...@@ -110,8 +98,8 @@ BufTableDelete(BufferDesc *buf)
buf->flags |= BM_DELETED; buf->flags |= BM_DELETED;
result = (LookupEnt *) result = (BufferLookupEnt *)
hash_search(SharedBufHash, (char *) &(buf->tag), HASH_REMOVE, &found); hash_search(SharedBufHash, (void *) &(buf->tag), HASH_REMOVE, &found);
if (!(result && found)) if (!(result && found))
{ {
...@@ -134,15 +122,15 @@ BufTableDelete(BufferDesc *buf) ...@@ -134,15 +122,15 @@ BufTableDelete(BufferDesc *buf)
bool bool
BufTableInsert(BufferDesc *buf) BufTableInsert(BufferDesc *buf)
{ {
LookupEnt *result; BufferLookupEnt *result;
bool found; bool found;
/* cannot insert it twice */ /* cannot insert it twice */
Assert(buf->flags & BM_DELETED); Assert(buf->flags & BM_DELETED);
buf->flags &= ~(BM_DELETED); buf->flags &= ~(BM_DELETED);
result = (LookupEnt *) result = (BufferLookupEnt *)
hash_search(SharedBufHash, (char *) &(buf->tag), HASH_ENTER, &found); hash_search(SharedBufHash, (void *) &(buf->tag), HASH_ENTER, &found);
if (!result) if (!result)
{ {
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/freespace/freespace.c,v 1.5 2001/09/29 04:02:23 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/freespace/freespace.c,v 1.6 2001/10/01 05:36:14 tgl Exp $
* *
* *
* NOTES: * NOTES:
...@@ -100,9 +100,6 @@ struct FSMRelation ...@@ -100,9 +100,6 @@ struct FSMRelation
FSMChunk *relChunks; /* linked list of page info chunks */ FSMChunk *relChunks; /* linked list of page info chunks */
}; };
#define SHMEM_FSMHASH_KEYSIZE sizeof(RelFileNode)
#define SHMEM_FSMHASH_DATASIZE (sizeof(FSMRelation) - SHMEM_FSMHASH_KEYSIZE)
/* /*
* Info about individual pages in a relation is stored in chunks to reduce * Info about individual pages in a relation is stored in chunks to reduce
* allocation overhead. Note that we allow any chunk of a relation's list * allocation overhead. Note that we allow any chunk of a relation's list
...@@ -180,8 +177,8 @@ InitFreeSpaceMap(void) ...@@ -180,8 +177,8 @@ InitFreeSpaceMap(void)
MemSet(FreeSpaceMap, 0, sizeof(FSMHeader)); MemSet(FreeSpaceMap, 0, sizeof(FSMHeader));
/* Create hashtable for FSMRelations */ /* Create hashtable for FSMRelations */
info.keysize = SHMEM_FSMHASH_KEYSIZE; info.keysize = sizeof(RelFileNode);
info.datasize = SHMEM_FSMHASH_DATASIZE; info.entrysize = sizeof(FSMRelation);
info.hash = tag_hash; info.hash = tag_hash;
FreeSpaceMap->relHash = ShmemInitHash("Free Space Map Hash", FreeSpaceMap->relHash = ShmemInitHash("Free Space Map Hash",
...@@ -224,9 +221,7 @@ FreeSpaceShmemSize(void) ...@@ -224,9 +221,7 @@ FreeSpaceShmemSize(void)
size = MAXALIGN(sizeof(FSMHeader)); size = MAXALIGN(sizeof(FSMHeader));
/* hash table, including the FSMRelation objects */ /* hash table, including the FSMRelation objects */
size += hash_estimate_size(MaxFSMRelations, size += hash_estimate_size(MaxFSMRelations, sizeof(FSMRelation));
SHMEM_FSMHASH_KEYSIZE,
SHMEM_FSMHASH_DATASIZE);
/* FSMChunk objects */ /* FSMChunk objects */
nchunks = (MaxFSMPages - 1) / CHUNKPAGES + 1; nchunks = (MaxFSMPages - 1) / CHUNKPAGES + 1;
...@@ -498,7 +493,7 @@ lookup_fsm_rel(RelFileNode *rel) ...@@ -498,7 +493,7 @@ lookup_fsm_rel(RelFileNode *rel)
bool found; bool found;
fsmrel = (FSMRelation *) hash_search(FreeSpaceMap->relHash, fsmrel = (FSMRelation *) hash_search(FreeSpaceMap->relHash,
(Pointer) rel, (void *) rel,
HASH_FIND, HASH_FIND,
&found); &found);
if (!fsmrel) if (!fsmrel)
...@@ -524,7 +519,7 @@ create_fsm_rel(RelFileNode *rel) ...@@ -524,7 +519,7 @@ create_fsm_rel(RelFileNode *rel)
bool found; bool found;
fsmrel = (FSMRelation *) hash_search(FreeSpaceMap->relHash, fsmrel = (FSMRelation *) hash_search(FreeSpaceMap->relHash,
(Pointer) rel, (void *) rel,
HASH_ENTER, HASH_ENTER,
&found); &found);
if (!fsmrel) if (!fsmrel)
...@@ -595,7 +590,7 @@ delete_fsm_rel(FSMRelation *fsmrel) ...@@ -595,7 +590,7 @@ delete_fsm_rel(FSMRelation *fsmrel)
unlink_fsm_rel(fsmrel); unlink_fsm_rel(fsmrel);
FreeSpaceMap->numRels--; FreeSpaceMap->numRels--;
result = (FSMRelation *) hash_search(FreeSpaceMap->relHash, result = (FSMRelation *) hash_search(FreeSpaceMap->relHash,
(Pointer) &(fsmrel->key), (void *) &(fsmrel->key),
HASH_REMOVE, HASH_REMOVE,
&found); &found);
if (!result || !found) if (!result || !found)
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.59 2001/09/29 04:02:23 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.60 2001/10/01 05:36:14 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -191,7 +191,7 @@ InitShmemIndex(void) ...@@ -191,7 +191,7 @@ InitShmemIndex(void)
/* create the shared memory shmem index */ /* create the shared memory shmem index */
info.keysize = SHMEM_INDEX_KEYSIZE; info.keysize = SHMEM_INDEX_KEYSIZE;
info.datasize = SHMEM_INDEX_DATASIZE; info.entrysize = sizeof(ShmemIndexEnt);
hash_flags = HASH_ELEM; hash_flags = HASH_ELEM;
/* This will acquire the shmem index lock, but not release it. */ /* This will acquire the shmem index lock, but not release it. */
...@@ -208,7 +208,7 @@ InitShmemIndex(void) ...@@ -208,7 +208,7 @@ InitShmemIndex(void)
strncpy(item.key, "ShmemIndex", SHMEM_INDEX_KEYSIZE); strncpy(item.key, "ShmemIndex", SHMEM_INDEX_KEYSIZE);
result = (ShmemIndexEnt *) result = (ShmemIndexEnt *)
hash_search(ShmemIndex, (char *) &item, HASH_ENTER, &found); hash_search(ShmemIndex, (void *) &item, HASH_ENTER, &found);
if (!result) if (!result)
elog(FATAL, "InitShmemIndex: corrupted shmem index"); elog(FATAL, "InitShmemIndex: corrupted shmem index");
...@@ -248,17 +248,15 @@ ShmemInitHash(char *name, /* table string name for shmem index */ ...@@ -248,17 +248,15 @@ ShmemInitHash(char *name, /* table string name for shmem index */
* can't grow or other backends wouldn't be able to find it. So, make * can't grow or other backends wouldn't be able to find it. So, make
* sure we make it big enough to start with. * sure we make it big enough to start with.
* *
* The segbase is for calculating pointer values. The shared memory * The shared memory allocator must be specified too.
* allocator must be specified too.
*/ */
infoP->dsize = infoP->max_dsize = hash_select_dirsize(max_size); infoP->dsize = infoP->max_dsize = hash_select_dirsize(max_size);
infoP->segbase = (long *) ShmemBase;
infoP->alloc = ShmemAlloc; infoP->alloc = ShmemAlloc;
hash_flags |= HASH_SHARED_MEM | HASH_DIRSIZE; hash_flags |= HASH_SHARED_MEM | HASH_DIRSIZE;
/* look it up in the shmem index */ /* look it up in the shmem index */
location = ShmemInitStruct(name, location = ShmemInitStruct(name,
sizeof(HHDR) + infoP->dsize * sizeof(SEG_OFFSET), sizeof(HASHHDR) + infoP->dsize * sizeof(HASHSEGMENT),
&found); &found);
/* /*
...@@ -266,18 +264,18 @@ ShmemInitHash(char *name, /* table string name for shmem index */ ...@@ -266,18 +264,18 @@ ShmemInitHash(char *name, /* table string name for shmem index */
* message since they have more information * message since they have more information
*/ */
if (location == NULL) if (location == NULL)
return 0; return NULL;
/* /*
* it already exists, attach to it rather than allocate and initialize * if it already exists, attach to it rather than allocate and initialize
* new space * new space
*/ */
if (found) if (found)
hash_flags |= HASH_ATTACH; hash_flags |= HASH_ATTACH;
/* Now provide the header and directory pointers */ /* Now provide the header and directory pointers */
infoP->hctl = (long *) location; infoP->hctl = (HASHHDR *) location;
infoP->dir = (long *) (((char *) location) + sizeof(HHDR)); infoP->dir = (HASHSEGMENT *) (((char *) location) + sizeof(HASHHDR));
return hash_create(init_size, infoP, hash_flags); return hash_create(init_size, infoP, hash_flags);
} }
...@@ -325,7 +323,7 @@ ShmemInitStruct(char *name, Size size, bool *foundPtr) ...@@ -325,7 +323,7 @@ ShmemInitStruct(char *name, Size size, bool *foundPtr)
/* look it up in the shmem index */ /* look it up in the shmem index */
result = (ShmemIndexEnt *) result = (ShmemIndexEnt *)
hash_search(ShmemIndex, (char *) &item, HASH_ENTER, foundPtr); hash_search(ShmemIndex, (void *) &item, HASH_ENTER, foundPtr);
if (!result) if (!result)
{ {
...@@ -359,7 +357,7 @@ ShmemInitStruct(char *name, Size size, bool *foundPtr) ...@@ -359,7 +357,7 @@ ShmemInitStruct(char *name, Size size, bool *foundPtr)
{ {
/* out of memory */ /* out of memory */
Assert(ShmemIndex); Assert(ShmemIndex);
hash_search(ShmemIndex, (char *) &item, HASH_REMOVE, foundPtr); hash_search(ShmemIndex, (void *) &item, HASH_REMOVE, foundPtr);
LWLockRelease(ShmemIndexLock); LWLockRelease(ShmemIndexLock);
*foundPtr = FALSE; *foundPtr = FALSE;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.98 2001/09/30 00:45:47 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.99 2001/10/01 05:36:14 tgl Exp $
* *
* NOTES * NOTES
* Outside modules can create a lock table and acquire/release * Outside modules can create a lock table and acquire/release
...@@ -308,8 +308,8 @@ LockMethodTableInit(char *tabName, ...@@ -308,8 +308,8 @@ LockMethodTableInit(char *tabName,
* allocate a hash table for LOCK structs. This is used to store * allocate a hash table for LOCK structs. This is used to store
* per-locked-object information. * per-locked-object information.
*/ */
info.keysize = SHMEM_LOCKTAB_KEYSIZE; info.keysize = sizeof(LOCKTAG);
info.datasize = SHMEM_LOCKTAB_DATASIZE; info.entrysize = sizeof(LOCK);
info.hash = tag_hash; info.hash = tag_hash;
hash_flags = (HASH_ELEM | HASH_FUNCTION); hash_flags = (HASH_ELEM | HASH_FUNCTION);
...@@ -328,8 +328,8 @@ LockMethodTableInit(char *tabName, ...@@ -328,8 +328,8 @@ LockMethodTableInit(char *tabName,
* allocate a hash table for HOLDER structs. This is used to store * allocate a hash table for HOLDER structs. This is used to store
* per-lock-holder information. * per-lock-holder information.
*/ */
info.keysize = SHMEM_HOLDERTAB_KEYSIZE; info.keysize = sizeof(HOLDERTAG);
info.datasize = SHMEM_HOLDERTAB_DATASIZE; info.entrysize = sizeof(HOLDER);
info.hash = tag_hash; info.hash = tag_hash;
hash_flags = (HASH_ELEM | HASH_FUNCTION); hash_flags = (HASH_ELEM | HASH_FUNCTION);
...@@ -485,7 +485,8 @@ LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag, ...@@ -485,7 +485,8 @@ LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
* Find or create a lock with this tag * Find or create a lock with this tag
*/ */
Assert(lockMethodTable->lockHash->hash == tag_hash); Assert(lockMethodTable->lockHash->hash == tag_hash);
lock = (LOCK *) hash_search(lockMethodTable->lockHash, (Pointer) locktag, lock = (LOCK *) hash_search(lockMethodTable->lockHash,
(void *) locktag,
HASH_ENTER, &found); HASH_ENTER, &found);
if (!lock) if (!lock)
{ {
...@@ -530,7 +531,8 @@ LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag, ...@@ -530,7 +531,8 @@ LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
* Find or create a holder entry with this tag * Find or create a holder entry with this tag
*/ */
holderTable = lockMethodTable->holderHash; holderTable = lockMethodTable->holderHash;
holder = (HOLDER *) hash_search(holderTable, (Pointer) &holdertag, holder = (HOLDER *) hash_search(holderTable,
(void *) &holdertag,
HASH_ENTER, &found); HASH_ENTER, &found);
if (!holder) if (!holder)
{ {
...@@ -655,7 +657,7 @@ LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag, ...@@ -655,7 +657,7 @@ LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
SHMQueueDelete(&holder->lockLink); SHMQueueDelete(&holder->lockLink);
SHMQueueDelete(&holder->procLink); SHMQueueDelete(&holder->procLink);
holder = (HOLDER *) hash_search(holderTable, holder = (HOLDER *) hash_search(holderTable,
(Pointer) holder, (void *) holder,
HASH_REMOVE, &found); HASH_REMOVE, &found);
if (!holder || !found) if (!holder || !found)
elog(NOTICE, "LockAcquire: remove holder, table corrupted"); elog(NOTICE, "LockAcquire: remove holder, table corrupted");
...@@ -1019,7 +1021,8 @@ LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag, ...@@ -1019,7 +1021,8 @@ LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
* Find a lock with this tag * Find a lock with this tag
*/ */
Assert(lockMethodTable->lockHash->hash == tag_hash); Assert(lockMethodTable->lockHash->hash == tag_hash);
lock = (LOCK *) hash_search(lockMethodTable->lockHash, (Pointer) locktag, lock = (LOCK *) hash_search(lockMethodTable->lockHash,
(void *) locktag,
HASH_FIND, &found); HASH_FIND, &found);
/* /*
...@@ -1051,7 +1054,8 @@ LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag, ...@@ -1051,7 +1054,8 @@ LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
TransactionIdStore(xid, &holdertag.xid); TransactionIdStore(xid, &holdertag.xid);
holderTable = lockMethodTable->holderHash; holderTable = lockMethodTable->holderHash;
holder = (HOLDER *) hash_search(holderTable, (Pointer) &holdertag, holder = (HOLDER *) hash_search(holderTable,
(void *) &holdertag,
HASH_FIND_SAVE, &found); HASH_FIND_SAVE, &found);
if (!holder || !found) if (!holder || !found)
{ {
...@@ -1124,7 +1128,7 @@ LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag, ...@@ -1124,7 +1128,7 @@ LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
*/ */
Assert(lockMethodTable->lockHash->hash == tag_hash); Assert(lockMethodTable->lockHash->hash == tag_hash);
lock = (LOCK *) hash_search(lockMethodTable->lockHash, lock = (LOCK *) hash_search(lockMethodTable->lockHash,
(Pointer) &(lock->tag), (void *) &(lock->tag),
HASH_REMOVE, HASH_REMOVE,
&found); &found);
if (!lock || !found) if (!lock || !found)
...@@ -1153,7 +1157,8 @@ LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag, ...@@ -1153,7 +1157,8 @@ LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
HOLDER_PRINT("LockRelease: deleting", holder); HOLDER_PRINT("LockRelease: deleting", holder);
SHMQueueDelete(&holder->lockLink); SHMQueueDelete(&holder->lockLink);
SHMQueueDelete(&holder->procLink); SHMQueueDelete(&holder->procLink);
holder = (HOLDER *) hash_search(holderTable, (Pointer) &holder, holder = (HOLDER *) hash_search(holderTable,
(void *) &holder,
HASH_REMOVE_SAVED, &found); HASH_REMOVE_SAVED, &found);
if (!holder || !found) if (!holder || !found)
{ {
...@@ -1306,7 +1311,7 @@ LockReleaseAll(LOCKMETHOD lockmethod, PROC *proc, ...@@ -1306,7 +1311,7 @@ LockReleaseAll(LOCKMETHOD lockmethod, PROC *proc,
* remove the holder entry from the hashtable * remove the holder entry from the hashtable
*/ */
holder = (HOLDER *) hash_search(lockMethodTable->holderHash, holder = (HOLDER *) hash_search(lockMethodTable->holderHash,
(Pointer) holder, (void *) holder,
HASH_REMOVE, HASH_REMOVE,
&found); &found);
if (!holder || !found) if (!holder || !found)
...@@ -1326,7 +1331,7 @@ LockReleaseAll(LOCKMETHOD lockmethod, PROC *proc, ...@@ -1326,7 +1331,7 @@ LockReleaseAll(LOCKMETHOD lockmethod, PROC *proc,
LOCK_PRINT("LockReleaseAll: deleting", lock, 0); LOCK_PRINT("LockReleaseAll: deleting", lock, 0);
Assert(lockMethodTable->lockHash->hash == tag_hash); Assert(lockMethodTable->lockHash->hash == tag_hash);
lock = (LOCK *) hash_search(lockMethodTable->lockHash, lock = (LOCK *) hash_search(lockMethodTable->lockHash,
(Pointer) &(lock->tag), (void *) &(lock->tag),
HASH_REMOVE, &found); HASH_REMOVE, &found);
if (!lock || !found) if (!lock || !found)
{ {
...@@ -1364,14 +1369,10 @@ LockShmemSize(int maxBackends) ...@@ -1364,14 +1369,10 @@ LockShmemSize(int maxBackends)
* lockMethodTable->ctl */ * lockMethodTable->ctl */
/* lockHash table */ /* lockHash table */
size += hash_estimate_size(max_table_size, size += hash_estimate_size(max_table_size, sizeof(LOCK));
SHMEM_LOCKTAB_KEYSIZE,
SHMEM_LOCKTAB_DATASIZE);
/* holderHash table */ /* holderHash table */
size += hash_estimate_size(max_table_size, size += hash_estimate_size(max_table_size, sizeof(HOLDER));
SHMEM_HOLDERTAB_KEYSIZE,
SHMEM_HOLDERTAB_DATASIZE);
/* /*
* Since the lockHash entry count above is only an estimate, add 10% * Since the lockHash entry count above is only an estimate, add 10%
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/smgr/Attic/mm.c,v 1.25 2001/09/29 04:02:25 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/smgr/Attic/mm.c,v 1.26 2001/10/01 05:36:15 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -103,12 +103,12 @@ mminit() ...@@ -103,12 +103,12 @@ mminit()
} }
info.keysize = sizeof(MMCacheTag); info.keysize = sizeof(MMCacheTag);
info.datasize = sizeof(MMHashEntry) - sizeof(MMCacheTag); info.entrysize = sizeof(MMHashEntry);
info.hash = tag_hash; info.hash = tag_hash;
MMCacheHT = (HTAB *) ShmemInitHash("Main memory store HT", MMCacheHT = ShmemInitHash("Main memory store HT",
MMNBUFFERS, MMNBUFFERS, MMNBUFFERS, MMNBUFFERS,
&info, (HASH_ELEM | HASH_FUNCTION)); &info, (HASH_ELEM | HASH_FUNCTION));
if (MMCacheHT == (HTAB *) NULL) if (MMCacheHT == (HTAB *) NULL)
{ {
...@@ -117,12 +117,12 @@ mminit() ...@@ -117,12 +117,12 @@ mminit()
} }
info.keysize = sizeof(MMRelTag); info.keysize = sizeof(MMRelTag);
info.datasize = sizeof(MMRelHashEntry) - sizeof(MMRelTag); info.entrysize = sizeof(MMRelHashEntry);
info.hash = tag_hash; info.hash = tag_hash;
MMRelCacheHT = (HTAB *) ShmemInitHash("Main memory rel HT", MMRelCacheHT = ShmemInitHash("Main memory rel HT",
MMNRELATIONS, MMNRELATIONS, MMNRELATIONS, MMNRELATIONS,
&info, (HASH_ELEM | HASH_FUNCTION)); &info, (HASH_ELEM | HASH_FUNCTION));
if (MMRelCacheHT == (HTAB *) NULL) if (MMRelCacheHT == (HTAB *) NULL)
{ {
...@@ -180,7 +180,8 @@ mmcreate(Relation reln) ...@@ -180,7 +180,8 @@ mmcreate(Relation reln)
tag.mmrt_dbid = MyDatabaseId; tag.mmrt_dbid = MyDatabaseId;
entry = (MMRelHashEntry *) hash_search(MMRelCacheHT, entry = (MMRelHashEntry *) hash_search(MMRelCacheHT,
(char *) &tag, HASH_ENTER, &found); (void *) &tag,
HASH_ENTER, &found);
if (entry == (MMRelHashEntry *) NULL) if (entry == (MMRelHashEntry *) NULL)
{ {
...@@ -224,7 +225,7 @@ mmunlink(RelFileNode rnode) ...@@ -224,7 +225,7 @@ mmunlink(RelFileNode rnode)
&& MMBlockTags[i].mmct_relid == rnode.relNode) && MMBlockTags[i].mmct_relid == rnode.relNode)
{ {
entry = (MMHashEntry *) hash_search(MMCacheHT, entry = (MMHashEntry *) hash_search(MMCacheHT,
(char *) &MMBlockTags[i], (void *) &MMBlockTags[i],
HASH_REMOVE, &found); HASH_REMOVE, &found);
if (entry == (MMHashEntry *) NULL || !found) if (entry == (MMHashEntry *) NULL || !found)
{ {
...@@ -239,7 +240,8 @@ mmunlink(RelFileNode rnode) ...@@ -239,7 +240,8 @@ mmunlink(RelFileNode rnode)
rtag.mmrt_dbid = rnode.tblNode; rtag.mmrt_dbid = rnode.tblNode;
rtag.mmrt_relid = rnode.relNode; rtag.mmrt_relid = rnode.relNode;
rentry = (MMRelHashEntry *) hash_search(MMRelCacheHT, (char *) &rtag, rentry = (MMRelHashEntry *) hash_search(MMRelCacheHT,
(void *) &rtag,
HASH_REMOVE, &found); HASH_REMOVE, &found);
if (rentry == (MMRelHashEntry *) NULL || !found) if (rentry == (MMRelHashEntry *) NULL || !found)
...@@ -302,7 +304,8 @@ mmextend(Relation reln, BlockNumber blocknum, char *buffer) ...@@ -302,7 +304,8 @@ mmextend(Relation reln, BlockNumber blocknum, char *buffer)
(*MMCurTop)++; (*MMCurTop)++;
} }
rentry = (MMRelHashEntry *) hash_search(MMRelCacheHT, (char *) &rtag, rentry = (MMRelHashEntry *) hash_search(MMRelCacheHT,
(void *) &rtag,
HASH_FIND, &found); HASH_FIND, &found);
if (rentry == (MMRelHashEntry *) NULL || !found) if (rentry == (MMRelHashEntry *) NULL || !found)
{ {
...@@ -312,7 +315,8 @@ mmextend(Relation reln, BlockNumber blocknum, char *buffer) ...@@ -312,7 +315,8 @@ mmextend(Relation reln, BlockNumber blocknum, char *buffer)
tag.mmct_blkno = rentry->mmrhe_nblocks; tag.mmct_blkno = rentry->mmrhe_nblocks;
entry = (MMHashEntry *) hash_search(MMCacheHT, (char *) &tag, entry = (MMHashEntry *) hash_search(MMCacheHT,
(void *) &tag,
HASH_ENTER, &found); HASH_ENTER, &found);
if (entry == (MMHashEntry *) NULL || found) if (entry == (MMHashEntry *) NULL || found)
{ {
...@@ -381,7 +385,8 @@ mmread(Relation reln, BlockNumber blocknum, char *buffer) ...@@ -381,7 +385,8 @@ mmread(Relation reln, BlockNumber blocknum, char *buffer)
tag.mmct_blkno = blocknum; tag.mmct_blkno = blocknum;
LWLockAcquire(MMCacheLock, LW_EXCLUSIVE); LWLockAcquire(MMCacheLock, LW_EXCLUSIVE);
entry = (MMHashEntry *) hash_search(MMCacheHT, (char *) &tag, entry = (MMHashEntry *) hash_search(MMCacheHT,
(void *) &tag,
HASH_FIND, &found); HASH_FIND, &found);
if (entry == (MMHashEntry *) NULL) if (entry == (MMHashEntry *) NULL)
...@@ -428,7 +433,8 @@ mmwrite(Relation reln, BlockNumber blocknum, char *buffer) ...@@ -428,7 +433,8 @@ mmwrite(Relation reln, BlockNumber blocknum, char *buffer)
tag.mmct_blkno = blocknum; tag.mmct_blkno = blocknum;
LWLockAcquire(MMCacheLock, LW_EXCLUSIVE); LWLockAcquire(MMCacheLock, LW_EXCLUSIVE);
entry = (MMHashEntry *) hash_search(MMCacheHT, (char *) &tag, entry = (MMHashEntry *) hash_search(MMCacheHT,
(void *) &tag,
HASH_FIND, &found); HASH_FIND, &found);
if (entry == (MMHashEntry *) NULL) if (entry == (MMHashEntry *) NULL)
...@@ -502,7 +508,8 @@ mmnblocks(Relation reln) ...@@ -502,7 +508,8 @@ mmnblocks(Relation reln)
LWLockAcquire(MMCacheLock, LW_EXCLUSIVE); LWLockAcquire(MMCacheLock, LW_EXCLUSIVE);
rentry = (MMRelHashEntry *) hash_search(MMRelCacheHT, (char *) &rtag, rentry = (MMRelHashEntry *) hash_search(MMRelCacheHT,
(void *) &rtag,
HASH_FIND, &found); HASH_FIND, &found);
if (rentry == (MMRelHashEntry *) NULL) if (rentry == (MMRelHashEntry *) NULL)
...@@ -558,16 +565,12 @@ MMShmemSize() ...@@ -558,16 +565,12 @@ MMShmemSize()
/* /*
* first compute space occupied by the (dbid,relid,blkno) hash table * first compute space occupied by the (dbid,relid,blkno) hash table
*/ */
size += hash_estimate_size(MMNBUFFERS, size += hash_estimate_size(MMNBUFFERS, sizeof(MMHashEntry));
0, /* MMHashEntry includes key */
sizeof(MMHashEntry));
/* /*
* now do the same for the rel hash table * now do the same for the rel hash table
*/ */
size += hash_estimate_size(MMNRELATIONS, size += hash_estimate_size(MMNRELATIONS, sizeof(MMRelHashEntry));
0, /* MMRelHashEntry includes key */
sizeof(MMRelHashEntry));
/* /*
* finally, add in the memory block we use directly * finally, add in the memory block we use directly
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
* Portions Copyright (c) 2000-2001, PostgreSQL Global Development Group * Portions Copyright (c) 2000-2001, PostgreSQL Global Development Group
* Copyright 1999 Jan Wieck * Copyright 1999 Jan Wieck
* *
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.25 2001/05/31 17:32:33 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.26 2001/10/01 05:36:16 tgl Exp $
* *
* ---------- * ----------
*/ */
...@@ -2988,12 +2988,13 @@ ri_InitHashTables(void) ...@@ -2988,12 +2988,13 @@ ri_InitHashTables(void)
memset(&ctl, 0, sizeof(ctl)); memset(&ctl, 0, sizeof(ctl));
ctl.keysize = sizeof(RI_QueryKey); ctl.keysize = sizeof(RI_QueryKey);
ctl.datasize = sizeof(void *); ctl.entrysize = sizeof(RI_QueryHashEntry);
ri_query_cache = hash_create(RI_INIT_QUERYHASHSIZE, &ctl, HASH_ELEM); ctl.hash = tag_hash;
ri_query_cache = hash_create(RI_INIT_QUERYHASHSIZE, &ctl,
HASH_ELEM | HASH_FUNCTION);
memset(&ctl, 0, sizeof(ctl));
ctl.keysize = sizeof(Oid); ctl.keysize = sizeof(Oid);
ctl.datasize = sizeof(Oid) + sizeof(FmgrInfo); ctl.entrysize = sizeof(RI_OpreqHashEntry);
ctl.hash = tag_hash; ctl.hash = tag_hash;
ri_opreq_cache = hash_create(RI_INIT_OPREQHASHSIZE, &ctl, ri_opreq_cache = hash_create(RI_INIT_OPREQHASHSIZE, &ctl,
HASH_ELEM | HASH_FUNCTION); HASH_ELEM | HASH_FUNCTION);
...@@ -3023,7 +3024,8 @@ ri_FetchPreparedPlan(RI_QueryKey *key) ...@@ -3023,7 +3024,8 @@ ri_FetchPreparedPlan(RI_QueryKey *key)
* Lookup for the key * Lookup for the key
*/ */
entry = (RI_QueryHashEntry *) hash_search(ri_query_cache, entry = (RI_QueryHashEntry *) hash_search(ri_query_cache,
(char *) key, HASH_FIND, &found); (void *) key,
HASH_FIND, &found);
if (entry == NULL) if (entry == NULL)
elog(FATAL, "error in RI plan cache"); elog(FATAL, "error in RI plan cache");
if (!found) if (!found)
...@@ -3054,7 +3056,8 @@ ri_HashPreparedPlan(RI_QueryKey *key, void *plan) ...@@ -3054,7 +3056,8 @@ ri_HashPreparedPlan(RI_QueryKey *key, void *plan)
* Add the new plan. * Add the new plan.
*/ */
entry = (RI_QueryHashEntry *) hash_search(ri_query_cache, entry = (RI_QueryHashEntry *) hash_search(ri_query_cache,
(char *) key, HASH_ENTER, &found); (void *) key,
HASH_ENTER, &found);
if (entry == NULL) if (entry == NULL)
elog(FATAL, "can't insert into RI plan cache"); elog(FATAL, "can't insert into RI plan cache");
entry->plan = plan; entry->plan = plan;
...@@ -3224,14 +3227,15 @@ ri_AttributesEqual(Oid typeid, Datum oldvalue, Datum newvalue) ...@@ -3224,14 +3227,15 @@ ri_AttributesEqual(Oid typeid, Datum oldvalue, Datum newvalue)
/* /*
* On the first call initialize the hashtable * On the first call initialize the hashtable
*/ */
if (!ri_query_cache) if (!ri_opreq_cache)
ri_InitHashTables(); ri_InitHashTables();
/* /*
* Try to find the '=' operator for this type in our cache * Try to find the '=' operator for this type in our cache
*/ */
entry = (RI_OpreqHashEntry *) hash_search(ri_opreq_cache, entry = (RI_OpreqHashEntry *) hash_search(ri_opreq_cache,
(char *) &typeid, HASH_FIND, &found); (void *) &typeid,
HASH_FIND, &found);
if (entry == NULL) if (entry == NULL)
elog(FATAL, "error in RI operator cache"); elog(FATAL, "error in RI operator cache");
...@@ -3271,9 +3275,8 @@ ri_AttributesEqual(Oid typeid, Datum oldvalue, Datum newvalue) ...@@ -3271,9 +3275,8 @@ ri_AttributesEqual(Oid typeid, Datum oldvalue, Datum newvalue)
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
entry = (RI_OpreqHashEntry *) hash_search(ri_opreq_cache, entry = (RI_OpreqHashEntry *) hash_search(ri_opreq_cache,
(char *) &typeid, (void *) &typeid,
HASH_ENTER, HASH_ENTER, &found);
&found);
if (entry == NULL) if (entry == NULL)
elog(FATAL, "can't insert into RI operator cache"); elog(FATAL, "can't insert into RI operator cache");
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.143 2001/08/25 18:52:42 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.144 2001/10/01 05:36:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -132,7 +132,7 @@ typedef struct relnodecacheent ...@@ -132,7 +132,7 @@ typedef struct relnodecacheent
} RelNodeCacheEnt; } RelNodeCacheEnt;
/* /*
* macros to manipulate name cache and id cache * macros to manipulate the lookup hashtables
*/ */
#define RelationCacheInsert(RELATION) \ #define RelationCacheInsert(RELATION) \
do { \ do { \
...@@ -149,7 +149,7 @@ do { \ ...@@ -149,7 +149,7 @@ do { \
/* used to give notice -- now just keep quiet */ ; \ /* used to give notice -- now just keep quiet */ ; \
namehentry->reldesc = RELATION; \ namehentry->reldesc = RELATION; \
idhentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \ idhentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \
(char *)&(RELATION->rd_id), \ (void *) &(RELATION->rd_id), \
HASH_ENTER, \ HASH_ENTER, \
&found); \ &found); \
if (idhentry == NULL) \ if (idhentry == NULL) \
...@@ -158,7 +158,7 @@ do { \ ...@@ -158,7 +158,7 @@ do { \
/* used to give notice -- now just keep quiet */ ; \ /* used to give notice -- now just keep quiet */ ; \
idhentry->reldesc = RELATION; \ idhentry->reldesc = RELATION; \
nodentry = (RelNodeCacheEnt*)hash_search(RelationNodeCache, \ nodentry = (RelNodeCacheEnt*)hash_search(RelationNodeCache, \
(char *)&(RELATION->rd_node), \ (void *) &(RELATION->rd_node), \
HASH_ENTER, \ HASH_ENTER, \
&found); \ &found); \
if (nodentry == NULL) \ if (nodentry == NULL) \
...@@ -172,7 +172,7 @@ do { \ ...@@ -172,7 +172,7 @@ do { \
do { \ do { \
RelNameCacheEnt *hentry; bool found; \ RelNameCacheEnt *hentry; bool found; \
hentry = (RelNameCacheEnt*)hash_search(RelationNameCache, \ hentry = (RelNameCacheEnt*)hash_search(RelationNameCache, \
(char *)NAME,HASH_FIND,&found); \ (void *) (NAME),HASH_FIND,&found); \
if (hentry == NULL) \ if (hentry == NULL) \
elog(FATAL, "error in CACHE"); \ elog(FATAL, "error in CACHE"); \
if (found) \ if (found) \
...@@ -186,7 +186,7 @@ do { \ ...@@ -186,7 +186,7 @@ do { \
RelIdCacheEnt *hentry; \ RelIdCacheEnt *hentry; \
bool found; \ bool found; \
hentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \ hentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \
(char *)&(ID),HASH_FIND, &found); \ (void *)&(ID),HASH_FIND, &found); \
if (hentry == NULL) \ if (hentry == NULL) \
elog(FATAL, "error in CACHE"); \ elog(FATAL, "error in CACHE"); \
if (found) \ if (found) \
...@@ -200,7 +200,7 @@ do { \ ...@@ -200,7 +200,7 @@ do { \
RelNodeCacheEnt *hentry; \ RelNodeCacheEnt *hentry; \
bool found; \ bool found; \
hentry = (RelNodeCacheEnt*)hash_search(RelationNodeCache, \ hentry = (RelNodeCacheEnt*)hash_search(RelationNodeCache, \
(char *)&(NODE),HASH_FIND, &found); \ (void *)&(NODE),HASH_FIND, &found); \
if (hentry == NULL) \ if (hentry == NULL) \
elog(FATAL, "error in CACHE"); \ elog(FATAL, "error in CACHE"); \
if (found) \ if (found) \
...@@ -223,14 +223,14 @@ do { \ ...@@ -223,14 +223,14 @@ do { \
if (!found) \ if (!found) \
elog(NOTICE, "trying to delete a reldesc that does not exist."); \ elog(NOTICE, "trying to delete a reldesc that does not exist."); \
idhentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \ idhentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \
(char *)&(RELATION->rd_id), \ (void *)&(RELATION->rd_id), \
HASH_REMOVE, &found); \ HASH_REMOVE, &found); \
if (idhentry == NULL) \ if (idhentry == NULL) \
elog(FATAL, "can't delete from relation descriptor cache"); \ elog(FATAL, "can't delete from relation descriptor cache"); \
if (!found) \ if (!found) \
elog(NOTICE, "trying to delete a reldesc that does not exist."); \ elog(NOTICE, "trying to delete a reldesc that does not exist."); \
nodentry = (RelNodeCacheEnt*)hash_search(RelationNodeCache, \ nodentry = (RelNodeCacheEnt*)hash_search(RelationNodeCache, \
(char *)&(RELATION->rd_node), \ (void *)&(RELATION->rd_node), \
HASH_REMOVE, &found); \ HASH_REMOVE, &found); \
if (nodentry == NULL) \ if (nodentry == NULL) \
elog(FATAL, "can't delete from relation descriptor cache"); \ elog(FATAL, "can't delete from relation descriptor cache"); \
...@@ -2092,17 +2092,19 @@ RelationCacheInitialize(void) ...@@ -2092,17 +2092,19 @@ RelationCacheInitialize(void)
/* /*
* create global caches * create global caches
*/ */
MemSet(&ctl, 0, (int) sizeof(ctl)); MemSet(&ctl, 0, sizeof(ctl));
ctl.keysize = sizeof(NameData); ctl.keysize = sizeof(NameData);
ctl.datasize = sizeof(Relation); ctl.entrysize = sizeof(RelNameCacheEnt);
RelationNameCache = hash_create(INITRELCACHESIZE, &ctl, HASH_ELEM); RelationNameCache = hash_create(INITRELCACHESIZE, &ctl, HASH_ELEM);
ctl.keysize = sizeof(Oid); ctl.keysize = sizeof(Oid);
ctl.entrysize = sizeof(RelIdCacheEnt);
ctl.hash = tag_hash; ctl.hash = tag_hash;
RelationIdCache = hash_create(INITRELCACHESIZE, &ctl, RelationIdCache = hash_create(INITRELCACHESIZE, &ctl,
HASH_ELEM | HASH_FUNCTION); HASH_ELEM | HASH_FUNCTION);
ctl.keysize = sizeof(RelFileNode); ctl.keysize = sizeof(RelFileNode);
ctl.entrysize = sizeof(RelNodeCacheEnt);
ctl.hash = tag_hash; ctl.hash = tag_hash;
RelationNodeCache = hash_create(INITRELCACHESIZE, &ctl, RelationNodeCache = hash_create(INITRELCACHESIZE, &ctl,
HASH_ELEM | HASH_FUNCTION); HASH_ELEM | HASH_FUNCTION);
...@@ -2182,17 +2184,19 @@ CreateDummyCaches(void) ...@@ -2182,17 +2184,19 @@ CreateDummyCaches(void)
oldcxt = MemoryContextSwitchTo(CacheMemoryContext); oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
MemSet(&ctl, 0, (int) sizeof(ctl)); MemSet(&ctl, 0, sizeof(ctl));
ctl.keysize = sizeof(NameData); ctl.keysize = sizeof(NameData);
ctl.datasize = sizeof(Relation); ctl.entrysize = sizeof(RelNameCacheEnt);
RelationNameCache = hash_create(INITRELCACHESIZE, &ctl, HASH_ELEM); RelationNameCache = hash_create(INITRELCACHESIZE, &ctl, HASH_ELEM);
ctl.keysize = sizeof(Oid); ctl.keysize = sizeof(Oid);
ctl.entrysize = sizeof(RelIdCacheEnt);
ctl.hash = tag_hash; ctl.hash = tag_hash;
RelationIdCache = hash_create(INITRELCACHESIZE, &ctl, RelationIdCache = hash_create(INITRELCACHESIZE, &ctl,
HASH_ELEM | HASH_FUNCTION); HASH_ELEM | HASH_FUNCTION);
ctl.keysize = sizeof(RelFileNode); ctl.keysize = sizeof(RelFileNode);
ctl.entrysize = sizeof(RelNodeCacheEnt);
ctl.hash = tag_hash; ctl.hash = tag_hash;
RelationNodeCache = hash_create(INITRELCACHESIZE, &ctl, RelationNodeCache = hash_create(INITRELCACHESIZE, &ctl,
HASH_ELEM | HASH_FUNCTION); HASH_ELEM | HASH_FUNCTION);
......
/*------------------------------------------------------------------------- /*-------------------------------------------------------------------------
* *
* dynahash.c * dynahash.c
* dynamic hashing * dynamic hash tables
*
* *
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/hash/dynahash.c,v 1.36 2001/06/22 19:16:23 wieck Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/hash/dynahash.c,v 1.37 2001/10/01 05:36:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -40,46 +41,40 @@ ...@@ -40,46 +41,40 @@
* Modified by sullivan@postgres.berkeley.edu April 1990 * Modified by sullivan@postgres.berkeley.edu April 1990
* changed ctl structure for shared memory * changed ctl structure for shared memory
*/ */
#include <sys/types.h>
#include "postgres.h" #include "postgres.h"
#include <sys/types.h>
#include "utils/dynahash.h" #include "utils/dynahash.h"
#include "utils/hsearch.h" #include "utils/hsearch.h"
#include "utils/memutils.h" #include "utils/memutils.h"
/* /*
* Fast MOD arithmetic, assuming that y is a power of 2 ! * Key (also entry) part of a HASHELEMENT
*/ */
#define ELEMENTKEY(helem) (((char *)(helem)) + MAXALIGN(sizeof(HASHELEMENT)))
/*
* Fast MOD arithmetic, assuming that y is a power of 2 !
*/
#define MOD(x,y) ((x) & ((y)-1)) #define MOD(x,y) ((x) & ((y)-1))
/* /*
* Private function prototypes * Private function prototypes
*/ */
static void *DynaHashAlloc(Size size); static void *DynaHashAlloc(Size size);
static uint32 call_hash(HTAB *hashp, char *k); static uint32 call_hash(HTAB *hashp, void *k);
static SEG_OFFSET seg_alloc(HTAB *hashp); static HASHSEGMENT seg_alloc(HTAB *hashp);
static int bucket_alloc(HTAB *hashp); static bool element_alloc(HTAB *hashp);
static int dir_realloc(HTAB *hashp); static bool dir_realloc(HTAB *hashp);
static int expand_table(HTAB *hashp); static bool expand_table(HTAB *hashp);
static int hdefault(HTAB *hashp); static bool hdefault(HTAB *hashp);
static int init_htab(HTAB *hashp, int nelem); static bool init_htab(HTAB *hashp, long nelem);
/* ---------------- /*
* memory allocation routines * memory allocation routines
*
* for postgres: all hash elements have to be in
* the global cache context. Otherwise the postgres
* garbage collector is going to corrupt them. -wei
*
* ??? the "cache" memory context is intended to store only
* system cache information. The user of the hashing
* routines should specify which context to use or we
* should create a separate memory context for these
* hash routines. For now I have modified this code to
* do the latter -cim 1/19/91
* ----------------
*/ */
static MemoryContext DynaHashCxt = NULL; static MemoryContext DynaHashCxt = NULL;
static MemoryContext CurrentDynaHashCxt = NULL; static MemoryContext CurrentDynaHashCxt = NULL;
...@@ -95,39 +90,22 @@ DynaHashAlloc(Size size) ...@@ -95,39 +90,22 @@ DynaHashAlloc(Size size)
#define MEM_FREE pfree #define MEM_FREE pfree
/*
* pointer access macros. Shared memory implementation cannot
* store pointers in the hash table data structures because
* pointer values will be different in different address spaces.
* these macros convert offsets to pointers and pointers to offsets.
* Shared memory need not be contiguous, but all addresses must be
* calculated relative to some offset (segbase).
*/
#define GET_SEG(hp,seg_num)\
(SEGMENT) (((unsigned long) (hp)->segbase) + (hp)->dir[seg_num])
#define GET_BUCKET(hp,bucket_offs)\
(ELEMENT *) (((unsigned long) (hp)->segbase) + bucket_offs)
#define MAKE_HASHOFFSET(hp,ptr)\
( ((unsigned long) ptr) - ((unsigned long) (hp)->segbase) )
#if HASH_STATISTICS #if HASH_STATISTICS
static long hash_accesses, static long hash_accesses,
hash_collisions, hash_collisions,
hash_expansions; hash_expansions;
#endif #endif
/************************** CREATE ROUTINES **********************/ /************************** CREATE ROUTINES **********************/
HTAB * HTAB *
hash_create(int nelem, HASHCTL *info, int flags) hash_create(long nelem, HASHCTL *info, int flags)
{ {
HHDR *hctl;
HTAB *hashp; HTAB *hashp;
HASHHDR *hctl;
/* First time through, create a memory context for hash tables */
if (!DynaHashCxt) if (!DynaHashCxt)
DynaHashCxt = AllocSetContextCreate(TopMemoryContext, DynaHashCxt = AllocSetContextCreate(TopMemoryContext,
"DynaHash", "DynaHash",
...@@ -135,62 +113,57 @@ hash_create(int nelem, HASHCTL *info, int flags) ...@@ -135,62 +113,57 @@ hash_create(int nelem, HASHCTL *info, int flags)
ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE); ALLOCSET_DEFAULT_MAXSIZE);
/* Select allocation context for this hash table */
if (flags & HASH_CONTEXT) if (flags & HASH_CONTEXT)
CurrentDynaHashCxt = info->hcxt; CurrentDynaHashCxt = info->hcxt;
else else
CurrentDynaHashCxt = DynaHashCxt; CurrentDynaHashCxt = DynaHashCxt;
/* Initialize the hash header */
hashp = (HTAB *) MEM_ALLOC(sizeof(HTAB)); hashp = (HTAB *) MEM_ALLOC(sizeof(HTAB));
if (!hashp)
return NULL;
MemSet(hashp, 0, sizeof(HTAB)); MemSet(hashp, 0, sizeof(HTAB));
if (flags & HASH_FUNCTION) if (flags & HASH_FUNCTION)
hashp->hash = info->hash; hashp->hash = info->hash;
else else
{ hashp->hash = string_hash; /* default hash function */
/* default */
hashp->hash = string_hash;
}
if (flags & HASH_SHARED_MEM) if (flags & HASH_SHARED_MEM)
{ {
/* /*
* ctl structure is preallocated for shared memory tables. Note * ctl structure is preallocated for shared memory tables. Note
* that HASH_DIRSIZE had better be set as well. * that HASH_DIRSIZE had better be set as well.
*/ */
hashp->hctl = info->hctl;
hashp->hctl = (HHDR *) info->hctl; hashp->dir = info->dir;
hashp->segbase = (char *) info->segbase;
hashp->alloc = info->alloc; hashp->alloc = info->alloc;
hashp->dir = (SEG_OFFSET *) info->dir;
hashp->hcxt = NULL; hashp->hcxt = NULL;
/* hash table already exists, we're just attaching to it */ /* hash table already exists, we're just attaching to it */
if (flags & HASH_ATTACH) if (flags & HASH_ATTACH)
return hashp; return hashp;
} }
else else
{ {
/* setup hash table defaults */ /* setup hash table defaults */
hashp->hctl = NULL; hashp->hctl = NULL;
hashp->alloc = MEM_ALLOC;
hashp->dir = NULL; hashp->dir = NULL;
hashp->segbase = NULL; hashp->alloc = MEM_ALLOC;
hashp->hcxt = DynaHashCxt; hashp->hcxt = DynaHashCxt;
} }
if (!hashp->hctl) if (!hashp->hctl)
{ {
hashp->hctl = (HHDR *) hashp->alloc(sizeof(HHDR)); hashp->hctl = (HASHHDR *) hashp->alloc(sizeof(HASHHDR));
if (!hashp->hctl) if (!hashp->hctl)
return 0; return NULL;
} }
if (!hdefault(hashp)) if (!hdefault(hashp))
return 0; return NULL;
hctl = hashp->hctl; hctl = hashp->hctl;
#ifdef HASH_STATISTICS #ifdef HASH_STATISTICS
hctl->accesses = hctl->collisions = 0; hctl->accesses = hctl->collisions = 0;
...@@ -222,24 +195,26 @@ hash_create(int nelem, HASHCTL *info, int flags) ...@@ -222,24 +195,26 @@ hash_create(int nelem, HASHCTL *info, int flags)
if (flags & HASH_ELEM) if (flags & HASH_ELEM)
{ {
hctl->keysize = info->keysize; hctl->keysize = info->keysize;
hctl->datasize = info->datasize; hctl->entrysize = info->entrysize;
} }
if (flags & HASH_ALLOC) if (flags & HASH_ALLOC)
hashp->alloc = info->alloc; hashp->alloc = info->alloc;
else else
{ {
if (flags & HASH_CONTEXT) if (flags & HASH_CONTEXT)
{ {
/* hash table structures live in child of given context */
CurrentDynaHashCxt = AllocSetContextCreate(info->hcxt, CurrentDynaHashCxt = AllocSetContextCreate(info->hcxt,
"DynaHashTable", "DynaHashTable",
ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE); ALLOCSET_DEFAULT_MAXSIZE);
hashp->hcxt = CurrentDynaHashCxt; hashp->hcxt = CurrentDynaHashCxt;
} }
else else
{ {
/* hash table structures live in child of DynaHashCxt */
CurrentDynaHashCxt = AllocSetContextCreate(DynaHashCxt, CurrentDynaHashCxt = AllocSetContextCreate(DynaHashCxt,
"DynaHashTable", "DynaHashTable",
ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_MINSIZE,
...@@ -249,57 +224,54 @@ hash_create(int nelem, HASHCTL *info, int flags) ...@@ -249,57 +224,54 @@ hash_create(int nelem, HASHCTL *info, int flags)
} }
} }
if (init_htab(hashp, nelem)) if (!init_htab(hashp, nelem))
{ {
hash_destroy(hashp); hash_destroy(hashp);
return 0; return NULL;
} }
return hashp; return hashp;
} }
/* /*
* Set default HHDR parameters. * Set default HASHHDR parameters.
*/ */
static int static bool
hdefault(HTAB *hashp) hdefault(HTAB *hashp)
{ {
HHDR *hctl; HASHHDR *hctl = hashp->hctl;
MemSet(hashp->hctl, 0, sizeof(HHDR)); MemSet(hctl, 0, sizeof(HASHHDR));
hctl = hashp->hctl;
hctl->ssize = DEF_SEGSIZE; hctl->ssize = DEF_SEGSIZE;
hctl->sshift = DEF_SEGSIZE_SHIFT; hctl->sshift = DEF_SEGSIZE_SHIFT;
hctl->dsize = DEF_DIRSIZE; hctl->dsize = DEF_DIRSIZE;
hctl->ffactor = DEF_FFACTOR; hctl->ffactor = DEF_FFACTOR;
hctl->nkeys = 0; hctl->nentries = 0;
hctl->nsegs = 0; hctl->nsegs = 0;
/* I added these MS. */ /* I added these MS. */
/* default memory allocation for hash buckets */ /* rather pointless defaults for key & entry size */
hctl->keysize = sizeof(char *); hctl->keysize = sizeof(char *);
hctl->datasize = sizeof(char *); hctl->entrysize = 2 * sizeof(char *);
/* table has no fixed maximum size */ /* table has no fixed maximum size */
hctl->max_dsize = NO_MAX_DSIZE; hctl->max_dsize = NO_MAX_DSIZE;
/* garbage collection for HASH_REMOVE */ /* garbage collection for HASH_REMOVE */
hctl->freeBucketIndex = INVALID_INDEX; hctl->freeList = NULL;
return 1; return true;
} }
static int static bool
init_htab(HTAB *hashp, int nelem) init_htab(HTAB *hashp, long nelem)
{ {
SEG_OFFSET *segp; HASHHDR *hctl = hashp->hctl;
HASHSEGMENT *segp;
int nbuckets; int nbuckets;
int nsegs; int nsegs;
HHDR *hctl;
hctl = hashp->hctl;
/* /*
* Divide number of elements by the fill factor to determine a desired * Divide number of elements by the fill factor to determine a desired
...@@ -329,29 +301,29 @@ init_htab(HTAB *hashp, int nelem) ...@@ -329,29 +301,29 @@ init_htab(HTAB *hashp, int nelem)
if (!(hashp->dir)) if (!(hashp->dir))
hctl->dsize = nsegs; hctl->dsize = nsegs;
else else
return -1; return false;
} }
/* Allocate a directory */ /* Allocate a directory */
if (!(hashp->dir)) if (!(hashp->dir))
{ {
CurrentDynaHashCxt = hashp->hcxt; CurrentDynaHashCxt = hashp->hcxt;
hashp->dir = (SEG_OFFSET *) hashp->dir = (HASHSEGMENT *)
hashp->alloc(hctl->dsize * sizeof(SEG_OFFSET)); hashp->alloc(hctl->dsize * sizeof(HASHSEGMENT));
if (!hashp->dir) if (!hashp->dir)
return -1; return false;
} }
/* Allocate initial segments */ /* Allocate initial segments */
for (segp = hashp->dir; hctl->nsegs < nsegs; hctl->nsegs++, segp++) for (segp = hashp->dir; hctl->nsegs < nsegs; hctl->nsegs++, segp++)
{ {
*segp = seg_alloc(hashp); *segp = seg_alloc(hashp);
if (*segp == (SEG_OFFSET) 0) if (*segp == NULL)
return -1; return false;
} }
#if HASH_DEBUG #if HASH_DEBUG
fprintf(stderr, "%s\n%s%x\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n", fprintf(stderr, "%s\n%s%p\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
"init_htab:", "init_htab:",
"TABLE POINTER ", hashp, "TABLE POINTER ", hashp,
"DIRECTORY SIZE ", hctl->dsize, "DIRECTORY SIZE ", hctl->dsize,
...@@ -362,9 +334,9 @@ init_htab(HTAB *hashp, int nelem) ...@@ -362,9 +334,9 @@ init_htab(HTAB *hashp, int nelem)
"HIGH MASK ", hctl->high_mask, "HIGH MASK ", hctl->high_mask,
"LOW MASK ", hctl->low_mask, "LOW MASK ", hctl->low_mask,
"NSEGS ", hctl->nsegs, "NSEGS ", hctl->nsegs,
"NKEYS ", hctl->nkeys); "NENTRIES ", hctl->nentries);
#endif #endif
return 0; return true;
} }
/* /*
...@@ -375,14 +347,14 @@ init_htab(HTAB *hashp, int nelem) ...@@ -375,14 +347,14 @@ init_htab(HTAB *hashp, int nelem)
* NB: assumes that all hash structure parameters have default values! * NB: assumes that all hash structure parameters have default values!
*/ */
long long
hash_estimate_size(long num_entries, long keysize, long datasize) hash_estimate_size(long num_entries, long entrysize)
{ {
long size = 0; long size = 0;
long nBuckets, long nBuckets,
nSegments, nSegments,
nDirEntries, nDirEntries,
nRecordAllocs, nElementAllocs,
recordSize; elementSize;
/* estimate number of buckets wanted */ /* estimate number of buckets wanted */
nBuckets = 1L << my_log2((num_entries - 1) / DEF_FFACTOR + 1); nBuckets = 1L << my_log2((num_entries - 1) / DEF_FFACTOR + 1);
...@@ -394,16 +366,15 @@ hash_estimate_size(long num_entries, long keysize, long datasize) ...@@ -394,16 +366,15 @@ hash_estimate_size(long num_entries, long keysize, long datasize)
nDirEntries <<= 1; /* dir_alloc doubles dsize at each call */ nDirEntries <<= 1; /* dir_alloc doubles dsize at each call */
/* fixed control info */ /* fixed control info */
size += MAXALIGN(sizeof(HHDR)); /* but not HTAB, per above */ size += MAXALIGN(sizeof(HASHHDR)); /* but not HTAB, per above */
/* directory */ /* directory */
size += MAXALIGN(nDirEntries * sizeof(SEG_OFFSET)); size += MAXALIGN(nDirEntries * sizeof(HASHSEGMENT));
/* segments */ /* segments */
size += nSegments * MAXALIGN(DEF_SEGSIZE * sizeof(BUCKET_INDEX)); size += nSegments * MAXALIGN(DEF_SEGSIZE * sizeof(HASHBUCKET));
/* records --- allocated in groups of BUCKET_ALLOC_INCR */ /* elements --- allocated in groups of HASHELEMENT_ALLOC_INCR */
recordSize = sizeof(BUCKET_INDEX) + keysize + datasize; elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(entrysize);
recordSize = MAXALIGN(recordSize); nElementAllocs = (num_entries - 1) / HASHELEMENT_ALLOC_INCR + 1;
nRecordAllocs = (num_entries - 1) / BUCKET_ALLOC_INCR + 1; size += nElementAllocs * HASHELEMENT_ALLOC_INCR * elementSize;
size += nRecordAllocs * BUCKET_ALLOC_INCR * recordSize;
return size; return size;
} }
...@@ -439,40 +410,11 @@ hash_select_dirsize(long num_entries) ...@@ -439,40 +410,11 @@ hash_select_dirsize(long num_entries)
/********************** DESTROY ROUTINES ************************/ /********************** DESTROY ROUTINES ************************/
/*
* XXX this sure looks thoroughly broken to me --- tgl 2/99.
* It's freeing every entry individually --- but they weren't
* allocated individually, see bucket_alloc!! Why doesn't it crash?
* ANSWER: it probably does crash, but is never invoked in normal
* operations...
*
* Thomas is right, it does crash. Therefore I changed the code
* to use a separate memory context which is a child of the DynaHashCxt
* by default. And the HASHCTL structure got extended with a hcxt
* field, where someone can specify an explicit context (giving new
* flag HASH_CONTEXT) and forget about hash_destroy() completely.
* The shmem operations aren't changed, but in shmem mode a destroy
* doesn't work anyway. Jan Wieck 03/2001.
*/
void void
hash_destroy(HTAB *hashp) hash_destroy(HTAB *hashp)
{ {
if (hashp != NULL) if (hashp != NULL)
{ {
#if 0
SEG_OFFSET segNum;
SEGMENT segp;
int nsegs = hashp->hctl->nsegs;
int j;
BUCKET_INDEX *elp,
p,
q;
ELEMENT *curr;
#endif
/* cannot destroy a shared memory hash table */
Assert(!hashp->segbase);
/* allocation method must be one we know how to free, too */ /* allocation method must be one we know how to free, too */
Assert(hashp->alloc == MEM_ALLOC); Assert(hashp->alloc == MEM_ALLOC);
/* so this hashtable must have it's own context */ /* so this hashtable must have it's own context */
...@@ -481,40 +423,18 @@ hash_destroy(HTAB *hashp) ...@@ -481,40 +423,18 @@ hash_destroy(HTAB *hashp)
hash_stats("destroy", hashp); hash_stats("destroy", hashp);
/* /*
* Free buckets, dir etc. by destroying the hash tables * Free buckets, dir etc. by destroying the hash table's
* memory context. * memory context.
*/ */
MemoryContextDelete(hashp->hcxt); MemoryContextDelete(hashp->hcxt);
#if 0
/*
* Dead code - replaced by MemoryContextDelete() above
*/
for (segNum = 0; nsegs > 0; nsegs--, segNum++)
{
segp = GET_SEG(hashp, segNum);
for (j = 0, elp = segp; j < hashp->hctl->ssize; j++, elp++)
{
for (p = *elp; p != INVALID_INDEX; p = q)
{
curr = GET_BUCKET(hashp, p);
q = curr->next;
MEM_FREE((char *) curr);
}
}
MEM_FREE((char *) segp);
}
MEM_FREE((char *) hashp->dir);
#endif
/* /*
* Free the HTAB and control structure, which are allocated * Free the HTAB and control structure, which are allocated
* in the parent context (DynaHashCxt or the context given * in the parent context (DynaHashCxt or the context given
* by the caller of hash_create(). * by the caller of hash_create()).
*/ */
MEM_FREE((char *) hashp->hctl); MEM_FREE(hashp->hctl);
MEM_FREE((char *) hashp); MEM_FREE(hashp);
} }
} }
...@@ -526,8 +446,8 @@ hash_stats(char *where, HTAB *hashp) ...@@ -526,8 +446,8 @@ hash_stats(char *where, HTAB *hashp)
fprintf(stderr, "%s: this HTAB -- accesses %ld collisions %ld\n", fprintf(stderr, "%s: this HTAB -- accesses %ld collisions %ld\n",
where, hashp->hctl->accesses, hashp->hctl->collisions); where, hashp->hctl->accesses, hashp->hctl->collisions);
fprintf(stderr, "hash_stats: keys %ld keysize %ld maxp %d segmentcount %d\n", fprintf(stderr, "hash_stats: entries %ld keysize %ld maxp %d segmentcount %d\n",
hashp->hctl->nkeys, hashp->hctl->keysize, hashp->hctl->nentries, hashp->hctl->keysize,
hashp->hctl->max_bucket, hashp->hctl->nsegs); hashp->hctl->max_bucket, hashp->hctl->nsegs);
fprintf(stderr, "%s: total accesses %ld total collisions %ld\n", fprintf(stderr, "%s: total accesses %ld total collisions %ld\n",
where, hash_accesses, hash_collisions); where, hash_accesses, hash_collisions);
...@@ -541,9 +461,9 @@ hash_stats(char *where, HTAB *hashp) ...@@ -541,9 +461,9 @@ hash_stats(char *where, HTAB *hashp)
/*******************************SEARCH ROUTINES *****************************/ /*******************************SEARCH ROUTINES *****************************/
static uint32 static uint32
call_hash(HTAB *hashp, char *k) call_hash(HTAB *hashp, void *k)
{ {
HHDR *hctl = hashp->hctl; HASHHDR *hctl = hashp->hctl;
long hash_val, long hash_val,
bucket; bucket;
...@@ -553,7 +473,7 @@ call_hash(HTAB *hashp, char *k) ...@@ -553,7 +473,7 @@ call_hash(HTAB *hashp, char *k)
if (bucket > hctl->max_bucket) if (bucket > hctl->max_bucket)
bucket = bucket & hctl->low_mask; bucket = bucket & hctl->low_mask;
return bucket; return (uint32) bucket;
} }
/* /*
...@@ -566,31 +486,34 @@ call_hash(HTAB *hashp, char *k) ...@@ -566,31 +486,34 @@ call_hash(HTAB *hashp, char *k)
* foundPtr is TRUE if we found an element in the table * foundPtr is TRUE if we found an element in the table
* (FALSE if we entered one). * (FALSE if we entered one).
*/ */
long * void *
hash_search(HTAB *hashp, hash_search(HTAB *hashp,
char *keyPtr, void *keyPtr,
HASHACTION action, /* HASH_FIND / HASH_ENTER / HASH_REMOVE HASHACTION action, /* HASH_FIND / HASH_ENTER / HASH_REMOVE
* HASH_FIND_SAVE / HASH_REMOVE_SAVED */ * HASH_FIND_SAVE / HASH_REMOVE_SAVED */
bool *foundPtr) bool *foundPtr)
{ {
HASHHDR *hctl;
uint32 bucket; uint32 bucket;
long segment_num; long segment_num;
long segment_ndx; long segment_ndx;
SEGMENT segp; HASHSEGMENT segp;
ELEMENT *curr; HASHBUCKET currBucket;
HHDR *hctl; HASHBUCKET *prevBucketPtr;
BUCKET_INDEX currIndex;
BUCKET_INDEX *prevIndexPtr;
char *destAddr;
static struct State static struct State
{ {
ELEMENT *currElem; HASHBUCKET currBucket;
BUCKET_INDEX currIndex; HASHBUCKET *prevBucketPtr;
BUCKET_INDEX *prevIndex;
} saveState; } saveState;
Assert((hashp && keyPtr)); Assert(hashp);
Assert((action == HASH_FIND) || (action == HASH_REMOVE) || (action == HASH_ENTER) || (action == HASH_FIND_SAVE) || (action == HASH_REMOVE_SAVED)); Assert(keyPtr);
Assert((action == HASH_FIND) ||
(action == HASH_REMOVE) ||
(action == HASH_ENTER) ||
(action == HASH_FIND_SAVE) ||
(action == HASH_REMOVE_SAVED));
hctl = hashp->hctl; hctl = hashp->hctl;
...@@ -598,16 +521,16 @@ hash_search(HTAB *hashp, ...@@ -598,16 +521,16 @@ hash_search(HTAB *hashp,
hash_accesses++; hash_accesses++;
hashp->hctl->accesses++; hashp->hctl->accesses++;
#endif #endif
if (action == HASH_REMOVE_SAVED) if (action == HASH_REMOVE_SAVED)
{ {
curr = saveState.currElem; currBucket = saveState.currBucket;
currIndex = saveState.currIndex; prevBucketPtr = saveState.prevBucketPtr;
prevIndexPtr = saveState.prevIndex;
/* /*
* Try to catch subsequent errors * Try to catch subsequent errors
*/ */
Assert(saveState.currElem && !(saveState.currElem = 0)); Assert(currBucket && !(saveState.currBucket = NULL));
} }
else else
{ {
...@@ -615,25 +538,22 @@ hash_search(HTAB *hashp, ...@@ -615,25 +538,22 @@ hash_search(HTAB *hashp,
segment_num = bucket >> hctl->sshift; segment_num = bucket >> hctl->sshift;
segment_ndx = MOD(bucket, hctl->ssize); segment_ndx = MOD(bucket, hctl->ssize);
segp = GET_SEG(hashp, segment_num); segp = hashp->dir[segment_num];
Assert(segp); Assert(segp);
prevIndexPtr = &segp[segment_ndx]; prevBucketPtr = &segp[segment_ndx];
currIndex = *prevIndexPtr; currBucket = *prevBucketPtr;
/* /*
* Follow collision chain * Follow collision chain looking for matching key
*/ */
for (curr = NULL; currIndex != INVALID_INDEX;) while (currBucket != NULL)
{ {
/* coerce bucket index into a pointer */ if (memcmp(ELEMENTKEY(currBucket), keyPtr, hctl->keysize) == 0)
curr = GET_BUCKET(hashp, currIndex);
if (!memcmp((char *) &(curr->key), keyPtr, hctl->keysize))
break; break;
prevIndexPtr = &(curr->next); prevBucketPtr = &(currBucket->link);
currIndex = *prevIndexPtr; currBucket = *prevBucketPtr;
#if HASH_STATISTICS #if HASH_STATISTICS
hash_collisions++; hash_collisions++;
hashp->hctl->collisions++; hashp->hctl->collisions++;
...@@ -645,48 +565,52 @@ hash_search(HTAB *hashp, ...@@ -645,48 +565,52 @@ hash_search(HTAB *hashp,
* if we found an entry or if we weren't trying to insert, we're done * if we found an entry or if we weren't trying to insert, we're done
* now. * now.
*/ */
*foundPtr = (bool) (currIndex != INVALID_INDEX); *foundPtr = (bool) (currBucket != NULL);
switch (action) switch (action)
{ {
case HASH_ENTER: case HASH_ENTER:
if (currIndex != INVALID_INDEX) if (currBucket != NULL)
return &(curr->key); return (void *) ELEMENTKEY(currBucket);
break; break;
case HASH_REMOVE: case HASH_REMOVE:
case HASH_REMOVE_SAVED: case HASH_REMOVE_SAVED:
if (currIndex != INVALID_INDEX) if (currBucket != NULL)
{ {
Assert(hctl->nkeys > 0); Assert(hctl->nentries > 0);
hctl->nkeys--; hctl->nentries--;
/* remove record from hash bucket's chain. */ /* remove record from hash bucket's chain. */
*prevIndexPtr = curr->next; *prevBucketPtr = currBucket->link;
/* add the record to the freelist for this table. */ /* add the record to the freelist for this table. */
curr->next = hctl->freeBucketIndex; currBucket->link = hctl->freeList;
hctl->freeBucketIndex = currIndex; hctl->freeList = currBucket;
/* /*
* better hope the caller is synchronizing access to this * better hope the caller is synchronizing access to this
* element, because someone else is going to reuse it the * element, because someone else is going to reuse it the
* next time something is added to the table * next time something is added to the table
*/ */
return &(curr->key); return (void *) ELEMENTKEY(currBucket);
} }
return (long *) TRUE; return (void *) TRUE;
case HASH_FIND: case HASH_FIND:
if (currIndex != INVALID_INDEX) if (currBucket != NULL)
return &(curr->key); return (void *) ELEMENTKEY(currBucket);
return (long *) TRUE; return (void *) TRUE;
case HASH_FIND_SAVE: case HASH_FIND_SAVE:
if (currIndex != INVALID_INDEX) if (currBucket != NULL)
{ {
saveState.currElem = curr; saveState.currBucket = currBucket;
saveState.prevIndex = prevIndexPtr; saveState.prevBucketPtr = prevBucketPtr;
saveState.currIndex = currIndex; return (void *) ELEMENTKEY(currBucket);
return &(curr->key);
} }
return (long *) TRUE; return (void *) TRUE;
default: default:
/* can't get here */ /* can't get here */
return NULL; return NULL;
...@@ -696,39 +620,36 @@ hash_search(HTAB *hashp, ...@@ -696,39 +620,36 @@ hash_search(HTAB *hashp,
* If we got here, then we didn't find the element and we have to * If we got here, then we didn't find the element and we have to
* insert it into the hash table * insert it into the hash table
*/ */
Assert(currIndex == INVALID_INDEX); Assert(currBucket == NULL);
/* get the next free bucket */ /* get the next free bucket */
currIndex = hctl->freeBucketIndex; currBucket = hctl->freeList;
if (currIndex == INVALID_INDEX) if (currBucket == NULL)
{ {
/* no free elements. allocate another chunk of buckets */ /* no free elements. allocate another chunk of buckets */
if (!bucket_alloc(hashp)) if (!element_alloc(hashp))
return NULL; return NULL;
currIndex = hctl->freeBucketIndex; currBucket = hctl->freeList;
} }
Assert(currIndex != INVALID_INDEX); Assert(currBucket != NULL);
curr = GET_BUCKET(hashp, currIndex); hctl->freeList = currBucket->link;
hctl->freeBucketIndex = curr->next;
/* link into chain */ /* link into chain */
*prevIndexPtr = currIndex; *prevBucketPtr = currBucket;
currBucket->link = NULL;
/* copy key into record */ /* copy key into record */
destAddr = (char *) &(curr->key); memcpy(ELEMENTKEY(currBucket), keyPtr, hctl->keysize);
memmove(destAddr, keyPtr, hctl->keysize);
curr->next = INVALID_INDEX;
/* /*
* let the caller initialize the data field after hash_search returns. * let the caller initialize the data field after hash_search returns.
*/ */
/* memmove(destAddr,keyPtr,hctl->keysize+hctl->datasize); */
/* /*
* Check if it is time to split the segment * Check if it is time to split the segment
*/ */
if (++hctl->nkeys / (hctl->max_bucket + 1) > hctl->ffactor) if (++hctl->nentries / (hctl->max_bucket + 1) > hctl->ffactor)
{ {
/* /*
...@@ -737,14 +658,15 @@ hash_search(HTAB *hashp, ...@@ -737,14 +658,15 @@ hash_search(HTAB *hashp,
*/ */
expand_table(hashp); expand_table(hashp);
} }
return &(curr->key);
return (void *) ELEMENTKEY(currBucket);
} }
/* /*
* hash_seq_init/_search * hash_seq_init/_search
* Sequentially search through hash table and return * Sequentially search through hash table and return
* all the elements one by one, return NULL on error and * all the elements one by one, return NULL on error and
* return (long *) TRUE in the end. * return (void *) TRUE in the end.
* *
* NOTE: caller may delete the returned element before continuing the scan. * NOTE: caller may delete the returned element before continuing the scan.
* However, deleting any other element while the scan is in progress is * However, deleting any other element while the scan is in progress is
...@@ -757,31 +679,31 @@ hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp) ...@@ -757,31 +679,31 @@ hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
{ {
status->hashp = hashp; status->hashp = hashp;
status->curBucket = 0; status->curBucket = 0;
status->curIndex = INVALID_INDEX; status->curEntry = NULL;
} }
long * void *
hash_seq_search(HASH_SEQ_STATUS *status) hash_seq_search(HASH_SEQ_STATUS *status)
{ {
HTAB *hashp = status->hashp; HTAB *hashp = status->hashp;
HHDR *hctl = hashp->hctl; HASHHDR *hctl = hashp->hctl;
while (status->curBucket <= hctl->max_bucket) while (status->curBucket <= hctl->max_bucket)
{ {
long segment_num; long segment_num;
long segment_ndx; long segment_ndx;
SEGMENT segp; HASHSEGMENT segp;
if (status->curIndex != INVALID_INDEX) if (status->curEntry != NULL)
{ {
/* Continuing scan of curBucket... */ /* Continuing scan of curBucket... */
ELEMENT *curElem; HASHELEMENT *curElem;
curElem = GET_BUCKET(hashp, status->curIndex); curElem = status->curEntry;
status->curIndex = curElem->next; status->curEntry = curElem->link;
if (status->curIndex == INVALID_INDEX) /* end of this bucket */ if (status->curEntry == NULL) /* end of this bucket */
++status->curBucket; ++status->curBucket;
return &(curElem->key); return (void *) ELEMENTKEY(curElem);
} }
/* /*
...@@ -793,10 +715,10 @@ hash_seq_search(HASH_SEQ_STATUS *status) ...@@ -793,10 +715,10 @@ hash_seq_search(HASH_SEQ_STATUS *status)
/* /*
* first find the right segment in the table directory. * first find the right segment in the table directory.
*/ */
segp = GET_SEG(hashp, segment_num); segp = hashp->dir[segment_num];
if (segp == NULL) if (segp == NULL)
/* this is probably an error */ /* this is probably an error */
return (long *) NULL; return NULL;
/* /*
* now find the right index into the segment for the first item in * now find the right index into the segment for the first item in
...@@ -806,13 +728,13 @@ hash_seq_search(HASH_SEQ_STATUS *status) ...@@ -806,13 +728,13 @@ hash_seq_search(HASH_SEQ_STATUS *status)
* directory of valid stuff. if there are elements in the bucket * directory of valid stuff. if there are elements in the bucket
* chains that point to the freelist we're in big trouble. * chains that point to the freelist we're in big trouble.
*/ */
status->curIndex = segp[segment_ndx]; status->curEntry = segp[segment_ndx];
if (status->curIndex == INVALID_INDEX) /* empty bucket */ if (status->curEntry == NULL) /* empty bucket */
++status->curBucket; ++status->curBucket;
} }
return (long *) TRUE; /* out of buckets */ return (void *) TRUE; /* out of buckets */
} }
...@@ -821,11 +743,11 @@ hash_seq_search(HASH_SEQ_STATUS *status) ...@@ -821,11 +743,11 @@ hash_seq_search(HASH_SEQ_STATUS *status)
/* /*
* Expand the table by adding one more hash bucket. * Expand the table by adding one more hash bucket.
*/ */
static int static bool
expand_table(HTAB *hashp) expand_table(HTAB *hashp)
{ {
HHDR *hctl; HASHHDR *hctl = hashp->hctl;
SEGMENT old_seg, HASHSEGMENT old_seg,
new_seg; new_seg;
long old_bucket, long old_bucket,
new_bucket; new_bucket;
...@@ -833,18 +755,15 @@ expand_table(HTAB *hashp) ...@@ -833,18 +755,15 @@ expand_table(HTAB *hashp)
new_segndx; new_segndx;
long old_segnum, long old_segnum,
old_segndx; old_segndx;
ELEMENT *chain; HASHBUCKET *oldlink,
BUCKET_INDEX *old, *newlink;
*newbi; HASHBUCKET currElement,
BUCKET_INDEX chainIndex, nextElement;
nextIndex;
#ifdef HASH_STATISTICS #ifdef HASH_STATISTICS
hash_expansions++; hash_expansions++;
#endif #endif
hctl = hashp->hctl;
new_bucket = hctl->max_bucket + 1; new_bucket = hctl->max_bucket + 1;
new_segnum = new_bucket >> hctl->sshift; new_segnum = new_bucket >> hctl->sshift;
new_segndx = MOD(new_bucket, hctl->ssize); new_segndx = MOD(new_bucket, hctl->ssize);
...@@ -854,9 +773,9 @@ expand_table(HTAB *hashp) ...@@ -854,9 +773,9 @@ expand_table(HTAB *hashp)
/* Allocate new segment if necessary -- could fail if dir full */ /* Allocate new segment if necessary -- could fail if dir full */
if (new_segnum >= hctl->dsize) if (new_segnum >= hctl->dsize)
if (!dir_realloc(hashp)) if (!dir_realloc(hashp))
return 0; return false;
if (!(hashp->dir[new_segnum] = seg_alloc(hashp))) if (!(hashp->dir[new_segnum] = seg_alloc(hashp)))
return 0; return false;
hctl->nsegs++; hctl->nsegs++;
} }
...@@ -890,137 +809,118 @@ expand_table(HTAB *hashp) ...@@ -890,137 +809,118 @@ expand_table(HTAB *hashp)
old_segnum = old_bucket >> hctl->sshift; old_segnum = old_bucket >> hctl->sshift;
old_segndx = MOD(old_bucket, hctl->ssize); old_segndx = MOD(old_bucket, hctl->ssize);
old_seg = GET_SEG(hashp, old_segnum); old_seg = hashp->dir[old_segnum];
new_seg = GET_SEG(hashp, new_segnum); new_seg = hashp->dir[new_segnum];
old = &old_seg[old_segndx]; oldlink = &old_seg[old_segndx];
newbi = &new_seg[new_segndx]; newlink = &new_seg[new_segndx];
for (chainIndex = *old;
chainIndex != INVALID_INDEX; for (currElement = *oldlink;
chainIndex = nextIndex) currElement != NULL;
currElement = nextElement)
{ {
chain = GET_BUCKET(hashp, chainIndex); nextElement = currElement->link;
nextIndex = chain->next; if ((long) call_hash(hashp, (void *) ELEMENTKEY(currElement))
if ((long) call_hash(hashp, (char *) &(chain->key)) == old_bucket) == old_bucket)
{ {
*old = chainIndex; *oldlink = currElement;
old = &chain->next; oldlink = &currElement->link;
} }
else else
{ {
*newbi = chainIndex; *newlink = currElement;
newbi = &chain->next; newlink = &currElement->link;
} }
} }
/* don't forget to terminate the rebuilt hash chains... */ /* don't forget to terminate the rebuilt hash chains... */
*old = INVALID_INDEX; *oldlink = NULL;
*newbi = INVALID_INDEX; *newlink = NULL;
return 1;
return true;
} }
static int static bool
dir_realloc(HTAB *hashp) dir_realloc(HTAB *hashp)
{ {
char *p; HASHSEGMENT *p;
char *old_p; HASHSEGMENT *old_p;
long new_dsize; long new_dsize;
long old_dirsize; long old_dirsize;
long new_dirsize; long new_dirsize;
if (hashp->hctl->max_dsize != NO_MAX_DSIZE) if (hashp->hctl->max_dsize != NO_MAX_DSIZE)
return 0; return false;
/* Reallocate directory */ /* Reallocate directory */
new_dsize = hashp->hctl->dsize << 1; new_dsize = hashp->hctl->dsize << 1;
old_dirsize = hashp->hctl->dsize * sizeof(SEG_OFFSET); old_dirsize = hashp->hctl->dsize * sizeof(HASHSEGMENT);
new_dirsize = new_dsize * sizeof(SEG_OFFSET); new_dirsize = new_dsize * sizeof(HASHSEGMENT);
old_p = hashp->dir;
CurrentDynaHashCxt = hashp->hcxt; CurrentDynaHashCxt = hashp->hcxt;
old_p = (char *) hashp->dir; p = (HASHSEGMENT *) hashp->alloc((Size) new_dirsize);
p = (char *) hashp->alloc((Size) new_dirsize);
if (p != NULL) if (p != NULL)
{ {
memmove(p, old_p, old_dirsize); memcpy(p, old_p, old_dirsize);
MemSet(p + old_dirsize, 0, new_dirsize - old_dirsize); MemSet(((char *) p) + old_dirsize, 0, new_dirsize - old_dirsize);
MEM_FREE((char *) old_p); MEM_FREE((char *) old_p);
hashp->dir = (SEG_OFFSET *) p; hashp->dir = p;
hashp->hctl->dsize = new_dsize; hashp->hctl->dsize = new_dsize;
return 1; return true;
} }
return 0;
return false;
} }
static SEG_OFFSET static HASHSEGMENT
seg_alloc(HTAB *hashp) seg_alloc(HTAB *hashp)
{ {
SEGMENT segp; HASHSEGMENT segp;
SEG_OFFSET segOffset;
CurrentDynaHashCxt = hashp->hcxt; CurrentDynaHashCxt = hashp->hcxt;
segp = (SEGMENT) hashp->alloc(sizeof(BUCKET_INDEX) * hashp->hctl->ssize); segp = (HASHSEGMENT) hashp->alloc(sizeof(HASHBUCKET) * hashp->hctl->ssize);
if (!segp) if (!segp)
return 0; return NULL;
MemSet((char *) segp, 0, MemSet(segp, 0, sizeof(HASHBUCKET) * hashp->hctl->ssize);
(long) sizeof(BUCKET_INDEX) * hashp->hctl->ssize);
segOffset = MAKE_HASHOFFSET(hashp, segp); return segp;
return segOffset;
} }
/* /*
* allocate some new buckets and link them into the free list * allocate some new elements and link them into the free list
*/ */
static int static bool
bucket_alloc(HTAB *hashp) element_alloc(HTAB *hashp)
{ {
HASHHDR *hctl = hashp->hctl;
Size elementSize;
HASHELEMENT *tmpElement;
int i; int i;
ELEMENT *tmpBucket;
long bucketSize;
BUCKET_INDEX tmpIndex,
lastIndex;
/* Each bucket has a BUCKET_INDEX header plus user data. */
bucketSize = sizeof(BUCKET_INDEX) + hashp->hctl->keysize + hashp->hctl->datasize;
/* make sure its aligned correctly */ /* Each element has a HASHELEMENT header plus user data. */
bucketSize = MAXALIGN(bucketSize); elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(hctl->entrysize);
CurrentDynaHashCxt = hashp->hcxt; CurrentDynaHashCxt = hashp->hcxt;
tmpBucket = (ELEMENT *) hashp->alloc(BUCKET_ALLOC_INCR * bucketSize); tmpElement = (HASHELEMENT *)
hashp->alloc(HASHELEMENT_ALLOC_INCR * elementSize);
if (!tmpBucket)
return 0;
/* tmpIndex is the shmem offset into the first bucket of the array */ if (!tmpElement)
tmpIndex = MAKE_HASHOFFSET(hashp, tmpBucket); return false;
/* set the freebucket list to point to the first bucket */ /* link all the new entries into the freelist */
lastIndex = hashp->hctl->freeBucketIndex; for (i = 0; i < HASHELEMENT_ALLOC_INCR; i++)
hashp->hctl->freeBucketIndex = tmpIndex;
/*
* initialize each bucket to point to the one behind it. NOTE: loop
* sets last bucket incorrectly; we fix below.
*/
for (i = 0; i < BUCKET_ALLOC_INCR; i++)
{ {
tmpBucket = GET_BUCKET(hashp, tmpIndex); tmpElement->link = hctl->freeList;
tmpIndex += bucketSize; hctl->freeList = tmpElement;
tmpBucket->next = tmpIndex; tmpElement = (HASHELEMENT *) (((char *) tmpElement) + elementSize);
} }
/* return true;
* the last bucket points to the old freelist head (which is probably
* invalid or we wouldn't be here)
*/
tmpBucket->next = lastIndex;
return 1;
} }
/* calculate ceil(log base 2) of num */ /* calculate ceil(log base 2) of num */
......
/*------------------------------------------------------------------------- /*-------------------------------------------------------------------------
* *
* hashfn.c * hashfn.c
* Hash functions for use in dynahash.c hashtables
* *
* *
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
...@@ -8,7 +9,7 @@ ...@@ -8,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/hash/hashfn.c,v 1.13 2001/01/24 19:43:15 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/hash/hashfn.c,v 1.14 2001/10/01 05:36:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -17,155 +18,100 @@ ...@@ -17,155 +18,100 @@
#include "utils/hsearch.h" #include "utils/hsearch.h"
/* /*
* Assume that we've already split the bucket to which this * string_hash: hash function for keys that are null-terminated strings.
* key hashes, calculate that bucket, and check that in fact *
* we did already split it. * NOTE: since dynahash.c backs this up with a fixed-length memcmp(),
* the key must actually be zero-padded to the specified maximum length
* to work correctly. However, if it is known that nothing after the
* first zero byte is interesting, this is the right hash function to use.
*
* NOTE: this is the default hash function if none is specified.
*/ */
long long
string_hash(char *key, int keysize) string_hash(void *key, int keysize)
{ {
int h;
unsigned char *k = (unsigned char *) key; unsigned char *k = (unsigned char *) key;
long h = 0;
h = 0;
/*
* Convert string to integer
*/
while (*k) while (*k)
h = h * PRIME1 ^ (*k++ - ' '); h = (h * PRIME1) ^ (*k++);
h %= PRIME2; h %= PRIME2;
return h; return h;
} }
/*
* tag_hash: hash function for fixed-size tag values
*
* NB: we assume that the supplied key is aligned at least on an 'int'
* boundary, if its size is >= sizeof(int).
*/
long long
tag_hash(int *key, int keysize) tag_hash(void *key, int keysize)
{ {
int *k = (int *) key;
long h = 0; long h = 0;
/* /*
* Convert tag to integer; Use four byte chunks in a "jump table" to * Use four byte chunks in a "jump table" to go a little faster.
* go a little faster. Currently the maximum keysize is 16 (mar 17 *
* 1992) I have put in cases for up to 24. Bigger than this will * Currently the maximum keysize is 16 (mar 17 1992). I have put in
* resort to the old behavior of the for loop. (see the default case). * cases for up to 32. Bigger than this will resort to a for loop
* (see the default case).
*/ */
switch (keysize) switch (keysize)
{ {
case 8 * sizeof(int):
h = (h * PRIME1) ^ (*k++);
/* fall through */
case 7 * sizeof(int):
h = (h * PRIME1) ^ (*k++);
/* fall through */
case 6 * sizeof(int): case 6 * sizeof(int):
h = h * PRIME1 ^ (*key); h = (h * PRIME1) ^ (*k++);
key++;
/* fall through */ /* fall through */
case 5 * sizeof(int): case 5 * sizeof(int):
h = h * PRIME1 ^ (*key); h = (h * PRIME1) ^ (*k++);
key++;
/* fall through */ /* fall through */
case 4 * sizeof(int): case 4 * sizeof(int):
h = h * PRIME1 ^ (*key); h = (h * PRIME1) ^ (*k++);
key++;
/* fall through */ /* fall through */
case 3 * sizeof(int): case 3 * sizeof(int):
h = h * PRIME1 ^ (*key); h = (h * PRIME1) ^ (*k++);
key++;
/* fall through */ /* fall through */
case 2 * sizeof(int): case 2 * sizeof(int):
h = h * PRIME1 ^ (*key); h = (h * PRIME1) ^ (*k++);
key++;
/* fall through */ /* fall through */
case sizeof(int): case sizeof(int):
h = h * PRIME1 ^ (*key); h = (h * PRIME1) ^ (*k++);
key++;
break; break;
default: default:
for (; keysize >= (int) sizeof(int); keysize -= sizeof(int), key++) /* Do an int at a time */
h = h * PRIME1 ^ (*key); for (; keysize >= (int) sizeof(int); keysize -= sizeof(int))
h = (h * PRIME1) ^ (*k++);
/*
* now let's grab the last few bytes of the tag if the tag has /* Cope with any partial-int leftover bytes */
* (size % 4) != 0 (which it sometimes will on a sun3). if (keysize > 0)
*/
if (keysize)
{ {
char *keytmp = (char *) key; unsigned char *keybyte = (unsigned char *) k;
switch (keysize) do
{ h = (h * PRIME1) ^ (*keybyte++);
case 3: while (--keysize > 0);
h = h * PRIME1 ^ (*keytmp);
keytmp++;
/* fall through */
case 2:
h = h * PRIME1 ^ (*keytmp);
keytmp++;
/* fall through */
case 1:
h = h * PRIME1 ^ (*keytmp);
break;
}
} }
break; break;
} }
h %= PRIME2; h %= PRIME2;
return h;
}
/*
* This is INCREDIBLY ugly, but fast.
* We break the string up into 8 byte units. On the first time
* through the loop we get the "leftover bytes" (strlen % 8).
* On every other iteration, we perform 8 HASHC's so we handle
* all 8 bytes. Essentially, this saves us 7 cmp & branch
* instructions. If this routine is heavily used enough, it's
* worth the ugly coding
*/
#ifdef NOT_USED
long
disk_hash(char *key)
{
int n = 0;
char *str = key;
int len = strlen(key);
int loop;
#define HASHC n = *str++ + 65599 * n return h;
if (len > 0)
{
loop = (len + 8 - 1) >> 3;
switch (len & (8 - 1))
{
case 0:
do
{ /* All fall throughs */
HASHC;
case 7:
HASHC;
case 6:
HASHC;
case 5:
HASHC;
case 4:
HASHC;
case 3:
HASHC;
case 2:
HASHC;
case 1:
HASHC;
} while (--loop);
}
}
return n;
} }
#endif
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/portalmem.c,v 1.41 2001/03/22 04:00:08 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/mmgr/portalmem.c,v 1.42 2001/10/01 05:36:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -123,7 +123,7 @@ EnablePortalManager(void) ...@@ -123,7 +123,7 @@ EnablePortalManager(void)
ALLOCSET_DEFAULT_MAXSIZE); ALLOCSET_DEFAULT_MAXSIZE);
ctl.keysize = MAX_PORTALNAME_LEN; ctl.keysize = MAX_PORTALNAME_LEN;
ctl.datasize = sizeof(Portal); ctl.entrysize = sizeof(PortalHashEnt);
/* /*
* use PORTALS_PER_USER, defined in utils/portal.h as a guess of how * use PORTALS_PER_USER, defined in utils/portal.h as a guess of how
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: buf_internals.h,v 1.50 2001/09/29 04:02:26 tgl Exp $ * $Id: buf_internals.h,v 1.51 2001/10/01 05:36:17 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -115,6 +115,13 @@ typedef struct sbufdesc ...@@ -115,6 +115,13 @@ typedef struct sbufdesc
#define BL_IO_IN_PROGRESS (1 << 0) /* unimplemented */ #define BL_IO_IN_PROGRESS (1 << 0) /* unimplemented */
#define BL_PIN_COUNT_LOCK (1 << 1) #define BL_PIN_COUNT_LOCK (1 << 1)
/* entry for buffer hashtable */
typedef struct
{
BufferTag key;
Buffer id;
} BufferLookupEnt;
/* /*
* mao tracing buffer allocation * mao tracing buffer allocation
*/ */
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: lock.h,v 1.55 2001/09/30 00:45:48 momjian Exp $ * $Id: lock.h,v 1.56 2001/10/01 05:36:17 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -169,9 +169,6 @@ typedef struct LOCK ...@@ -169,9 +169,6 @@ typedef struct LOCK
int nGranted; /* total of granted[] array */ int nGranted; /* total of granted[] array */
} LOCK; } LOCK;
#define SHMEM_LOCKTAB_KEYSIZE sizeof(LOCKTAG)
#define SHMEM_LOCKTAB_DATASIZE (sizeof(LOCK) - SHMEM_LOCKTAB_KEYSIZE)
#define LOCK_LOCKMETHOD(lock) ((lock).tag.lockmethod) #define LOCK_LOCKMETHOD(lock) ((lock).tag.lockmethod)
...@@ -222,9 +219,6 @@ typedef struct HOLDER ...@@ -222,9 +219,6 @@ typedef struct HOLDER
SHM_QUEUE procLink; /* list link for process's list of holders */ SHM_QUEUE procLink; /* list link for process's list of holders */
} HOLDER; } HOLDER;
#define SHMEM_HOLDERTAB_KEYSIZE sizeof(HOLDERTAG)
#define SHMEM_HOLDERTAB_DATASIZE (sizeof(HOLDER) - SHMEM_HOLDERTAB_KEYSIZE)
#define HOLDER_LOCKMETHOD(holder) \ #define HOLDER_LOCKMETHOD(holder) \
(((LOCK *) MAKE_PTR((holder).tag.lock))->tag.lockmethod) (((LOCK *) MAKE_PTR((holder).tag.lock))->tag.lockmethod)
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: shmem.h,v 1.31 2001/09/29 04:02:27 tgl Exp $ * $Id: shmem.h,v 1.32 2001/10/01 05:36:17 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -73,9 +73,7 @@ extern void *ShmemInitStruct(char *name, Size size, bool *foundPtr); ...@@ -73,9 +73,7 @@ extern void *ShmemInitStruct(char *name, Size size, bool *foundPtr);
/* size constants for the shmem index table */ /* size constants for the shmem index table */
/* max size of data structure string name */ /* max size of data structure string name */
#define SHMEM_INDEX_KEYSIZE (50) #define SHMEM_INDEX_KEYSIZE (48)
/* data in shmem index table hash bucket */
#define SHMEM_INDEX_DATASIZE (sizeof(ShmemIndexEnt) - SHMEM_INDEX_KEYSIZE)
/* maximum size of the shmem index table */ /* maximum size of the shmem index table */
#define SHMEM_INDEX_SIZE (100) #define SHMEM_INDEX_SIZE (100)
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: hsearch.h,v 1.20 2001/06/22 19:16:24 wieck Exp $ * $Id: hsearch.h,v 1.21 2001/10/01 05:36:17 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -31,31 +31,32 @@ ...@@ -31,31 +31,32 @@
* tables, the initial directory size can be left at the default. * tables, the initial directory size can be left at the default.
*/ */
#define DEF_SEGSIZE 256 #define DEF_SEGSIZE 256
#define DEF_SEGSIZE_SHIFT 8/* must be log2(DEF_SEGSIZE) */ #define DEF_SEGSIZE_SHIFT 8 /* must be log2(DEF_SEGSIZE) */
#define DEF_DIRSIZE 256 #define DEF_DIRSIZE 256
#define DEF_FFACTOR 1/* default fill factor */ #define DEF_FFACTOR 1 /* default fill factor */
#define PRIME1 37 /* for the hash function */ #define PRIME1 37 /* for the hash function */
#define PRIME2 1048583 #define PRIME2 1048583
/* /*
* Hash bucket is actually bigger than this. Key field can have * HASHELEMENT is the private part of a hashtable entry. The caller's data
* variable length and a variable length data field follows it. * follows the HASHELEMENT structure (on a MAXALIGN'd boundary). The hash key
* is expected to be at the start of the caller's hash entry structure.
*/ */
typedef struct element typedef struct HASHELEMENT
{ {
unsigned long next; /* secret from user */ struct HASHELEMENT *link; /* link to next entry in same bucket */
long key; } HASHELEMENT;
} ELEMENT;
typedef unsigned long BUCKET_INDEX; /* A hash bucket is a linked list of HASHELEMENTs */
typedef HASHELEMENT *HASHBUCKET;
/* segment is an array of bucket pointers */ /* A hash segment is an array of bucket headers */
typedef BUCKET_INDEX *SEGMENT; typedef HASHBUCKET *HASHSEGMENT;
typedef unsigned long SEG_OFFSET;
typedef struct hashhdr /* Header structure for a hash table --- contains all changeable info */
typedef struct HASHHDR
{ {
long dsize; /* Directory Size */ long dsize; /* Directory Size */
long ssize; /* Segment Size --- must be power of 2 */ long ssize; /* Segment Size --- must be power of 2 */
...@@ -64,65 +65,66 @@ typedef struct hashhdr ...@@ -64,65 +65,66 @@ typedef struct hashhdr
long high_mask; /* Mask to modulo into entire table */ long high_mask; /* Mask to modulo into entire table */
long low_mask; /* Mask to modulo into lower half of table */ long low_mask; /* Mask to modulo into lower half of table */
long ffactor; /* Fill factor */ long ffactor; /* Fill factor */
long nkeys; /* Number of keys in hash table */ long nentries; /* Number of entries in hash table */
long nsegs; /* Number of allocated segments */ long nsegs; /* Number of allocated segments */
long keysize; /* hash key length in bytes */ long keysize; /* hash key length in bytes */
long datasize; /* elem data length in bytes */ long entrysize; /* total user element size in bytes */
long max_dsize; /* 'dsize' limit if directory is fixed long max_dsize; /* 'dsize' limit if directory is fixed
* size */ * size */
BUCKET_INDEX freeBucketIndex; /* index of first free bucket */ HASHELEMENT *freeList; /* linked list of free elements */
#ifdef HASH_STATISTICS #ifdef HASH_STATISTICS
long accesses; long accesses;
long collisions; long collisions;
#endif #endif
} HHDR; } HASHHDR;
typedef struct htab /*
* Top control structure for a hashtable --- need not be shared, since
* no fields change at runtime
*/
typedef struct HTAB
{ {
HHDR *hctl; /* shared control information */ HASHHDR *hctl; /* shared control information */
long (*hash) (); /* Hash Function */ long (*hash) (void *key, int keysize); /* Hash Function */
char *segbase; /* segment base address for calculating HASHSEGMENT *dir; /* directory of segment starts */
* pointer values */
SEG_OFFSET *dir; /* 'directory' of segm starts */
void *(*alloc) (Size);/* memory allocator */ void *(*alloc) (Size);/* memory allocator */
MemoryContext hcxt; /* memory context if default allocator used */ MemoryContext hcxt; /* memory context if default allocator used */
} HTAB; } HTAB;
typedef struct hashctl /* Parameter data structure for hash_create */
/* Only those fields indicated by hash_flags need be set */
typedef struct HASHCTL
{ {
long ssize; /* Segment Size */ long ssize; /* Segment Size */
long dsize; /* Dirsize Size */ long dsize; /* (initial) Directory Size */
long ffactor; /* Fill factor */ long ffactor; /* Fill factor */
long (*hash) (); /* Hash Function */ long (*hash) (void *key, int keysize); /* Hash Function */
long keysize; /* hash key length in bytes */ long keysize; /* hash key length in bytes */
long datasize; /* elem data length in bytes */ long entrysize; /* total user element size in bytes */
long max_dsize; /* limit to dsize if directory size is long max_dsize; /* limit to dsize if directory size is
* limited */ * limited */
long *segbase; /* base for calculating bucket + seg ptrs */
void *(*alloc) (Size);/* memory allocation function */ void *(*alloc) (Size);/* memory allocation function */
long *dir; /* directory if allocated already */ HASHSEGMENT *dir; /* directory of segment starts */
long *hctl; /* location of header information in shd HASHHDR *hctl; /* location of header in shared mem */
* mem */ MemoryContext hcxt; /* memory context to use for allocations */
MemoryContext hcxt; /* memory context to use for all allocations */
} HASHCTL; } HASHCTL;
/* Flags to indicate action for hctl */ /* Flags to indicate which parameters are supplied */
#define HASH_SEGMENT 0x002 /* Setting segment size */ #define HASH_SEGMENT 0x002 /* Setting segment size */
#define HASH_DIRSIZE 0x004 /* Setting directory size */ #define HASH_DIRSIZE 0x004 /* Setting directory size */
#define HASH_FFACTOR 0x008 /* Setting fill factor */ #define HASH_FFACTOR 0x008 /* Setting fill factor */
#define HASH_FUNCTION 0x010 /* Set user defined hash function */ #define HASH_FUNCTION 0x010 /* Set user defined hash function */
#define HASH_ELEM 0x020 /* Setting key/data size */ #define HASH_ELEM 0x020 /* Setting key/entry size */
#define HASH_SHARED_MEM 0x040 /* Setting shared mem const */ #define HASH_SHARED_MEM 0x040 /* Setting shared mem const */
#define HASH_ATTACH 0x080 /* Do not initialize hctl */ #define HASH_ATTACH 0x080 /* Do not initialize hctl */
#define HASH_ALLOC 0x100 /* Setting memory allocator */ #define HASH_ALLOC 0x100 /* Setting memory allocator */
#define HASH_CONTEXT 0x200 /* Setting explicit memory context */ #define HASH_CONTEXT 0x200 /* Setting explicit memory context */
/* seg_alloc assumes that INVALID_INDEX is 0 */ /* max_dsize value to indicate expansible directory */
#define INVALID_INDEX (0)
#define NO_MAX_DSIZE (-1) #define NO_MAX_DSIZE (-1)
/* number of hash buckets allocated at once */ /* number of hash elements allocated at once */
#define BUCKET_ALLOC_INCR (30) #define HASHELEMENT_ALLOC_INCR (32)
/* hash_search operations */ /* hash_search operations */
typedef enum typedef enum
...@@ -138,27 +140,27 @@ typedef enum ...@@ -138,27 +140,27 @@ typedef enum
typedef struct typedef struct
{ {
HTAB *hashp; HTAB *hashp;
long curBucket; long curBucket; /* index of current bucket */
BUCKET_INDEX curIndex; HASHELEMENT *curEntry; /* current entry in bucket */
} HASH_SEQ_STATUS; } HASH_SEQ_STATUS;
/* /*
* prototypes from functions in dynahash.c * prototypes for functions in dynahash.c
*/ */
extern HTAB *hash_create(int nelem, HASHCTL *info, int flags); extern HTAB *hash_create(long nelem, HASHCTL *info, int flags);
extern void hash_destroy(HTAB *hashp); extern void hash_destroy(HTAB *hashp);
extern void hash_stats(char *where, HTAB *hashp); extern void hash_stats(char *where, HTAB *hashp);
extern long *hash_search(HTAB *hashp, char *keyPtr, HASHACTION action, extern void *hash_search(HTAB *hashp, void *keyPtr, HASHACTION action,
bool *foundPtr); bool *foundPtr);
extern void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp); extern void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp);
extern long *hash_seq_search(HASH_SEQ_STATUS *status); extern void *hash_seq_search(HASH_SEQ_STATUS *status);
extern long hash_estimate_size(long num_entries, long keysize, long datasize); extern long hash_estimate_size(long num_entries, long entrysize);
extern long hash_select_dirsize(long num_entries); extern long hash_select_dirsize(long num_entries);
/* /*
* prototypes from functions in hashfn.c * prototypes for functions in hashfn.c
*/ */
extern long string_hash(char *key, int keysize); extern long string_hash(void *key, int keysize);
extern long tag_hash(int *key, int keysize); extern long tag_hash(void *key, int keysize);
#endif /* HSEARCH_H */ #endif /* HSEARCH_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment