palloc.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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-2016, 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 void *repalloc(void *pointer, Size size);
  74. extern void pfree(void *pointer);
  75. /*
  76. * The result of palloc() is always word-aligned, so we can skip testing
  77. * alignment of the pointer when deciding which MemSet variant to use.
  78. * Note that this variant does not offer any advantage, and should not be
  79. * used, unless its "sz" argument is a compile-time constant; therefore, the
  80. * issue that it evaluates the argument multiple times isn't a problem in
  81. * practice.
  82. */
  83. #define palloc0fast(sz) \
  84. ( MemSetTest(0, sz) ? \
  85. MemoryContextAllocZeroAligned(CurrentMemoryContext, sz) : \
  86. MemoryContextAllocZero(CurrentMemoryContext, sz) )
  87. /* Higher-limit allocators. */
  88. extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
  89. extern void *repalloc_huge(void *pointer, Size size);
  90. /*
  91. * Although this header file is nominally backend-only, certain frontend
  92. * programs like pg_controldata include it via postgres.h. For some compilers
  93. * it's necessary to hide the inline definition of MemoryContextSwitchTo in
  94. * this scenario; hence the #ifndef FRONTEND.
  95. */
  96. #ifndef FRONTEND
  97. static inline MemoryContext
  98. MemoryContextSwitchTo(MemoryContext context)
  99. {
  100. MemoryContext old = CurrentMemoryContext;
  101. CurrentMemoryContext = context;
  102. return old;
  103. }
  104. #endif /* FRONTEND */
  105. /* Registration of memory context reset/delete callbacks */
  106. extern void MemoryContextRegisterResetCallback(MemoryContext context,
  107. MemoryContextCallback *cb);
  108. /*
  109. * These are like standard strdup() except the copied string is
  110. * allocated in a context, not with malloc().
  111. */
  112. extern char *MemoryContextStrdup(MemoryContext context, const char *string);
  113. extern char *pstrdup(const char *in);
  114. extern char *pnstrdup(const char *in, Size len);
  115. /* sprintf into a palloc'd buffer --- these are in psprintf.c */
  116. extern char *psprintf(const char *fmt,...) pg_attribute_printf(1, 2);
  117. extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
  118. #endif /* PALLOC_H */