Skip to content

Commit 3ac88fd

Browse files
committed
Convert macros to static inline functions (buf_internals.h)
Dilip Kumar, reviewed by Vignesh C, Ashutosh Sharma, and me. Discussion: http://postgr.es/m/CAFiTN-tYbM7D+2UGiNc2kAFMSQTa5FTeYvmg-Vj2HvPdVw2Gvg@mail.gmail.com
1 parent 03361a3 commit 3ac88fd

File tree

4 files changed

+112
-76
lines changed

4 files changed

+112
-76
lines changed

src/backend/storage/buffer/buf_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ InitBufferPool(void)
116116
{
117117
BufferDesc *buf = GetBufferDescriptor(i);
118118

119-
CLEAR_BUFFERTAG(buf->tag);
119+
ClearBufferTag(&buf->tag);
120120

121121
pg_atomic_init_u32(&buf->state, 0);
122122
buf->wait_backend_pgprocno = INVALID_PGPROCNO;

src/backend/storage/buffer/bufmgr.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ PrefetchSharedBuffer(SMgrRelation smgr_reln,
515515
Assert(BlockNumberIsValid(blockNum));
516516

517517
/* create a tag so we can lookup the buffer */
518-
INIT_BUFFERTAG(newTag, smgr_reln->smgr_rlocator.locator,
518+
InitBufferTag(&newTag, &smgr_reln->smgr_rlocator.locator,
519519
forkNum, blockNum);
520520

521521
/* determine its hash code and partition lock ID */
@@ -632,7 +632,7 @@ ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockN
632632

633633
ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
634634
ReservePrivateRefCountEntry();
635-
INIT_BUFFERTAG(tag, rlocator, forkNum, blockNum);
635+
InitBufferTag(&tag, &rlocator, forkNum, blockNum);
636636

637637
if (BufferIsLocal(recent_buffer))
638638
{
@@ -642,7 +642,7 @@ ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockN
642642
buf_state = pg_atomic_read_u32(&bufHdr->state);
643643

644644
/* Is it still valid and holding the right tag? */
645-
if ((buf_state & BM_VALID) && BUFFERTAGS_EQUAL(tag, bufHdr->tag))
645+
if ((buf_state & BM_VALID) && BufferTagsEqual(&tag, &bufHdr->tag))
646646
{
647647
/*
648648
* Bump buffer's ref and usage counts. This is equivalent of
@@ -679,7 +679,7 @@ ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockN
679679
else
680680
buf_state = LockBufHdr(bufHdr);
681681

682-
if ((buf_state & BM_VALID) && BUFFERTAGS_EQUAL(tag, bufHdr->tag))
682+
if ((buf_state & BM_VALID) && BufferTagsEqual(&tag, &bufHdr->tag))
683683
{
684684
/*
685685
* It's now safe to pin the buffer. We can't pin first and ask
@@ -1134,7 +1134,7 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
11341134
uint32 buf_state;
11351135

11361136
/* create a tag so we can lookup the buffer */
1137-
INIT_BUFFERTAG(newTag, smgr->smgr_rlocator.locator, forkNum, blockNum);
1137+
InitBufferTag(&newTag, &smgr->smgr_rlocator.locator, forkNum, blockNum);
11381138

11391139
/* determine its hash code and partition lock ID */
11401140
newHash = BufTableHashCode(&newTag);
@@ -1517,7 +1517,7 @@ InvalidateBuffer(BufferDesc *buf)
15171517
buf_state = LockBufHdr(buf);
15181518

15191519
/* If it's changed while we were waiting for lock, do nothing */
1520-
if (!BUFFERTAGS_EQUAL(buf->tag, oldTag))
1520+
if (!BufferTagsEqual(&buf->tag, &oldTag))
15211521
{
15221522
UnlockBufHdr(buf, buf_state);
15231523
LWLockRelease(oldPartitionLock);
@@ -1549,7 +1549,7 @@ InvalidateBuffer(BufferDesc *buf)
15491549
* linear scans of the buffer array don't think the buffer is valid.
15501550
*/
15511551
oldFlags = buf_state & BUF_FLAG_MASK;
1552-
CLEAR_BUFFERTAG(buf->tag);
1552+
ClearBufferTag(&buf->tag);
15531553
buf_state &= ~(BUF_FLAG_MASK | BUF_USAGECOUNT_MASK);
15541554
UnlockBufHdr(buf, buf_state);
15551555

@@ -3365,7 +3365,7 @@ FindAndDropRelationBuffers(RelFileLocator rlocator, ForkNumber forkNum,
33653365
uint32 buf_state;
33663366

33673367
/* create a tag so we can lookup the buffer */
3368-
INIT_BUFFERTAG(bufTag, rlocator, forkNum, curBlock);
3368+
InitBufferTag(&bufTag, &rlocator, forkNum, curBlock);
33693369

33703370
/* determine its hash code and partition lock ID */
33713371
bufHash = BufTableHashCode(&bufTag);

src/backend/storage/buffer/localbuf.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ PrefetchLocalBuffer(SMgrRelation smgr, ForkNumber forkNum,
6868
BufferTag newTag; /* identity of requested block */
6969
LocalBufferLookupEnt *hresult;
7070

71-
INIT_BUFFERTAG(newTag, smgr->smgr_rlocator.locator, forkNum, blockNum);
71+
InitBufferTag(&newTag, &smgr->smgr_rlocator.locator, forkNum, blockNum);
7272

7373
/* Initialize local buffers if first request in this session */
7474
if (LocalBufHash == NULL)
@@ -117,7 +117,7 @@ LocalBufferAlloc(SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum,
117117
bool found;
118118
uint32 buf_state;
119119

120-
INIT_BUFFERTAG(newTag, smgr->smgr_rlocator.locator, forkNum, blockNum);
120+
InitBufferTag(&newTag, &smgr->smgr_rlocator.locator, forkNum, blockNum);
121121

122122
/* Initialize local buffers if first request in this session */
123123
if (LocalBufHash == NULL)
@@ -131,7 +131,7 @@ LocalBufferAlloc(SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum,
131131
{
132132
b = hresult->id;
133133
bufHdr = GetLocalBufferDescriptor(b);
134-
Assert(BUFFERTAGS_EQUAL(bufHdr->tag, newTag));
134+
Assert(BufferTagsEqual(&bufHdr->tag, &newTag));
135135
#ifdef LBDEBUG
136136
fprintf(stderr, "LB ALLOC (%u,%d,%d) %d\n",
137137
smgr->smgr_rlocator.locator.relNumber, forkNum, blockNum, -b - 1);
@@ -253,7 +253,7 @@ LocalBufferAlloc(SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum,
253253
if (!hresult) /* shouldn't happen */
254254
elog(ERROR, "local buffer hash table corrupted");
255255
/* mark buffer invalid just in case hash insert fails */
256-
CLEAR_BUFFERTAG(bufHdr->tag);
256+
ClearBufferTag(&bufHdr->tag);
257257
buf_state &= ~(BM_VALID | BM_TAG_VALID);
258258
pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
259259
}
@@ -354,7 +354,7 @@ DropRelationLocalBuffers(RelFileLocator rlocator, ForkNumber forkNum,
354354
if (!hresult) /* shouldn't happen */
355355
elog(ERROR, "local buffer hash table corrupted");
356356
/* Mark buffer invalid */
357-
CLEAR_BUFFERTAG(bufHdr->tag);
357+
ClearBufferTag(&bufHdr->tag);
358358
buf_state &= ~BUF_FLAG_MASK;
359359
buf_state &= ~BUF_USAGECOUNT_MASK;
360360
pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
@@ -398,7 +398,7 @@ DropRelationAllLocalBuffers(RelFileLocator rlocator)
398398
if (!hresult) /* shouldn't happen */
399399
elog(ERROR, "local buffer hash table corrupted");
400400
/* Mark buffer invalid */
401-
CLEAR_BUFFERTAG(bufHdr->tag);
401+
ClearBufferTag(&bufHdr->tag);
402402
buf_state &= ~BUF_FLAG_MASK;
403403
buf_state &= ~BUF_USAGECOUNT_MASK;
404404
pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);

src/include/storage/buf_internals.h

Lines changed: 97 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
* relation is visible yet (its xact may have started before the xact that
8686
* created the rel). The storage manager must be able to cope anyway.
8787
*
88-
* Note: if there's any pad bytes in the struct, INIT_BUFFERTAG will have
88+
* Note: if there's any pad bytes in the struct, InitBufferTag will have
8989
* to be fixed to zero them, since this struct is used as a hash key.
9090
*/
9191
typedef struct buftag
@@ -95,42 +95,57 @@ typedef struct buftag
9595
BlockNumber blockNum; /* blknum relative to begin of reln */
9696
} BufferTag;
9797

98-
#define CLEAR_BUFFERTAG(a) \
99-
( \
100-
(a).rlocator.spcOid = InvalidOid, \
101-
(a).rlocator.dbOid = InvalidOid, \
102-
(a).rlocator.relNumber = InvalidRelFileNumber, \
103-
(a).forkNum = InvalidForkNumber, \
104-
(a).blockNum = InvalidBlockNumber \
105-
)
106-
107-
#define INIT_BUFFERTAG(a,xx_rlocator,xx_forkNum,xx_blockNum) \
108-
( \
109-
(a).rlocator = (xx_rlocator), \
110-
(a).forkNum = (xx_forkNum), \
111-
(a).blockNum = (xx_blockNum) \
112-
)
113-
114-
#define BUFFERTAGS_EQUAL(a,b) \
115-
( \
116-
RelFileLocatorEquals((a).rlocator, (b).rlocator) && \
117-
(a).blockNum == (b).blockNum && \
118-
(a).forkNum == (b).forkNum \
119-
)
98+
static inline void
99+
ClearBufferTag(BufferTag *tag)
100+
{
101+
tag->rlocator.spcOid = InvalidOid;
102+
tag->rlocator.dbOid = InvalidOid;
103+
tag->rlocator.relNumber = InvalidRelFileNumber;
104+
tag->forkNum = InvalidForkNumber;
105+
tag->blockNum = InvalidBlockNumber;
106+
}
107+
108+
static inline void
109+
InitBufferTag(BufferTag *tag, const RelFileLocator *rlocator,
110+
ForkNumber forkNum, BlockNumber blockNum)
111+
{
112+
tag->rlocator = *rlocator;
113+
tag->forkNum = forkNum;
114+
tag->blockNum = blockNum;
115+
}
116+
117+
static inline bool
118+
BufferTagsEqual(const BufferTag *tag1, const BufferTag *tag2)
119+
{
120+
return RelFileLocatorEquals(tag1->rlocator, tag2->rlocator) &&
121+
(tag1->blockNum == tag2->blockNum) &&
122+
(tag1->forkNum == tag2->forkNum);
123+
}
120124

121125
/*
122126
* The shared buffer mapping table is partitioned to reduce contention.
123127
* To determine which partition lock a given tag requires, compute the tag's
124128
* hash code with BufTableHashCode(), then apply BufMappingPartitionLock().
125129
* NB: NUM_BUFFER_PARTITIONS must be a power of 2!
126130
*/
127-
#define BufTableHashPartition(hashcode) \
128-
((hashcode) % NUM_BUFFER_PARTITIONS)
129-
#define BufMappingPartitionLock(hashcode) \
130-
(&MainLWLockArray[BUFFER_MAPPING_LWLOCK_OFFSET + \
131-
BufTableHashPartition(hashcode)].lock)
132-
#define BufMappingPartitionLockByIndex(i) \
133-
(&MainLWLockArray[BUFFER_MAPPING_LWLOCK_OFFSET + (i)].lock)
131+
static inline uint32
132+
BufTableHashPartition(uint32 hashcode)
133+
{
134+
return hashcode % NUM_BUFFER_PARTITIONS;
135+
}
136+
137+
static inline LWLock *
138+
BufMappingPartitionLock(uint32 hashcode)
139+
{
140+
return &MainLWLockArray[BUFFER_MAPPING_LWLOCK_OFFSET +
141+
BufTableHashPartition(hashcode)].lock;
142+
}
143+
144+
static inline LWLock *
145+
BufMappingPartitionLockByIndex(uint32 index)
146+
{
147+
return &MainLWLockArray[BUFFER_MAPPING_LWLOCK_OFFSET + index].lock;
148+
}
134149

135150
/*
136151
* BufferDesc -- shared descriptor/state data for a single shared buffer.
@@ -220,37 +235,6 @@ typedef union BufferDescPadded
220235
char pad[BUFFERDESC_PAD_TO_SIZE];
221236
} BufferDescPadded;
222237

223-
#define GetBufferDescriptor(id) (&BufferDescriptors[(id)].bufferdesc)
224-
#define GetLocalBufferDescriptor(id) (&LocalBufferDescriptors[(id)])
225-
226-
#define BufferDescriptorGetBuffer(bdesc) ((bdesc)->buf_id + 1)
227-
228-
#define BufferDescriptorGetIOCV(bdesc) \
229-
(&(BufferIOCVArray[(bdesc)->buf_id]).cv)
230-
#define BufferDescriptorGetContentLock(bdesc) \
231-
((LWLock*) (&(bdesc)->content_lock))
232-
233-
extern PGDLLIMPORT ConditionVariableMinimallyPadded *BufferIOCVArray;
234-
235-
/*
236-
* The freeNext field is either the index of the next freelist entry,
237-
* or one of these special values:
238-
*/
239-
#define FREENEXT_END_OF_LIST (-1)
240-
#define FREENEXT_NOT_IN_LIST (-2)
241-
242-
/*
243-
* Functions for acquiring/releasing a shared buffer header's spinlock. Do
244-
* not apply these to local buffers!
245-
*/
246-
extern uint32 LockBufHdr(BufferDesc *desc);
247-
#define UnlockBufHdr(desc, s) \
248-
do { \
249-
pg_write_barrier(); \
250-
pg_atomic_write_u32(&(desc)->state, (s) & (~BM_LOCKED)); \
251-
} while (0)
252-
253-
254238
/*
255239
* The PendingWriteback & WritebackContext structure are used to keep
256240
* information about pending flush requests to be issued to the OS.
@@ -276,11 +260,63 @@ typedef struct WritebackContext
276260

277261
/* in buf_init.c */
278262
extern PGDLLIMPORT BufferDescPadded *BufferDescriptors;
263+
extern PGDLLIMPORT ConditionVariableMinimallyPadded *BufferIOCVArray;
279264
extern PGDLLIMPORT WritebackContext BackendWritebackContext;
280265

281266
/* in localbuf.c */
282267
extern PGDLLIMPORT BufferDesc *LocalBufferDescriptors;
283268

269+
270+
static inline BufferDesc *
271+
GetBufferDescriptor(uint32 id)
272+
{
273+
return &(BufferDescriptors[id]).bufferdesc;
274+
}
275+
276+
static inline BufferDesc *
277+
GetLocalBufferDescriptor(uint32 id)
278+
{
279+
return &LocalBufferDescriptors[id];
280+
}
281+
282+
static inline Buffer
283+
BufferDescriptorGetBuffer(const BufferDesc *bdesc)
284+
{
285+
return (Buffer) (bdesc->buf_id + 1);
286+
}
287+
288+
static inline ConditionVariable *
289+
BufferDescriptorGetIOCV(const BufferDesc *bdesc)
290+
{
291+
return &(BufferIOCVArray[bdesc->buf_id]).cv;
292+
}
293+
294+
static inline LWLock *
295+
BufferDescriptorGetContentLock(const BufferDesc *bdesc)
296+
{
297+
return (LWLock *) (&bdesc->content_lock);
298+
}
299+
300+
/*
301+
* The freeNext field is either the index of the next freelist entry,
302+
* or one of these special values:
303+
*/
304+
#define FREENEXT_END_OF_LIST (-1)
305+
#define FREENEXT_NOT_IN_LIST (-2)
306+
307+
/*
308+
* Functions for acquiring/releasing a shared buffer header's spinlock. Do
309+
* not apply these to local buffers!
310+
*/
311+
extern uint32 LockBufHdr(BufferDesc *desc);
312+
313+
static inline void
314+
UnlockBufHdr(BufferDesc *desc, uint32 buf_state)
315+
{
316+
pg_write_barrier();
317+
pg_atomic_write_u32(&desc->state, buf_state & (~BM_LOCKED));
318+
}
319+
284320
/* in bufmgr.c */
285321

286322
/*

0 commit comments

Comments
 (0)