diff options
author | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2013-02-12 10:33:40 -0300 |
---|---|---|
committer | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2013-02-12 11:21:05 -0300 |
commit | 8396447cdbdff0b62914748de2fec04281dc9114 (patch) | |
tree | dd95fd78c6c507ef6f3c9d9c4cd769aeda7c9fcc /src/backend | |
parent | 0cb1fac3b19f01025b63d2cdceabb8767185da28 (diff) | |
download | postgresql-8396447cdbdff0b62914748de2fec04281dc9114.tar.gz postgresql-8396447cdbdff0b62914748de2fec04281dc9114.zip |
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/backend')
-rw-r--r-- | src/backend/storage/file/copydir.c | 11 | ||||
-rw-r--r-- | src/backend/utils/mmgr/mcxt.c | 78 |
2 files changed, 42 insertions, 47 deletions
diff --git a/src/backend/storage/file/copydir.c b/src/backend/storage/file/copydir.c index 017f862dc05..391359cdd92 100644 --- a/src/backend/storage/file/copydir.c +++ b/src/backend/storage/file/copydir.c @@ -26,17 +26,6 @@ #include "storage/fd.h" #include "miscadmin.h" -/* - * 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 - static void fsync_fname(char *fname, bool isdir); diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index d71f206dee4..8dd3cf48896 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 |