Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0099b94

Browse files
committed
Convert confusing macros in multixact.c to static inline functions
The macros were confused about the argument data types. All the arguments were called 'xid', and some of the macros included casts to TransactionId, even though the arguments were actually either MultiXactIds or MultiXactOffsets. It compiles to the same thing, because TransactionId, MultiXactId and MultiXactOffset are all typedefs of uint32, but it was highly misleading. Author: Maxim Orlov <orlovmg@gmail.com> Discussion: https://www.postgresql.org/message-id/CACG%3DezbLUG-OD1osAW3OchOMxZtdxHh2itYR9Zhh-a13wEBEQw%40mail.gmail.com Discussion: https://www.postgresql.org/message-id/ff143b24-a093-40da-9833-d36b83726bdf%40iki.fi
1 parent 92aff00 commit 0099b94

File tree

1 file changed

+60
-19
lines changed

1 file changed

+60
-19
lines changed

src/backend/access/transam/multixact.c

+60-19
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,23 @@
108108
/* We need four bytes per offset */
109109
#define MULTIXACT_OFFSETS_PER_PAGE (BLCKSZ / sizeof(MultiXactOffset))
110110

111-
#define MultiXactIdToOffsetPage(xid) \
112-
((xid) / (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
113-
#define MultiXactIdToOffsetEntry(xid) \
114-
((xid) % (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
115-
#define MultiXactIdToOffsetSegment(xid) (MultiXactIdToOffsetPage(xid) / SLRU_PAGES_PER_SEGMENT)
111+
static inline int64
112+
MultiXactIdToOffsetPage(MultiXactId multi)
113+
{
114+
return multi / MULTIXACT_OFFSETS_PER_PAGE;
115+
}
116+
117+
static inline int
118+
MultiXactIdToOffsetEntry(MultiXactId multi)
119+
{
120+
return multi % MULTIXACT_OFFSETS_PER_PAGE;
121+
}
122+
123+
static inline int
124+
MultiXactIdToOffsetSegment(MultiXactId multi)
125+
{
126+
return MultiXactIdToOffsetPage(multi) / SLRU_PAGES_PER_SEGMENT;
127+
}
116128

117129
/*
118130
* The situation for members is a bit more complex: we store one byte of
@@ -156,30 +168,59 @@
156168
((uint32) ((0xFFFFFFFF % MULTIXACT_MEMBERS_PER_PAGE) + 1))
157169

158170
/* page in which a member is to be found */
159-
#define MXOffsetToMemberPage(xid) ((xid) / (TransactionId) MULTIXACT_MEMBERS_PER_PAGE)
160-
#define MXOffsetToMemberSegment(xid) (MXOffsetToMemberPage(xid) / SLRU_PAGES_PER_SEGMENT)
171+
static inline int64
172+
MXOffsetToMemberPage(MultiXactOffset offset)
173+
{
174+
return offset / MULTIXACT_MEMBERS_PER_PAGE;
175+
}
176+
177+
static inline int
178+
MXOffsetToMemberSegment(MultiXactOffset offset)
179+
{
180+
return MXOffsetToMemberPage(offset) / SLRU_PAGES_PER_SEGMENT;
181+
}
161182

162183
/* Location (byte offset within page) of flag word for a given member */
163-
#define MXOffsetToFlagsOffset(xid) \
164-
((((xid) / (TransactionId) MULTIXACT_MEMBERS_PER_MEMBERGROUP) % \
165-
(TransactionId) MULTIXACT_MEMBERGROUPS_PER_PAGE) * \
166-
(TransactionId) MULTIXACT_MEMBERGROUP_SIZE)
167-
#define MXOffsetToFlagsBitShift(xid) \
168-
(((xid) % (TransactionId) MULTIXACT_MEMBERS_PER_MEMBERGROUP) * \
169-
MXACT_MEMBER_BITS_PER_XACT)
184+
static inline int
185+
MXOffsetToFlagsOffset(MultiXactOffset offset)
186+
{
187+
MultiXactOffset group = offset / MULTIXACT_MEMBERS_PER_MEMBERGROUP;
188+
int grouponpg = group % MULTIXACT_MEMBERGROUPS_PER_PAGE;
189+
int byteoff = grouponpg * MULTIXACT_MEMBERGROUP_SIZE;
190+
191+
return byteoff;
192+
}
193+
194+
static inline int
195+
MXOffsetToFlagsBitShift(MultiXactOffset offset)
196+
{
197+
int member_in_group = offset % MULTIXACT_MEMBERS_PER_MEMBERGROUP;
198+
int bshift = member_in_group * MXACT_MEMBER_BITS_PER_XACT;
199+
200+
return bshift;
201+
}
170202

171203
/* Location (byte offset within page) of TransactionId of given member */
172-
#define MXOffsetToMemberOffset(xid) \
173-
(MXOffsetToFlagsOffset(xid) + MULTIXACT_FLAGBYTES_PER_GROUP + \
174-
((xid) % MULTIXACT_MEMBERS_PER_MEMBERGROUP) * sizeof(TransactionId))
204+
static inline int
205+
MXOffsetToMemberOffset(MultiXactOffset offset)
206+
{
207+
int member_in_group = offset % MULTIXACT_MEMBERS_PER_MEMBERGROUP;
208+
209+
return MXOffsetToFlagsOffset(offset) +
210+
MULTIXACT_FLAGBYTES_PER_GROUP +
211+
member_in_group * sizeof(TransactionId);
212+
}
175213

176214
/* Multixact members wraparound thresholds. */
177215
#define MULTIXACT_MEMBER_SAFE_THRESHOLD (MaxMultiXactOffset / 2)
178216
#define MULTIXACT_MEMBER_DANGER_THRESHOLD \
179217
(MaxMultiXactOffset - MaxMultiXactOffset / 4)
180218

181-
#define PreviousMultiXactId(xid) \
182-
((xid) == FirstMultiXactId ? MaxMultiXactId : (xid) - 1)
219+
static inline MultiXactId
220+
PreviousMultiXactId(MultiXactId multi)
221+
{
222+
return multi == FirstMultiXactId ? MaxMultiXactId : multi - 1;
223+
}
183224

184225
/*
185226
* Links to shared-memory data structures for MultiXact control

0 commit comments

Comments
 (0)