mirror of
https://github.com/postgres/postgres.git
synced 2025-07-08 11:42:09 +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:
@ -28,6 +28,8 @@
|
||||
#ifndef PALLOC_H
|
||||
#define PALLOC_H
|
||||
|
||||
#ifndef FRONTEND
|
||||
|
||||
/*
|
||||
* Type MemoryContextData is declared in nodes/memnodes.h. Most users
|
||||
* of memory allocation should just treat it as an abstract type, so we
|
||||
@ -49,10 +51,6 @@ 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))
|
||||
|
||||
/*
|
||||
* The result of palloc() is always word-aligned, so we can skip testing
|
||||
* alignment of the pointer when deciding which MemSet variant to use.
|
||||
@ -66,20 +64,11 @@ extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
|
||||
MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
|
||||
MemoryContextAllocZero(CurrentMemoryContext, sz) )
|
||||
|
||||
extern void pfree(void *pointer);
|
||||
|
||||
extern void *repalloc(void *pointer, Size size);
|
||||
|
||||
/*
|
||||
* MemoryContextSwitchTo can't be a macro in standard C compilers.
|
||||
* But we can make it an inline function if the compiler supports it.
|
||||
* See STATIC_IF_INLINE in c.h.
|
||||
*
|
||||
* This file has to be includable by some non-backend code such as
|
||||
* pg_resetxlog, so don't expose the CurrentMemoryContext reference
|
||||
* if FRONTEND is defined.
|
||||
*/
|
||||
#ifndef FRONTEND
|
||||
|
||||
#ifndef PG_USE_INLINE
|
||||
extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
|
||||
@ -94,22 +83,19 @@ MemoryContextSwitchTo(MemoryContext context)
|
||||
return old;
|
||||
}
|
||||
#endif /* PG_USE_INLINE || MCXT_INCLUDE_DEFINITIONS */
|
||||
#endif /* !FRONTEND */
|
||||
|
||||
/*
|
||||
* These are like standard strdup() except the copied string is
|
||||
* allocated in a context, not with malloc().
|
||||
*/
|
||||
extern char *MemoryContextStrdup(MemoryContext context, const char *string);
|
||||
#endif /* !FRONTEND */
|
||||
|
||||
#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
|
||||
extern void *palloc(Size size);
|
||||
extern void *palloc0(Size size);
|
||||
extern void pfree(void *pointer);
|
||||
extern void *repalloc(void *pointer, Size size);
|
||||
|
||||
#endif /* PALLOC_H */
|
||||
|
Reference in New Issue
Block a user