Don't #include utils/palloc.h in common/fe_memutils.h.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 26 Apr 2014 18:14:28 +0000 (14:14 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 26 Apr 2014 18:14:28 +0000 (14:14 -0400)
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.

src/common/psprintf.c
src/include/common/fe_memutils.h
src/include/utils/palloc.h

index 026040f7b76f7a3db7db7b396c379e99fdd426a6..b01e195360904bfbdeee38602d1a73c837746406 100644 (file)
  */
 
 #ifndef FRONTEND
+
 #include "postgres.h"
+
+#include "utils/memutils.h"
+
 #else
+
 #include "postgres_fe.h"
-#endif
 
-#include "utils/memutils.h"
+/* It's possible we could use a different value for this in frontend code */
+#define MaxAllocSize   ((Size) 0x3fffffff)     /* 1 gigabyte - 1 */
+
+#endif
 
 
 /*
index 96ee7b3ef66641fd3c3f9be5b9378aad796b4b78..3da1891ef2c8ae45bddb78c60081c0087c3c3b63 100644 (file)
@@ -9,12 +9,24 @@
 #ifndef FE_MEMUTILS_H
 #define FE_MEMUTILS_H
 
+/* "Safe" memory allocation functions --- these exit(1) on failure */
 extern char *pg_strdup(const char *in);
 extern void *pg_malloc(size_t size);
 extern void *pg_malloc0(size_t size);
 extern void *pg_realloc(void *pointer, size_t size);
 extern void pg_free(void *pointer);
 
-#include "utils/palloc.h"
+/* Equivalent functions, deliberately named the same as backend functions */
+extern char *pstrdup(const char *in);
+extern void *palloc(Size size);
+extern void *palloc0(Size size);
+extern void *repalloc(void *pointer, Size size);
+extern void pfree(void *pointer);
+
+/* sprintf into a palloc'd buffer --- these are in psprintf.c */
+extern char *psprintf(const char *fmt,...)
+__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
+extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args)
+__attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 0)));
 
 #endif   /* FE_MEMUTILS_H */
index ff440f7c89c13ffd7bd8165082476450cd71cba1..61d03351d13bbc9024f1ed7a70fa05250686049f 100644 (file)
  */
 typedef struct MemoryContextData *MemoryContext;
 
-#ifndef FRONTEND
-
 /*
  * CurrentMemoryContext is the default allocation context for palloc().
- * We declare it here so that palloc() can be a macro. Avoid accessing it
- * directly!  Instead, use MemoryContextSwitchTo() to change the setting.
+ * Avoid accessing it directly!  Instead, use MemoryContextSwitchTo()
+ * to change the setting.
  */
 extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
 
@@ -51,9 +49,10 @@ extern void *MemoryContextAlloc(MemoryContext context, Size size);
 extern void *MemoryContextAllocZero(MemoryContext context, Size size);
 extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
 
-/* Higher-limit allocators. */
-extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
-extern void *repalloc_huge(void *pointer, Size size);
+extern void *palloc(Size size);
+extern void *palloc0(Size size);
+extern void *repalloc(void *pointer, Size size);
+extern void pfree(void *pointer);
 
 /*
  * The result of palloc() is always word-aligned, so we can skip testing
@@ -68,6 +67,10 @@ extern void *repalloc_huge(void *pointer, Size size);
        MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
        MemoryContextAllocZero(CurrentMemoryContext, sz) )
 
+/* Higher-limit allocators. */
+extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
+extern void *repalloc_huge(void *pointer, Size size);
+
 /*
  * MemoryContextSwitchTo can't be a macro in standard C compilers.
  * But we can make it an inline function if the compiler supports it.
@@ -93,17 +96,9 @@ MemoryContextSwitchTo(MemoryContext context)
  * allocated in a context, not with malloc().
  */
 extern char *MemoryContextStrdup(MemoryContext context, const char *string);
-#endif   /* !FRONTEND */
-
 extern char *pstrdup(const char *in);
 extern char *pnstrdup(const char *in, Size len);
 
-/* basic memory allocation functions */
-extern void *palloc(Size size);
-extern void *palloc0(Size size);
-extern void pfree(void *pointer);
-extern void *repalloc(void *pointer, Size size);
-
 /* sprintf into a palloc'd buffer --- these are in psprintf.c */
 extern char *psprintf(const char *fmt,...)
 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));