2013-01-02 10:12 keltezéssel, Magnus Hagander írta:
On Wed, Jan 2, 2013 at 9:59 AM, Boszormenyi Zoltan <z...@cybertec.at <mailto:z...@cybertec.at>> wrote:

    2013-01-02 01:24 keltezéssel, Tom Lane írta:

        Boszormenyi Zoltan <z...@cybertec.at <mailto:z...@cybertec.at>> writes:

            2013-01-01 17:18 keltezéssel, Magnus Hagander írta:

                That way we can get around the whole need for changing memory 
allocation
                across all the
                frontends, no? Like the attached.

            Sure it's simpler but then the consistent look of the code is lost.
            What about the other patch to unify pg_malloc and friends?
            Basically all client code boils down to
                  fprintf(stderr, ...)
            in different disguise in their error reporting, so that patch can
            also be simplified but it seems that the atexit() - either 
explicitly
            or hidden behind InitPostgresFrontend() - cannot be avoided.

        Meh.  I find it seriously wrongheaded that something as minor as an
        escape_quotes() function should get to dictate both malloc wrappers
        and error recovery handling throughout every program that might use it.


    Actually, the unification of pg_malloc and friends wasn't dictated
    by this little code, it was just that pg_basebackup doesn't provide
    a pg_malloc implementation (only pg_malloc0) that is used by
    initdb's escape_quotes() function. Then I noticed how wide these
    almost identical functions have spread into client apps already.

    I would say this unification patch is completely orthogonal to
    the patch in $SUBJECT. I will post it in a different thread if it's
    wanted at all. The extra atexit() handler is not needed if a simple
    fprintf(stderr, ...) error reporting is enough in all clients.
    As far as I saw, all clients do exactly this but some of them hide
    this behind #define's.


Please do keep that one separate - let's avoid unnecessary feature-creep, whether it's good or bad features.

The patch is attached. There is no extra atexit() code in this one.

I did this over my pg_basebackup patch, there are two chunks
that gets rejected if applied without it: one in initdb.c, the other is
in src/port/Makefile. It's because the modified codes are too close
to each other.

Best regards,
Zoltán Böszörményi

--
----------------------------------
Zoltán Böszörményi
Cybertec Schönig & Schönig GmbH
Gröhrmühlgasse 26
A-2700 Wiener Neustadt, Austria
Web: http://www.postgresql-support.de
     http://www.postgresql.at/

diff -durpN postgresql.2/contrib/oid2name/oid2name.c postgresql.3/contrib/oid2name/oid2name.c
--- postgresql.2/contrib/oid2name/oid2name.c	2012-10-03 10:40:48.241207023 +0200
+++ postgresql.3/contrib/oid2name/oid2name.c	2013-01-02 13:40:08.483260103 +0100
@@ -50,9 +50,6 @@ struct options
 /* function prototypes */
 static void help(const char *progname);
 void		get_opts(int, char **, struct options *);
-void	   *pg_malloc(size_t size);
-void	   *pg_realloc(void *ptr, size_t size);
-char	   *pg_strdup(const char *str);
 void		add_one_elt(char *eltname, eary *eary);
 char	   *get_comma_elts(eary *eary);
 PGconn	   *sql_conn(struct options *);
@@ -201,53 +198,6 @@ help(const char *progname)
 		   progname, progname);
 }
 
