memutils.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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-2016, 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) -1 >> 1) /* SIZE_MAX / 2 */
  40. #define AllocHugeSizeIsValid(size) ((Size) (size) <= MaxAllocHugeSize)
  41. /*
  42. * All chunks allocated by any memory context manager are required to be
  43. * preceded by a StandardChunkHeader at a spacing of STANDARDCHUNKHEADERSIZE.
  44. * A currently-allocated chunk must contain a backpointer to its owning
  45. * context as well as the allocated size of the chunk. The backpointer is
  46. * used by pfree() and repalloc() to find the context to call. The allocated
  47. * size is not absolutely essential, but it's expected to be needed by any
  48. * reasonable implementation.
  49. */
  50. typedef struct StandardChunkHeader
  51. {
  52. MemoryContext context; /* owning context */
  53. Size size; /* size of data space allocated in chunk */
  54. #ifdef MEMORY_CONTEXT_CHECKING
  55. /* when debugging memory usage, also store actual requested size */
  56. Size requested_size;
  57. #endif
  58. } StandardChunkHeader;
  59. #define STANDARDCHUNKHEADERSIZE MAXALIGN(sizeof(StandardChunkHeader))
  60. /*
  61. * Standard top-level memory contexts.
  62. *
  63. * Only TopMemoryContext and ErrorContext are initialized by
  64. * MemoryContextInit() itself.
  65. */
  66. extern PGDLLIMPORT MemoryContext TopMemoryContext;
  67. extern PGDLLIMPORT MemoryContext ErrorContext;
  68. extern PGDLLIMPORT MemoryContext PostmasterContext;
  69. extern PGDLLIMPORT MemoryContext CacheMemoryContext;
  70. extern PGDLLIMPORT MemoryContext MessageContext;
  71. extern PGDLLIMPORT MemoryContext TopTransactionContext;
  72. extern PGDLLIMPORT MemoryContext CurTransactionContext;
  73. /* This is a transient link to the active portal's memory context: */
  74. extern PGDLLIMPORT MemoryContext PortalContext;
  75. /* Backwards compatibility macro */
  76. #define MemoryContextResetAndDeleteChildren(ctx) MemoryContextReset(ctx)
  77. /*
  78. * Memory-context-type-independent functions in mcxt.c
  79. */
  80. extern void MemoryContextInit(void);
  81. extern void MemoryContextReset(MemoryContext context);
  82. extern void MemoryContextDelete(MemoryContext context);
  83. extern void MemoryContextResetOnly(MemoryContext context);
  84. extern void MemoryContextResetChildren(MemoryContext context);
  85. extern void MemoryContextDeleteChildren(MemoryContext context);
  86. extern void MemoryContextSetParent(MemoryContext context,
  87. MemoryContext new_parent);
  88. extern Size GetMemoryChunkSpace(void *pointer);
  89. extern MemoryContext GetMemoryChunkContext(void *pointer);
  90. extern MemoryContext MemoryContextGetParent(MemoryContext context);
  91. extern bool MemoryContextIsEmpty(MemoryContext context);
  92. extern void MemoryContextStats(MemoryContext context);
  93. extern void MemoryContextStatsDetail(MemoryContext context, int max_children);
  94. extern void MemoryContextAllowInCriticalSection(MemoryContext context,
  95. bool allow);
  96. #ifdef MEMORY_CONTEXT_CHECKING
  97. extern void MemoryContextCheck(MemoryContext context);
  98. #endif
  99. extern bool MemoryContextContains(MemoryContext context, void *pointer);
  100. /*
  101. * This routine handles the context-type-independent part of memory
  102. * context creation. It's intended to be called from context-type-
  103. * specific creation routines, and noplace else.
  104. */
  105. extern MemoryContext MemoryContextCreate(NodeTag tag, Size size,
  106. MemoryContextMethods *methods,
  107. MemoryContext parent,
  108. const char *name);
  109. /*
  110. * Memory-context-type-specific functions
  111. */
  112. /* aset.c */
  113. extern MemoryContext AllocSetContextCreate(MemoryContext parent,
  114. const char *name,
  115. Size minContextSize,
  116. Size initBlockSize,
  117. Size maxBlockSize);
  118. /*
  119. * Recommended default alloc parameters, suitable for "ordinary" contexts
  120. * that might hold quite a lot of data.
  121. */
  122. #define ALLOCSET_DEFAULT_MINSIZE 0
  123. #define ALLOCSET_DEFAULT_INITSIZE (8 * 1024)
  124. #define ALLOCSET_DEFAULT_MAXSIZE (8 * 1024 * 1024)
  125. #define ALLOCSET_DEFAULT_SIZES \
  126. ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
  127. /*
  128. * Recommended alloc parameters for "small" contexts that are never expected
  129. * to contain much data (for example, a context to contain a query plan).
  130. */
  131. #define ALLOCSET_SMALL_MINSIZE 0
  132. #define ALLOCSET_SMALL_INITSIZE (1 * 1024)
  133. #define ALLOCSET_SMALL_MAXSIZE (8 * 1024)
  134. #define ALLOCSET_SMALL_SIZES \
  135. ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE
  136. /*
  137. * Recommended alloc parameters for contexts that should start out small,
  138. * but might sometimes grow big.
  139. */
  140. #define ALLOCSET_START_SMALL_SIZES \
  141. ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
  142. /*
  143. * Threshold above which a request in an AllocSet context is certain to be
  144. * allocated separately (and thereby have constant allocation overhead).
  145. * Few callers should be interested in this, but tuplesort/tuplestore need
  146. * to know it.
  147. */
  148. #define ALLOCSET_SEPARATE_THRESHOLD 8192
  149. #endif /* MEMUTILS_H */