1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-03 15:22:11 +03:00

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.
This commit is contained in:
Alvaro Herrera
2013-02-12 10:33:40 -03:00
parent 0cb1fac3b1
commit 8396447cdb
59 changed files with 332 additions and 684 deletions

View File

@@ -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