-void *
-pg_malloc(size_t size)
-{
-	void	   *ptr;
-
-	/* Avoid unportable behavior of malloc(0) */
-	if (size == 0)
-		size = 1;
-	ptr = malloc(size);
-	if (!ptr)
-	{
-		fprintf(stderr, "out of memory\n");
-		exit(1);
-	}
-	return ptr;
-}
-
-void *
-pg_realloc(void *ptr, size_t size)
-{
-	void	   *result;
-
-	/* Avoid unportable behavior of realloc(NULL, 0) */
-	if (ptr == NULL && size == 0)
-		size = 1;
-	result = realloc(ptr, size);
-	if (!result)
-	{
-		fprintf(stderr, "out of memory\n");
-		exit(1);
-	}
-	return result;
-}
-
-char *
-pg_strdup(const char *str)
-{
-	char	   *result = strdup(str);
-
-	if (!result)
-	{
-		fprintf(stderr, "out of memory\n");
-		exit(1);
-	}
-	return result;
-}
-
 /*
  * add_one_elt
  *
diff -durpN postgresql.2/contrib/pgbench/pgbench.c postgresql.3/contrib/pgbench/pgbench.c
--- postgresql.2/contrib/pgbench/pgbench.c	2013-01-02 09:19:03.655519614 +0100
+++ postgresql.3/contrib/pgbench/pgbench.c	2013-01-02 13:40:26.539378189 +0100
@@ -296,58 +296,6 @@ static void setalarm(int seconds);
 static void *threadRun(void *arg);
 
 
-/*
- * routines to check mem allocations and fail noisily.
- */
-static void *
-pg_malloc(size_t size)
-{
-	void	   *result;
-
-	/* Avoid unportable behavior of malloc(0) */
-	if (size == 0)
-		size = 1;
-	result = malloc(size);
-	if (!result)
-	{
-		fprintf(stderr, "out of memory\n");
-		exit(1);
-	}
-	return result;
-}
-
-static void *
-pg_realloc(void *ptr, size_t size)
-{
-	void	   *result;
-
-	/* Avoid unportable behavior of realloc(NULL, 0) */
-	if (ptr == NULL && size == 0)
-		size = 1;
-	result = realloc(ptr, size);
-	if (!result)
-	{
-		fprintf(stderr, "out of memory\n");
-		exit(1);
-	}
-	return result;
-}
-
-static char *
-pg_strdup(const char *s)
-{
-	char	   *result;
-
-	result = strdup(s);
-	if (!result)
-	{
-		fprintf(stderr, "out of memory\n");
-		exit(1);
-	}
-	return result;
-}
-
-
 static void
 usage(void)
 {
diff -durpN postgresql.2/contrib/pg_upgrade/pg_upgrade.h postgresql.3/contrib/pg_upgrade/pg_upgrade.h
--- postgresql.2/contrib/pg_upgrade/pg_upgrade.h	2013-01-02 09:19:03.654519603 +0100
+++ postgresql.3/contrib/pg_upgrade/pg_upgrade.h	2013-01-02 12:47:14.578678309 +0100
@@ -438,10 +438,6 @@ void
 prep_status(const char *fmt,...)
 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
 void		check_ok(void);
-char	   *pg_strdup(const char *s);
-void	   *pg_malloc(size_t size);
-void	   *pg_realloc(void *ptr, size_t size);
-void		pg_free(void *ptr);
 const char *getErrorText(int errNum);
 unsigned int str2uint(const char *str);
 void		pg_putenv(const char *var, const char *val);
diff -durpN postgresql.2/contrib/pg_upgrade/util.c postgresql.3/contrib/pg_upgrade/util.c
--- postgresql.2/contrib/pg_upgrade/util.c	2013-01-02 09:19:03.655519614 +0100
+++ postgresql.3/contrib/pg_upgrade/util.c	2013-01-02 12:47:14.579678318 +0100
@@ -213,55 +213,6 @@ get_user_info(char **user_name)
 }
 
 
