Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit976fa10

Browse files
committed
Add support for easily declaring static inline functions
We already had those, but they forced modules to spell out the functionbodies twice. Eliminate some duplicates we had already grown.Extracted from a somewhat larger patch from Andres Freund.
1 parent08c8058 commit976fa10

File tree

7 files changed

+59
-117
lines changed

7 files changed

+59
-117
lines changed

‎src/backend/nodes/list.c

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
#include"postgres.h"
1717

18+
/* see pg_list.h */
19+
#definePG_LIST_INCLUDE_DEFINITIONS
20+
1821
#include"nodes/pg_list.h"
1922

2023

@@ -1223,31 +1226,6 @@ list_copy_tail(const List *oldlist, int nskip)
12231226
returnnewlist;
12241227
}
12251228

1226-
/*
1227-
* pg_list.h defines inline versions of these functions if allowed by the
1228-
* compiler; in which case the definitions below are skipped.
1229-
*/
1230-
#ifndefUSE_INLINE
1231-
1232-
ListCell*
1233-
list_head(constList*l)
1234-
{
1235-
returnl ?l->head :NULL;
1236-
}
1237-
1238-
ListCell*
1239-
list_tail(List*l)
1240-
{
1241-
returnl ?l->tail :NULL;
1242-
}
1243-
1244-
int
1245-
list_length(constList*l)
1246-
{
1247-
returnl ?l->length :0;
1248-
}
1249-
#endif/* ! USE_INLINE */
1250-
12511229
/*
12521230
* Temporary compatibility functions
12531231
*

‎src/backend/utils/mmgr/mcxt.c

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
#include"postgres.h"
2323

24+
/* see palloc.h */
25+
#defineMCXT_INCLUDE_DEFINITIONS
26+
2427
#include"utils/memutils.h"
2528

2629

@@ -695,28 +698,6 @@ repalloc(void *pointer, Size size)
695698
pointer,size);
696699
}
697700

698-
/*
699-
* MemoryContextSwitchTo
700-
*Returns the current context; installs the given context.
701-
*
702-
* palloc.h defines an inline version of this function if allowed by the
703-
* compiler; in which case the definition below is skipped.
704-
*/
705-
#ifndefUSE_INLINE
706-
707-
MemoryContext
708-
MemoryContextSwitchTo(MemoryContextcontext)
709-
{
710-
MemoryContextold;
711-
712-
AssertArg(MemoryContextIsValid(context));
713-
714-
old=CurrentMemoryContext;
715-
CurrentMemoryContext=context;
716-
returnold;
717-
}
718-
#endif/* ! USE_INLINE */
719-
720701
/*
721702
* MemoryContextStrdup
722703
*Like strdup(), but allocate from the specified context

‎src/backend/utils/sort/sortsupport.c

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
#include"postgres.h"
1717

18+
/* See sortsupport.h */
19+
#defineSORTSUPPORT_INCLUDE_DEFINITIONS
20+
1821
#include"fmgr.h"
1922
#include"utils/lsyscache.h"
2023
#include"utils/sortsupport.h"
@@ -28,50 +31,6 @@ typedef struct
2831
}SortShimExtra;
2932

3033

31-
/*
32-
* sortsupport.h defines inline versions of these functions if allowed by the
33-
* compiler; in which case the definitions below are skipped.
34-
*/
35-
#ifndefUSE_INLINE
36-
37-
/*
38-
* Apply a sort comparator function and return a 3-way comparison result.
39-
* This takes care of handling reverse-sort and NULLs-ordering properly.
40-
*/
41-
int
42-
ApplySortComparator(Datumdatum1,boolisNull1,
43-
Datumdatum2,boolisNull2,
44-
SortSupportssup)
45-
{
46-
intcompare;
47-
48-
if (isNull1)
49-
{
50-
if (isNull2)
51-
compare=0;/* NULL "=" NULL */
52-
elseif (ssup->ssup_nulls_first)
53-
compare=-1;/* NULL "<" NOT_NULL */
54-
else
55-
compare=1;/* NULL ">" NOT_NULL */
56-
}
57-
elseif (isNull2)
58-
{
59-
if (ssup->ssup_nulls_first)
60-
compare=1;/* NOT_NULL ">" NULL */
61-
else
62-
compare=-1;/* NOT_NULL "<" NULL */
63-
}
64-
else
65-
{
66-
compare= (*ssup->comparator) (datum1,datum2,ssup);
67-
if (ssup->ssup_reverse)
68-
compare=-compare;
69-
}
70-
71-
returncompare;
72-
}
73-
#endif/* ! USE_INLINE */
74-
7534
/*
7635
* Shim function for calling an old-style comparator
7736
*

‎src/include/c.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,24 @@ typedef NameData *Name;
748748
#endif/* HAVE__BUILTIN_TYPES_COMPATIBLE_P */
749749

750750

