2
0

memnodes.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*-------------------------------------------------------------------------
  2. *
  3. * memnodes.h
  4. * POSTGRES memory context node definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/nodes/memnodes.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef MEMNODES_H
  15. #define MEMNODES_H
  16. #include "nodes/nodes.h"
  17. /*
  18. * MemoryContextCounters
  19. * Summarization state for MemoryContextStats collection.
  20. *
  21. * The set of counters in this struct is biased towards AllocSet; if we ever
  22. * add any context types that are based on fundamentally different approaches,
  23. * we might need more or different counters here. A possible API spec then
  24. * would be to print only nonzero counters, but for now we just summarize in
  25. * the format historically used by AllocSet.
  26. */
  27. typedef struct MemoryContextCounters
  28. {
  29. Size nblocks; /* Total number of malloc blocks */
  30. Size freechunks; /* Total number of free chunks */
  31. Size totalspace; /* Total bytes requested from malloc */
  32. Size freespace; /* The unused portion of totalspace */
  33. } MemoryContextCounters;
  34. /*
  35. * MemoryContext
  36. * A logical context in which memory allocations occur.
  37. *
  38. * MemoryContext itself is an abstract type that can have multiple
  39. * implementations.
  40. * The function pointers in MemoryContextMethods define one specific
  41. * implementation of MemoryContext --- they are a virtual function table
  42. * in C++ terms.
  43. *
  44. * Node types that are actual implementations of memory contexts must
  45. * begin with the same fields as MemoryContextData.
  46. *
  47. * Note: for largely historical reasons, typedef MemoryContext is a pointer
  48. * to the context struct rather than the struct type itself.
  49. */
  50. typedef void (*MemoryStatsPrintFunc) (MemoryContext context, void *passthru,
  51. const char *stats_string,
  52. bool print_to_stderr);
  53. typedef struct MemoryContextMethods
  54. {
  55. void *(*alloc) (MemoryContext context, Size size);
  56. /* call this free_p in case someone #define's free() */
  57. void (*free_p) (MemoryContext context, void *pointer);
  58. void *(*realloc) (MemoryContext context, void *pointer, Size size);
  59. void (*reset) (MemoryContext context);
  60. void (*delete_context) (MemoryContext context);
  61. Size (*get_chunk_space) (MemoryContext context, void *pointer);
  62. bool (*is_empty) (MemoryContext context);
  63. void (*stats) (MemoryContext context,
  64. MemoryStatsPrintFunc printfunc, void *passthru,
  65. MemoryContextCounters *totals,
  66. bool print_to_stderr);
  67. #ifdef MEMORY_CONTEXT_CHECKING
  68. void (*check) (MemoryContext context);
  69. #endif
  70. } MemoryContextMethods;
  71. typedef struct MemoryContextData
  72. {
  73. NodeTag type; /* identifies exact kind of context */
  74. /* these two fields are placed here to minimize alignment wastage: */
  75. bool isReset; /* T = no space alloced since last reset */
  76. bool allowInCritSection; /* allow palloc in critical section */
  77. Size mem_allocated; /* track memory allocated for this context */
  78. const MemoryContextMethods *methods; /* virtual function table */
  79. MemoryContext parent; /* NULL if no parent (toplevel context) */
  80. MemoryContext firstchild; /* head of linked list of children */
  81. MemoryContext prevchild; /* previous child of same parent */
  82. MemoryContext nextchild; /* next child of same parent */
  83. const char *name; /* context name (just for debugging) */
  84. const char *ident; /* context ID if any (just for debugging) */
  85. MemoryContextCallback *reset_cbs; /* list of reset/delete callbacks */
  86. } MemoryContextData;
  87. /* utils/palloc.h contains typedef struct MemoryContextData *MemoryContext */
  88. /*
  89. * MemoryContextIsValid
  90. * True iff memory context is valid.
  91. *
  92. * Add new context types to the set accepted by this macro.
  93. */
  94. #define MemoryContextIsValid(context) \
  95. ((context) != NULL && \
  96. (IsA((context), AllocSetContext) || \
  97. IsA((context), SlabContext) || \
  98. IsA((context), GenerationContext)))
  99. #endif /* MEMNODES_H */