Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 976fa10

Browse files
committed
Add support for easily declaring static inline functions
We already had those, but they forced modules to spell out the function bodies twice. Eliminate some duplicates we had already grown. Extracted from a somewhat larger patch from Andres Freund.
1 parent 08c8058 commit 976fa10

File tree

7 files changed

+59
-117
lines changed

7 files changed

+59
-117
lines changed

src/backend/nodes/list.c

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

18+
/* see pg_list.h */
19+
#define PG_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
return newlist;
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-
#ifndef USE_INLINE
1231-
1232-
ListCell *
1233-
list_head(const List *l)
1234-
{
1235-
return l ? l->head : NULL;
1236-
}
1237-
1238-
ListCell *
1239-
list_tail(List *l)
1240-
{
1241-
return l ? l->tail : NULL;
1242-
}
1243-
1244-
int
1245-
list_length(const List *l)
1246-
{
1247-
return l ? l->length : 0;
1248-
}
1249-
#endif /* ! USE_INLINE */
1250-
12511229
/*
12521230
* Temporary compatibility functions
12531231
*

src/backend/utils/mmgr/mcxt.c

+3-22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
#include "postgres.h"
2323

24+
/* see palloc.h */
25+
#define MCXT_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-
#ifndef USE_INLINE
706-
707-
MemoryContext
708-
MemoryContextSwitchTo(MemoryContext context)
709-
{
710-
MemoryContext old;
711-
712-
AssertArg(MemoryContextIsValid(context));
713-
714-
old = CurrentMemoryContext;
715-
CurrentMemoryContext = context;
716-
return old;
717-
}
718-
#endif /* ! USE_INLINE */
719-
720701
/*
721702
* MemoryContextStrdup
722703
* Like strdup(), but allocate from the specified context

src/backend/utils/sort/sortsupport.c

+3-44
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
#include "postgres.h"
1717

18+
/* See sortsupport.h */
19+
#define SORTSUPPORT_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-
#ifndef USE_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(Datum datum1, bool isNull1,
43-
Datum datum2, bool isNull2,
44-
SortSupport ssup)
45-
{
46-
int compare;
47-
48-
if (isNull1)
49-
{
50-
if (isNull2)
51-
compare = 0; /* NULL "=" NULL */
52-
else if (ssup->ssup_nulls_first)
53-
compare = -1; /* NULL "<" NOT_NULL */
54-
else
55-
compare = 1; /* NULL ">" NOT_NULL */
56-
}
57-
else if (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-
return compare;
72-
}
73-
#endif /* ! USE_INLINE */
74-
7534
/*
7635
* Shim function for calling an old-style comparator
7736
*

src/include/c.h

+18
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+
#ifdef USE_INLINE
764+
#define STATIC_IF_INLINE static inline
765+
#else
766+
#define STATIC_IF_INLINE
767+
#endif /* USE_INLINE */
768+
751769
/* ----------------------------------------------------------------
752770
* Section 7: random stuff
753771
* ----------------------------------------------------------------

src/include/nodes/pg_list.h

+11-11
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-
#ifdef USE_INLINE
78-
79-
static inline ListCell *
78+
#ifndef USE_INLINE
79+
extern ListCell *list_head(const List *l);
80+
extern ListCell *list_tail(List *l);
81+
extern int list_length(const List *l);
82+
#endif /* USE_INLINE */
83+
#if defined(USE_INLINE) || defined(PG_LIST_INCLUDE_DEFINITIONS)
84+
STATIC_IF_INLINE ListCell *
8085
list_head(const List *l)
8186
{
8287
return l ? l->head : NULL;
8388
}
8489

85-
static inline ListCell *
90+
STATIC_IF_INLINE ListCell *
8691
list_tail(List *l)
8792
{
8893
return l ? l->tail : NULL;
8994
}
9095

91-
static inline int
96+
STATIC_IF_INLINE int
9297
list_length(const List *l)
9398
{
9499
return l ? l->length : 0;
95100
}
96-
#else
97-
98-
extern ListCell *list_head(const List *l);
99-
extern ListCell *list_tail(List *l);
100-
extern int list_length(const List *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

+9-5
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+
#ifndef FRONTEND
8283

83-
static inline MemoryContext
84+
#ifndef USE_INLINE
85+
extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
86+
#endif /* !USE_INLINE */
87+
#if defined(USE_INLINE) || defined(MCXT_INCLUDE_DEFINITIONS)
88+
STATIC_IF_INLINE MemoryContext
8489
MemoryContextSwitchTo(MemoryContext context)
8590
{
8691
MemoryContext old = CurrentMemoryContext;
8792

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

93-
extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
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

+12-10
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-
#ifdef USE_INLINE
106-
104+
/*
105+
* ApplySortComparator should be inlined if possible. See STATIC_IF_INLINE
106+
* in c.h.
107+
*/
108+
#ifndef USE_INLINE
109+
extern int ApplySortComparator(Datum datum1, bool isNull1,
110+
Datum datum2, bool isNull2,
111+
SortSupport ssup);
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-
static inline int
118+
STATIC_IF_INLINE int
112119
ApplySortComparator(Datum datum1, bool isNull1,
113120
Datum datum2, bool isNull2,
114121
SortSupport ssup)
@@ -140,12 +147,7 @@ ApplySortComparator(Datum datum1, bool isNull1,
140147

141148
return compare;
142149
}
143-
#else
144-
145-
extern int ApplySortComparator(Datum datum1, bool isNull1,
146-
Datum datum2, bool isNull2,
147-
SortSupport ssup);
148-
#endif /* USE_INLINE */
150+
#endif /* USE_INLINE || SORTSUPPORT_INCLUDE_DEFINITIONS */
149151

150152
/* Other functions in utils/sort/sortsupport.c */
151153
extern void PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup);

0 commit comments

Comments
 (0)