src/backend/utils/mmgr/aset.c
static void
AllocSetCheck(MemoryContext context)
{
	AllocSet	set = (AllocSet) context;
	const char *name = set->header.name;
	AllocBlock	prevblock;
	AllocBlock	block;
	Size		total_allocated = 0;

	for (prevblock = NULL, block = set->blocks;
		 block != NULL;
		 prevblock = block, block = block->next)
	{
		char	   *bpoz = ((char *) block) + ALLOC_BLOCKHDRSZ;
		long		blk_used = block->freeptr - bpoz;
		long		blk_data = 0;
		long		nchunks = 0;

		if (set->keeper == block)
			total_allocated += block->endptr - ((char *) set);
		else
			total_allocated += block->endptr - ((char *) block);

		/*
		 * Empty block - empty can be keeper-block only
		 */
		if (!blk_used)
		{
			if (set->keeper != block)
				elog(WARNING, "problem in alloc set %s: empty block %p",
					 name, block);
		}

		/*
		 * Check block header fields
		 */
		if (block->aset != set ||
			block->prev != prevblock ||
			block->freeptr < bpoz ||
			block->freeptr > block->endptr)
			elog(WARNING, "problem in alloc set %s: corrupt header in block %p",
				 name, block);

		/*
		 * Chunk walker
		 */
		while (bpoz < block->freeptr)
		{
			AllocChunk	chunk = (AllocChunk) bpoz;
			Size		chsize,
						dsize;

			/* Allow access to private part of chunk header. */
			VALGRIND_MAKE_MEM_DEFINED(chunk, ALLOCCHUNK_PRIVATE_LEN);

			chsize = chunk->size;	/* aligned chunk size */
			dsize = chunk->requested_size;	/* real data */

			/*
			 * Check chunk size
			 */
			if (dsize > chsize)
				elog(WARNING, "problem in alloc set %s: req size > alloc size for chunk %p in block %p",
					 name, chunk, block);
			if (chsize < (1 << ALLOC_MINBITS))
				elog(WARNING, "problem in alloc set %s: bad size %zu for chunk %p in block %p",
					 name, chsize, chunk, block);

			/* single-chunk block? */
			if (chsize > set->allocChunkLimit &&
				chsize + ALLOC_CHUNKHDRSZ != blk_used)
				elog(WARNING, "problem in alloc set %s: bad single-chunk %p in block %p",
					 name, chunk, block);

			/*
			 * If chunk is allocated, check for correct aset pointer. (If it's
			 * free, the aset is the freelist pointer, which we can't check as
			 * easily...)  Note this is an incomplete test, since palloc(0)
			 * produces an allocated chunk with requested_size == 0.
			 */
			if (dsize > 0 && chunk->aset != (void *) set)
				elog(WARNING, "problem in alloc set %s: bogus aset link in block %p, chunk %p",
					 name, block, chunk);

			/*
			 * Check for overwrite of padding space in an allocated chunk.
			 */
			if (chunk->aset == (void *) set && dsize < chsize &&
				!sentinel_ok(chunk, ALLOC_CHUNKHDRSZ + dsize))
				elog(WARNING, "problem in alloc set %s: detected write past chunk end in block %p, chunk %p",
					 name, block, chunk);

			/*
			 * If chunk is allocated, disallow external access to private part
			 * of chunk header.
			 */
			if (chunk->aset == (void *) set)
				VALGRIND_MAKE_MEM_NOACCESS(chunk, ALLOCCHUNK_PRIVATE_LEN);

			blk_data += chsize;
			nchunks++;

			bpoz += ALLOC_CHUNKHDRSZ + chsize;
		}

		if ((blk_data + (nchunks * ALLOC_CHUNKHDRSZ)) != blk_used)
			elog(WARNING, "problem in alloc set %s: found inconsistent memory block %p",
				 name, block);
	}

	Assert(total_allocated == context->mem_allocated);
}
/*
 * AllocSetCheck
 *		Walk through chunks and check consistency of memory.
 *
 * NOTE: report errors as WARNING, *not* ERROR or FATAL.  Otherwise you'll
 * find yourself in an infinite loop when trouble occurs, because this
 * routine will be entered again when elog cleanup tries to release memory!
 */
AllocSetCheck
Walk through chunks and check consistency of memory.