-void *
-pg_malloc(size_t size)
-{
-	void	   *p;
-
-	/* Avoid unportable behavior of malloc(0) */
-	if (size == 0)
-		size = 1;
-	p = malloc(size);
-	if (p == NULL)
-		pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
-	return p;
-}
-
-void *
-pg_realloc(void *ptr, size_t size)
-{
-	void	   *p;
-
-	/* Avoid unportable behavior of realloc(NULL, 0) */
-	if (ptr == NULL && size == 0)
-		size = 1;
-	p = realloc(ptr, size);
-	if (p == NULL)
-		pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
-	return p;
-}
-
-
-void
-pg_free(void *ptr)
-{
-	if (ptr != NULL)
-		free(ptr);
-}
-
-
-char *
-pg_strdup(const char *s)
-{
-	char	   *result = strdup(s);
-
-	if (result == NULL)
-		pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
-
-	return result;
-}
-
-
 /*
  * getErrorText()
  *
diff -durpN postgresql.2/src/bin/initdb/initdb.c postgresql.3/src/bin/initdb/initdb.c
--- postgresql.2/src/bin/initdb/initdb.c	2013-01-02 11:28:34.369505958 +0100
+++ postgresql.3/src/bin/initdb/initdb.c	2013-01-02 13:38:44.281677642 +0100
@@ -200,8 +200,6 @@ const char *subdirs[] = {
 static char bin_path[MAXPGPATH];
 static char backend_exec[MAXPGPATH];
 
-static void *pg_malloc(size_t size);
-static char *pg_strdup(const char *s);
 static char **replace_token(char **lines,
 			  const char *token, const char *replacement);
 
@@ -317,43 +315,6 @@ do { \
 #define DIR_SEP "\\"
 #endif
 
-/*
- * routines to check mem allocations and fail noisily.
- *
- * Note that we can't call exit_nicely() on a memory failure, as it calls
- * rmtree() which needs memory allocation. So we just exit with a bang.
- */
-static void *
-pg_malloc(size_t size)
-{
-	void	   *result;
-
-	/* Avoid unportable behavior of malloc(0) */
-	if (size == 0)
-		size = 1;
-	result = malloc(size);
-	if (!result)
-	{
-		fprintf(stderr, _("%s: out of memory\n"), progname);
-		exit(1);
-	}
-	return result;
-}
-
-static char *
-pg_strdup(const char *s)
-{
-	char	   *result;
-
-	result = strdup(s);
-	if (!result)
-	{
-		fprintf(stderr, _("%s: out of memory\n"), progname);
-		exit(1);
-	}
-	return result;
-}
-
 static char *
 escape_quotes(const char *src)
 {
diff -durpN postgresql.2/src/bin/pg_basebackup/streamutil.c postgresql.3/src/bin/pg_basebackup/streamutil.c
--- postgresql.2/src/bin/pg_basebackup/streamutil.c	2013-01-02 09:19:03.856521815 +0100
+++ postgresql.3/src/bin/pg_basebackup/streamutil.c	2013-01-02 12:57:57.401140484 +0100
@@ -26,43 +26,6 @@ static char *dbpassword = NULL;
 PGconn	   *conn = NULL;
 
 /*
- * strdup() and malloc() replacements that print an error and exit
- * if something goes wrong. Can never return NULL.
- */
-char *
-pg_strdup(const char *s)
-{
-	char	   *result;
-
-	result = strdup(s);
-	if (!result)
-	{
-		fprintf(stderr, _("%s: out of memory\n"), progname);
-		exit(1);
-	}
-	return result;
-}
-
-void *
-pg_malloc0(size_t size)
-{
-	void	   *result;
-
-	/* Avoid unportable behavior of malloc(0) */
-	if (size == 0)
-		size = 1;
-	result = malloc(size);
-	if (!result)
-	{
-		fprintf(stderr, _("%s: out of memory\n"), progname);
-		exit(1);
-	}
-	MemSet(result, 0, size);
-	return result;
-}
-
-
-/*
  * Connect to the server. Returns a valid PGconn pointer if connected,
  * or NULL on non-permanent error. On permanent error, the function will
  * call exit(1) directly.
diff -durpN postgresql.2/src/bin/pg_basebackup/streamutil.h postgresql.3/src/bin/pg_basebackup/streamutil.h
--- postgresql.2/src/bin/pg_basebackup/streamutil.h	2012-10-03 10:40:48.299207401 +0200
+++ postgresql.3/src/bin/pg_basebackup/streamutil.h	2013-01-02 13:34:42.760112017 +0100
@@ -15,8 +15,4 @@ extern PGconn *conn;
 	exit(code);									\
 	}
 
-
-extern char *pg_strdup(const char *s);
-extern void *pg_malloc0(size_t size);
-
 extern PGconn *GetConnection(void);
diff -durpN postgresql.2/src/bin/pg_ctl/pg_ctl.c postgresql.3/src/bin/pg_ctl/pg_ctl.c
--- postgresql.2/src/bin/pg_ctl/pg_ctl.c	2013-01-02 09:19:03.857521826 +0100
+++ postgresql.3/src/bin/pg_ctl/pg_ctl.c	2013-01-02 13:36:01.760623946 +0100
@@ -118,8 +118,6 @@ write_stderr(const char *fmt,...)
 /* This extension allows gcc to check the format string for consistency with
    the supplied arguments. */
 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
-static void *pg_malloc(size_t size);
-static char *pg_strdup(const char *s);
 static void do_advice(void);
 static void do_help(void);
 static void set_mode(char *modeopt);
@@ -226,42 +224,6 @@ write_stderr(const char *fmt,...)
 }
 
 /*
- * routines to check memory allocations and fail noisily.
- */
-
-static void *
-pg_malloc(size_t size)
-{
-	void	   *result;
-
-	/* Avoid unportable behavior of malloc(0) */
-	if (size == 0)
-		size = 1;
-	result = malloc(size);
-	if (!result)
-	{
-		write_stderr(_("%s: out of memory\n"), progname);
-		exit(1);
-	}
-	return result;
-}
-
-
-static char *
-pg_strdup(const char *s)
-{
-	char	   *result;
-
-	result = strdup(s);
-	if (!result)
-	{
-		write_stderr(_("%s: out of memory\n"), progname);
-		exit(1);
-	}
-	return result;
-}
-
-/*
  * Given an already-localized string, print it to stdout unless the
  * user has specified that no messages should be printed.
  */
