mirror of
https://github.com/postgres/postgres.git
synced 2026-01-29 12:02:15 +03:00
This introduces a bump MemoryContext type. The bump context is best suited for short-lived memory contexts which require only allocations of memory and never a pfree or repalloc, which are unsupported. Memory palloc'd into a bump context has no chunk header. This makes bump a useful context type when lots of small allocations need to be done without any need to pfree those allocations. Allocation sizes are rounded up to the next MAXALIGN boundary, so with this and no chunk header, allocations are very compact indeed. Allocations are also very fast as bump does not check any freelists to try and make use of previously free'd chunks. It just checks if there is enough room on the current block, and if so it bumps the freeptr beyond this chunk and returns the value that the freeptr was previously pointing to. Simple and fast. A new block is malloc'd when there's not enough space in the current block. Code using the bump allocator must take care never to call any functions which could try to call realloc() (or variants), pfree(), GetMemoryChunkContext() or GetMemoryChunkSpace() on a bump allocated chunk. Due to lack of chunk headers, these operations are unsupported. To increase the chances of catching such issues, when compiled with MEMORY_CONTEXT_CHECKING, bump allocated chunks are given a header and any attempt to perform an unsupported operation will result in an ERROR. Without MEMORY_CONTEXT_CHECKING, code attempting an unsupported operation could result in a segfault. A follow-on commit will implement the first user of bump. Author: David Rowley Reviewed-by: Nathan Bossart Reviewed-by: Matthias van de Meent Reviewed-by: Tomas Vondra Reviewed-by: John Naylor Discussion: https://postgr.es/m/CAApHDvqGSpCU95TmM=Bp=6xjL_nLys4zdZOpfNyWBk97Xrdj2w@mail.gmail.com
193 lines
6.7 KiB
C
193 lines
6.7 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* memutils.h
|
|
* This file contains declarations for memory allocation utility
|
|
* functions. These are functions that are not quite widely used
|
|
* enough to justify going in utils/palloc.h, but are still part
|
|
* of the API of the memory management subsystem.
|
|
*
|
|
*
|
|
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/include/utils/memutils.h
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#ifndef MEMUTILS_H
|
|
#define MEMUTILS_H
|
|
|
|
#include "nodes/memnodes.h"
|
|
|
|
|
|
/*
|
|
* MaxAllocSize, MaxAllocHugeSize
|
|
* Quasi-arbitrary limits on size of allocations.
|
|
*
|
|
* Note:
|
|
* There is no guarantee that smaller allocations will succeed, but
|
|
* larger requests will be summarily denied.
|
|
*
|
|
* palloc() enforces MaxAllocSize, chosen to correspond to the limiting size
|
|
* of varlena objects under TOAST. See VARSIZE_4B() and related macros in
|
|
* postgres.h. Many datatypes assume that any allocatable size can be
|
|
* represented in a varlena header. This limit also permits a caller to use
|
|
* an "int" variable for an index into or length of an allocation. Callers
|
|
* careful to avoid these hazards can access the higher limit with
|
|
* MemoryContextAllocHuge(). Both limits permit code to assume that it may
|
|
* compute twice an allocation's size without overflow.
|
|
*/
|
|
#define MaxAllocSize ((Size) 0x3fffffff) /* 1 gigabyte - 1 */
|
|
|
|
#define AllocSizeIsValid(size) ((Size) (size) <= MaxAllocSize)
|
|
|
|
/* Must be less than SIZE_MAX */
|
|
#define MaxAllocHugeSize (SIZE_MAX / 2)
|
|
|
|
#define InvalidAllocSize SIZE_MAX
|
|
|
|
#define AllocHugeSizeIsValid(size) ((Size) (size) <= MaxAllocHugeSize)
|
|
|
|
|
|
/*
|
|
* Standard top-level memory contexts.
|
|
*
|
|
* Only TopMemoryContext and ErrorContext are initialized by
|
|
* MemoryContextInit() itself.
|
|
*/
|
|
extern PGDLLIMPORT MemoryContext TopMemoryContext;
|
|
extern PGDLLIMPORT MemoryContext ErrorContext;
|
|
extern PGDLLIMPORT MemoryContext PostmasterContext;
|
|
extern PGDLLIMPORT MemoryContext CacheMemoryContext;
|
|
extern PGDLLIMPORT MemoryContext MessageContext;
|
|
extern PGDLLIMPORT MemoryContext TopTransactionContext;
|
|
extern PGDLLIMPORT MemoryContext CurTransactionContext;
|
|
|
|
/* This is a transient link to the active portal's memory context: */
|
|
extern PGDLLIMPORT MemoryContext PortalContext;
|
|
|
|
|
|
/*
|
|
* Memory-context-type-independent functions in mcxt.c
|
|
*/
|
|
extern void MemoryContextInit(void);
|
|
extern void MemoryContextReset(MemoryContext context);
|
|
extern void MemoryContextDelete(MemoryContext context);
|
|
extern void MemoryContextResetOnly(MemoryContext context);
|
|
extern void MemoryContextResetChildren(MemoryContext context);
|
|
extern void MemoryContextDeleteChildren(MemoryContext context);
|
|
extern void MemoryContextSetIdentifier(MemoryContext context, const char *id);
|
|
extern void MemoryContextSetParent(MemoryContext context,
|
|
MemoryContext new_parent);
|
|
extern MemoryContext GetMemoryChunkContext(void *pointer);
|
|
extern Size GetMemoryChunkSpace(void *pointer);
|
|
extern MemoryContext MemoryContextGetParent(MemoryContext context);
|
|
extern bool MemoryContextIsEmpty(MemoryContext context);
|
|
extern Size MemoryContextMemAllocated(MemoryContext context, bool recurse);
|
|
extern void MemoryContextMemConsumed(MemoryContext context,
|
|
MemoryContextCounters *consumed);
|
|
extern void MemoryContextStats(MemoryContext context);
|
|
extern void MemoryContextStatsDetail(MemoryContext context,
|
|
int max_level, int max_children,
|
|
bool print_to_stderr);
|
|
extern void MemoryContextAllowInCriticalSection(MemoryContext context,
|
|
bool allow);
|
|
|
|
#ifdef MEMORY_CONTEXT_CHECKING
|
|
extern void MemoryContextCheck(MemoryContext context);
|
|
#endif
|
|
|
|
/* Handy macro for copying and assigning context ID ... but note double eval */
|
|
#define MemoryContextCopyAndSetIdentifier(cxt, id) \
|
|
MemoryContextSetIdentifier(cxt, MemoryContextStrdup(cxt, id))
|
|
|
|
extern void HandleLogMemoryContextInterrupt(void);
|
|
extern void ProcessLogMemoryContextInterrupt(void);
|
|
|
|
/*
|
|
* Memory-context-type-specific functions
|
|
*/
|
|
|
|
/* aset.c */
|
|
extern MemoryContext AllocSetContextCreateInternal(MemoryContext parent,
|
|
const char *name,
|
|
Size minContextSize,
|
|
Size initBlockSize,
|
|
Size maxBlockSize);
|
|
|
|
/*
|
|
* This wrapper macro exists to check for non-constant strings used as context
|
|
* names; that's no longer supported. (Use MemoryContextSetIdentifier if you
|
|
* want to provide a variable identifier.)
|
|
*/
|
|
#ifdef HAVE__BUILTIN_CONSTANT_P
|
|
#define AllocSetContextCreate(parent, name, ...) \
|
|
(StaticAssertExpr(__builtin_constant_p(name), \
|
|
"memory context names must be constant strings"), \
|
|
AllocSetContextCreateInternal(parent, name, __VA_ARGS__))
|
|
#else
|
|
#define AllocSetContextCreate \
|
|
AllocSetContextCreateInternal
|
|
#endif
|
|
|
|
/* slab.c */
|
|
extern MemoryContext SlabContextCreate(MemoryContext parent,
|
|
const char *name,
|
|
Size blockSize,
|
|
Size chunkSize);
|
|
|
|
/* generation.c */
|
|
extern MemoryContext GenerationContextCreate(MemoryContext parent,
|
|
const char *name,
|
|
Size minContextSize,
|
|
Size initBlockSize,
|
|
Size maxBlockSize);
|
|
|
|
/* bump.c */
|
|
extern MemoryContext BumpContextCreate(MemoryContext parent,
|
|
const char *name,
|
|
Size minContextSize,
|
|
Size initBlockSize,
|
|
Size maxBlockSize);
|
|
|
|
/*
|
|
* Recommended default alloc parameters, suitable for "ordinary" contexts
|
|
* that might hold quite a lot of data.
|
|
*/
|
|
#define ALLOCSET_DEFAULT_MINSIZE 0
|
|
#define ALLOCSET_DEFAULT_INITSIZE (8 * 1024)
|
|
#define ALLOCSET_DEFAULT_MAXSIZE (8 * 1024 * 1024)
|
|
#define ALLOCSET_DEFAULT_SIZES \
|
|
ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
|
|
|
|
/*
|
|
* Recommended alloc parameters for "small" contexts that are never expected
|
|
* to contain much data (for example, a context to contain a query plan).
|
|
*/
|
|
#define ALLOCSET_SMALL_MINSIZE 0
|
|
#define ALLOCSET_SMALL_INITSIZE (1 * 1024)
|
|
#define ALLOCSET_SMALL_MAXSIZE (8 * 1024)
|
|
#define ALLOCSET_SMALL_SIZES \
|
|
ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE
|
|
|
|
/*
|
|
* Recommended alloc parameters for contexts that should start out small,
|
|
* but might sometimes grow big.
|
|
*/
|
|
#define ALLOCSET_START_SMALL_SIZES \
|
|
ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
|
|
|
|
|
|
/*
|
|
* Threshold above which a request in an AllocSet context is certain to be
|
|
* allocated separately (and thereby have constant allocation overhead).
|
|
* Few callers should be interested in this, but tuplesort/tuplestore need
|
|
* to know it.
|
|
*/
|
|
#define ALLOCSET_SEPARATE_THRESHOLD 8192
|
|
|
|
#define SLAB_DEFAULT_BLOCK_SIZE (8 * 1024)
|
|
#define SLAB_LARGE_BLOCK_SIZE (8 * 1024 * 1024)
|
|
|
|
#endif /* MEMUTILS_H */
|