summaryrefslogtreecommitdiff
path: root/src/backend/storage/buffer/buf_table.c
blob: 0b4e0ea58fb15a39378b2616aebbee36041787d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*-------------------------------------------------------------------------
 *
 * buf_table.c
 *	  routines for finding buffers in the buffer pool.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_table.c,v 1.14 1999/02/13 23:17:55 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
/*
 * OLD COMMENTS
 *
 * Data Structures:
 *
 *		Buffers are identified by their BufferTag (buf.h).	This
 * file contains routines for allocating a shmem hash table to
 * map buffer tags to buffer descriptors.
 *
 * Synchronization:
 *
 *	All routines in this file assume buffer manager spinlock is
 *	held by their caller.
 */

#include "postgres.h"

#include "storage/bufmgr.h"
#include "storage/buf_internals.h"		/* where the declarations go */
#include "storage/shmem.h"
#include "storage/spin.h"
#include "utils/hsearch.h"

static HTAB *SharedBufHash;

typedef struct lookup
{
	BufferTag	key;
	Buffer		id;
} LookupEnt;

/*
 * Initialize shmem hash table for mapping buffers
 */
void
InitBufTable()
{
	HASHCTL		info;
	int			hash_flags;

	/* assume lock is held */

	/* BufferTag maps to Buffer */
	info.keysize = sizeof(BufferTag);
	info.datasize = sizeof(Buffer);
	info.hash = tag_hash;

	hash_flags = (HASH_ELEM | HASH_FUNCTION);


	SharedBufHash = (HTAB *) ShmemInitHash("Shared Buffer Lookup Table",
										   NBuffers, NBuffers,
										   &info, hash_flags);

	if (!SharedBufHash)
	{
		elog(FATAL, "couldn't initialize shared buffer pool Hash Tbl");
		exit(1);
	}

}

BufferDesc *
BufTableLookup(BufferTag *tagPtr)
{
	LookupEnt  *result;
	bool		found;

	if (tagPtr->blockNum == P_NEW)
		return NULL;

	result = (LookupEnt *)
		hash_search(SharedBufHash, (char *) tagPtr, HASH_FIND, &found);

	if (!result)
	{
		elog(ERROR, "BufTableLookup: BufferLookup table corrupted");
		return NULL;
	}
	if (!found)
		return NULL;
	return &(BufferDescriptors[result->id]);
}

/*
 * BufTableDelete
 */
bool
BufTableDelete(BufferDesc *buf)
{
	LookupEnt  *result;
	bool		found;

	/*
	 * buffer not initialized or has been removed from table already.
	 * BM_DELETED keeps us from removing buffer twice.
	 */
	if (buf->flags & BM_DELETED)
		return TRUE;

	buf->flags |= BM_DELETED;

	result = (LookupEnt *)
		hash_search(SharedBufHash, (char *) &(buf->tag), HASH_REMOVE, &found);

	if (!(result && found))
	{
		elog(ERROR, "BufTableDelete: BufferLookup table corrupted");
		return FALSE;
	}

	return TRUE;
}

bool
BufTableInsert(BufferDesc *buf)
{
	LookupEnt  *result;
	bool		found;

	/* cannot insert it twice */
	Assert(buf->flags & BM_DELETED);
	buf->flags &= ~(BM_DELETED);

	result = (LookupEnt *)
		hash_search(SharedBufHash, (char *) &(buf->tag), HASH_ENTER, &found);

	if (!result)
	{
		Assert(0);
		elog(ERROR, "BufTableInsert: BufferLookup table corrupted");
		return FALSE;
	}
	/* found something else in the table ! */
	if (found)
	{
		Assert(0);
		elog(ERROR, "BufTableInsert: BufferLookup table corrupted");
		return FALSE;
	}

	result->id = buf->buf_id;
	return TRUE;
}

/* prints out collision stats for the buf table */
#ifdef NOT_USED
void
DBG_LookupListCheck(int nlookup)
{
	nlookup = 10;

	hash_stats("Shared", SharedBufHash);
}

#endif