src/backend/utils/mmgr/aset.c
static void
AllocSetFree(MemoryContext context, void *pointer)
{
	AllocSet	set = (AllocSet) context;
	AllocChunk	chunk = AllocPointerGetChunk(pointer);

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

#ifdef MEMORY_CONTEXT_CHECKING
	/* Test for someone scribbling on unused space in chunk */
	if (chunk->requested_size < chunk->size)
		if (!sentinel_ok(pointer, chunk->requested_size))
			elog(WARNING, "detected write past chunk end in %s %p",
				 set->header.name, chunk);
#endif

	if (chunk->size > set->allocChunkLimit)
	{
		/*
		 * Big chunks are certain to have been allocated as single-chunk
		 * blocks.  Just unlink that block and return it to malloc().
		 */
		AllocBlock	block = (AllocBlock) (((char *) chunk) - ALLOC_BLOCKHDRSZ);

		/*
		 * Try to verify that we have a sane block pointer: it should
		 * reference the correct aset, and freeptr and endptr should point
		 * just past the chunk.
		 */
		if (block->aset != set ||
			block->freeptr != block->endptr ||
			block->freeptr != ((char *) block) +
			(chunk->size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ))
			elog(ERROR, "could not find block containing chunk %p", chunk);

		/* OK, remove block from aset's list and free it */
		if (block->prev)
			block->prev->next = block->next;
		else
			set->blocks = block->next;
		if (block->next)
			block->next->prev = block->prev;

		context->mem_allocated -= block->endptr - ((char *) block);

#ifdef CLOBBER_FREED_MEMORY
		wipe_mem(block, block->freeptr - ((char *) block));
#endif
		free(block);
	}
	else
	{
		/* Normal case, put the chunk into appropriate freelist */
		int			fidx = AllocSetFreeIndex(chunk->size);

		chunk->aset = (void *) set->freelist[fidx];

#ifdef CLOBBER_FREED_MEMORY
		wipe_mem(pointer, chunk->size);
#endif

#ifdef MEMORY_CONTEXT_CHECKING
		/* Reset requested_size to 0 in chunks that are on freelist */
		chunk->requested_size = 0;
#endif
		set->freelist[fidx] = chunk;
	}
}
/*
 * AllocSetFree
 *		Frees allocated memory; memory is removed from the set.
 */
AllocSetFree
Frees allocated memory; memory is removed from the set.
AllocSetFree Frees allocated memory ; memory is removed from the set .
static void
AllocSetFree(MemoryContext context, void *pointer)
{
	AllocSet	set = (AllocSet) context;
	AllocChunk	chunk = AllocPointerGetChunk(pointer);
	VALGRIND_MAKE_MEM_DEFINED(chunk, ALLOCCHUNK_PRIVATE_LEN);
#ifdef MEMORY_CONTEXT_CHECKING
	if (chunk->requested_size < chunk->size)
		if (!sentinel_ok(pointer, chunk->requested_size))
			elog(WARNING, "detected write past chunk end in %s %p",
				 set->header.name, chunk);
#endif
	if (chunk->size > set->allocChunkLimit)
	{
		AllocBlock	block = (AllocBlock) (((char *) chunk) - ALLOC_BLOCKHDRSZ);
		if (block->aset != set ||
			block->freeptr != block->endptr ||
			block->freeptr != ((char *) block) +
			(chunk->size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ))
			elog(ERROR, "could not find block containing chunk %p", chunk);
		if (block->prev)
			block->prev->next = block->next;
		else
			set->blocks = block->next;
		if (block->next)
			block->next->prev = block->prev;
		context->mem_allocated -= block->endptr - ((char *) block);
#ifdef CLOBBER_FREED_MEMORY
		wipe_mem(block, block->freeptr - ((char *) block));
#endif
		free(block);
	}
	else
	{
		int			fidx = AllocSetFreeIndex(chunk->size);
		chunk->aset = (void *) set->freelist[fidx];
#ifdef CLOBBER_FREED_MEMORY
		wipe_mem(pointer, chunk->size);
#endif
#ifdef MEMORY_CONTEXT_CHECKING
		chunk->requested_size = 0;
#endif
		set->freelist[fidx] = chunk;
	}
}
static void AllocSetFree ( MemoryContext context , void * pointer ) { AllocSet set = ( AllocSet ) context ; AllocChunk chunk = AllocPointerGetChunk ( pointer ) ; VALGRIND_MAKE_MEM_DEFINED ( chunk , ALLOCCHUNK_PRIVATE_LEN ) ; #ifdef MEMORY_CONTEXT_CHECKING if ( chunk -> requested_size < chunk -> size ) if ( ! sentinel_ok ( pointer , chunk -> requested_size ) ) elog ( WARNING , " " , set -> header . name , chunk ) ; #endif if ( chunk -> size > set -> allocChunkLimit ) { AllocBlock block = ( AllocBlock ) ( ( ( char * ) chunk ) - ALLOC_BLOCKHDRSZ ) ; if ( block -> aset != set || block -> freeptr != block -> endptr || block -> freeptr != ( ( char * ) block ) + ( chunk -> size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ ) ) elog ( ERROR , " " , chunk ) ; if ( block -> prev ) block -> prev -> next = block -> next ; else set -> blocks = block -> next ; if ( block -> next ) block -> next -> prev = block -> prev ; context -> mem_allocated -= block -> endptr - ( ( char * ) block ) ; #ifdef CLOBBER_FREED_MEMORY wipe_mem ( block , block -> freeptr - ( ( char * ) block ) ) ; #endif free ( block ) ; } else { int fidx = AllocSetFreeIndex ( chunk -> size ) ; chunk -> aset = ( void * ) set -> freelist [ fidx ] ; #ifdef CLOBBER_FREED_MEMORY wipe_mem ( pointer , chunk -> size ) ; #endif #ifdef MEMORY_CONTEXT_CHECKING chunk -> requested_size = 0 ; #endif set -> freelist [ fidx ] = chunk ; } }
AllocSetFree
              Frees allocated memory; memory is removed from the set.
AllocSetFree Frees allocated memory ; memory is removed from the set .
/* Allow access to private part of chunk header. */ /* Test for someone scribbling on unused space in chunk */ /*
		 * Big chunks are certain to have been allocated as single-chunk
		 * blocks.  Just unlink that block and return it to malloc().
		 */ /*
		 * Try to verify that we have a sane block pointer: it should
		 * reference the correct aset, and freeptr and endptr should point
		 * just past the chunk.
		 */ /* OK, remove block from aset's list and free it */ /* Normal case, put the chunk into appropriate freelist */ /* Reset requested_size to 0 in chunks that are on freelist */