Improve performance of and reduce overheads of memory management
[pgsql.git] / src / include / utils / memutils_internal.h
blob9a9f52ef16466e95878adc77dbfeecf5c37d1de9
1 /*-------------------------------------------------------------------------
3 * memutils_internal.h
4 * This file contains declarations for memory allocation utility
5 * functions for internal use.
8 * Portions Copyright (c) 2022, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/include/utils/memutils_internal.h
13 *-------------------------------------------------------------------------
16 #ifndef MEMUTILS_INTERNAL_H
17 #define MEMUTILS_INTERNAL_H
19 #include "utils/memutils.h"
21 /* These functions implement the MemoryContext API for AllocSet context. */
22 extern void *AllocSetAlloc(MemoryContext context, Size size);
23 extern void AllocSetFree(void *pointer);
24 extern void *AllocSetRealloc(void *pointer, Size size);
25 extern void AllocSetReset(MemoryContext context);
26 extern void AllocSetDelete(MemoryContext context);
27 extern MemoryContext AllocSetGetChunkContext(void *pointer);
28 extern Size AllocSetGetChunkSpace(void *pointer);
29 extern bool AllocSetIsEmpty(MemoryContext context);
30 extern void AllocSetStats(MemoryContext context,
31 MemoryStatsPrintFunc printfunc, void *passthru,
32 MemoryContextCounters *totals,
33 bool print_to_stderr);
34 #ifdef MEMORY_CONTEXT_CHECKING
35 extern void AllocSetCheck(MemoryContext context);
36 #endif
38 /* These functions implement the MemoryContext API for Generation context. */
39 extern void *GenerationAlloc(MemoryContext context, Size size);
40 extern void GenerationFree(void *pointer);
41 extern void *GenerationRealloc(void *pointer, Size size);
42 extern void GenerationReset(MemoryContext context);
43 extern void GenerationDelete(MemoryContext context);
44 extern MemoryContext GenerationGetChunkContext(void *pointer);
45 extern Size GenerationGetChunkSpace(void *pointer);
46 extern bool GenerationIsEmpty(MemoryContext context);
47 extern void GenerationStats(MemoryContext context,
48 MemoryStatsPrintFunc printfunc, void *passthru,
49 MemoryContextCounters *totals,
50 bool print_to_stderr);
51 #ifdef MEMORY_CONTEXT_CHECKING
52 extern void GenerationCheck(MemoryContext context);
53 #endif
56 /* These functions implement the MemoryContext API for Slab context. */
57 extern void *SlabAlloc(MemoryContext context, Size size);
58 extern void SlabFree(void *pointer);
59 extern void *SlabRealloc(void *pointer, Size size);
60 extern void SlabReset(MemoryContext context);
61 extern void SlabDelete(MemoryContext context);
62 extern MemoryContext SlabGetChunkContext(void *pointer);
63 extern Size SlabGetChunkSpace(void *pointer);
64 extern bool SlabIsEmpty(MemoryContext context);
65 extern void SlabStats(MemoryContext context,
66 MemoryStatsPrintFunc printfunc, void *passthru,
67 MemoryContextCounters *totals,
68 bool print_to_stderr);
69 #ifdef MEMORY_CONTEXT_CHECKING
70 extern void SlabCheck(MemoryContext context);
71 #endif
74 * MemoryContextMethodID
75 * A unique identifier for each MemoryContext implementation which
76 * indicates the index into the mcxt_methods[] array. See mcxt.c.
78 typedef enum MemoryContextMethodID
80 MCTX_ASET_ID,
81 MCTX_GENERATION_ID,
82 MCTX_SLAB_ID,
83 } MemoryContextMethodID;
86 * The number of bits that 8-byte memory chunk headers can use to encode the
87 * MemoryContextMethodID.
89 #define MEMORY_CONTEXT_METHODID_BITS 3
90 #define MEMORY_CONTEXT_METHODID_MASK \
91 UINT64CONST((1 << MEMORY_CONTEXT_METHODID_BITS) - 1)
94 * This routine handles the context-type-independent part of memory
95 * context creation. It's intended to be called from context-type-
96 * specific creation routines, and noplace else.
98 extern void MemoryContextCreate(MemoryContext node,
99 NodeTag tag,
100 MemoryContextMethodID method_id,
101 MemoryContext parent,
102 const char *name);
105 * GetMemoryChunkMethodID
106 * Return the MemoryContextMethodID from the uint64 chunk header which
107 * directly precedes 'pointer'.
109 static inline MemoryContextMethodID
110 GetMemoryChunkMethodID(void *pointer)
112 uint64 header;
115 * Try to detect bogus pointers handed to us, poorly though we can.
116 * Presumably, a pointer that isn't MAXALIGNED isn't pointing at an
117 * allocated chunk.
119 Assert(pointer != NULL);
120 Assert(pointer == (void *) MAXALIGN(pointer));
122 header = *((uint64 *) ((char *) pointer - sizeof(uint64)));
124 return (MemoryContextMethodID) (header & MEMORY_CONTEXT_METHODID_MASK);
127 #endif /* MEMUTILS_INTERNAL_H */