diff -durpN postgresql.2/src/bin/pg_dump/common.c postgresql.3/src/bin/pg_dump/common.c
--- postgresql.2/src/bin/pg_dump/common.c	2013-01-02 09:19:03.857521826 +0100
+++ postgresql.3/src/bin/pg_dump/common.c	2013-01-02 12:47:14.587678379 +0100
@@ -18,7 +18,6 @@
 #include <ctype.h>
 
 #include "catalog/pg_class.h"
-#include "dumpmem.h"
 #include "dumputils.h"
 
 
diff -durpN postgresql.2/src/bin/pg_dump/compress_io.c postgresql.3/src/bin/pg_dump/compress_io.c
--- postgresql.2/src/bin/pg_dump/compress_io.c	2013-01-02 09:19:03.857521826 +0100
+++ postgresql.3/src/bin/pg_dump/compress_io.c	2013-01-02 12:47:14.587678379 +0100
@@ -53,7 +53,6 @@
  */
 
 #include "compress_io.h"
-#include "dumpmem.h"
 #include "dumputils.h"
 
 /*----------------------
diff -durpN postgresql.2/src/bin/pg_dump/dumpmem.c postgresql.3/src/bin/pg_dump/dumpmem.c
--- postgresql.2/src/bin/pg_dump/dumpmem.c	2013-01-02 09:19:03.857521826 +0100
+++ postgresql.3/src/bin/pg_dump/dumpmem.c	1970-01-01 01:00:00.000000000 +0100
@@ -1,76 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * dumpmem.c
- *	  Memory allocation routines used by pg_dump, pg_dumpall, and pg_restore
- *
- * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- *
- * IDENTIFICATION
- *	  src/bin/pg_dump/dumpmem.c
- *
- *-------------------------------------------------------------------------
- */
-#include "postgres_fe.h"
-
-#include "dumputils.h"
-#include "dumpmem.h"
-
-
-/*
- * Safer versions of some standard C library functions. If an
- * out-of-memory condition occurs, these functions will bail out via exit();
- *therefore, their return value is guaranteed to be non-NULL.
- */
-
-char *
-pg_strdup(const char *string)
-{
-	char	   *tmp;
-
-	if (!string)
-		exit_horribly(NULL, "cannot duplicate null pointer\n");
-	tmp = strdup(string);
-	if (!tmp)
-		exit_horribly(NULL, "out of memory\n");
-	return tmp;
-}
-
-void *
-pg_malloc(size_t size)
-{
-	void	   *tmp;
-
-	/* Avoid unportable behavior of malloc(0) */
-	if (size == 0)
-		size = 1;
-	tmp = malloc(size);
-	if (!tmp)
-		exit_horribly(NULL, "out of memory\n");
-	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)
-		exit_horribly(NULL, "out of memory\n");
-	return tmp;
-}
diff -durpN postgresql.2/src/bin/pg_dump/dumpmem.h postgresql.3/src/bin/pg_dump/dumpmem.h
--- postgresql.2/src/bin/pg_dump/dumpmem.h	2013-01-02 09:19:03.857521826 +0100
+++ postgresql.3/src/bin/pg_dump/dumpmem.h	1970-01-01 01:00:00.000000000 +0100
@@ -1,22 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * dumpmem.h
- *	  Memory allocation routines used by pg_dump, pg_dumpall, and pg_restore
- *
- * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- * src/bin/pg_dump/dumpmem.h
- *
- *-------------------------------------------------------------------------
- */
-
-#ifndef DUMPMEM_H
-#define DUMPMEM_H
-
-extern char *pg_strdup(const char *string);
-extern void *pg_malloc(size_t size);
-extern void *pg_malloc0(size_t size);
-extern void *pg_realloc(void *ptr, size_t size);
-
-#endif   /* DUMPMEM_H */
diff -durpN postgresql.2/src/bin/pg_dump/Makefile postgresql.3/src/bin/pg_dump/Makefile
--- postgresql.2/src/bin/pg_dump/Makefile	2013-01-02 09:19:03.857521826 +0100
+++ postgresql.3/src/bin/pg_dump/Makefile	2013-01-02 12:47:21.267724965 +0100
@@ -20,7 +20,7 @@ override CPPFLAGS := -I$(libpq_srcdir) $
 
 OBJS=	pg_backup_archiver.o pg_backup_db.o pg_backup_custom.o \
 	pg_backup_null.o pg_backup_tar.o \
