PostgreSQL Source Code  git master
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.

Data Structures

union  PGChecksummablePage
 

Macros

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

Functions

static uint32 pg_checksum_block (const PGChecksummablePage *page)
 
uint16 pg_checksum_page (char *page, BlockNumber blkno)
 

Variables

static const uint32 checksumBaseOffsets [N_SUMS]
 

Macro Definition Documentation

◆ CHECKSUM_COMP

#define CHECKSUM_COMP (   checksum,
  value 
)
Value:
do { \
uint32 __tmp = (checksum) ^ (value); \
(checksum) = __tmp * FNV_PRIME ^ (__tmp >> 17); \
} while (0)
static struct @145 value
#define FNV_PRIME

Definition at line 135 of file checksum_impl.h.

Referenced by pg_checksum_block().

◆ FNV_PRIME

#define FNV_PRIME   16777619

Definition at line 108 of file checksum_impl.h.

◆ N_SUMS

#define N_SUMS   32

Definition at line 106 of file checksum_impl.h.

Referenced by pg_checksum_block().

Function Documentation

◆ pg_checksum_block()

static uint32 pg_checksum_block ( const PGChecksummablePage page)
static

Definition at line 146 of file checksum_impl.h.

References Assert, CHECKSUM_COMP, checksumBaseOffsets, PGChecksummablePage::data, i, and N_SUMS.

Referenced by pg_checksum_page().

147 {
148  uint32 sums[N_SUMS];
149  uint32 result = 0;
150  uint32 i,
151  j;
152 
153  /* ensure that the size is compatible with the algorithm */
154  Assert(sizeof(PGChecksummablePage) == BLCKSZ);
155 
156  /* initialize partial checksums to their corresponding offsets */
157  memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets));
158 
159  /* main checksum calculation */
160  for (i = 0; i < (uint32) (BLCKSZ / (sizeof(uint32) * N_SUMS)); i++)
161  for (j = 0; j < N_SUMS; j++)
162  CHECKSUM_COMP(sums[j], page->data[i][j]);
163 
164  /* finally add in two rounds of zeroes for additional mixing */
165  for (i = 0; i < 2; i++)
166  for (j = 0; j < N_SUMS; j++)
167  CHECKSUM_COMP(sums[j], 0);
168 
169  /* xor fold partial checksums together */
170  for (i = 0; i < N_SUMS; i++)
171  result ^= sums[i];
172 
173  return result;
174 }
static const uint32 checksumBaseOffsets[N_SUMS]
unsigned int uint32
Definition: c.h:358
uint32 data[BLCKSZ/(sizeof(uint32) *N_SUMS)][N_SUMS]
#define Assert(condition)
Definition: c.h:732
#define N_SUMS
int i
#define CHECKSUM_COMP(checksum, value)

◆ pg_checksum_page()

uint16 pg_checksum_page ( char *  page,
BlockNumber  blkno 
)

Definition at line 187 of file checksum_impl.h.

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

Referenced by page_checksum(), PageIsVerified(), PageSetChecksumCopy(), PageSetChecksumInplace(), rewriteVisibilityMap(), scan_file(), and sendFile().

188 {
189  PGChecksummablePage *cpage = (PGChecksummablePage *) page;
190  uint16 save_checksum;
191  uint32 checksum;
192 
193  /* We only calculate the checksum for properly-initialized pages */
194  Assert(!PageIsNew(&cpage->phdr));
195 
196  /*
197  * Save pd_checksum and temporarily set it to zero, so that the checksum
198  * calculation isn't affected by the old checksum stored on the page.
199  * Restore it after, because actually updating the checksum is NOT part of
200  * the API of this function.
201  */
202  save_checksum = cpage->phdr.pd_checksum;
203  cpage->phdr.pd_checksum = 0;
204  checksum = pg_checksum_block(cpage);
205  cpage->phdr.pd_checksum = save_checksum;
206 
207  /* Mix in the block number to detect transposed pages */
208  checksum ^= blkno;
209 
210  /*
211  * Reduce to a uint16 (to fit in the pd_checksum field) with an offset of
212  * one. That avoids checksums of zero, which seems like a good idea.
213  */
214  return (checksum % 65535) + 1;
215 }
static uint32 pg_checksum_block(const PGChecksummablePage *page)
uint16 pd_checksum
Definition: bufpage.h:156
unsigned short uint16
Definition: c.h:357
unsigned int uint32
Definition: c.h:358
#define Assert(condition)
Definition: c.h:732
#define PageIsNew(page)
Definition: bufpage.h:229
PageHeaderData phdr

Variable Documentation

◆ checksumBaseOffsets

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 121 of file checksum_impl.h.

Referenced by pg_checksum_block().