diff --git a/src/backend/nodes/nodes.c b/src/backend/nodes/nodes.c index 938cfd91a8cf6ec2996fe1be252ee8f26e1580ed..6d1deadb0bbd464f6b5c10624fe3931d0f4396ce 100644 --- a/src/backend/nodes/nodes.c +++ b/src/backend/nodes/nodes.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/nodes/nodes.c,v 1.17 2002/10/11 04:16:44 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/nodes/nodes.c,v 1.18 2002/11/10 02:17:25 momjian Exp $ * * HISTORY * Andrew Yu Oct 20, 1994 file creation @@ -28,15 +28,5 @@ * macro makeNode. eg. to create a Resdom node, use makeNode(Resdom) * */ -Node * -newNode(Size size, NodeTag tag) -{ - Node *newNode; +Node *newNodeMacroHolder; - Assert(size >= sizeof(Node)); /* need the tag, at least */ - - newNode = (Node *) palloc(size); - MemSet((char *) newNode, 0, size); - newNode->type = tag; - return newNode; -} diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index b7dd265863f08f0f654d61e7f6cb5b5e96bc1184..d2432c0068880c48045b8eb3993f1afe6db01580 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.34 2002/10/11 04:16:44 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.35 2002/11/10 02:17:25 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -452,6 +452,29 @@ MemoryContextAlloc(MemoryContext context, Size size) return (*context->methods->alloc) (context, size); } +/* + * 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. + */ +void * +MemoryContextAllocZero(MemoryContext context, Size size) +{ + void *ret; + + AssertArg(MemoryContextIsValid(context)); + + if (!AllocSizeIsValid(size)) + elog(ERROR, "MemoryContextAllocZero: invalid request size %lu", + (unsigned long) size); + + ret = (*context->methods->alloc) (context, size); + MemSet(ret, 0, size); + return ret; +} + /* * pfree * Release an allocated chunk. diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index aad54a9c26c09c5665e28a533ce9bd7c62fb237d..cec753092108e48aaf4da4ba45018bc644883c9a 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: nodes.h,v 1.121 2002/11/06 00:00:44 tgl Exp $ + * $Id: nodes.h,v 1.122 2002/11/10 02:17:25 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -262,6 +262,24 @@ typedef struct Node #define nodeTag(nodeptr) (((Node*)(nodeptr))->type) +/* + * There is no way to dereference the palloc'ed pointer to assign the + * tag, and return the pointer itself, so we need a holder variable. + * Fortunately, this function isn't recursive so we just define + * a global variable for this purpose. + */ +extern Node *newNodeMacroHolder; + +#define newNode(size, tag) \ +( \ + AssertMacro((size) >= sizeof(Node)), /* need the tag, at least */ \ +\ + newNodeMacroHolder = (Node *) palloc0(size), \ + newNodeMacroHolder->type = (tag), \ + newNodeMacroHolder \ +) + + #define makeNode(_type_) ((_type_ *) newNode(sizeof(_type_),T_##_type_)) #define NodeSetTag(nodeptr,t) (((Node*)(nodeptr))->type = (t)) @@ -283,11 +301,6 @@ typedef struct Node * ---------------------------------------------------------------- */ -/* - * nodes/nodes.c - */ -extern Node *newNode(Size size, NodeTag tag); - /* * nodes/{outfuncs.c,print.c} */ diff --git a/src/include/utils/palloc.h b/src/include/utils/palloc.h index 44adc1ed969b50ec942051b279389737216c2ed2..89b5c151a2c58d19ab1adf4224fd02237c90eb10 100644 --- a/src/include/utils/palloc.h +++ b/src/include/utils/palloc.h @@ -21,7 +21,7 @@ * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: palloc.h,v 1.21 2002/10/11 04:16:44 momjian Exp $ + * $Id: palloc.h,v 1.22 2002/11/10 02:17:25 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -46,9 +46,12 @@ extern DLLIMPORT MemoryContext CurrentMemoryContext; * Fundamental memory-allocation operations (more are in utils/memutils.h) */ extern void *MemoryContextAlloc(MemoryContext context, Size size); +extern void *MemoryContextAllocZero(MemoryContext context, Size size); #define palloc(sz) MemoryContextAlloc(CurrentMemoryContext, (sz)) +#define palloc0(sz) MemoryContextAllocZero(CurrentMemoryContext, (sz)) + extern void pfree(void *pointer); extern void *repalloc(void *pointer, Size size);