palloc.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*-------------------------------------------------------------------------
  2. *
  3. * palloc.h
  4. * POSTGRES memory allocator definitions.
  5. *
  6. * This file contains the basic memory allocation interface that is
  7. * needed by almost every backend module. It is included directly by
  8. * postgres.h, so the definitions here are automatically available
  9. * everywhere. Keep it lean!
  10. *
  11. * Memory allocation occurs within "contexts". Every chunk obtained from
  12. * palloc()/MemoryContextAlloc() is allocated within a specific context.
  13. * The entire contents of a context can be freed easily and quickly by
  14. * resetting or deleting the context --- this is both faster and less
  15. * prone to memory-leakage bugs than releasing chunks individually.
  16. * We organize contexts into context trees to allow fine-grain control
  17. * over chunk lifetime while preserving the certainty that we will free
  18. * everything that should be freed. See utils/mmgr/README for more info.
  19. *
  20. *
  21. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  22. * Portions Copyright (c) 1994, Regents of the University of California
  23. *
  24. * src/include/utils/palloc.h
  25. *
  26. *-------------------------------------------------------------------------
  27. */
  28. #ifndef PALLOC_H
  29. #define PALLOC_H
  30. /*
  31. * Type MemoryContextData is declared in nodes/memnodes.h. Most users
  32. * of memory allocation should just treat it as an abstract type, so we
  33. * do not provide the struct contents here.
  34. */
  35. typedef struct MemoryContextData *MemoryContext;
  36. /*
  37. * A memory context can have callback functions registered on it. Any such
  38. * function will be called once just before the context is next reset or
  39. * deleted. The MemoryContextCallback struct describing such a callback
  40. * typically would be allocated within the context itself, thereby avoiding
  41. * any need to manage it explicitly (the reset/delete action will free it).
  42. */
  43. typedef void (*MemoryContextCallbackFunction) (void *arg);
  44. typedef struct MemoryContextCallback
  45. {
  46. MemoryContextCallbackFunction func; /* function to call */
  47. void *arg; /* argument to pass it */
  48. struct MemoryContextCallback *next; /* next in list of callbacks */
  49. } MemoryContextCallback;
  50. /*
  51. * CurrentMemoryContext is the default allocation context for palloc().
  52. * Avoid accessing it directly! Instead, use MemoryContextSwitchTo()
  53. * to change the setting.
  54. */
  55. extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
  56. /*
  57. * Flags for MemoryContextAllocExtended.
  58. */
  59. #define MCXT_ALLOC_HUGE 0x01 /* allow huge allocation (> 1 GB) */
  60. #define MCXT_ALLOC_NO_OOM 0x02 /* no failure if out-of-memory */
  61. #define MCXT_ALLOC_ZERO 0x04 /* zero allocated memory */
  62. /*
  63. * Fundamental memory-allocation operations (more are in utils/memutils.h)
  64. */
  65. extern void *MemoryContextAlloc(MemoryContext context, Size size);
  66. extern void *MemoryContextAllocZero(MemoryContext context, Size size);
  67. extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
  68. extern void *MemoryContextAllocExtended(MemoryContext context,
  69. Size size, int flags);
  70. extern void *palloc(Size size);
  71. extern void *palloc0(Size size);
  72. extern void *palloc_extended(Size size, int flags);
  73. extern pg_nodiscard void *repalloc(void *pointer, Size size);
  74. extern void pfree(void *pointer);
  75. /*
  76. * Variants with easier notation and more type safety
  77. */
  78. /*
  79. * Allocate space for one object of type "type"
  80. */
  81. #define palloc_object(type) ((type *) palloc(sizeof(type)))
  82. #define palloc0_object(type) ((type *) palloc0(sizeof(type)))
  83. /*
  84. * Allocate space for "count" objects of type "type"
  85. */
  86. #define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
  87. #define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))
  88. /*
  89. * Change size of allocation pointed to by "pointer" to have space for "count"
  90. * objects of type "type"
  91. */
  92. #define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))
  93. /*
  94. * The result of palloc() is always word-aligned, so we can skip testing
  95. * alignment of the pointer when deciding which MemSet variant to use.
  96. * Note that this variant does not offer any advantage, and should not be
  97. * used, unless its "sz" argument is a compile-time constant; therefore, the
  98. * issue that it evaluates the argument multiple times isn't a problem in
  99. * practice.
  100. */
  101. #define palloc0fast(sz) \
  102. ( MemSetTest(0, sz) ? \
  103. MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
  104. MemoryContextAllocZero(CurrentMemoryContext, sz) )
  105. /* Higher-limit allocators. */
  106. extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
  107. extern pg_nodiscard void *repalloc_huge(void *pointer, Size size);
  108. /*
  109. * Although this header file is nominally backend-only, certain frontend
  110. * programs like pg_controldata include it via postgres.h. For some compilers
  111. * it's necessary to hide the inline definition of MemoryContextSwitchTo in
  112. * this scenario; hence the #ifndef FRONTEND.
  113. */
  114. #ifndef FRONTEND
  115. static inline MemoryContext
  116. MemoryContextSwitchTo(MemoryContext context)
  117. {
  118. MemoryContext old = CurrentMemoryContext;
  119. CurrentMemoryContext = context;
  120. return old;
  121. }
  122. #endif /* FRONTEND */
  123. /* Registration of memory context reset/delete callbacks */
  124. extern void MemoryContextRegisterResetCallback(MemoryContext context,
  125. MemoryContextCallback *cb);
  126. /*
  127. * These are like standard strdup() except the copied string is
  128. * allocated in a context, not with malloc().
  129. */
  130. extern char *MemoryContextStrdup(MemoryContext context, const char *string);
  131. extern char *pstrdup(const char *in);
  132. extern char *pnstrdup(const char *in, Size len);
  133. extern char *pchomp(const char *in);
  134. /* sprintf into a palloc'd buffer --- these are in psprintf.c */
  135. extern char *psprintf(const char *fmt,...) pg_attribute_printf(1, 2);
  136. extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
  137. #endif /* PALLOC_H */