src/backend/utils/mmgr/mcxt.c
void
MemoryContextCreate(MemoryContext node,
					NodeTag tag,
					const MemoryContextMethods *methods,
					MemoryContext parent,
					const char *name)
{
	/* Creating new memory contexts is not allowed in a critical section */
	Assert(CritSectionCount == 0);

	/* Initialize all standard fields of memory context header */
	node->type = tag;
	node->isReset = true;
	node->methods = methods;
	node->parent = parent;
	node->firstchild = NULL;
	node->mem_allocated = 0;
	node->prevchild = NULL;
	node->name = name;
	node->ident = NULL;
	node->reset_cbs = NULL;

	/* OK to link node into context tree */
	if (parent)
	{
		node->nextchild = parent->firstchild;
		if (parent->firstchild != NULL)
			parent->firstchild->prevchild = node;
		parent->firstchild = node;
		/* inherit allowInCritSection flag from parent */
		node->allowInCritSection = parent->allowInCritSection;
	}
	else
	{
		node->nextchild = NULL;
		node->allowInCritSection = false;
	}

	VALGRIND_CREATE_MEMPOOL(node, 0, false);
}
/*
 * MemoryContextCreate
 *		Context-type-independent part of context creation.
 *
 * This is only intended to be called by context-type-specific
 * context creation routines, not by the unwashed masses.
 *
 * The memory context creation procedure goes like this:
 *	1.  Context-type-specific routine makes some initial space allocation,
 *		including enough space for the context header.  If it fails,
 *		it can ereport() with no damage done.
 *	2.	Context-type-specific routine sets up all type-specific fields of
 *		the header (those beyond MemoryContextData proper), as well as any
 *		other management fields it needs to have a fully valid context.
 *		Usually, failure in this step is impossible, but if it's possible
 *		the initial space allocation should be freed before ereport'ing.
 *	3.	Context-type-specific routine calls MemoryContextCreate() to fill in
 *		the generic header fields and link the context into the context tree.
 *	4.  We return to the context-type-specific routine, which finishes
 *		up type-specific initialization.  This routine can now do things
 *		that might fail (like allocate more memory), so long as it's
 *		sure the node is left in a state that delete will handle.
 *
 * node: the as-yet-uninitialized common part of the context header node.
 * tag: NodeTag code identifying the memory context type.
 * methods: context-type-specific methods (usually statically allocated).
 * parent: parent context, or NULL if this will be a top-level context.
 * name: name of context (must be statically allocated).
 *
 * Context routines generally assume that MemoryContextCreate can't fail,
 * so this can contain Assert but not elog/ereport.
 */
MemoryContextCreate
Context-type-independent part of context creation.

This is only intended to be called by context-type-specific
context creation routines, not by the unwashed masses.

The memory context creation procedure goes like this:
1.  Context-type-specific routine makes some initial space allocation,
including enough space for the context header.  If it fails,
it can ereport() with no damage done.
2.      Context-type-specific routine sets up all type-specific fields of
the header (those beyond MemoryContextData proper), as well as any
other management fields it needs to have a fully valid context.
Usually, failure in this step is impossible, but if it's possible
the initial space allocation should be freed before ereport'ing.
3.      Context-type-specific routine calls MemoryContextCreate() to fill in
the generic header fields and link the context into the context tree.
4.  We return to the context-type-specific routine, which finishes
up type-specific initialization.  This routine can now do things
that might fail (like allocate more memory), so long as it's
sure the node is left in a state that delete will handle.

the as-yet-uninitialized common part of the context header node.
tag: NodeTag code identifying the memory context type.
methods: context-type-specific methods (usually statically allocated).
parent: parent context, or NULL if this will be a top-level context.
name: name of context (must be statically allocated).

Context routines generally assume that MemoryContextCreate can't fail,
so this can contain Assert but not elog/ereport.
MemoryContextCreate Context - type - independent part of context creation . This is only intended to be called by context - type - specific context creation routines not by the unwashed masses . The memory context creation procedure goes like this : 1 . Context - type - specific routine makes some initial space allocation including enough space for the context header . If it fails it can ereport () with no damage done . 2 . Context - type - specific routine sets up all type - specific fields of the header ( those beyond MemoryContextData proper ) as well as any other management fields it needs to have a fully valid context . Usually failure in this step is impossible but if it ' s possible the initial space allocation should be freed before ereport ' ing . 3 . Context - type - specific routine calls MemoryContextCreate () to fill in the generic header fields and link the context into the context tree . 4 . We return to the context - type - specific routine which finishes up type - specific initialization . This routine can now do things that might fail ( like allocate more memory ) so long as it ' s sure the node is left in a state that delete will handle . the as - yet - uninitialized common part of the context header node . tag : NodeTag code identifying the memory context type . methods : context - type - specific methods ( usually statically allocated ) . parent : parent context or NULL if this will be a top - level context . name : name of context ( must be statically allocated ) . Context routines generally assume that MemoryContextCreate can ' t fail so this can contain Assert but not elog / ereport .
void
MemoryContextCreate(MemoryContext node,
					NodeTag tag,
					const MemoryContextMethods *methods,
					MemoryContext parent,
					const char *name)
{
	Assert(CritSectionCount == 0);
	node->type = tag;
	node->isReset = true;
	node->methods = methods;
	node->parent = parent;
	node->firstchild = NULL;
	node->mem_allocated = 0;
	node->prevchild = NULL;
	node->name = name;
	node->ident = NULL;
	node->reset_cbs = NULL;
	if (parent)
	{
		node->nextchild = parent->firstchild;
		if (parent->firstchild != NULL)
			parent->firstchild->prevchild = node;
		parent->firstchild = node;
		node->allowInCritSection = parent->allowInCritSection;
	}
	else
	{
		node->nextchild = NULL;
		node->allowInCritSection = false;
	}
	VALGRIND_CREATE_MEMPOOL(node, 0, false);
}
void MemoryContextCreate ( MemoryContext node , NodeTag tag , const MemoryContextMethods * methods , MemoryContext parent , const char * name ) { Assert ( CritSectionCount == 0 ) ; node -> type = tag ; node -> isReset = true ; node -> methods = methods ; node -> parent = parent ; node -> firstchild = NULL ; node -> mem_allocated = 0 ; node -> prevchild = NULL ; node -> name = name ; node -> ident = NULL ; node -> reset_cbs = NULL ; if ( parent ) { node -> nextchild = parent -> firstchild ; if ( parent -> firstchild != NULL ) parent -> firstchild -> prevchild = node ; parent -> firstchild = node ; node -> allowInCritSection = parent -> allowInCritSection ; } else { node -> nextchild = NULL ; node -> allowInCritSection = false ; } VALGRIND_CREATE_MEMPOOL ( node , 0 , false ) ; }
MemoryContextCreate
              Context-type-independent part of context creation.
MemoryContextCreate Context - type - independent part of context creation .
/* Creating new memory contexts is not allowed in a critical section */ /* Initialize all standard fields of memory context header */ /* OK to link node into context tree */ /* inherit allowInCritSection flag from parent */