gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

224 行
7.5KB

  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-2019, 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 void MemoryContextStats(MemoryContext context);
  74. extern void MemoryContextStatsDetail(MemoryContext context, int max_children);
  75. extern void MemoryContextAllowInCriticalSection(MemoryContext context,
  76. bool allow);
  77. #ifdef MEMORY_CONTEXT_CHECKING
  78. extern void MemoryContextCheck(MemoryContext context);
  79. #endif
  80. extern bool MemoryContextContains(MemoryContext context, void *pointer);
  81. /* Handy macro for copying and assigning context ID ... but note double eval */
  82. #define MemoryContextCopyAndSetIdentifier(cxt, id) \
  83. MemoryContextSetIdentifier(cxt, MemoryContextStrdup(cxt, id))
  84. /*
  85. * GetMemoryChunkContext
  86. * Given a currently-allocated chunk, determine the context
  87. * it belongs to.
  88. *
  89. * All chunks allocated by any memory context manager are required to be
  90. * preceded by the corresponding MemoryContext stored, without padding, in the
  91. * preceding sizeof(void*) bytes. A currently-allocated chunk must contain a
  92. * backpointer to its owning context. The backpointer is used by pfree() and
  93. * repalloc() to find the context to call.
  94. */
  95. #ifndef FRONTEND
  96. static inline MemoryContext
  97. GetMemoryChunkContext(void *pointer)
  98. {
  99. MemoryContext context;
  100. /*
  101. * Try to detect bogus pointers handed to us, poorly though we can.
  102. * Presumably, a pointer that isn't MAXALIGNED isn't pointing at an
  103. * allocated chunk.
  104. */
  105. Assert(pointer != NULL);
  106. Assert(pointer == (void *) MAXALIGN(pointer));
  107. /*
  108. * OK, it's probably safe to look at the context.
  109. */
  110. context = *(MemoryContext *) (((char *) pointer) - sizeof(void *));
  111. AssertArg(MemoryContextIsValid(context));
  112. return context;
  113. }
  114. #endif
  115. /*
  116. * This routine handles the context-type-independent part of memory
  117. * context creation. It's intended to be called from context-type-
  118. * specific creation routines, and noplace else.
  119. */
  120. extern void MemoryContextCreate(MemoryContext node,
  121. NodeTag tag,
  122. const MemoryContextMethods *methods,
  123. MemoryContext parent,
  124. const char *name);
  125. /*
  126. * Memory-context-type-specific functions
  127. */
  128. /* aset.c */
  129. extern MemoryContext AllocSetContextCreateInternal(MemoryContext parent,
  130. const char *name,
  131. Size minContextSize,
  132. Size initBlockSize,
  133. Size maxBlockSize);
  134. /*
  135. * This wrapper macro exists to check for non-constant strings used as context
  136. * names; that's no longer supported. (Use MemoryContextSetIdentifier if you
  137. * want to provide a variable identifier.)
  138. */
  139. #ifdef HAVE__BUILTIN_CONSTANT_P
  140. #define AllocSetContextCreate(parent, name, ...) \
  141. (StaticAssertExpr(__builtin_constant_p(name), \
  142. "memory context names must be constant strings"), \
  143. AllocSetContextCreateInternal(parent, name, __VA_ARGS__))
  144. #else
  145. #define AllocSetContextCreate \
  146. AllocSetContextCreateInternal
  147. #endif
  148. /* slab.c */
  149. extern MemoryContext SlabContextCreate(MemoryContext parent,
  150. const char *name,
  151. Size blockSize,
  152. Size chunkSize);
  153. /* generation.c */
  154. extern MemoryContext GenerationContextCreate(MemoryContext parent,
  155. const char *name,
  156. Size blockSize);
  157. /*
  158. * Recommended default alloc parameters, suitable for "ordinary" contexts
  159. * that might hold quite a lot of data.
  160. */
  161. #define ALLOCSET_DEFAULT_MINSIZE 0
  162. #define ALLOCSET_DEFAULT_INITSIZE (8 * 1024)
  163. #define ALLOCSET_DEFAULT_MAXSIZE (8 * 1024 * 1024)
  164. #define ALLOCSET_DEFAULT_SIZES \
  165. ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
  166. /*
  167. * Recommended alloc parameters for "small" contexts that are never expected
  168. * to contain much data (for example, a context to contain a query plan).
  169. */
  170. #define ALLOCSET_SMALL_MINSIZE 0
  171. #define ALLOCSET_SMALL_INITSIZE (1 * 1024)
  172. #define ALLOCSET_SMALL_MAXSIZE (8 * 1024)
  173. #define ALLOCSET_SMALL_SIZES \
  174. ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE
  175. /*
  176. * Recommended alloc parameters for contexts that should start out small,
  177. * but might sometimes grow big.
  178. */
  179. #define ALLOCSET_START_SMALL_SIZES \
  180. ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
  181. /*
  182. * Threshold above which a request in an AllocSet context is certain to be
  183. * allocated separately (and thereby have constant allocation overhead).
  184. * Few callers should be interested in this, but tuplesort/tuplestore need
  185. * to know it.
  186. */
  187. #define ALLOCSET_SEPARATE_THRESHOLD 8192
  188. #define SLAB_DEFAULT_BLOCK_SIZE (8 * 1024)
  189. #define SLAB_LARGE_BLOCK_SIZE (8 * 1024 * 1024)
  190. #endif /* MEMUTILS_H */
上海开阖软件有限公司 沪ICP备12045867号-1