diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2002-12-16 16:22:46 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2002-12-16 16:22:46 +0000 |
commit | 88177f77b17ef478da1dbca9acb5e3a61b346613 (patch) | |
tree | 6aca48712e12bbdd7a7bab849dcaac3f25684e32 /src/backend/utils | |
parent | e64c7feb2fd80c89d2220cbe9e026a031f34509c (diff) | |
download | postgresql-88177f77b17ef478da1dbca9acb5e3a61b346613.tar.gz postgresql-88177f77b17ef478da1dbca9acb5e3a61b346613.zip |
Code review for palloc0 patch --- avoid dangerous and unnecessary
practice of evaluating MemSet's arguments multiple times, except for
the special case of newNode(), where we can assume the argument is
a constant sizeof() operator.
Also, add GetMemoryChunkContext() to mcxt.c's API, in preparation for
fixing recent GEQO breakage.
Diffstat (limited to 'src/backend/utils')
-rw-r--r-- | src/backend/utils/mmgr/mcxt.c | 66 |
1 files changed, 61 insertions, 5 deletions
diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index 9ce0c4ca72f..48545b15128 100644 --- a/src/backend/utils/mmgr/mcxt.c +++ b/src/backend/utils/mmgr/mcxt.c @@ -14,7 +14,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.37 2002/12/15 21:01:34 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.38 2002/12/16 16:22:46 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -258,6 +258,35 @@ GetMemoryChunkSpace(void *pointer) } /* + * GetMemoryChunkContext + * Given a currently-allocated chunk, determine the context + * it belongs to. + */ +MemoryContext +GetMemoryChunkContext(void *pointer) +{ + StandardChunkHeader *header; + + /* + * 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)); + + /* + * OK, it's probably safe to look at the chunk header. + */ + header = (StandardChunkHeader *) + ((char *) pointer - STANDARDCHUNKHEADERSIZE); + + AssertArg(MemoryContextIsValid(header->context)); + + return header->context; +} + +/* * MemoryContextStats * Print statistics about the named context and all its descendants. * @@ -453,25 +482,52 @@ MemoryContextAlloc(MemoryContext context, Size size) } /* - * MemoryContextAllocPalloc0 + * MemoryContextAllocZero * Like MemoryContextAlloc, but clears allocated memory * * We could just call MemoryContextAlloc then clear the memory, but this - * function is called too many times, so we have a separate version. + * is a very common combination, so we provide the combined operation. */ void * -MemoryContextAllocPalloc0(MemoryContext context, Size size) +MemoryContextAllocZero(MemoryContext context, Size size) { void *ret; AssertArg(MemoryContextIsValid(context)); if (!AllocSizeIsValid(size)) - elog(ERROR, "MemoryContextAllocZero: invalid request size %lu", + elog(ERROR, "MemoryContextAlloc: invalid request size %lu", (unsigned long) size); ret = (*context->methods->alloc) (context, size); + + MemSetAligned(ret, 0, size); + + return ret; +} + +/* + * MemoryContextAllocZeroAligned + * MemoryContextAllocZero where length is suitable for MemSetLoop + * + * This might seem overly specialized, but it's not because newNode() + * is so often called with compile-time-constant sizes. + */ +void * +MemoryContextAllocZeroAligned(MemoryContext context, Size size) +{ + void *ret; + + AssertArg(MemoryContextIsValid(context)); + + if (!AllocSizeIsValid(size)) + elog(ERROR, "MemoryContextAlloc: invalid request size %lu", + (unsigned long) size); + + ret = (*context->methods->alloc) (context, size); + MemSetLoop(ret, 0, size); + return ret; } |