PostgreSQL Source Code: src/include/utils/memutils.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifndef MEMUTILS_H
18#define MEMUTILS_H
19
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#define MaxAllocSize ((Size) 0x3fffffff)
44
45#define AllocSizeIsValid(size) ((Size) (size) <= MaxAllocSize)
46
47
48#define MaxAllocHugeSize (SIZE_MAX / 2)
49
50#define InvalidAllocSize SIZE_MAX
51
52#define AllocHugeSizeIsValid(size) ((Size) (size) <= MaxAllocHugeSize)
53
54
55
56
57
58
59#define MEMORY_CONTEXT_IDENT_SHMEM_SIZE 64
60
61#define MEMORY_CONTEXT_REPORT_MAX_PER_BACKEND ((size_t) (1 * 1024 * 1024))
62
63
64
65
66
67
68
69#define MAX_MEMORY_CONTEXT_STATS_SIZE (sizeof(MemoryStatsEntry) + \
70 (100 * sizeof(int)) + (2 * MEMORY_CONTEXT_IDENT_SHMEM_SIZE))
71
72
73
74
75
76
77
85
86
88
89
90
91
92
111 int max_level, int max_children,
112 bool print_to_stderr);
114 bool allow);
115
116#ifdef MEMORY_CONTEXT_CHECKING
117extern void MemoryContextCheck(MemoryContext context);
118#endif
119
120
121#define MemoryContextCopyAndSetIdentifier(cxt, id) \
122 MemoryContextSetIdentifier(cxt, MemoryContextStrdup(cxt, id))
123
126
127
128
129
130
131
133 const char *name,
134 Size minContextSize,
135 Size initBlockSize,
136 Size maxBlockSize);
137
138
139
140
141
142
143#ifdef HAVE__BUILTIN_CONSTANT_P
144#define AllocSetContextCreate(parent, name, ...) \
145 (StaticAssertExpr(__builtin_constant_p(name), \
146 "memory context names must be constant strings"), \
147 AllocSetContextCreateInternal(parent, name, __VA_ARGS__))
148#else
149#define AllocSetContextCreate \
150 AllocSetContextCreateInternal
151#endif
152
153
155 const char *name,
156 Size blockSize,
157 Size chunkSize);
158
159
161 const char *name,
162 Size minContextSize,
163 Size initBlockSize,
164 Size maxBlockSize);
165
166
168 const char *name,
169 Size minContextSize,
170 Size initBlockSize,
171 Size maxBlockSize);
172
173
174
175
176
177#define ALLOCSET_DEFAULT_MINSIZE 0
178#define ALLOCSET_DEFAULT_INITSIZE (8 * 1024)
179#define ALLOCSET_DEFAULT_MAXSIZE (8 * 1024 * 1024)
180#define ALLOCSET_DEFAULT_SIZES \
181 ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
182
183
184
185
186
187#define ALLOCSET_SMALL_MINSIZE 0
188#define ALLOCSET_SMALL_INITSIZE (1 * 1024)
189#define ALLOCSET_SMALL_MAXSIZE (8 * 1024)
190#define ALLOCSET_SMALL_SIZES \
191 ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE
192
193
194
195
196
197#define ALLOCSET_START_SMALL_SIZES \
198 ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE
199
200
201
202
203
204
205
206
207#define ALLOCSET_SEPARATE_THRESHOLD 8192
208
209#define SLAB_DEFAULT_BLOCK_SIZE (8 * 1024)
210#define SLAB_LARGE_BLOCK_SIZE (8 * 1024 * 1024)
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238static inline bool
240{
241 const unsigned char *p = (const unsigned char *) ptr;
242 const unsigned char *end = &p[len];
243 const unsigned char *aligned_end = (const unsigned char *)
244 ((uintptr_t) end & (~(sizeof(size_t) - 1)));
245
246 if (len < sizeof(size_t))
247 {
248 while (p < end)
249 {
250 if (*p++ != 0)
251 return false;
252 }
253 return true;
254 }
255
256
257 if (len < sizeof(size_t) * 8)
258 {
259
260 while (((uintptr_t) p & (sizeof(size_t) - 1)) != 0)
261 {
262 if (p == end)
263 return true;
264 if (*p++ != 0)
265 return false;
266 }
267
268
269
270
271
272
273
274 for (; p < aligned_end; p += sizeof(size_t))
275 {
276 if (*(size_t *) p != 0)
277 return false;
278 }
279
280
281 while (p < end)
282 {
283 if (*p++ != 0)
284 return false;
285 }
286 return true;
287 }
288
289
290
291
292 while (((uintptr_t) p & (sizeof(size_t) - 1)) != 0)
293 {
294 if (p == end)
295 return true;
296
297 if (*p++ != 0)
298 return false;
299 }
300
301
302
303
304
305
306
307
308
309
310
311 for (; p < aligned_end - (sizeof(size_t) * 7); p += sizeof(size_t) * 8)
312 {
313 if ((((size_t *) p)[0] != 0) | (((size_t *) p)[1] != 0) |
314 (((size_t *) p)[2] != 0) | (((size_t *) p)[3] != 0) |
315 (((size_t *) p)[4] != 0) | (((size_t *) p)[5] != 0) |
316 (((size_t *) p)[6] != 0) | (((size_t *) p)[7] != 0))
317 return false;
318 }
319
320
321
322
323
324
325
326 for (; p < aligned_end; p += sizeof(size_t))
327 {
328 if (*(size_t *) p != 0)
329 return false;
330 }
331
332
333 while (p < end)
334 {
335 if (*p++ != 0)
336 return false;
337 }
338
339 return true;
340}
341
342
344{
357
358
359
360
361
362
363
365{
369
370
371
372
373
375{
384
385
386
387
388
390{
394
404#endif
void HandleGetMemoryContextInterrupt(void)
PGDLLIMPORT MemoryContext CurTransactionContext
bool MemoryContextIsEmpty(MemoryContext context)
void MemoryContextMemConsumed(MemoryContext context, MemoryContextCounters *consumed)
void MemoryContextReset(MemoryContext context)
PGDLLIMPORT MemoryContext MessageContext
struct MemoryStatsBackendState MemoryStatsBackendState
Size MemoryContextReportingShmemSize(void)
PGDLLIMPORT MemoryContext TopMemoryContext
PGDLLIMPORT MemoryContext TopTransactionContext
PGDLLIMPORT dsa_area * MemoryStatsDsaArea
void HandleLogMemoryContextInterrupt(void)
const char * ContextTypeToString(NodeTag type)
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
void AtProcExit_memstats_cleanup(int code, Datum arg)
Size GetMemoryChunkSpace(void *pointer)
void ProcessGetMemoryContextInterrupt(void)
void MemoryContextDeleteChildren(MemoryContext context)
MemoryContext GetMemoryChunkContext(void *pointer)
PGDLLIMPORT MemoryContext PostmasterContext
void MemoryContextStatsDetail(MemoryContext context, int max_level, int max_children, bool print_to_stderr)
Size MemoryContextMemAllocated(MemoryContext context, bool recurse)
void MemoryContextStats(MemoryContext context)
MemoryContext SlabContextCreate(MemoryContext parent, const char *name, Size blockSize, Size chunkSize)
void MemoryContextInit(void)
MemoryContext BumpContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
PGDLLIMPORT MemoryContext CacheMemoryContext
PGDLLIMPORT MemoryStatsBackendState * memCxtState
struct MemoryStatsContextId MemoryStatsContextId
struct MemoryStatsCtl MemoryStatsCtl
MemoryContext MemoryContextGetParent(MemoryContext context)
void ProcessLogMemoryContextInterrupt(void)
void MemoryContextReportingShmemInit(void)
struct MemoryStatsEntry MemoryStatsEntry
PGDLLIMPORT MemoryContext PortalContext
MemoryContext GenerationContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
static bool pg_memory_is_all_zeros(const void *ptr, size_t len)
void MemoryContextDelete(MemoryContext context)
void MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)
PGDLLIMPORT MemoryStatsCtl * memCxtArea
void MemoryContextResetChildren(MemoryContext context)
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
void MemoryContextResetOnly(MemoryContext context)
PGDLLIMPORT MemoryContext ErrorContext
MemoryContext AllocSetContextCreateInternal(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
TimestampTz stats_timestamp
ConditionVariable memcxt_cv
dsa_pointer memstats_dsa_pointer
dsa_handle memstats_dsa_handle