-	pg_backup_directory.o dumpmem.o dumputils.o compress_io.o $(WIN32RES)
+	pg_backup_directory.o dumputils.o compress_io.o $(WIN32RES)
 
 KEYWRDOBJS = keywords.o kwlookup.o
 
@@ -35,8 +35,8 @@ pg_dump: pg_dump.o common.o pg_dump_sort
 pg_restore: pg_restore.o $(OBJS) $(KEYWRDOBJS) | submake-libpq submake-libpgport
 	$(CC) $(CFLAGS) pg_restore.o $(KEYWRDOBJS) $(OBJS) $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
 
-pg_dumpall: pg_dumpall.o dumputils.o dumpmem.o $(KEYWRDOBJS) | submake-libpq submake-libpgport
-	$(CC) $(CFLAGS) pg_dumpall.o dumputils.o dumpmem.o $(KEYWRDOBJS) $(WIN32RES) $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
+pg_dumpall: pg_dumpall.o dumputils.o $(KEYWRDOBJS) | submake-libpq submake-libpgport
+	$(CC) $(CFLAGS) pg_dumpall.o dumputils.o $(KEYWRDOBJS) $(WIN32RES) $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
 
 install: all installdirs
 	$(INSTALL_PROGRAM) pg_dump$(X) '$(DESTDIR)$(bindir)'/pg_dump$(X)
diff -durpN postgresql.2/src/bin/pg_dump/nls.mk postgresql.3/src/bin/pg_dump/nls.mk
--- postgresql.2/src/bin/pg_dump/nls.mk	2012-04-16 19:57:22.596917323 +0200
+++ postgresql.3/src/bin/pg_dump/nls.mk	2013-01-02 12:47:21.268724970 +0100
@@ -3,7 +3,7 @@ CATALOG_NAME     = pg_dump
 AVAIL_LANGUAGES  = de es fr it ja ko pt_BR sv tr zh_CN zh_TW
 GETTEXT_FILES    = pg_backup_archiver.c pg_backup_db.c pg_backup_custom.c \
                    pg_backup_null.c pg_backup_tar.c \
-                   pg_backup_directory.c dumpmem.c dumputils.c compress_io.c \
+                   pg_backup_directory.c dumputils.c compress_io.c \
                    pg_dump.c common.c pg_dump_sort.c \
                    pg_restore.c pg_dumpall.c \
                    ../../port/exec.c
diff -durpN postgresql.2/src/bin/pg_dump/pg_backup_archiver.c postgresql.3/src/bin/pg_dump/pg_backup_archiver.c
--- postgresql.2/src/bin/pg_dump/pg_backup_archiver.c	2012-10-21 10:56:15.398945610 +0200
+++ postgresql.3/src/bin/pg_dump/pg_backup_archiver.c	2013-01-02 12:47:21.270724967 +0100
@@ -21,7 +21,6 @@
  */
 
 #include "pg_backup_db.h"
