--- contrib/oid2name/oid2name.c | 52 +------------ contrib/pg_upgrade/pg_upgrade.h | 5 +- contrib/pg_upgrade/util.c | 49 ------------- contrib/pgbench/pgbench.c | 54 +------------- src/backend/utils/mmgr/mcxt.c | 78 +++++++++++--------- src/bin/initdb/initdb.c | 40 +--------- src/bin/pg_basebackup/pg_basebackup.c | 2 +- src/bin/pg_basebackup/pg_receivexlog.c | 1 + src/bin/pg_basebackup/receivelog.c | 1 + src/bin/pg_basebackup/streamutil.c | 38 +--------- src/bin/pg_basebackup/streamutil.h | 4 - src/bin/pg_ctl/pg_ctl.c | 39 +--------- src/bin/pg_dump/Makefile | 6 +- src/bin/pg_dump/common.c | 1 - src/bin/pg_dump/compress_io.c | 1 - src/bin/pg_dump/dumpmem.c | 76 ------------------- src/bin/pg_dump/dumpmem.h | 22 ------ src/bin/pg_dump/dumputils.h | 1 + src/bin/pg_dump/pg_backup_archiver.c | 1 - src/bin/pg_dump/pg_backup_custom.c | 2 +- src/bin/pg_dump/pg_backup_db.c | 1 - src/bin/pg_dump/pg_backup_directory.c | 1 - src/bin/pg_dump/pg_backup_null.c | 1 - src/bin/pg_dump/pg_backup_tar.c | 1 - src/bin/pg_dump/pg_dump.c | 1 - src/bin/pg_dump/pg_dump_sort.c | 1 - src/bin/pg_dump/pg_dumpall.c | 1 - src/bin/pg_dump/pg_restore.c | 1 - src/bin/psql/common.c | 50 ------------- src/bin/psql/common.h | 10 +-- src/bin/scripts/common.c | 49 ------------- src/bin/scripts/common.h | 5 +- src/include/port/palloc.h | 19 +++++ src/include/utils/palloc.h | 12 +-- src/port/Makefile | 8 +- src/port/dirmod.c | 75 +------------------ src/port/palloc.c | 130 +++++++++++++++++++++++++++++++++ 37 files changed, 218 insertions(+), 621 deletions(-) delete mode 100644 src/bin/pg_dump/dumpmem.c delete mode 100644 src/bin/pg_dump/dumpmem.h create mode 100644 src/include/port/palloc.h create mode 100644 src/port/palloc.c
diff --git a/contrib/oid2name/oid2name.c b/contrib/oid2name/oid2name.c index a666731..dfd8105 100644 --- a/contrib/oid2name/oid2name.c +++ b/contrib/oid2name/oid2name.c @@ -9,6 +9,8 @@ */ #include "postgres_fe.h" +#include "port/palloc.h" + #include <unistd.h> #ifdef HAVE_GETOPT_H #include <getopt.h> @@ -50,9 +52,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 +200,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 --git a/contrib/pg_upgrade/pg_upgrade.h b/contrib/pg_upgrade/pg_upgrade.h index c1a2f53..3324918 100644 --- a/contrib/pg_upgrade/pg_upgrade.h +++ b/contrib/pg_upgrade/pg_upgrade.h @@ -11,6 +11,7 @@ #include <sys/time.h> #include "libpq-fe.h" +#include "port/palloc.h" /* Use port in the private/dynamic port number range */ #define DEF_PGUPORT 50432 @@ -438,10 +439,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 --git a/contrib/pg_upgrade/util.c b/contrib/pg_upgrade/util.c index c91003a..80d0733 100644 --- a/contrib/pg_upgrade/util.c +++ b/contrib/pg_upgrade/util.c @@ -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 --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c index 3ca120f..e0ec166 100644 --- a/contrib/pgbench/pgbench.c +++ b/contrib/pgbench/pgbench.c @@ -33,6 +33,7 @@ #include "postgres_fe.h" +#include "port/palloc.h" #include "getopt_long.h" #include "libpq-fe.h" #include "libpq/pqsignal.h" @@ -298,59 +299,6 @@ static char *select_only = { 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 --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index d71f206..8dd3cf4 100644 --- a/src/backend/utils/mmgr/mcxt.c +++ b/src/backend/utils/mmgr/mcxt.c @@ -634,6 +634,42 @@ MemoryContextAllocZeroAligned(MemoryContext context, Size size) return ret; } +void * +palloc(Size size) +{ + /* duplicates MemoryContextAlloc to avoid increased overhead */ + AssertArg(MemoryContextIsValid(CurrentMemoryContext)); + + if (!AllocSizeIsValid(size)) + elog(ERROR, "invalid memory alloc request size %lu", + (unsigned long) size); + + CurrentMemoryContext->isReset = false; + + return (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext, size); +} + +void * +palloc0(Size size) +{ + /* duplicates MemoryContextAllocZero to avoid increased overhead */ + void *ret; + + AssertArg(MemoryContextIsValid(CurrentMemoryContext)); + + if (!AllocSizeIsValid(size)) + elog(ERROR, "invalid memory alloc request size %lu", + (unsigned long) size); + + CurrentMemoryContext->isReset = false; + + ret = (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext, size); + + MemSetAligned(ret, 0, size); + + return ret; +} + /* * pfree * Release an allocated chunk. @@ -715,6 +751,12 @@ MemoryContextStrdup(MemoryContext context, const char *string) return nstr; } +char * +pstrdup(const char *in) +{ + return MemoryContextStrdup(CurrentMemoryContext, in); +} + /* * pnstrdup * Like pstrdup(), but append null byte to a @@ -729,39 +771,3 @@ pnstrdup(const char *in, Size len) out[len] = '\0'; return out; } - - -#if defined(WIN32) || defined(__CYGWIN__) -/* - * Memory support routines for libpgport on Win32 - * - * Win32 can't load a library that PGDLLIMPORTs a variable - * if the link object files also PGDLLIMPORT the same variable. - * For this reason, libpgport can't reference CurrentMemoryContext - * in the palloc macro calls. - * - * To fix this, we create several functions here that allow us to - * manage memory without doing the inline in libpgport. - */ -void * -pgport_palloc(Size sz) -{ - return palloc(sz); -} - - -char * -pgport_pstrdup(const char *str) -{ - return pstrdup(str); -} - - -/* Doesn't reference a PGDLLIMPORT variable, but here for completeness. */ -void -pgport_pfree(void *pointer) -{ - pfree(pointer); -} - -#endif diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 1bba426..e9aae26 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -56,6 +56,7 @@ #include <signal.h> #include <time.h> +#include "port/palloc.h" #include "libpq/pqsignal.h" #include "mb/pg_wchar.h" #include "getaddrinfo.h" @@ -200,8 +201,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 +316,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 --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 661cf24..b238927 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -15,6 +15,7 @@ #include "libpq-fe.h" #include "pqexpbuffer.h" #include "pgtar.h" +#include "port/palloc.h" #include <unistd.h> #include <dirent.h> @@ -32,7 +33,6 @@ #include "receivelog.h" #include "streamutil.h" - /* Global options */ char *basedir = NULL; char format = 'p'; /* p(lain)/t(ar) */ diff --git a/src/bin/pg_basebackup/pg_receivexlog.c b/src/bin/pg_basebackup/pg_receivexlog.c index 7f2db19..b9ccb62 100644 --- a/src/bin/pg_basebackup/pg_receivexlog.c +++ b/src/bin/pg_basebackup/pg_receivexlog.c @@ -16,6 +16,7 @@ #include "libpq-fe.h" #include "libpq/pqsignal.h" #include "access/xlog_internal.h" +#include "port/palloc.h" #include "receivelog.h" #include "streamutil.h" diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c index cf9cea1..f4f883c 100644 --- a/src/bin/pg_basebackup/receivelog.c +++ b/src/bin/pg_basebackup/receivelog.c @@ -23,6 +23,7 @@ #include "libpq-fe.h" #include "access/xlog_internal.h" +#include "port/palloc.h" #include "receivelog.h" #include "streamutil.h" diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c index 71abbfa..184b459 100644 --- a/src/bin/pg_basebackup/streamutil.c +++ b/src/bin/pg_basebackup/streamutil.c @@ -13,6 +13,7 @@ #include "postgres_fe.h" #include "streamutil.h" +#include "port/palloc.h" #include <stdio.h> #include <string.h> @@ -26,43 +27,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 --git a/src/bin/pg_basebackup/streamutil.h b/src/bin/pg_basebackup/streamutil.h index fdf3641..4f5ff91 100644 --- a/src/bin/pg_basebackup/streamutil.h +++ b/src/bin/pg_basebackup/streamutil.h @@ -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 --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index e412d71..c07e525 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -33,6 +33,7 @@ #include <sys/resource.h> #endif +#include "port/palloc.h" #include "libpq/pqsignal.h" #include "getopt_long.h" #include "miscadmin.h" @@ -118,8 +119,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 +225,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 --git a/src/bin/pg_dump/Makefile b/src/bin/pg_dump/Makefile index 57aea6f..a6ab39d 100644 --- a/src/bin/pg_dump/Makefile +++ b/src/bin/pg_dump/Makefile @@ -20,7 +20,7 @@ override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS) 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.o $(OBJS) $(KEYWRDOBJS) | submake-libpq 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 --git a/src/bin/pg_dump/common.c b/src/bin/pg_dump/common.c index 99a3fe3..3362081 100644 --- a/src/bin/pg_dump/common.c +++ b/src/bin/pg_dump/common.c @@ -18,7 +18,6 @@ #include <ctype.h> #include "catalog/pg_class.h" -#include "dumpmem.h" #include "dumputils.h" diff --git a/src/bin/pg_dump/compress_io.c b/src/bin/pg_dump/compress_io.c index aac991b..768b923 100644 --- a/src/bin/pg_dump/compress_io.c +++ b/src/bin/pg_dump/compress_io.c @@ -53,7 +53,6 @@ */ #include "compress_io.h" -#include "dumpmem.h" #include "dumputils.h" /*---------------------- diff --git a/src/bin/pg_dump/dumpmem.c b/src/bin/pg_dump/dumpmem.c deleted file mode 100644 index d96655e..0000000 --- a/src/bin/pg_dump/dumpmem.c +++ /dev/null @@ -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 --git a/src/bin/pg_dump/dumpmem.h b/src/bin/pg_dump/dumpmem.h deleted file mode 100644 index 0a4ab1b..0000000 --- a/src/bin/pg_dump/dumpmem.h +++ /dev/null @@ -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 --git a/src/bin/pg_dump/dumputils.h b/src/bin/pg_dump/dumputils.h index ac7480c..d58afb2 100644 --- a/src/bin/pg_dump/dumputils.h +++ b/src/bin/pg_dump/dumputils.h @@ -17,6 +17,7 @@ #define DUMPUTILS_H #include "libpq-fe.h" +#include "port/palloc.h" #include "pqexpbuffer.h" typedef enum /* bits returned by set_dump_section */ diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 1fead28..ec044e0 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -21,7 +21,6 @@ */ #include "pg_backup_db.h" -#include "dumpmem.h" #include "dumputils.h" #include <ctype.h> diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup_custom.c index c9adc6f..fc8b95b 100644 --- a/src/bin/pg_dump/pg_backup_custom.c +++ b/src/bin/pg_dump/pg_backup_custom.c @@ -26,7 +26,7 @@ #include "compress_io.h" #include "dumputils.h" -#include "dumpmem.h" +#include "port/palloc.h" /*-------- * Routines in the format interface diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c index c295fc5..21c203e 100644 --- a/src/bin/pg_dump/pg_backup_db.c +++ b/src/bin/pg_dump/pg_backup_db.c @@ -11,7 +11,6 @@ */ #include "pg_backup_db.h" -#include "dumpmem.h" #include "dumputils.h" #include <unistd.h> diff --git a/src/bin/pg_dump/pg_backup_directory.c b/src/bin/pg_dump/pg_backup_directory.c index 2dcf7be..5b71eba 100644 --- a/src/bin/pg_dump/pg_backup_directory.c +++ b/src/bin/pg_dump/pg_backup_directory.c @@ -34,7 +34,6 @@ */ #include "compress_io.h" -#include "dumpmem.h" #include "dumputils.h" #include <dirent.h> diff --git a/src/bin/pg_dump/pg_backup_null.c b/src/bin/pg_dump/pg_backup_null.c index ba1e461..6ac8198 100644 --- a/src/bin/pg_dump/pg_backup_null.c +++ b/src/bin/pg_dump/pg_backup_null.c @@ -23,7 +23,6 @@ */ #include "pg_backup_archiver.h" -#include "dumpmem.h" #include "dumputils.h" #include <unistd.h> /* for dup */ diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_tar.c index 5e56070..046fe7c 100644 --- a/src/bin/pg_dump/pg_backup_tar.c +++ b/src/bin/pg_dump/pg_backup_tar.c @@ -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 "pgtar.h" diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 9b71c75..46c22c0 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -59,7 +59,6 @@ #include "pg_backup_archiver.h" #include "pg_backup_db.h" -#include "dumpmem.h" #include "dumputils.h" extern char *optarg; diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c index f828a45..955c231 100644 --- a/src/bin/pg_dump/pg_dump_sort.c +++ b/src/bin/pg_dump/pg_dump_sort.c @@ -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 --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 9d3ff55..6378e4d 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -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 --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 49d799b..1523616 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -41,7 +41,6 @@ #include "pg_backup_archiver.h" -#include "dumpmem.h" #include "dumputils.h" #include <ctype.h> diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 5fb0316..6f216a2 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -33,56 +33,6 @@ static bool command_no_begin(const char *query); 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 --git a/src/bin/psql/common.h b/src/bin/psql/common.h index f9f3b1f..c6d2fea 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -11,18 +11,10 @@ #include "postgres_fe.h" #include <setjmp.h> #include "libpq-fe.h" +#include "port/palloc.h" #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 --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c index c7cc04a..03193b6 100644 --- a/src/bin/scripts/common.c +++ b/src/bin/scripts/common.c @@ -278,55 +278,6 @@ executeMaintenanceCommand(PGconn *conn, const char *query, bool echo) } /* - * "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 --git a/src/bin/scripts/common.h b/src/bin/scripts/common.h index a174da7..ad0aaaf 100644 --- a/src/bin/scripts/common.h +++ b/src/bin/scripts/common.h @@ -12,6 +12,7 @@ #include "libpq-fe.h" #include "getopt_long.h" /* pgrminclude ignore */ #include "pqexpbuffer.h" /* pgrminclude ignore */ +#include "port/palloc.h" /* pgrminclude ignore */ enum trivalue { @@ -50,8 +51,4 @@ extern bool yesno_prompt(const char *question); 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 --git a/src/include/port/palloc.h b/src/include/port/palloc.h new file mode 100644 index 0000000..a7900bf --- /dev/null +++ b/src/include/port/palloc.h @@ -0,0 +1,19 @@ +/* + * common.h + * Common support routines for bin/scripts/ + * + * Copyright (c) 2003-2013, PostgreSQL Global Development Group + * + * src/bin/scripts/common.h + */ + +#ifndef PGPORT_PALLOC_H +#define PGPORT_PALLOC_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 *pointer, size_t size); +extern void pg_free(void *pointer); + +#endif /* PGPORT_PALLOC_H */ diff --git a/src/include/utils/palloc.h b/src/include/utils/palloc.h index d86558e..91eb898 100644 --- a/src/include/utils/palloc.h +++ b/src/include/utils/palloc.h @@ -49,9 +49,8 @@ extern void *MemoryContextAlloc(MemoryContext context, Size size); extern void *MemoryContextAllocZero(MemoryContext context, Size size); extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size); -#define palloc(sz) MemoryContextAlloc(CurrentMemoryContext, (sz)) - -#define palloc0(sz) MemoryContextAllocZero(CurrentMemoryContext, (sz)) +extern void *palloc(Size size); +extern void *palloc0(Size size); /* * The result of palloc() is always word-aligned, so we can skip testing @@ -102,14 +101,9 @@ MemoryContextSwitchTo(MemoryContext context) */ extern char *MemoryContextStrdup(MemoryContext context, const char *string); -#define pstrdup(str) MemoryContextStrdup(CurrentMemoryContext, (str)) +extern char *pstrdup(const char *in); extern char *pnstrdup(const char *in, Size len); -#if defined(WIN32) || defined(__CYGWIN__) -extern void *pgport_palloc(Size sz); -extern char *pgport_pstrdup(const char *str); -extern void pgport_pfree(void *pointer); -#endif #endif /* PALLOC_H */ diff --git a/src/port/Makefile b/src/port/Makefile index a3db615..58e8da5 100644 --- a/src/port/Makefile +++ b/src/port/Makefile @@ -30,12 +30,16 @@ include $(top_builddir)/src/Makefile.global override CPPFLAGS := -I$(top_builddir)/src/port -DFRONTEND $(CPPFLAGS) LIBS += $(PTHREAD_LIBS) -OBJS = $(LIBOBJS) chklocale.o dirmod.o erand48.o exec.o fls.o inet_net_ntop.o \ +# files built for backend & frontend +COMMON_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 \ pgstrcasecmp.o qsort.o qsort_arg.o quotes.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 -OBJS_SRV = $(OBJS:%.o=%_srv.o) +OBJS_SRV = $(COMMON_OBJS:%.o=%_srv.o) + +# frontend files +OBJS = $(COMMON_OBJS) palloc.o all: libpgport.a libpgport_srv.a diff --git a/src/port/dirmod.c b/src/port/dirmod.c index e7ff1f0..2bff7c8 100644 --- a/src/port/dirmod.c +++ b/src/port/dirmod.c @@ -19,6 +19,7 @@ #include "postgres.h" #else #include "postgres_fe.h" +#include "utils/palloc.h" #endif /* Don't modify declarations in system headers */ @@ -40,80 +41,6 @@ #endif #endif - -#ifndef FRONTEND - -/* - * On Windows, call non-macro versions of palloc; we can't reference - * CurrentMemoryContext in this file because of PGDLLIMPORT conflict. - */ -#if defined(WIN32) || defined(__CYGWIN__) -#undef palloc -#undef pstrdup -#define palloc(sz) pgport_palloc(sz) -#define pstrdup(str) pgport_pstrdup(str) -#endif -#else /* FRONTEND */ - -/* - * In frontend, fake palloc behavior with these - */ -#undef palloc -#undef pstrdup -#define palloc(sz) fe_palloc(sz) -#define pstrdup(str) fe_pstrdup(str) -#define repalloc(pointer,sz) fe_repalloc(pointer,sz) -#define pfree(pointer) free(pointer) - -static void * -fe_palloc(Size size) -{ - void *res; - - /* Avoid unportable behavior of malloc(0) */ - if (size == 0) - size = 1; - res = malloc(size); - if (res == NULL) - { - fprintf(stderr, _("out of memory\n")); - exit(1); - } - return res; -} - -static char * -fe_pstrdup(const char *string) -{ - char *res; - - if ((res = strdup(string)) == NULL) - { - fprintf(stderr, _("out of memory\n")); - exit(1); - } - return res; -} - -static void * -fe_repalloc(void *pointer, Size size) -{ - void *res; - - /* Avoid unportable behavior of realloc(NULL, 0) */ - if (pointer == NULL && size == 0) - size = 1; - res = realloc(pointer, size); - if (res == NULL) - { - fprintf(stderr, _("out of memory\n")); - exit(1); - } - return res; -} -#endif /* FRONTEND */ - - #if defined(WIN32) || defined(__CYGWIN__) /* diff --git a/src/port/palloc.c b/src/port/palloc.c new file mode 100644 index 0000000..39eb845 --- /dev/null +++ b/src/port/palloc.c @@ -0,0 +1,130 @@ +/*------------------------------------------------------------------------- + * + * palloc.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/port/palloc.c + * + *------------------------------------------------------------------------- + */ + +#ifndef FRONTEND +#error "This file is not expected to be compiled for backend code" +#endif + +#include "postgres_fe.h" +#include "port/palloc.h" +#include "utils/palloc.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(). 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_free(void *ptr) +{ + if (ptr != NULL) + free(ptr); +} + +/* + * Emulation of memory management functions commonly used in backend code that + * are useful if code should be used by both front and backend code. + */ +void * +palloc(Size size) +{ + return pg_malloc(size); +} + +void * +palloc0(Size size) +{ + return pg_malloc0(size); +} + +void +pfree(void *pointer) +{ + return pg_free(pointer); +} + +char * +pstrdup(const char *string) +{ + return pg_strdup(string); +} + +void * +repalloc(void *pointer, Size size) +{ + return pg_realloc(pointer, size); +} +
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers