memnodes.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*-------------------------------------------------------------------------
  2. *
  3. * memnodes.h
  4. * POSTGRES memory context node definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2016, 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, though for now we have only AllocSetContext.
  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 MemoryContext.
  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 struct MemoryContextMethods
  51. {
  52. void *(*alloc) (MemoryContext context, Size size);
  53. /* call this free_p in case someone #define's free() */
  54. void (*free_p) (MemoryContext context, void *pointer);
  55. void *(*realloc) (MemoryContext context, void *pointer, Size size);
  56. void (*init) (MemoryContext context);
  57. void (*reset) (MemoryContext context);
  58. void (*delete_context) (MemoryContext context);
  59. Size (*get_chunk_space) (MemoryContext context, void *pointer);
  60. bool (*is_empty) (MemoryContext context);
  61. void (*stats) (MemoryContext context, int level, bool print,
  62. MemoryContextCounters *totals);
  63. #ifdef MEMORY_CONTEXT_CHECKING
  64. void (*check) (MemoryContext context);
  65. #endif
  66. } MemoryContextMethods;
  67. typedef struct MemoryContextData
  68. {
  69. NodeTag type; /* identifies exact kind of context */
  70. /* these two fields are placed here to minimize alignment wastage: */
  71. bool isReset; /* T = no space alloced since last reset */
  72. bool allowInCritSection; /* allow palloc in critical section */
  73. MemoryContextMethods *methods; /* virtual function table */
  74. MemoryContext parent; /* NULL if no parent (toplevel context) */
  75. MemoryContext firstchild; /* head of linked list of children */
  76. MemoryContext prevchild; /* previous child of same parent */
  77. MemoryContext nextchild; /* next child of same parent */
  78. char *name; /* context name (just for debugging) */
  79. MemoryContextCallback *reset_cbs; /* list of reset/delete callbacks */
  80. } MemoryContextData;
  81. /* utils/palloc.h contains typedef struct MemoryContextData *MemoryContext */
  82. /*
  83. * MemoryContextIsValid
  84. * True iff memory context is valid.
  85. *
  86. * Add new context types to the set accepted by this macro.
  87. */
  88. #define MemoryContextIsValid(context) \
  89. ((context) != NULL && \
  90. (IsA((context), AllocSetContext)))
  91. #endif /* MEMNODES_H */