-#include "dumpmem.h"
 #include "dumputils.h"
 
 #include <ctype.h>
diff -durpN postgresql.2/src/bin/pg_dump/pg_backup_custom.c postgresql.3/src/bin/pg_dump/pg_backup_custom.c
--- postgresql.2/src/bin/pg_dump/pg_backup_custom.c	2012-10-03 10:40:48.303207429 +0200
+++ postgresql.3/src/bin/pg_dump/pg_backup_custom.c	2013-01-02 12:47:21.270724967 +0100
@@ -26,7 +26,6 @@
 
 #include "compress_io.h"
 #include "dumputils.h"
-#include "dumpmem.h"
 
 /*--------
  * Routines in the format interface
diff -durpN postgresql.2/src/bin/pg_dump/pg_backup_db.c postgresql.3/src/bin/pg_dump/pg_backup_db.c
--- postgresql.2/src/bin/pg_dump/pg_backup_db.c	2012-07-25 10:51:14.973507544 +0200
+++ postgresql.3/src/bin/pg_dump/pg_backup_db.c	2013-01-02 12:47:21.271724969 +0100
@@ -11,7 +11,6 @@
  */
 
 #include "pg_backup_db.h"
-#include "dumpmem.h"
 #include "dumputils.h"
 
 #include <unistd.h>
diff -durpN postgresql.2/src/bin/pg_dump/pg_backup_directory.c postgresql.3/src/bin/pg_dump/pg_backup_directory.c
--- postgresql.2/src/bin/pg_dump/pg_backup_directory.c	2013-01-02 09:19:03.858521837 +0100
+++ postgresql.3/src/bin/pg_dump/pg_backup_directory.c	2013-01-02 12:47:21.272724974 +0100
@@ -34,7 +34,6 @@
  */
 
 #include "compress_io.h"
-#include "dumpmem.h"
 #include "dumputils.h"
 
 #include <dirent.h>
diff -durpN postgresql.2/src/bin/pg_dump/pg_backup_null.c postgresql.3/src/bin/pg_dump/pg_backup_null.c
--- postgresql.2/src/bin/pg_dump/pg_backup_null.c	2012-04-16 19:57:22.598917347 +0200
+++ postgresql.3/src/bin/pg_dump/pg_backup_null.c	2013-01-02 12:47:21.272724974 +0100
@@ -23,7 +23,6 @@
  */
 
 #include "pg_backup_archiver.h"
-#include "dumpmem.h"
 #include "dumputils.h"
 
 #include <unistd.h>				/* for dup */
diff -durpN postgresql.2/src/bin/pg_dump/pg_backup_tar.c postgresql.3/src/bin/pg_dump/pg_backup_tar.c
--- postgresql.2/src/bin/pg_dump/pg_backup_tar.c	2013-01-02 09:19:03.858521837 +0100
+++ postgresql.3/src/bin/pg_dump/pg_backup_tar.c	2013-01-02 12:47:21.273724983 +0100
@@ -28,7 +28,6 @@
 #include "pg_backup.h"
 #include "pg_backup_archiver.h"
 #include "pg_backup_tar.h"
-#include "dumpmem.h"
 #include "dumputils.h"
 
 #include <sys/stat.h>
diff -durpN postgresql.2/src/bin/pg_dump/pg_dumpall.c postgresql.3/src/bin/pg_dump/pg_dumpall.c
--- postgresql.2/src/bin/pg_dump/pg_dumpall.c	2013-01-02 09:19:03.861521870 +0100
+++ postgresql.3/src/bin/pg_dump/pg_dumpall.c	2013-01-02 12:47:21.273724983 +0100
@@ -23,7 +23,6 @@
 #include "getopt_long.h"
 
 #include "dumputils.h"
-#include "dumpmem.h"
 #include "pg_backup.h"
 
 /* version string we expect back from pg_dump */
diff -durpN postgresql.2/src/bin/pg_dump/pg_dump.c postgresql.3/src/bin/pg_dump/pg_dump.c
--- postgresql.2/src/bin/pg_dump/pg_dump.c	2013-01-02 09:19:03.861521870 +0100
+++ postgresql.3/src/bin/pg_dump/pg_dump.c	2013-01-02 12:47:21.276725015 +0100
@@ -59,7 +59,6 @@
 
 #include "pg_backup_archiver.h"
 #include "pg_backup_db.h"