751+
/*
752+
* Function inlining support -- Allow modules to define functions that may be
753+
* inlined, if the compiler supports it.
754+
*
755+
* The function bodies must be defined in the module header prefixed by
756+
* STATIC_IF_INLINE, protected by a cpp symbol that the module's .c file must
757+
* define. If the compiler doesn't support inline functions, the function
758+
* definitions are pulled in by the .c file as regular (not inline) symbols.
759+
*
760+
* The header must also declare the functions' prototypes, protected by
761+
* !USE_INLINE.
762+
*/
763+
#ifdefUSE_INLINE
764+
#defineSTATIC_IF_INLINE static inline
765+
#else
766+
#defineSTATIC_IF_INLINE
767+
#endif/* USE_INLINE */
768+
751769
/* ----------------------------------------------------------------
752770
*Section 7:random stuff
753771
* ----------------------------------------------------------------

‎src/include/nodes/pg_list.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,32 @@ struct ListCell
7373
* them as macros, since we want to avoid double-evaluation of macro
7474
* arguments. Therefore, we implement them using static inline functions
7575
* if supported by the compiler, or as regular functions otherwise.
76+
* See STATIC_IF_INLINE in c.h.
7677
*/
77-
#ifdefUSE_INLINE
78-
79-
staticinlineListCell*
78+
#ifndefUSE_INLINE
79+
externListCell*list_head(constList*l);
80+
externListCell*list_tail(List*l);
81+
externintlist_length(constList*l);
82+
#endif/* USE_INLINE */
83+
#if defined(USE_INLINE)|| defined(PG_LIST_INCLUDE_DEFINITIONS)
84+
STATIC_IF_INLINEListCell*
8085
list_head(constList*l)
8186
{
8287
returnl ?l->head :NULL;
8388
}
8489

85-
staticinlineListCell*
90+
STATIC_IF_INLINEListCell*
8691
list_tail(List*l)
8792
{
8893
returnl ?l->tail :NULL;
8994
}
9095

91-
staticinlineint
96+
STATIC_IF_INLINEint
9297
list_length(constList*l)
9398
{
9499
returnl ?l->length :0;
95100
}
96-
#else
97-
98-
externListCell*list_head(constList*l);
99-
externListCell*list_tail(List*l);
100-
externintlist_length(constList*l);
101-
#endif/* USE_INLINE */
101+
#endif/* USE_INLINE || PG_LIST_INCLUDE_DEFINITIONS */
102102

103103
/*
104104
* NB: There is an unfortunate legacy from a previous incarnation of

‎src/include/utils/palloc.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,29 @@ extern void *repalloc(void *pointer, Size size);
7373
/*
7474
* MemoryContextSwitchTo can't be a macro in standard C compilers.
7575
* But we can make it an inline function if the compiler supports it.
76+
* See STATIC_IF_INLINE in c.h.
7677
*
7778
* This file has to be includable by some non-backend code such as
7879
* pg_resetxlog, so don't expose the CurrentMemoryContext reference
7980
* if FRONTEND is defined.
8081
*/
81-
#if defined(USE_INLINE)&& !defined(FRONTEND)
82+
#ifndefFRONTEND
8283

83-
staticinlineMemoryContext
84+
#ifndefUSE_INLINE
85+
externMemoryContextMemoryContextSwitchTo(MemoryContextcontext);
86+
#endif/* !USE_INLINE */
87+
#if defined(USE_INLINE)|| defined(MCXT_INCLUDE_DEFINITIONS)
88+
STATIC_IF_INLINEMemoryContext
8489
MemoryContextSwitchTo(MemoryContextcontext)
8590
{
8691
MemoryContextold=CurrentMemoryContext;
8792

8893
CurrentMemoryContext=context;
8994
returnold;
9095
}
91-
#else
96+
#endif
9297

93-
externMemoryContextMemoryContextSwitchTo(MemoryContextcontext);
94-
#endif/* USE_INLINE && !FRONTEND */
98+
#endif/* !FRONTEND */
9599

96100
/*
97101
* These are like standard strdup() except the copied string is

‎src/include/utils/sortsupport.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,21 @@ typedef struct SortSupportData
101101
}SortSupportData;
102102

103103

104-
/* ApplySortComparator should be inlined if possible */
105-
#ifdefUSE_INLINE
106-
104+
/*
105+
* ApplySortComparator should be inlined if possible. See STATIC_IF_INLINE
106+
* in c.h.
107+
*/
108+
#ifndefUSE_INLINE
109+
externintApplySortComparator(Datumdatum1,boolisNull1,
110+
Datumdatum2,boolisNull2,
111+
SortSupportssup);
112+
#endif/* !USE_INLINE */
113+
#if defined(USE_INLINE)|| defined(SORTSUPPORT_INCLUDE_DEFINITIONS)
107114
/*
108115
* Apply a sort comparator function and return a 3-way comparison result.
109116
* This takes care of handling reverse-sort and NULLs-ordering properly.
110117
*/
111-
staticinlineint
118+
STATIC_IF_INLINEint
112119
ApplySortComparator(Datumdatum1,boolisNull1,
113120
Datumdatum2,boolisNull2,
114121
SortSupportssup)
@@ -140,12 +147,7 @@ ApplySortComparator(Datum datum1, bool isNull1,
140147

141148
returncompare;
142149
}
143-
#else
144-
145-
externintApplySortComparator(Datumdatum1,boolisNull1,
146-
Datumdatum2,boolisNull2,
147-
SortSupportssup);
148-
#endif/* USE_INLINE */
150+
#endif/* USE_INLINE || SORTSUPPORT_INCLUDE_DEFINITIONS */
149151

150152
/* Other functions in utils/sort/sortsupport.c */
151153
externvoidPrepareSortSupportComparisonShim(OidcmpFunc,SortSupportssup);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp