summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorAlvaro Herrera2013-02-12 13:33:40 +0000
committerAlvaro Herrera2013-02-12 14:21:05 +0000
commit8396447cdbdff0b62914748de2fec04281dc9114 (patch)
treedd95fd78c6c507ef6f3c9d9c4cd769aeda7c9fcc /src/common
parent0cb1fac3b19f01025b63d2cdceabb8767185da28 (diff)
Create libpgcommon, and move pg_malloc et al to it
libpgcommon is a new static library to allow sharing code among the various frontend programs and backend; this lets us eliminate duplicate implementations of common routines. We avoid libpgport, because that's intended as a place for porting issues; per discussion, it seems better to keep them separate. The first use case, and the only implemented by this patch, is pg_malloc and friends, which many frontend programs were already using. At the same time, we can use this to provide palloc emulation functions for the frontend; this way, some palloc-using files in the backend can also be used by the frontend cleanly. To do this, we change palloc() in the backend to be a function instead of a macro on top of MemoryContextAlloc(). This was previously believed to cause loss of performance, but this implementation has been tweaked by Tom and Andres so that on modern compilers it provides a slight improvement over the previous one. This lets us clean up some places that were already with localized hacks. Most of the pg_malloc/palloc changes in this patch were authored by Andres Freund. Zoltán Böszörményi also independently provided a form of that. libpgcommon infrastructure was authored by Álvaro.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/Makefile64
-rw-r--r--src/common/fe_memutils.c128
2 files changed, 192 insertions, 0 deletions
diff --git a/src/common/Makefile b/src/common/Makefile
new file mode 100644
index 00000000000..3aa6e70322f
--- /dev/null
+++ b/src/common/Makefile
@@ -0,0 +1,64 @@
+#-------------------------------------------------------------------------
+#
+# Makefile
+# Makefile for src/common
+#
+# This makefile generates two outputs:
+#
+# libpgcommon.a - contains object files with FRONTEND defined,
+# for use by client application and libraries
+#
+# libpgcommon_srv.a - contains object files without FRONTEND defined,
+# for use only by the backend binaries
+#
+# IDENTIFICATION
+# src/common/Makefile
+#
+#-------------------------------------------------------------------------
+
+subdir = src/common
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+
+override CPPFLAGS := -DFRONTEND $(CPPFLAGS)
+LIBS += $(PTHREAD_LIBS)
+
+OBJS_COMMON =
+
+OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o
+
+OBJS_SRV = $(OBJS_COMMON:%.o=%_srv.o)
+
+all: libpgcommon.a libpgcommon_srv.a
+
+# libpgcommon is needed by some contrib
+install: all installdirs
+ $(INSTALL_STLIB) libpgcommon.a '$(DESTDIR)$(libdir)/libpgcommon.a'
+
+installdirs:
+ $(MKDIR_P) '$(DESTDIR)$(libdir)'
+
+uninstall:
+ rm -f '$(DESTDIR)$(libdir)/libpgcommon.a'
+
+libpgcommon.a: $(OBJS_FRONTEND)
+ $(AR) $(AROPT) $@ $^
+
+#
+# Server versions of object files
+#
+
+libpgcommon_srv.a: $(OBJS_SRV)
+ $(AR) $(AROPT) $@ $^
+
+# Because this uses its own compilation rule, it doesn't use the
+# dependency tracking logic from Makefile.global. To make sure that
+# dependency tracking works anyway for the *_srv.o files, depend on
+# their *.o siblings as well, which do have proper dependencies. It's
+# a hack that might fail someday if there is a *_srv.o without a
+# corresponding *.o, but it works for now.
+%_srv.o: %.c %.o
+ $(CC) $(CFLAGS) $(subst -DFRONTEND,, $(CPPFLAGS)) -c $< -o $@
+
+clean distclean maintainer-clean:
+ rm -f libpgcommon.a libpgcommon_srv.a $(OBJS_FRONTEND) $(OBJS_SRV)
diff --git a/src/common/fe_memutils.c b/src/common/fe_memutils.c
new file mode 100644
index 00000000000..7d4d99eefd5
--- /dev/null
+++ b/src/common/fe_memutils.c
@@ -0,0 +1,128 @@
+/*-------------------------------------------------------------------------
+ *
+ * fe_memutils.c
+ * memory management support for frontend code
+ *
+ * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/common/fe_memutils.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifndef FRONTEND
+#error "This file is not expected to be compiled for backend code"
+#endif
+
+#include "postgres_fe.h"
+
+void *
+pg_malloc(size_t size)
+{
+ void *tmp;
+
+ /* Avoid unportable behavior of malloc(0) */
+ if (size == 0)
+ size = 1;
+ tmp = malloc(size);
+ if (!tmp)
+ {
+ fprintf(stderr, _("out of memory\n"));
+ exit(EXIT_FAILURE);
+ }
+ return tmp;
+}
+
+void *
+pg_malloc0(size_t size)
+{
+ void *tmp;
+
+ tmp = pg_malloc(size);
+ MemSet(tmp, 0, size);
+ return tmp;
+}
+
+void *
+pg_realloc(void *ptr, size_t size)
+{
+ void *tmp;
+
+ /* Avoid unportable behavior of realloc(NULL, 0) */
+ if (ptr == NULL && size == 0)
+ size = 1;
+ tmp = realloc(ptr, size);
+ if (!tmp)
+ {
+ fprintf(stderr, _("out of memory\n"));
+ exit(EXIT_FAILURE);
+ }
+ return tmp;
+}
+
+/*
+ * "Safe" wrapper around strdup().
+ */
+char *
+pg_strdup(const char *string)
+{
+ char *tmp;
+
+ if (!string)
+ {
+ fprintf(stderr,
+ _("cannot duplicate null pointer (internal error)\n"));
+ exit(EXIT_FAILURE);
+ }
+ tmp = strdup(string);
+ if (!tmp)
+ {
+ fprintf(stderr, _("out of memory\n"));
+ exit(EXIT_FAILURE);
+ }
+ return tmp;
+}
+
+void
+pg_free(void *ptr)
+{
+ if (ptr != NULL)
+ free(ptr);
+}
+
+/*
+ * Frontend emulation of backend memory management functions. Useful for
+ * programs that compile backend files.
+ */
+void *
+palloc(Size size)
+{
+ return pg_malloc(size);
+}
+
+void *
+palloc0(Size size)
+{
+ return pg_malloc0(size);
+}
+
+void
+pfree(void *pointer)
+{
+ pg_free(pointer);
+}
+
+char *
+pstrdup(const char *string)
+{
+ return pg_strdup(string);
+}
+
+void *
+repalloc(void *pointer, Size size)
+{
+ return pg_realloc(pointer, size);
+}