-#include "dumpmem.h"
 #include "dumputils.h"
 
 extern char *optarg;
diff -durpN postgresql.2/src/bin/pg_dump/pg_dump_sort.c postgresql.3/src/bin/pg_dump/pg_dump_sort.c
--- postgresql.2/src/bin/pg_dump/pg_dump_sort.c	2013-01-02 09:19:03.861521870 +0100
+++ postgresql.3/src/bin/pg_dump/pg_dump_sort.c	2013-01-02 12:47:21.276725015 +0100
@@ -15,7 +15,6 @@
  */
 #include "pg_backup_archiver.h"
 #include "dumputils.h"
-#include "dumpmem.h"
 
 /* translator: this is a module name */
 static const char *modulename = gettext_noop("sorter");
diff -durpN postgresql.2/src/bin/pg_dump/pg_restore.c postgresql.3/src/bin/pg_dump/pg_restore.c
--- postgresql.2/src/bin/pg_dump/pg_restore.c	2012-10-14 11:09:08.143146185 +0200
+++ postgresql.3/src/bin/pg_dump/pg_restore.c	2013-01-02 12:47:21.277725022 +0100
@@ -41,7 +41,6 @@
 
 #include "pg_backup_archiver.h"
 
-#include "dumpmem.h"
 #include "dumputils.h"
 
 #include <ctype.h>
