]> granicus.if.org Git - postgresql/blob - src/backend/storage/buffer/buf_table.c
Fix another passel of include-file breakage. Kris Jurka, Tom Lane
[postgresql] / src / backend / storage / buffer / buf_table.c
1 /*-------------------------------------------------------------------------
2  *
3  * buf_table.c
4  *        routines for mapping BufferTags to buffer indexes.
5  *
6  * Note: the routines in this file do no locking of their own.  The caller
7  * must hold a suitable lock on the BufMappingLock, as specified in the
8  * comments.
9  *
10  *
11  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  *
15  * IDENTIFICATION
16  *        $PostgreSQL: pgsql/src/backend/storage/buffer/buf_table.c,v 1.46 2006/07/14 16:59:19 tgl Exp $
17  *
18  *-------------------------------------------------------------------------
19  */
20 #include "postgres.h"
21
22 #include "storage/bufmgr.h"
23 #include "storage/buf_internals.h"
24
25
26 /* entry for buffer lookup hashtable */
27 typedef struct
28 {
29         BufferTag       key;                    /* Tag of a disk page */
30         int                     id;                             /* Associated buffer ID */
31 } BufferLookupEnt;
32
33 static HTAB *SharedBufHash;
34
35
36 /*
37  * Estimate space needed for mapping hashtable
38  *              size is the desired hash table size (possibly more than NBuffers)
39  */
40 Size
41 BufTableShmemSize(int size)
42 {
43         return hash_estimate_size(size, sizeof(BufferLookupEnt));
44 }
45
46 /*
47  * Initialize shmem hash table for mapping buffers
48  *              size is the desired hash table size (possibly more than NBuffers)
49  */
50 void
51 InitBufTable(int size)
52 {
53         HASHCTL         info;
54
55         /* assume no locking is needed yet */
56
57         /* BufferTag maps to Buffer */
58         info.keysize = sizeof(BufferTag);
59         info.entrysize = sizeof(BufferLookupEnt);
60         info.hash = tag_hash;
61
62         SharedBufHash = ShmemInitHash("Shared Buffer Lookup Table",
63                                                                   size, size,
64                                                                   &info,
65                                                                   HASH_ELEM | HASH_FUNCTION);
66
67         if (!SharedBufHash)
68                 elog(FATAL, "could not initialize shared buffer hash table");
69 }
70
71 /*
72  * BufTableLookup
73  *              Lookup the given BufferTag; return buffer ID, or -1 if not found
74  *
75  * Caller must hold at least share lock on BufMappingLock
76  */
77 int
78 BufTableLookup(BufferTag *tagPtr)
79 {
80         BufferLookupEnt *result;
81
82         result = (BufferLookupEnt *)
83                 hash_search(SharedBufHash, (void *) tagPtr, HASH_FIND, NULL);
84
85         if (!result)
86                 return -1;
87
88         return result->id;
89 }
90
91 /*
92  * BufTableInsert
93  *              Insert a hashtable entry for given tag and buffer ID,
94  *              unless an entry already exists for that tag
95  *
96  * Returns -1 on successful insertion.  If a conflicting entry exists
97  * already, returns the buffer ID in that entry.
98  *
99  * Caller must hold write lock on BufMappingLock
100  */
101 int
102 BufTableInsert(BufferTag *tagPtr, int buf_id)
103 {
104         BufferLookupEnt *result;
105         bool            found;
106
107         Assert(buf_id >= 0);            /* -1 is reserved for not-in-table */
108         Assert(tagPtr->blockNum != P_NEW);      /* invalid tag */
109
110         result = (BufferLookupEnt *)
111                 hash_search(SharedBufHash, (void *) tagPtr, HASH_ENTER, &found);
112
113         if (found)                                      /* found something already in the table */
114                 return result->id;
115
116         result->id = buf_id;
117
118         return -1;
119 }
120
121 /*
122  * BufTableDelete
123  *              Delete the hashtable entry for given tag (which must exist)
124  *
125  * Caller must hold write lock on BufMappingLock
126  */
127 void
128 BufTableDelete(BufferTag *tagPtr)
129 {
130         BufferLookupEnt *result;
131
132         result = (BufferLookupEnt *)
133                 hash_search(SharedBufHash, (void *) tagPtr, HASH_REMOVE, NULL);
134
135         if (!result)                            /* shouldn't happen */
136                 elog(ERROR, "shared buffer hash table corrupted");
137 }