2
0

memutils.h 7.8 KB

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