16
16
*
17
17
*
18
18
* IDENTIFICATION
19
- * $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.35 2000/11/20 16:47:32 petere Exp $
19
+ * $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.36 2000/11/30 01:39:07 tgl Exp $
20
20
*
21
21
*-------------------------------------------------------------------------
22
22
*/
33
33
#include <signal.h>
34
34
35
35
#include "executor/execdebug.h"
36
+ #include "storage/buf_internals.h"
37
+ #include "storage/bufmgr.h"
36
38
#include "storage/smgr.h"
37
39
#include "utils/relcache.h"
38
40
39
41
extern long int LocalBufferFlushCount ;
40
42
41
43
int NLocBuffer = 64 ;
42
44
BufferDesc * LocalBufferDescriptors = NULL ;
45
+ Block * LocalBufferBlockPointers = NULL ;
43
46
long * LocalRefCount = NULL ;
44
47
45
48
static int nextFreeLocalBuf = 0 ;
@@ -135,14 +138,24 @@ LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr)
135
138
bufHdr -> flags &= ~BM_DIRTY ;
136
139
137
140
/*
138
- * lazy memory allocation. (see MAKE_PTR for why we need to do
139
- * MAKE_OFFSET.)
141
+ * lazy memory allocation: allocate space on first use of a buffer.
140
142
*/
141
143
if (bufHdr -> data == (SHMEM_OFFSET ) 0 )
142
144
{
143
145
char * data = (char * ) malloc (BLCKSZ );
144
146
147
+ if (data == NULL )
148
+ elog (FATAL , "Out of memory in LocalBufferAlloc" );
149
+ /*
150
+ * This is a bit of a hack: bufHdr->data needs to be a shmem offset
151
+ * for consistency with the shared-buffer case, so make it one
152
+ * even though it's not really a valid shmem offset.
153
+ */
145
154
bufHdr -> data = MAKE_OFFSET (data );
155
+ /*
156
+ * Set pointer for use by BufferGetBlock() macro.
157
+ */
158
+ LocalBufferBlockPointers [- (bufHdr -> buf_id + 2 )] = (Block ) data ;
146
159
}
147
160
148
161
* foundPtr = FALSE;
@@ -223,7 +236,7 @@ FlushLocalBuffer(Buffer buffer, bool sync, bool release)
223
236
/*
224
237
* InitLocalBuffer -
225
238
* init the local buffer cache. Since most queries (esp. multi-user ones)
226
- * don't involve local buffers, we delay allocating memory for actual the
239
+ * don't involve local buffers, we delay allocating actual memory for the
227
240
* buffer until we need it.
228
241
*/
229
242
void
@@ -235,8 +248,9 @@ InitLocalBuffer(void)
235
248
* these aren't going away. I'm not gonna use palloc.
236
249
*/
237
250
LocalBufferDescriptors =
238
- (BufferDesc * ) malloc (sizeof (BufferDesc ) * NLocBuffer );
239
- MemSet (LocalBufferDescriptors , 0 , sizeof (BufferDesc ) * NLocBuffer );
251
+ (BufferDesc * ) calloc (NLocBuffer , sizeof (BufferDesc ));
252
+ LocalBufferBlockPointers = (Block * ) calloc (NLocBuffer , sizeof (Block ));
253
+ LocalRefCount = (long * ) calloc (NLocBuffer , sizeof (long ));
240
254
nextFreeLocalBuf = 0 ;
241
255
242
256
for (i = 0 ; i < NLocBuffer ; i ++ )
@@ -251,9 +265,6 @@ InitLocalBuffer(void)
251
265
*/
252
266
buf -> buf_id = - i - 2 ;
253
267
}
254
-
255
- LocalRefCount = (long * ) malloc (sizeof (long ) * NLocBuffer );
256
- MemSet (LocalRefCount , 0 , sizeof (long ) * NLocBuffer );
257
268
}
258
269
259
270
/*
@@ -308,7 +319,6 @@ ResetLocalBufferPool(void)
308
319
309
320
buf -> tag .rnode .relNode = InvalidOid ;
310
321
buf -> flags &= ~BM_DIRTY ;
311
- buf -> buf_id = - i - 2 ;
312
322
}
313
323
314
324
MemSet (LocalRefCount , 0 , sizeof (long ) * NLocBuffer );
0 commit comments