Permalink
Browse files

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.
  • Loading branch information...
1 parent 0cb1fac commit 8396447cdbdff0b62914748de2fec04281dc9114 @alvherre alvherre committed Feb 12, 2013
Showing with 332 additions and 684 deletions.
  1. +0 −50 contrib/oid2name/oid2name.c
  2. +1 −1 contrib/pg_upgrade/check.c
  3. +1 −1 contrib/pg_upgrade/controldata.c
  4. +1 −1 contrib/pg_upgrade/dump.c
  5. +1 −1 contrib/pg_upgrade/exec.c
  6. +1 −1 contrib/pg_upgrade/file.c
  7. +1 −1 contrib/pg_upgrade/function.c
  8. +1 −1 contrib/pg_upgrade/info.c
  9. +1 −1 contrib/pg_upgrade/option.c
  10. +1 −1 contrib/pg_upgrade/page.c
  11. +1 −1 contrib/pg_upgrade/parallel.c
  12. +1 −1 contrib/pg_upgrade/pg_upgrade.c
  13. +0 −4 contrib/pg_upgrade/pg_upgrade.h
  14. +1 −1 contrib/pg_upgrade/relfilenode.c
  15. +1 −1 contrib/pg_upgrade/server.c
  16. +1 −1 contrib/pg_upgrade/tablespace.c
  17. +1 −50 contrib/pg_upgrade/util.c
  18. +1 −1 contrib/pg_upgrade/version.c
  19. +1 −1 contrib/pg_upgrade/version_old_8_3.c
  20. +0 −53 contrib/pgbench/pgbench.c
  21. +1 −0 src/Makefile
  22. +14 −11 src/Makefile.global.in
  23. +0 −11 src/backend/storage/file/copydir.c
  24. +42 −36 src/backend/utils/mmgr/mcxt.c
  25. +0 −39 src/bin/initdb/initdb.c
  26. +0 −37 src/bin/pg_basebackup/streamutil.c
  27. +0 −4 src/bin/pg_basebackup/streamutil.h
  28. +0 −38 src/bin/pg_ctl/pg_ctl.c
  29. +3 −3 src/bin/pg_dump/Makefile
  30. +0 −2 src/bin/pg_dump/common.c
  31. +0 −1 src/bin/pg_dump/compress_io.c
  32. +0 −76 src/bin/pg_dump/dumpmem.c
  33. +0 −22 src/bin/pg_dump/dumpmem.h
  34. +0 −1 src/bin/pg_dump/dumputils.c
  35. +1 −1 src/bin/pg_dump/nls.mk
  36. +0 −1 src/bin/pg_dump/pg_backup_archiver.c
  37. +0 −1 src/bin/pg_dump/pg_backup_custom.c
  38. +0 −1 src/bin/pg_dump/pg_backup_db.c
  39. +0 −1 src/bin/pg_dump/pg_backup_directory.c
  40. +0 −1 src/bin/pg_dump/pg_backup_null.c
  41. +0 −1 src/bin/pg_dump/pg_backup_tar.c
  42. +0 −1 src/bin/pg_dump/pg_dump.c
  43. +0 −1 src/bin/pg_dump/pg_dump_sort.c
  44. +0 −1 src/bin/pg_dump/pg_dumpall.c
  45. +0 −1 src/bin/pg_dump/pg_restore.c
  46. +3 −2 src/bin/pg_resetxlog/pg_resetxlog.c
  47. +0 −50 src/bin/psql/common.c
  48. +0 −9 src/bin/psql/common.h
  49. +0 −49 src/bin/scripts/common.c
  50. +0 −4 src/bin/scripts/common.h
  51. +64 −0 src/common/Makefile
  52. +128 −0 src/common/fe_memutils.c
  53. +20 −0 src/include/common/fe_memutils.h
  54. +2 −0 src/include/postgres_fe.h
  55. +8 −22 src/include/utils/palloc.h
  56. +0 −74 src/port/dirmod.c
  57. +1 −0 src/tools/msvc/Install.pm
  58. +27 −8 src/tools/msvc/Mkvcbuild.pm
  59. +1 −1 src/tools/msvc/Project.pm
@@ -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
*
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/check.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/controldata.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/dump.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/exec.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/file.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/function.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/info.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/option.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "miscadmin.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/page.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/parallel.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -35,7 +35,7 @@
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -451,10 +451,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);
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/relfilenode.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/server.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/tablespace.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
View
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/util.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -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()
*
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/version.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
@@ -7,7 +7,7 @@
* contrib/pg_upgrade/version_old_8_3.c
*/
-#include "postgres.h"
+#include "postgres_fe.h"
#include "pg_upgrade.h"
View
@@ -320,59 +320,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)
{
View
@@ -13,6 +13,7 @@ top_builddir = ..
include Makefile.global
SUBDIRS = \
+ common \
port \
timezone \
backend \
Oops, something went wrong.

0 comments on commit 8396447

Please sign in to comment.