Remove MemoryContextContains().
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 6 Oct 2022 17:35:31 +0000 (13:35 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 6 Oct 2022 17:35:31 +0000 (13:35 -0400)
MemoryContextContains is no longer reliable in the wake of c6e0fe1f2,
because there's no longer very much redundancy in chunk headers.
(It wasn't *completely* reliable even before that, as there was a
chance of a false positive if you passed it something that didn't
point to an mcxt chunk at all.  But it was generally good enough.)

Hence, remove it.  There is no remaining core code that requires it.
Extensions that have been using it might be able to substitute a
test like "GetMemoryChunkContext(ptr) == context", recognizing that
this explicitly requires that the pointer point to some chunk.

Tom Lane and David Rowley

Discussion: https://postgr.es/m/1913788.1664898906@sss.pgh.pa.us

src/backend/utils/mmgr/mcxt.c
src/include/utils/memutils.h

index 115a64cfe40746a557e3431328851fe913f622a5..c80236d285ae6e421d7bbeb8b39aa3cae8305a28 100644 (file)
@@ -224,10 +224,8 @@ MemoryContextResetOnly(MemoryContext context)
                 * If context->ident points into the context's memory, it will become
                 * a dangling pointer.  We could prevent that by setting it to NULL
                 * here, but that would break valid coding patterns that keep the
-                * ident elsewhere, e.g. in a parent context.  Another idea is to use
-                * MemoryContextContains(), but we don't require ident strings to be
-                * in separately-palloc'd chunks, so that risks false positives.  So
-                * for now we assume the programmer got it right.
+                * ident elsewhere, e.g. in a parent context.  So for now we assume
+                * the programmer got it right.
                 */
 
                context->methods->reset(context);
@@ -482,15 +480,6 @@ MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)
 MemoryContext
 GetMemoryChunkContext(void *pointer)
 {
-       /*
-        * Try to detect bogus pointers handed to us, poorly though we can.
-        * Presumably, a pointer that isn't MAXALIGNED isn't pointing at an
-        * allocated chunk.
-        */
-       Assert(pointer != NULL);
-       Assert(pointer == (void *) MAXALIGN(pointer));
-       /* adding further Asserts here? See pre-checks in MemoryContextContains */
-
        return MCXT_METHOD(pointer, get_chunk_context) (pointer);
 }
 
@@ -813,49 +802,6 @@ MemoryContextCheck(MemoryContext context)
 }
 #endif
 
-/*
- * MemoryContextContains
- *             Detect whether an allocated chunk of memory belongs to a given
- *             context or not.
- *
- * Caution: 'pointer' must point to a pointer which was allocated by a
- * MemoryContext.  It's not safe or valid to use this function on arbitrary
- * pointers as obtaining the MemoryContext which 'pointer' belongs to requires
- * possibly several pointer dereferences.
- */
-bool
-MemoryContextContains(MemoryContext context, void *pointer)
-{
-       /*
-        * Temporarily make this always return false as we don't yet have a fully
-        * baked idea on how to make it work correctly with the new MemoryChunk
-        * code.
-        */
-       return false;
-
-#ifdef NOT_USED
-       MemoryContext ptr_context;
-
-       /*
-        * NB: We must perform run-time checks here which GetMemoryChunkContext()
-        * does as assertions before calling GetMemoryChunkContext().
-        *
-        * Try to detect bogus pointers handed to us, poorly though we can.
-        * Presumably, a pointer that isn't MAXALIGNED isn't pointing at an
-        * allocated chunk.
-        */
-       if (pointer == NULL || pointer != (void *) MAXALIGN(pointer))
-               return false;
-
-       /*
-        * OK, it's probably safe to look at the context.
-        */
-       ptr_context = GetMemoryChunkContext(pointer);
-
-       return ptr_context == context;
-#endif
-}
-
 /*
  * MemoryContextCreate
  *             Context-type-independent part of context creation.
index 52bc41ec53bb53428c86f4c30e3735091a79eb25..4f6c5435cae9a2f77f2e886b68666a2e9b5e8620 100644 (file)
@@ -96,7 +96,6 @@ extern void MemoryContextAllowInCriticalSection(MemoryContext context,
 #ifdef MEMORY_CONTEXT_CHECKING
 extern void MemoryContextCheck(MemoryContext context);
 #endif
-extern bool MemoryContextContains(MemoryContext context, void *pointer);
 
 /* Handy macro for copying and assigning context ID ... but note double eval */
 #define MemoryContextCopyAndSetIdentifier(cxt, id) \