mirror of https://github.com/postgres/postgres
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.pull/3/head
parent
0cb1fac3b1
commit
8396447cdb
@ -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; |
||||
} |
@ -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 */ |
@ -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)
|
@ -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); |
||||
} |
@ -0,0 +1,20 @@ |
||||
/*
|
||||
* fe_memutils.h |
||||
* memory management support for frontend code |
||||
* |
||||
* Copyright (c) 2003-2013, PostgreSQL Global Development Group |
||||
* |
||||
* src/include/common/fe_memutils.h |
||||
*/ |
||||
#ifndef FE_MEMUTILS_H |
||||
#define FE_MEMUTILS_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); |
||||
|
||||
#include "utils/palloc.h" |
||||
|
||||
#endif /* FE_MEMUTILS_H */ |
Loading…
Reference in new issue