Skip to content

Commit e4c1a49

Browse files
committed
Don't #include utils/palloc.h in common/fe_memutils.h.
This breaks the principle that common/ ought not depend on anything in the server, not only code-wise but in the headers. The only arguable advantage is avoidance of duplication of half a dozen extern declarations, and even that is rather dubious, considering that the previous coding was wrong about which declarations to duplicate: it exposed pnstrdup() to frontend code even though no such function is provided in fe_memutils.c. On the same principle, don't #include utils/memutils.h in the frontend build of psprintf.c. This requires duplicating the definition of MaxAllocSize, but that seems fine to me: there's no a-priori reason why frontend code should use the same size limit as the backend anyway. In passing, clean up some rather odd layout and ordering choices that were imposed on palloc.h to reduce the number of #ifdefs required by the previous approach. Per gripe from Christoph Berg. There's still more work to do to make include/common/ clean, but this part seems reasonably noncontroversial.
1 parent c0bd128 commit e4c1a49

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/include/common/fe_memutils.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
#ifndef FE_MEMUTILS_H
1010
#define FE_MEMUTILS_H
1111

12+
/* "Safe" memory allocation functions --- these exit(1) on failure */
1213
extern char *pg_strdup(const char *in);
1314
extern void *pg_malloc(size_t size);
1415
extern void *pg_malloc0(size_t size);
1516
extern void *pg_realloc(void *pointer, size_t size);
1617
extern void pg_free(void *pointer);
1718

18-
#include "utils/palloc.h"
19+
/* Equivalent functions, deliberately named the same as backend functions */
20+
extern char *pstrdup(const char *in);
21+
extern void *palloc(Size size);
22+
extern void *palloc0(Size size);
23+
extern void *repalloc(void *pointer, Size size);
24+
extern void pfree(void *pointer);
1925

2026
#endif /* FE_MEMUTILS_H */

src/include/utils/palloc.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@
3535
*/
3636
typedef struct MemoryContextData *MemoryContext;
3737

38-
#ifndef FRONTEND
39-
4038
/*
4139
* CurrentMemoryContext is the default allocation context for palloc().
42-
* We declare it here so that palloc() can be a macro. Avoid accessing it
43-
* directly! Instead, use MemoryContextSwitchTo() to change the setting.
40+
* Avoid accessing it directly! Instead, use MemoryContextSwitchTo()
41+
* to change the setting.
4442
*/
4543
extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
4644

@@ -51,6 +49,11 @@ extern void *MemoryContextAlloc(MemoryContext context, Size size);
5149
extern void *MemoryContextAllocZero(MemoryContext context, Size size);
5250
extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
5351

52+
extern void *palloc(Size size);
53+
extern void *palloc0(Size size);
54+
extern void *repalloc(void *pointer, Size size);
55+
extern void pfree(void *pointer);
56+
5457
/*
5558
* The result of palloc() is always word-aligned, so we can skip testing
5659
* alignment of the pointer when deciding which MemSet variant to use.
@@ -89,13 +92,8 @@ MemoryContextSwitchTo(MemoryContext context)
8992
* allocated in a context, not with malloc().
9093
*/
9194
extern char *MemoryContextStrdup(MemoryContext context, const char *string);
92-
#endif /* !FRONTEND */
9395

9496
extern char *pstrdup(const char *in);
9597
extern char *pnstrdup(const char *in, Size len);
96-
extern void *palloc(Size size);
97-
extern void *palloc0(Size size);
98-
extern void pfree(void *pointer);
99-
extern void *repalloc(void *pointer, Size size);
10098

10199
#endif /* PALLOC_H */

0 commit comments

Comments
 (0)