PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
checksum_impl.h File Reference
#include "storage/bufpage.h"
Include dependency graph for checksum_impl.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define N_SUMS   32
 
#define FNV_PRIME   16777619
 
#define CHECKSUM_COMP(checksum, value)
 

Functions

static uint32 pg_checksum_block (char *data, uint32 size)
 
uint16 pg_checksum_page (char *page, BlockNumber blkno)
 

Variables

static const uint32 checksumBaseOffsets [N_SUMS]
 

Macro Definition Documentation

#define CHECKSUM_COMP (   checksum,
  value 
)
Value:
do { \
uint32 __tmp = (checksum) ^ (value); \
(checksum) = __tmp * FNV_PRIME ^ (__tmp >> 17); \
} while (0)
static struct @76 value
unsigned int uint32
Definition: c.h:265
#define FNV_PRIME

Definition at line 128 of file checksum_impl.h.

Referenced by pg_checksum_block().

#define FNV_PRIME   16777619

Definition at line 108 of file checksum_impl.h.

#define N_SUMS   32

Definition at line 106 of file checksum_impl.h.

Referenced by pg_checksum_block().

Function Documentation

static uint32 pg_checksum_block ( char *  data,
uint32  size 
)
static

Definition at line 139 of file checksum_impl.h.

References Assert, CHECKSUM_COMP, checksumBaseOffsets, i, and N_SUMS.

Referenced by pg_checksum_page().

140 {
141  uint32 sums[N_SUMS];
142  uint32 (*dataArr)[N_SUMS] = (uint32 (*)[N_SUMS]) data;
143  uint32 result = 0;
144  uint32 i,
145  j;
146 
147  /* ensure that the size is compatible with the algorithm */
148  Assert((size % (sizeof(uint32) * N_SUMS)) == 0);
149 
150  /* initialize partial checksums to their corresponding offsets */
151  memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets));
152 
153  /* main checksum calculation */
154  for (i = 0; i < size / sizeof(uint32) / N_SUMS; i++)
155  for (j = 0; j < N_SUMS; j++)
156  CHECKSUM_COMP(sums[j], dataArr[i][j]);
157 
158  /* finally add in two rounds of zeroes for additional mixing */
159  for (i = 0; i < 2; i++)
160  for (j = 0; j < N_SUMS; j++)
161  CHECKSUM_COMP(sums[j], 0);
162 
163  /* xor fold partial checksums together */
164  for (i = 0; i < N_SUMS; i++)
165  result ^= sums[i];
166 
167  return result;
168 }
static const uint32 checksumBaseOffsets[N_SUMS]
unsigned int uint32
Definition: c.h:265
#define Assert(condition)
Definition: c.h:671
#define N_SUMS
int i
#define CHECKSUM_COMP(checksum, value)
uint16 pg_checksum_page ( char *  page,
BlockNumber  blkno 
)

Definition at line 179 of file checksum_impl.h.

References Assert, PageIsNew, PageHeaderData::pd_checksum, and pg_checksum_block().

Referenced by PageIsVerified(), PageSetChecksumCopy(), PageSetChecksumInplace(), and rewriteVisibilityMap().

180 {
181  PageHeader phdr = (PageHeader) page;
182  uint16 save_checksum;
183  uint32 checksum;
184 
185  /* We only calculate the checksum for properly-initialized pages */
186  Assert(!PageIsNew(page));
187 
188  /*
189  * Save pd_checksum and temporarily set it to zero, so that the checksum
190  * calculation isn't affected by the old checksum stored on the page.
191  * Restore it after, because actually updating the checksum is NOT part of
192  * the API of this function.
193  */
194  save_checksum = phdr->pd_checksum;
195  phdr->pd_checksum = 0;
196  checksum = pg_checksum_block(page, BLCKSZ);
197  phdr->pd_checksum = save_checksum;
198 
199  /* Mix in the block number to detect transposed pages */
200  checksum ^= blkno;
201 
202  /*
203  * Reduce to a uint16 (to fit in the pd_checksum field) with an offset of
204  * one. That avoids checksums of zero, which seems like a good idea.
205  */
206  return (checksum % 65535) + 1;
207 }
static uint32 pg_checksum_block(char *data, uint32 size)
uint16 pd_checksum
Definition: bufpage.h:152
unsigned short uint16
Definition: c.h:264
unsigned int uint32
Definition: c.h:265
PageHeaderData * PageHeader
Definition: bufpage.h:162
#define Assert(condition)
Definition: c.h:671
#define PageIsNew(page)
Definition: bufpage.h:226

Variable Documentation

const uint32 checksumBaseOffsets[N_SUMS]
static
Initial value:
= {
0x5B1F36E9, 0xB8525960, 0x02AB50AA, 0x1DE66D2A,
0x79FF467A, 0x9BB9F8A3, 0x217E7CD2, 0x83E13D2C,
0xF8D4474F, 0xE39EB970, 0x42C6AE16, 0x993216FA,
0x7B093B5D, 0x98DAFF3C, 0xF718902A, 0x0B1C9CDB,
0xE58F764B, 0x187636BC, 0x5D7B3BB1, 0xE73DE7DE,
0x92BEC979, 0xCCA6C0B2, 0x304A0979, 0x85AA43D4,
0x783125BB, 0x6CA8EAA2, 0xE407EAC6, 0x4B5CFC3E,
0x9FBF8C76, 0x15CA20BE, 0xF2CA9FD3, 0x959BD756
}

Definition at line 114 of file checksum_impl.h.

Referenced by pg_checksum_block().