/* * MemoryContextDelete * Delete a context and its descendants, and release all space * allocated therein. * * The type-specific delete routine removes all subsidiary storage * for the context, but we have to delete the context node itself, * as well as recurse to get the children. We must also delink the * node from its parent, if it has one. */ void MemoryContextDelete(MemoryContext context) { AssertArg(MemoryContextIsValid(context)); /* We had better not be deleting TopMemoryContext ... */ Assert(context != TopMemoryContext); /* And not CurrentMemoryContext, either */ Assert(context != CurrentMemoryContext); MemoryContextDeleteChildren(context); /* * It's not entirely clear whether 'tis better to do this before or after * delinking the context; but an error in a callback will likely result in * leaking the whole context (if it's not a root context) if we do it * after, so let's do it before. */ MemoryContextCallResetCallbacks(context); /* * We delink the context from its parent before deleting it, so that if * there's an error we won't have deleted/busted contexts still attached * to the context tree. Better a leak than a crash. */ MemoryContextSetParent(context, NULL); (*context->methods->delete_context) (context); VALGRIND_DESTROY_MEMPOOL(context); pfree(context); }
/* * Transfer ownership of an expanded object to a new___ parent memory context. * The object must be referenced by a R/W pointer, and what we return is * always its "standard" R/W pointer, which is certain to have the same * lifespan as the object itself. (The passed-in pointer might not, and * in any case wouldn't provide a unique identifier if it's not that one.) */ Datum TransferExpandedObject(Datum d, MemoryContext new_parent) { ExpandedObjectHeader *eohptr = DatumGetEOHP(d); /* Assert caller gave a R/W pointer */ Assert(VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(d))); /* Transfer ownership */ MemoryContextSetParent(eohptr->eoh_context, new_parent); /* Return the object's standard read-write pointer */ return EOHPGetRWDatum(eohptr); }
/* * MemoryContextDelete * Delete a context and its descendants, and release all space * allocated therein. * * The type-specific delete routine removes all subsidiary storage * for the context, but we have to delete the context node itself, * as well as recurse to get the children. We must also delink the * node from its parent, if it has one. */ void MemoryContextDelete(MemoryContext context) { AssertArg(MemoryContextIsValid(context)); /* We had better not be deleting TopMemoryContext ... */ Assert(context != TopMemoryContext); /* And not CurrentMemoryContext, either */ Assert(context != CurrentMemoryContext); MemoryContextDeleteChildren(context); /* * We delink the context from its parent before deleting it, so that if * there's an error we won't have deleted/busted contexts still attached * to the context tree. Better a leak than a crash. */ MemoryContextSetParent(context, NULL); (*context->methods->delete_context) (context); pfree(context); }