report errors as WARNING, *not* ERROR or FATAL.  Otherwise you'll
find yourself in an infinite loop when trouble occurs, because this
routine will be entered again when elog cleanup tries to release memory!
AllocSetCheck Walk through chunks and check consistency of memory . report errors as WARNING * not * ERROR or FATAL . Otherwise you ' ll find yourself in an infinite loop when trouble occurs because this routine will be entered again when elog cleanup tries to release memory!
static void
AllocSetCheck(MemoryContext context)
{
	AllocSet	set = (AllocSet) context;
	const char *name = set->header.name;
	AllocBlock	prevblock;
	AllocBlock	block;
	Size		total_allocated = 0;
	for (prevblock = NULL, block = set->blocks;
		 block != NULL;
		 prevblock = block, block = block->next)
	{
		char	   *bpoz = ((char *) block) + ALLOC_BLOCKHDRSZ;
		long		blk_used = block->freeptr - bpoz;
		long		blk_data = 0;
		long		nchunks = 0;
		if (set->keeper == block)
			total_allocated += block->endptr - ((char *) set);
		else
			total_allocated += block->endptr - ((char *) block);
		if (!blk_used)
		{
			if (set->keeper != block)
				elog(WARNING, "problem in alloc set %s: empty block %p",
					 name, block);
		}
		if (block->aset != set ||
			block->prev != prevblock ||
			block->freeptr < bpoz ||
			block->freeptr > block->endptr)
			elog(WARNING, "problem in alloc set %s: corrupt header in block %p",
				 name, block);
		while (bpoz < block->freeptr)
		{
			AllocChunk	chunk = (AllocChunk) bpoz;
			Size		chsize,
						dsize;
			VALGRIND_MAKE_MEM_DEFINED(chunk, ALLOCCHUNK_PRIVATE_LEN);
			chsize = chunk->size;	
			dsize = chunk->requested_size;	
			if (dsize > chsize)
				elog(WARNING, "problem in alloc set %s: req size > alloc size for chunk %p in block %p",
					 name, chunk, block);
			if (chsize < (1 << ALLOC_MINBITS))
				elog(WARNING, "problem in alloc set %s: bad size %zu for chunk %p in block %p",
					 name, chsize, chunk, block);
			if (chsize > set->allocChunkLimit &&
				chsize + ALLOC_CHUNKHDRSZ != blk_used)
				elog(WARNING, "problem in alloc set %s: bad single-chunk %p in block %p",
					 name, chunk, block);
			if (dsize > 0 && chunk->aset != (void *) set)
				elog(WARNING, "problem in alloc set %s: bogus aset link in block %p, chunk %p",
					 name, block, chunk);
			if (chunk->aset == (void *) set && dsize < chsize &&
				!sentinel_ok(chunk, ALLOC_CHUNKHDRSZ + dsize))
				elog(WARNING, "problem in alloc set %s: detected write past chunk end in block %p, chunk %p",
					 name, block, chunk);
			if (chunk->aset == (void *) set)
				VALGRIND_MAKE_MEM_NOACCESS(chunk, ALLOCCHUNK_PRIVATE_LEN);
			blk_data += chsize;
			nchunks++;
			bpoz += ALLOC_CHUNKHDRSZ + chsize;
		}
		if ((blk_data + (nchunks * ALLOC_CHUNKHDRSZ)) != blk_used)
			elog(WARNING, "problem in alloc set %s: found inconsistent memory block %p",
				 name, block);
	}
	Assert(total_allocated == context->mem_allocated);
}
static void AllocSetCheck ( MemoryContext context ) { AllocSet set = ( AllocSet ) context ; const char * name = set -> header . name ; AllocBlock prevblock ; AllocBlock block ; Size total_allocated = 0 ; for ( prevblock = NULL , block = set -> blocks ; block != NULL ; prevblock = block , block = block -> next ) { char * bpoz = ( ( char * ) block ) + ALLOC_BLOCKHDRSZ ; long blk_used = block -> freeptr - bpoz ; long blk_data = 0 ; long nchunks = 0 ; if ( set -> keeper == block ) total_allocated += block -> endptr - ( ( char * ) set ) ; else total_allocated += block -> endptr - ( ( char * ) block ) ; if ( ! blk_used ) { if ( set -> keeper != block ) elog ( WARNING , " " , name , block ) ; } if ( block -> aset != set || block -> prev != prevblock || block -> freeptr < bpoz || block -> freeptr > block -> endptr ) elog ( WARNING , " " , name , block ) ; while ( bpoz < block -> freeptr ) { AllocChunk chunk = ( AllocChunk ) bpoz ; Size chsize , dsize ; VALGRIND_MAKE_MEM_DEFINED ( chunk , ALLOCCHUNK_PRIVATE_LEN ) ; chsize = chunk -> size ; dsize = chunk -> requested_size ; if ( dsize > chsize ) elog ( WARNING , " " , name , chunk , block ) ; if ( chsize < ( 1 << ALLOC_MINBITS ) ) elog ( WARNING , " " , name , chsize , chunk , block ) ; if ( chsize > set -> allocChunkLimit && chsize + ALLOC_CHUNKHDRSZ != blk_used ) elog ( WARNING , " " , name , chunk , block ) ; if ( dsize > 0 && chunk -> aset != ( void * ) set ) elog ( WARNING , " " , name , block , chunk ) ; if ( chunk -> aset == ( void * ) set && dsize < chsize && ! sentinel_ok ( chunk , ALLOC_CHUNKHDRSZ + dsize ) ) elog ( WARNING , " " , name , block , chunk ) ; if ( chunk -> aset == ( void * ) set ) VALGRIND_MAKE_MEM_NOACCESS ( chunk , ALLOCCHUNK_PRIVATE_LEN ) ; blk_data += chsize ; nchunks ++ ; bpoz += ALLOC_CHUNKHDRSZ + chsize ; } if ( ( blk_data + ( nchunks * ALLOC_CHUNKHDRSZ ) ) != blk_used ) elog ( WARNING , " " , name , block ) ; } Assert ( total_allocated == context -> mem_allocated ) ; }
AllocSetCheck
              Walk through chunks and check consistency of memory.
AllocSetCheck Walk through chunks and check consistency of memory .
/*
		 * Empty block - empty can be keeper-block only
		 */ /*
		 * Check block header fields
		 */ /*
		 * Chunk walker
		 */ /* Allow access to private part of chunk header. */ /* aligned chunk size */ /* real data */ /*
			 * Check chunk size
			 */ /* single-chunk block? */ /*
			 * If chunk is allocated, check for correct aset pointer. (If it's
			 * free, the aset is the freelist pointer, which we can't check as
			 * easily...)  Note this is an incomplete test, since palloc(0)
			 * produces an allocated chunk with requested_size == 0.
			 */ /*
			 * Check for overwrite of padding space in an allocated chunk.
			 */ /*
			 * If chunk is allocated, disallow external access to private part
			 * of chunk header.
			 */