src/backend/utils/mmgr/aset.c
static void
AllocSetReset(MemoryContext context)
{
	AllocSet	set = (AllocSet) context;
	AllocBlock	block;
	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

	/* Clear chunk freelists */
	MemSetAligned(set->freelist, 0, sizeof(set->freelist));

	block = set->blocks;

	/* New blocks list will be just the keeper block */
	set->blocks = set->keeper;

	while (block != NULL)
	{
		AllocBlock	next = block->next;

		if (block == set->keeper)
		{
			/* Reset the block, but don't return it to malloc */
			char	   *datastart = ((char *) block) + ALLOC_BLOCKHDRSZ;

#ifdef CLOBBER_FREED_MEMORY
			wipe_mem(datastart, block->freeptr - datastart);
#else
			/* wipe_mem() would have done this */
			VALGRIND_MAKE_MEM_NOACCESS(datastart, block->freeptr - datastart);
#endif
			block->freeptr = datastart;
			block->prev = NULL;
			block->next = NULL;
		}
		else
		{
			/* Normal case, release the block */
			context->mem_allocated -= block->endptr - ((char *) block);

#ifdef CLOBBER_FREED_MEMORY
			wipe_mem(block, block->freeptr - ((char *) block));
#endif
			free(block);
		}
		block = next;
	}

	Assert(context->mem_allocated == keepersize);

	/* Reset block size allocation sequence, too */
	set->nextBlockSize = set->initBlockSize;
}
/*
 * AllocSetReset
 *		Frees all memory which is allocated in the given set.
 *
 * Actually, this routine has some discretion about what to do.
 * It should mark all allocated chunks freed, but it need not necessarily
 * give back all the resources the set owns.  Our actual implementation is
 * that we give back all but the "keeper" block (which we must keep, since
 * it shares a malloc chunk with the context header).  In this way, we don't
 * thrash malloc() when a context is repeatedly reset after small allocations,
 * which is typical behavior for per-tuple contexts.
 */
AllocSetReset
Frees all memory which is allocated in the given set.

Actually, this routine has some discretion about what to do.
It should mark all allocated chunks freed, but it need not necessarily
give back all the resources the set owns.  Our actual implementation is
that we give back all but the "keeper" block (which we must keep, since
it shares a malloc chunk with the context header).  In this way, we don't
thrash malloc() when a context is repeatedly reset after small allocations,
which is typical behavior for per-tuple contexts.
AllocSetReset Frees all memory which is allocated in the given set . Actually this routine has some discretion about what to do . It should mark all allocated chunks freed but it need not necessarily give back all the resources the set owns . Our actual implementation is that we give back all but the " keeper " block ( which we must keep since it shares a malloc chunk with the context header ) . In this way we don ' t thrash malloc () when a context is repeatedly reset after small allocations which is typical behavior for per - tuple contexts .
static void
AllocSetReset(MemoryContext context)
{
	AllocSet	set = (AllocSet) context;
	AllocBlock	block;
	Size		keepersize PG_USED_FOR_ASSERTS_ONLY
	= set->keeper->endptr - ((char *) set);
	AssertArg(AllocSetIsValid(set));
#ifdef MEMORY_CONTEXT_CHECKING
	AllocSetCheck(context);
#endif
	MemSetAligned(set->freelist, 0, sizeof(set->freelist));
	block = set->blocks;
	set->blocks = set->keeper;
	while (block != NULL)
	{
		AllocBlock	next = block->next;
		if (block == set->keeper)
		{
			char	   *datastart = ((char *) block) + ALLOC_BLOCKHDRSZ;
#ifdef CLOBBER_FREED_MEMORY
			wipe_mem(datastart, block->freeptr - datastart);
#else
			VALGRIND_MAKE_MEM_NOACCESS(datastart, block->freeptr - datastart);
#endif
			block->freeptr = datastart;
			block->prev = NULL;
			block->next = NULL;
		}
		else
		{
			context->mem_allocated -= block->endptr - ((char *) block);
#ifdef CLOBBER_FREED_MEMORY
			wipe_mem(block, block->freeptr - ((char *) block));
#endif
			free(block);
		}
		block = next;
	}
	Assert(context->mem_allocated == keepersize);
	set->nextBlockSize = set->initBlockSize;
}
static void AllocSetReset ( MemoryContext context ) { AllocSet set = ( AllocSet ) context ; AllocBlock block ; Size keepersize PG_USED_FOR_ASSERTS_ONLY = set -> keeper -> endptr - ( ( char * ) set ) ; AssertArg ( AllocSetIsValid ( set ) ) ; #ifdef MEMORY_CONTEXT_CHECKING AllocSetCheck ( context ) ; #endif MemSetAligned ( set -> freelist , 0 , sizeof ( set -> freelist ) ) ; block = set -> blocks ; set -> blocks = set -> keeper ; while ( block != NULL ) { AllocBlock next = block -> next ; if ( block == set -> keeper ) { char * datastart = ( ( char * ) block ) + ALLOC_BLOCKHDRSZ ; #ifdef CLOBBER_FREED_MEMORY wipe_mem ( datastart , block -> freeptr - datastart ) ; #else VALGRIND_MAKE_MEM_NOACCESS ( datastart , block -> freeptr - datastart ) ; #endif block -> freeptr = datastart ; block -> prev = NULL ; block -> next = NULL ; } else { context -> mem_allocated -= block -> endptr - ( ( char * ) block ) ; #ifdef CLOBBER_FREED_MEMORY wipe_mem ( block , block -> freeptr - ( ( char * ) block ) ) ; #endif free ( block ) ; } block = next ; } Assert ( context -> mem_allocated == keepersize ) ; set -> nextBlockSize = set -> initBlockSize ; }
AllocSetReset
              Frees all memory which is allocated in the given set.
AllocSetReset Frees all memory which is allocated in the given set .
/* Check for corruption and leaks before freeing */ /* Clear chunk freelists */ /* New blocks list will be just the keeper block */ /* Reset the block, but don't return it to malloc */ /* wipe_mem() would have done this */ /* Normal case, release the block */ /* Reset block size allocation sequence, too */