Skip to content

Commit 6253f9d

Browse files
committed
In GCC-based builds, use a better newNode() macro that relies on GCC-specific
syntax to avoid a useless store into a global variable. Per experimentation, this works better than my original thought of trying to push the code into an out-of-line subroutine.
1 parent 7ad60b4 commit 6253f9d

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/backend/nodes/nodes.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/nodes/nodes.c,v 1.28 2008/01/01 19:45:50 momjian Exp $
12+
* $PostgreSQL: pgsql/src/backend/nodes/nodes.c,v 1.29 2008/08/29 22:49:07 tgl Exp $
1313
*
1414
* HISTORY
1515
* Andrew Yu Oct 20, 1994 file creation
@@ -22,6 +22,10 @@
2222

2323
/*
2424
* Support for newNode() macro
25+
*
26+
* In a GCC build there is no need for the global variable newNodeMacroHolder.
27+
* However, we create it anyway, to support the case of a non-GCC-built
28+
* loadable module being loaded into a GCC-built backend.
2529
*/
2630

2731
Node *newNodeMacroHolder;

src/include/nodes/nodes.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/nodes/nodes.h,v 1.209 2008/08/22 00:16:04 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/nodes/nodes.h,v 1.210 2008/08/29 22:49:07 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -384,6 +384,23 @@ typedef struct Node
384384
* !WARNING!: Avoid using newNode directly. You should be using the
385385
* macro makeNode. eg. to create a Query node, use makeNode(Query)
386386
*
387+
* Note: the size argument should always be a compile-time constant, so the
388+
* apparent risk of multiple evaluation doesn't matter in practice.
389+
*/
390+
#ifdef __GNUC__
391+
392+
/* With GCC, we can use a compound statement within an expression */
393+
#define newNode(size, tag) \
394+
({ Node *_result; \
395+
AssertMacro((size) >= sizeof(Node)); /* need the tag, at least */ \
396+
_result = (Node *) palloc0fast(size); \
397+
_result->type = (tag); \
398+
_result; \
399+
})
400+
401+
#else
402+
403+
/*
387404
* There is no way to dereference the palloc'ed pointer to assign the
388405
* tag, and also return the pointer itself, so we need a holder variable.
389406
* Fortunately, this macro isn't recursive so we just define
@@ -399,6 +416,8 @@ extern PGDLLIMPORT Node *newNodeMacroHolder;
399416
newNodeMacroHolder \
400417
)
401418

419+
#endif /* __GNUC__ */
420+
402421

403422
#define makeNode(_type_) ((_type_ *) newNode(sizeof(_type_),T_##_type_))
404423
#define NodeSetTag(nodeptr,t) (((Node*)(nodeptr))->type = (t))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy