I was scanning the source code of pgSQL and found an interesting macro.
#define SizeOfXLogRecord (offsetof(XLogRecord, xl_crc) + sizeof(pg_crc32c))
The definition of the XLogRecord
is
typedef struct XLogRecord
{
uint32 xl_tot_len; /* total len of entire record */
TransactionId xl_xid; /* xact id */
XLogRecPtr xl_prev; /* ptr to previous record in log */
uint8 xl_info; /* flag bits, see below */
RmgrId xl_rmid; /* resource manager for this record */
/* 2 bytes of padding here, initialize to zero */
pg_crc32c xl_crc; /* CRC for this record */
/* XLogRecordBlockHeaders and XLogRecordDataHeader follow, no padding */
} XLogRecord;
I am confused about why the micro uses the sum of offsetof
of the last member and the sizeof
the last member to define the size of the stuct? Would there be any difference if I directly use sizeof
of the struct?
By the way, what is the size of the struct XLogRecord
? I think it's 24 bytes because of memory alignment. But I saw from here that the size is 32 bytes (pciture here). Can someone also explain this?
Update: the size of XLogRecord
varies from versions. In the latest version this size is exactly 24 bytes.
Any help or information will be appreciated.
The code is an excerpt from https://github.com/postgres/postgres/blob/4035cd5d4eee4dae797bfc77ab07f8dcd8781b41/src/include/access/xlogrecord.h