LCOV - code coverage report
Current view: top level - src/include/utils - memutils.h (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 3 3 100.0 %
Date: 2020-05-29 01:06:25 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * memutils.h
       4             :  *    This file contains declarations for memory allocation utility
       5             :  *    functions.  These are functions that are not quite widely used
       6             :  *    enough to justify going in utils/palloc.h, but are still part
       7             :  *    of the API of the memory management subsystem.
       8             :  *
       9             :  *
      10             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
      11             :  * Portions Copyright (c) 1994, Regents of the University of California
      12             :  *
      13             :  * src/include/utils/memutils.h
      14             :  *
      15             :  *-------------------------------------------------------------------------
      16             :  */
      17             : #ifndef MEMUTILS_H
      18             : #define MEMUTILS_H
      19             : 
      20             : #include "nodes/memnodes.h"
      21             : 
      22             : 
      23             : /*
      24             :  * MaxAllocSize, MaxAllocHugeSize
      25             :  *      Quasi-arbitrary limits on size of allocations.
      26             :  *
      27             :  * Note:
      28             :  *      There is no guarantee that smaller allocations will succeed, but
      29             :  *      larger requests will be summarily denied.
      30             :  *
      31             :  * palloc() enforces MaxAllocSize, chosen to correspond to the limiting size
      32             :  * of varlena objects under TOAST.  See VARSIZE_4B() and related macros in
      33             :  * postgres.h.  Many datatypes assume that any allocatable size can be
      34             :  * represented in a varlena header.  This limit also permits a caller to use
      35             :  * an "int" variable for an index into or length of an allocation.  Callers
      36             :  * careful to avoid these hazards can access the higher limit with
      37             :  * MemoryContextAllocHuge().  Both limits permit code to assume that it may
      38             :  * compute twice an allocation's size without overflow.
      39             :  */
      40             : #define MaxAllocSize    ((Size) 0x3fffffff) /* 1 gigabyte - 1 */
      41             : 
      42             : #define AllocSizeIsValid(size)  ((Size) (size) <= MaxAllocSize)
      43             : 
      44             : #define MaxAllocHugeSize    (SIZE_MAX / 2)
      45             : 
      46             : #define AllocHugeSizeIsValid(size)  ((Size) (size) <= MaxAllocHugeSize)
      47             : 
      48             : 
      49             : /*
      50             :  * Standard top-level memory contexts.
      51             :  *
      52             :  * Only TopMemoryContext and ErrorContext are initialized by
      53             :  * MemoryContextInit() itself.
      54             :  */
      55             : extern PGDLLIMPORT MemoryContext TopMemoryContext;
      56             : extern PGDLLIMPORT MemoryContext ErrorContext;
      57             : extern PGDLLIMPORT MemoryContext PostmasterContext;
      58             : extern PGDLLIMPORT MemoryContext CacheMemoryContext;
      59             : extern PGDLLIMPORT MemoryContext MessageContext;
      60             : extern PGDLLIMPORT MemoryContext TopTransactionContext;
      61             : extern PGDLLIMPORT MemoryContext CurTransactionContext;
      62             : 
      63             : /* This is a transient link to the active portal's memory context: */
      64             : extern PGDLLIMPORT MemoryContext PortalContext;
      65             : 
      66             : /* Backwards compatibility macro */
      67             : #define MemoryContextResetAndDeleteChildren(ctx) MemoryContextReset(ctx)
      68             : 
      69             : 
      70             : /*
      71             :  * Memory-context-type-independent functions in mcxt.c
      72             :  */
      73             : extern void MemoryContextInit(void);
      74             : extern void MemoryContextReset(MemoryContext context);
      75             : extern void MemoryContextDelete(MemoryContext context);
      76             : extern void MemoryContextResetOnly(MemoryContext context);
      77             : extern void MemoryContextResetChildren(MemoryContext context);
      78             : extern void MemoryContextDeleteChildren(MemoryContext context);
      79             : extern void MemoryContextSetIdentifier(MemoryContext context, const char *id);
      80             : extern void MemoryContextSetParent(MemoryContext context,
      81             :                                    MemoryContext new_parent);
      82             : extern Size GetMemoryChunkSpace(void *pointer);
      83             : extern MemoryContext MemoryContextGetParent(MemoryContext context);
      84             : extern bool MemoryContextIsEmpty(MemoryContext context);
      85             : extern Size MemoryContextMemAllocated(MemoryContext context, bool recurse);
      86             : extern void MemoryContextStats(MemoryContext context);
      87             : extern void MemoryContextStatsDetail(MemoryContext context, int max_children);
      88             : extern void MemoryContextAllowInCriticalSection(MemoryContext context,
      89             :                                                 bool allow);
      90             : 
      91             : #ifdef MEMORY_CONTEXT_CHECKING
      92             : extern void MemoryContextCheck(MemoryContext context);
      93             : #endif
      94             : extern bool MemoryContextContains(MemoryContext context, void *pointer);
      95             : 
      96             : /* Handy macro for copying and assigning context ID ... but note double eval */
      97             : #define MemoryContextCopyAndSetIdentifier(cxt, id) \
      98             :     MemoryContextSetIdentifier(cxt, MemoryContextStrdup(cxt, id))
      99             : 
     100             : /*
     101             :  * GetMemoryChunkContext
     102             :  *      Given a currently-allocated chunk, determine the context
     103             :  *      it belongs to.
     104             :  *
     105             :  * All chunks allocated by any memory context manager are required to be
     106             :  * preceded by the corresponding MemoryContext stored, without padding, in the
     107             :  * preceding sizeof(void*) bytes.  A currently-allocated chunk must contain a
     108             :  * backpointer to its owning context.  The backpointer is used by pfree() and
     109             :  * repalloc() to find the context to call.
     110             :  */
     111             : #ifndef FRONTEND
     112             : static inline MemoryContext
     113   417250408 : GetMemoryChunkContext(void *pointer)
     114             : {
     115             :     MemoryContext context;
     116             : 
     117             :     /*
     118             :      * Try to detect bogus pointers handed to us, poorly though we can.
     119             :      * Presumably, a pointer that isn't MAXALIGNED isn't pointing at an
     120             :      * allocated chunk.
     121             :      */
     122             :     Assert(pointer != NULL);
     123             :     Assert(pointer == (void *) MAXALIGN(pointer));
     124             : 
     125             :     /*
     126             :      * OK, it's probably safe to look at the context.
     127             :      */
     128   417250408 :     context = *(MemoryContext *) (((char *) pointer) - sizeof(void *));
     129             : 
     130             :     AssertArg(MemoryContextIsValid(context));
     131             : 
     132   417250408 :     return context;
     133             : }
     134             : #endif
     135             : 
     136             : /*
     137             :  * This routine handles the context-type-independent part of memory
     138             :  * context creation.  It's intended to be called from context-type-
     139             :  * specific creation routines, and noplace else.
     140             :  */
     141             : extern void MemoryContextCreate(MemoryContext node,
     142             :                                 NodeTag tag,
     143             :                                 const MemoryContextMethods *methods,
     144             :                                 MemoryContext parent,
     145             :                                 const char *name);
     146             : 
     147             : 
     148             : /*
     149             :  * Memory-context-type-specific functions
     150             :  */
     151             : 
     152             : /* aset.c */
     153             : extern MemoryContext AllocSetContextCreateInternal(MemoryContext parent,
     154             :                                                    const char *name,
     155             :                                                    Size minContextSize,
     156             :                                                    Size initBlockSize,
     157             :                                                    Size maxBlockSize);
     158             : 
     159             : /*
     160             :  * This wrapper macro exists to check for non-constant strings used as context
     161             :  * names; that's no longer supported.  (Use MemoryContextSetIdentifier if you
     162             :  * want to provide a variable identifier.)
     163             :  */
     164             : #ifdef HAVE__BUILTIN_CONSTANT_P
     165             : #define AllocSetContextCreate(parent, name, ...) \
     166             :     (StaticAssertExpr(__builtin_constant_p(name), \
     167             :                       "memory context names must be constant strings"), \
     168             :      AllocSetContextCreateInternal(parent, name, __VA_ARGS__))
     169             : #else
     170             : #define AllocSetContextCreate \
     171             :     AllocSetContextCreateInternal
     172             : #endif
     173             : 
     174             : /* slab.c */
     175             : extern MemoryContext SlabContextCreate(MemoryContext parent,
     176             :                                        const char *name,
     177             :                                        Size blockSize,
     178             :                                        Size chunkSize);
     179             : 
     180             : /* generation.c */
     181             : extern MemoryContext GenerationContextCreate(MemoryContext parent,
     182             :                                              const char *name,
     183             :                                              Size blockSize);
     184             : 
     185             : /*
     186             :  * Recommended default alloc parameters, suitable for "ordinary" contexts
     187             :  * that might hold quite a lot of data.
     188             :  */
     189             : #define ALLOCSET_DEFAULT_MINSIZE   0
     190             : #define ALLOCSET_DEFAULT_INITSIZE  (8 * 1024)
     191             : #define ALLOCSET_DEFAULT_MAXSIZE   (8 * 1024 * 1024)
     192             : #define ALLOCSET_DEFAULT_SIZES \
     193             :     ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
     194             : 
     195             : /*
     196             :  * Recommended alloc parameters for "small" contexts that are never expected
     197             :  * to contain much data (for example, a context to contain a query plan).
     198             :  */
     199             : #define ALLOCSET_SMALL_MINSIZE   0
     200             : #define ALLOCSET_SMALL_INITSIZE  (1 * 1024)
     201             : #define ALLOCSET_SMALL_MAXSIZE   (8 * 1024)
     202             : #define ALLOCSET_SMALL_SIZES \
     203             :     ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE
     204             : 
     205             : /*
     206             :  * Recommended alloc parameters for contexts that should start out small,
     207             :  * but might sometimes grow big.
     208             :  */
     209             : #define ALLOCSET_START_SMALL_SIZES \
     210             :     ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
     211             : 
     212             : 
     213             : /*
     214             :  * Threshold above which a request in an AllocSet context is certain to be
     215             :  * allocated separately (and thereby have constant allocation overhead).
     216             :  * Few callers should be interested in this, but tuplesort/tuplestore need
     217             :  * to know it.
     218             :  */
     219             : #define ALLOCSET_SEPARATE_THRESHOLD  8192
     220             : 
     221             : #define SLAB_DEFAULT_BLOCK_SIZE     (8 * 1024)
     222             : #define SLAB_LARGE_BLOCK_SIZE       (8 * 1024 * 1024)
     223             : 
     224             : #endif                          /* MEMUTILS_H */

Generated by: LCOV version 1.13