src/backend/utils/mmgr/aset.c
static void
AllocSetDelete(MemoryContext context)
{
	AllocSet	set = (AllocSet) context;
	AllocBlock	block = set->blocks;
	Size		keepersize PG_USED_FOR_ASSERTS_ONLY
	= set->keeper->endptr - ((char *) set);

	AssertArg(AllocSetIsValid(set));

#ifdef MEMORY_CONTEXT_CHECKING
	/* Check for corruption and leaks before freeing */
	AllocSetCheck(context);
#endif

	/*
	 * If the context is a candidate for a freelist, put it into that freelist
	 * instead of destroying it.
	 */
	if (set->freeListIndex >= 0)
	{
		AllocSetFreeList *freelist = &context_freelists[set->freeListIndex];

		/*
		 * Reset the context, if it needs it, so that we aren't hanging on to
		 * more than the initial malloc chunk.
		 */
		if (!context->isReset)
			MemoryContextResetOnly(context);

		/*
		 * If the freelist is full, just discard what's already in it.  See
		 * comments with context_freelists[].
		 */
		if (freelist->num_free >= MAX_FREE_CONTEXTS)
		{
			while (freelist->first_free != NULL)
			{
				AllocSetContext *oldset = freelist->first_free;

				freelist->first_free = (AllocSetContext *) oldset->header.nextchild;
				freelist->num_free--;

				/* All that remains is to free the header/initial block */
				free(oldset);
			}
			Assert(freelist->num_free == 0);
		}

		/* Now add the just-deleted context to the freelist. */
		set->header.nextchild = (MemoryContext) freelist->first_free;
		freelist->first_free = set;
		freelist->num_free++;

		return;
	}

	/* Free all blocks, except the keeper which is part of context header */
	while (block != NULL)
	{
		AllocBlock	next = block->next;

		if (block != set->keeper)
			context->mem_allocated -= block->endptr - ((char *) block);

#ifdef CLOBBER_FREED_MEMORY
		wipe_mem(block, block->freeptr - ((char *) block));
#endif

		if (block != set->keeper)
			free(block);

		block = next;
	}

	Assert(context->mem_allocated == keepersize);

	/* Finally, free the context header, including the keeper block */
	free(set);
}
/*
 * AllocSetDelete
 *		Frees all memory which is allocated in the given set,
 *		in preparation for deletion of the set.
 *
 * Unlike AllocSetReset, this *must* free all resources of the set.
 */
AllocSetDelete
Frees all memory which is allocated in the given set,
in preparation for deletion of the set.

Unlike AllocSetReset, this *must* free all resources of the set.
AllocSetDelete Frees all memory which is allocated in the given set in preparation for deletion of the set . Unlike AllocSetReset this * must * free all resources of the set .
static void
AllocSetDelete(MemoryContext context)
{
	AllocSet	set = (AllocSet) context;
	AllocBlock	block = set->blocks;
	Size		keepersize PG_USED_FOR_ASSERTS_ONLY
	= set->keeper->endptr - ((char *) set);
	AssertArg(AllocSetIsValid(set));
#ifdef MEMORY_CONTEXT_CHECKING
	AllocSetCheck(context);
#endif
	if (set->freeListIndex >= 0)
	{
		AllocSetFreeList *freelist = &context_freelists[set->freeListIndex];
		if (!context->isReset)
			MemoryContextResetOnly(context);
		if (freelist->num_free >= MAX_FREE_CONTEXTS)
		{
			while (freelist->first_free != NULL)
			{
				AllocSetContext *oldset = freelist->first_free;
				freelist->first_free = (AllocSetContext *) oldset->header.nextchild;
				freelist->num_free--;
				free(oldset);
			}
			Assert(freelist->num_free == 0);
		}
		set->header.nextchild = (MemoryContext) freelist->first_free;
		freelist->first_free = set;
		freelist->num_free++;
		return;
	}
	while (block != NULL)
	{
		AllocBlock	next = block->next;
		if (block != set->keeper)
			context->mem_allocated -= block->endptr - ((char *) block);
#ifdef CLOBBER_FREED_MEMORY
		wipe_mem(block, block->freeptr - ((char *) block));
#endif
		if (block != set->keeper)
			free(block);
		block = next;
	}
	Assert(context->mem_allocated == keepersize);
	free(set);
}
static void AllocSetDelete ( MemoryContext context ) { AllocSet set = ( AllocSet ) context ; AllocBlock block = set -> blocks ; Size keepersize PG_USED_FOR_ASSERTS_ONLY = set -> keeper -> endptr - ( ( char * ) set ) ; AssertArg ( AllocSetIsValid ( set ) ) ; #ifdef MEMORY_CONTEXT_CHECKING AllocSetCheck ( context ) ; #endif if ( set -> freeListIndex >= 0 ) { AllocSetFreeList * freelist = & context_freelists [ set -> freeListIndex ] ; if ( ! context -> isReset ) MemoryContextResetOnly ( context ) ; if ( freelist -> num_free >= MAX_FREE_CONTEXTS ) { while ( freelist -> first_free != NULL ) { AllocSetContext * oldset = freelist -> first_free ; freelist -> first_free = ( AllocSetContext * ) oldset -> header . nextchild ; freelist -> num_free -- ; free ( oldset ) ; } Assert ( freelist -> num_free == 0 ) ; } set -> header . nextchild = ( MemoryContext ) freelist -> first_free ; freelist -> first_free = set ; freelist -> num_free ++ ; return ; } while ( block != NULL ) { AllocBlock next = block -> next ; if ( block != set -> keeper ) context -> mem_allocated -= block -> endptr - ( ( char * ) block ) ; #ifdef CLOBBER_FREED_MEMORY wipe_mem ( block , block -> freeptr - ( ( char * ) block ) ) ; #endif if ( block != set -> keeper ) free ( block ) ; block = next ; } Assert ( context -> mem_allocated == keepersize ) ; free ( set ) ; }
AllocSetDelete
              Frees all memory which is allocated in the given set,
              in preparation for deletion of the set.
AllocSetDelete Frees all memory which is allocated in the given set in preparation for deletion of the set .
/* Check for corruption and leaks before freeing */ /*
	 * If the context is a candidate for a freelist, put it into that freelist
	 * instead of destroying it.
	 */ /*
		 * Reset the context, if it needs it, so that we aren't hanging on to
		 * more than the initial malloc chunk.
		 */ /*
		 * If the freelist is full, just discard what's already in it.  See
		 * comments with context_freelists[].
		 */ /* All that remains is to free the header/initial block */ /* Now add the just-deleted context to the freelist. */ /* Free all blocks, except the keeper which is part of context header */ /* Finally, free the context header, including the keeper block */