diff -durpN postgresql.2/src/bin/psql/common.c postgresql.3/src/bin/psql/common.c
--- postgresql.2/src/bin/psql/common.c	2013-01-02 09:19:03.863521892 +0100
+++ postgresql.3/src/bin/psql/common.c	2013-01-02 12:47:21.278725029 +0100
@@ -33,56 +33,6 @@ static bool command_no_begin(const char
 static bool is_select_command(const char *query);
 
 /*
- * "Safe" wrapper around strdup()
- */
-char *
-pg_strdup(const char *string)
-{
-	char	   *tmp;
-
-	if (!string)
-	{
-		psql_error("%s: pg_strdup: cannot duplicate null pointer (internal error)\n",
-				pset.progname);
-		exit(EXIT_FAILURE);
-	}
-	tmp = strdup(string);
-	if (!tmp)
-	{
-		psql_error("out of memory\n");
-		exit(EXIT_FAILURE);
-	}
-	return tmp;
-}
-
-void *
-pg_malloc(size_t size)
-{
-	void	   *tmp;
-
-	/* Avoid unportable behavior of malloc(0) */
-	if (size == 0)
-		size = 1;
-	tmp = malloc(size);
-	if (!tmp)
-	{
-		psql_error("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;
-}
-
-/*
  * setQFout
  * -- handler for -o command line option and \o command
  *
diff -durpN postgresql.2/src/bin/psql/common.h postgresql.3/src/bin/psql/common.h
--- postgresql.2/src/bin/psql/common.h	2013-01-02 09:19:03.863521892 +0100
+++ postgresql.3/src/bin/psql/common.h	2013-01-02 12:47:21.279725035 +0100
@@ -14,15 +14,6 @@
 
 #define atooid(x)  ((Oid) strtoul((x), NULL, 10))
 
-/*
- * Safer versions of some standard C library functions. If an
- * out-of-memory condition occurs, these functions will bail out
- * safely; therefore, their return value is guaranteed to be non-NULL.
- */
-extern char *pg_strdup(const char *string);
-extern void *pg_malloc(size_t size);
-extern void *pg_malloc0(size_t size);
-
 extern bool setQFout(const char *fname);
 
 extern void
diff -durpN postgresql.2/src/bin/scripts/common.c postgresql.3/src/bin/scripts/common.c
--- postgresql.2/src/bin/scripts/common.c	2013-01-02 09:19:03.871521980 +0100
+++ postgresql.3/src/bin/scripts/common.c	2013-01-02 13:36:57.808986089 +0100
@@ -278,55 +278,6 @@ executeMaintenanceCommand(PGconn *conn,
 }
 
 /*
- * "Safe" wrapper around strdup().	Pulled from psql/common.c
- */
-char *
-pg_strdup(const char *string)
-{
-	char	   *tmp;
-
-	if (!string)
-	{
-		fprintf(stderr, _("pg_strdup: 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_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;
-}
-
-/*
  * Check yes/no answer in a localized way.	1=yes, 0=no, -1=neither.
  */
 
diff -durpN postgresql.2/src/bin/scripts/common.h postgresql.3/src/bin/scripts/common.h
--- postgresql.2/src/bin/scripts/common.h	2013-01-02 09:19:03.871521980 +0100
+++ postgresql.3/src/bin/scripts/common.h	2013-01-02 13:36:44.504900390 +0100
@@ -50,8 +50,4 @@ extern bool yesno_prompt(const char *que
 
 extern void setup_cancel_handler(void);
 
-extern char *pg_strdup(const char *string);
-extern void *pg_malloc(size_t size);
-extern void *pg_malloc0(size_t size);
-
 #endif   /* COMMON_H */
diff -durpN postgresql.2/src/include/port.h postgresql.3/src/include/port.h
--- postgresql.2/src/include/port.h	2013-01-02 11:28:47.256587185 +0100
+++ postgresql.3/src/include/port.h	2013-01-02 13:49:38.686060590 +0100
@@ -462,6 +462,17 @@ extern char *inet_net_ntop(int af, const
 /* port/pgcheckdir.c */
 extern int	pg_check_dir(const char *dir);
 
+/* port/pgmalloc.c */
+extern char *pg_strdup(const char *string);
+
+extern void *pg_malloc(size_t size);
+
+extern void *pg_malloc0(size_t size);
+
+extern void *pg_realloc(void *ptr, size_t size);
+
+extern void pg_free(void *ptr);
+
 /* port/pgmkdirp.c */
 extern int	pg_mkdir_p(char *path, int omode);
 
diff -durpN postgresql.2/src/port/Makefile postgresql.3/src/port/Makefile
--- postgresql.2/src/port/Makefile	2013-01-02 10:43:57.358410221 +0100
+++ postgresql.3/src/port/Makefile	2013-01-02 12:48:18.788120056 +0100
@@ -31,7 +31,7 @@ override CPPFLAGS := -I$(top_builddir)/s
 LIBS += $(PTHREAD_LIBS)
 
 OBJS = $(LIBOBJS) chklocale.o dirmod.o erand48.o exec.o fls.o inet_net_ntop.o \
-	noblock.o path.o pgcheckdir.o pg_crc.o pgmkdirp.o pgsleep.o \
+	noblock.o path.o pgcheckdir.o pg_crc.o pgmalloc.o pgmkdirp.o pgsleep.o \
 	pgstrcasecmp.o quotes.o qsort.o qsort_arg.o sprompt.o tar.o thread.o
 
 # foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
diff -durpN postgresql.2/src/port/pgmalloc.c postgresql.3/src/port/pgmalloc.c
--- postgresql.2/src/port/pgmalloc.c	1970-01-01 01:00:00.000000000 +0100
+++ postgresql.3/src/port/pgmalloc.c	2013-01-02 12:51:05.238252844 +0100
@@ -0,0 +1,96 @@
+/*-------------------------------------------------------------------------
+ *
+ * pgmalloc.c
+ *		Functions for allocating memory and
+ *		exiting on out-of-memory
+ *
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ *	  src/port/pgmalloc.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifdef FRONTEND
+#include "postgres_fe.h"
+
+#include <stdio.h>
+
+/*
+ * "Safe" wrapper around strdup()
+ */
+char *
+pg_strdup(const char *string)
+{
+	char	   *tmp;
+
+	if (!string)
+	{
+		fprintf(stderr, "pg_strdup: cannot duplicate null pointer (internal error)\n");
+		exit(1);
+	}
+	tmp = strdup(string);
+	if (!tmp)
+	{
+		fprintf(stderr, "pg_strdup: out of memory\n");
+		exit(1);
+	}
+	return tmp;
+}
+
+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, "pg_malloc: out of memory\n");
+		exit(1);
+	}
+	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, "pg_realloc: out of memory\n");
+		exit(1);
+	}
+	return tmp;
+}
+
+void
+pg_free(void *ptr)
+{
+	if (ptr != NULL) 
+		free(ptr);
+}
+
+#endif
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to