index 07732ba59bcd7ee0c0e3fe6f01ffa802321b38d3..6668bf135e9a81435c9a74bdec9898c0d5d6f67a 100644 (file)
bool
MemoryContextContains(MemoryContext context, void *pointer)
{
- MemoryContext ptr_context = GetMemoryChunkContext(pointer);
+ MemoryContext ptr_context;
+
+ /*
+ * NB: Can't use GetMemoryChunkContext() here - that performs assertions
+ * that aren't acceptable here since we might be passed memory not
+ * allocated by any memory context.
+ *
+ * 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 = *(MemoryContext *) (((char *) pointer) - sizeof(void *));
return ptr_context == context;
}