Debian Codegraph @

src/backend/utils/mmgr/mcxt.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * mcxt.c
4 * POSTGRES memory context management code.
5 *
6 * This module handles context management operations that are independent
7 * of the particular kind of context being operated on. It calls
8 * context-type-specific operations via the function pointers in a
9 * context's MemoryContextMethods struct.
10 *
11 *
12 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
14 *
15 *
16 * IDENTIFICATION
17 * src/backend/utils/mmgr/mcxt.c
18 *
19 *-------------------------------------------------------------------------
20 */
21
22 #include "postgres.h"
23
24 #include "common/int.h"
25 #include "mb/pg_wchar.h"
26 #include "miscadmin.h"
27 #include "utils/memdebug.h"
28 #include "utils/memutils.h"
29 #include "utils/memutils_internal.h"
30 #include "utils/memutils_memorychunk.h"
31
32
33 static void BogusFree(void *pointer);
34 static void *BogusRealloc(void *pointer, Size size, int flags);
35 static MemoryContext BogusGetChunkContext(void *pointer);
36 static Size BogusGetChunkSpace(void *pointer);
37
38 /*****************************************************************************
39 * GLOBAL MEMORY *
40 *****************************************************************************/
41 #define BOGUS_MCTX(id) \
42 [id].free_p = BogusFree, \
43 [id].realloc = BogusRealloc, \
44 [id].get_chunk_context = BogusGetChunkContext, \
45 [id].get_chunk_space = BogusGetChunkSpace
46
47 static const MemoryContextMethods mcxt_methods[] = {
48 /* aset.c */
49 [MCTX_ASET_ID].alloc = AllocSetAlloc,
50 [MCTX_ASET_ID].free_p = AllocSetFree,
51 [MCTX_ASET_ID].realloc = AllocSetRealloc,
52 [MCTX_ASET_ID].reset = AllocSetReset,
53 [MCTX_ASET_ID].delete_context = AllocSetDelete,
54 [MCTX_ASET_ID].get_chunk_context = AllocSetGetChunkContext,
55 [MCTX_ASET_ID].get_chunk_space = AllocSetGetChunkSpace,
56 [MCTX_ASET_ID].is_empty = AllocSetIsEmpty,
57 [MCTX_ASET_ID].stats = AllocSetStats,
58 #ifdef MEMORY_CONTEXT_CHECKING
59 [MCTX_ASET_ID].check = AllocSetCheck,
60 #endif
61
62 /* generation.c */
63 [MCTX_GENERATION_ID].alloc = GenerationAlloc,
64 [MCTX_GENERATION_ID].free_p = GenerationFree,
65 [MCTX_GENERATION_ID].realloc = GenerationRealloc,
66 [MCTX_GENERATION_ID].reset = GenerationReset,
67 [MCTX_GENERATION_ID].delete_context = GenerationDelete,
68 [MCTX_GENERATION_ID].get_chunk_context = GenerationGetChunkContext,
69 [MCTX_GENERATION_ID].get_chunk_space = GenerationGetChunkSpace,
70 [MCTX_GENERATION_ID].is_empty = GenerationIsEmpty,
71 [MCTX_GENERATION_ID].stats = GenerationStats,
72 #ifdef MEMORY_CONTEXT_CHECKING
73 [MCTX_GENERATION_ID].check = GenerationCheck,
74 #endif
75
76 /* slab.c */
77 [MCTX_SLAB_ID].alloc = SlabAlloc,
78 [MCTX_SLAB_ID].free_p = SlabFree,
79 [MCTX_SLAB_ID].realloc = SlabRealloc,
80 [MCTX_SLAB_ID].reset = SlabReset,
81 [MCTX_SLAB_ID].delete_context = SlabDelete,
82 [MCTX_SLAB_ID].get_chunk_context = SlabGetChunkContext,
83 [MCTX_SLAB_ID].get_chunk_space = SlabGetChunkSpace,
84 [MCTX_SLAB_ID].is_empty = SlabIsEmpty,
85 [MCTX_SLAB_ID].stats = SlabStats,
86 #ifdef MEMORY_CONTEXT_CHECKING
87 [MCTX_SLAB_ID].check = SlabCheck,
88 #endif
89
90 /* alignedalloc.c */
91 [MCTX_ALIGNED_REDIRECT_ID].alloc = NULL, /* not required */
92 [MCTX_ALIGNED_REDIRECT_ID].free_p = AlignedAllocFree,
93 [MCTX_ALIGNED_REDIRECT_ID].realloc = AlignedAllocRealloc,
94 [MCTX_ALIGNED_REDIRECT_ID].reset = NULL, /* not required */
95 [MCTX_ALIGNED_REDIRECT_ID].delete_context = NULL, /* not required */
96 [MCTX_ALIGNED_REDIRECT_ID].get_chunk_context = AlignedAllocGetChunkContext,
97 [MCTX_ALIGNED_REDIRECT_ID].get_chunk_space = AlignedAllocGetChunkSpace,
98 [MCTX_ALIGNED_REDIRECT_ID].is_empty = NULL, /* not required */
99 [MCTX_ALIGNED_REDIRECT_ID].stats = NULL, /* not required */
100 #ifdef MEMORY_CONTEXT_CHECKING
101 [MCTX_ALIGNED_REDIRECT_ID].check = NULL, /* not required */
102 #endif
103
104 /* bump.c */
105 [MCTX_BUMP_ID].alloc = BumpAlloc,
106 [MCTX_BUMP_ID].free_p = BumpFree,
107 [MCTX_BUMP_ID].realloc = BumpRealloc,
108 [MCTX_BUMP_ID].reset = BumpReset,
109 [MCTX_BUMP_ID].delete_context = BumpDelete,
110 [MCTX_BUMP_ID].get_chunk_context = BumpGetChunkContext,
111 [MCTX_BUMP_ID].get_chunk_space = BumpGetChunkSpace,
112 [MCTX_BUMP_ID].is_empty = BumpIsEmpty,
113 [MCTX_BUMP_ID].stats = BumpStats,
114 #ifdef MEMORY_CONTEXT_CHECKING
115 [MCTX_BUMP_ID].check = BumpCheck,
116 #endif
117
118
119 /*
120 * Reserved and unused IDs should have dummy entries here. This allows us
121 * to fail cleanly if a bogus pointer is passed to pfree or the like. It
122 * seems sufficient to provide routines for the methods that might get
123 * invoked from inspection of a chunk (see MCXT_METHOD calls below).
124 */
125 BOGUS_MCTX(MCTX_1_RESERVED_GLIBC_ID),
126 BOGUS_MCTX(MCTX_2_RESERVED_GLIBC_ID),
127 BOGUS_MCTX(MCTX_8_UNUSED_ID),
128 BOGUS_MCTX(MCTX_9_UNUSED_ID),
129 BOGUS_MCTX(MCTX_10_UNUSED_ID),
130 BOGUS_MCTX(MCTX_11_UNUSED_ID),
131 BOGUS_MCTX(MCTX_12_UNUSED_ID),
132 BOGUS_MCTX(MCTX_13_UNUSED_ID),
133 BOGUS_MCTX(MCTX_14_UNUSED_ID),
134 BOGUS_MCTX(MCTX_0_RESERVED_UNUSEDMEM_ID),
135 BOGUS_MCTX(MCTX_15_RESERVED_WIPEDMEM_ID)
136 };
137
138 #undef BOGUS_MCTX
139
140 /*
141 * CurrentMemoryContext
142 * Default memory context for allocations.
143 */
144 MemoryContext CurrentMemoryContext = NULL;
145
146 /*
147 * Standard top-level contexts. For a description of the purpose of each
148 * of these contexts, refer to src/backend/utils/mmgr/README
149 */
150 MemoryContext TopMemoryContext = NULL;
151 MemoryContext ErrorContext = NULL;
152 MemoryContext PostmasterContext = NULL;
153 MemoryContext CacheMemoryContext = NULL;
154 MemoryContext MessageContext = NULL;
155 MemoryContext TopTransactionContext = NULL;
156 MemoryContext CurTransactionContext = NULL;
157
158 /* This is a transient link to the active portal's memory context: */
159 MemoryContext PortalContext = NULL;
160
161 /* Is memory context logging currently in progress? */
162 static bool LogMemoryContextInProgress = false;
163
164 static void MemoryContextDeleteOnly(MemoryContext context);
165 static void MemoryContextCallResetCallbacks(MemoryContext context);
166 static void MemoryContextStatsInternal(MemoryContext context, int level,
167 int max_level, int max_children,
168 MemoryContextCounters *totals,
169 bool print_to_stderr);
170 static void MemoryContextStatsPrint(MemoryContext context, void *passthru,
171 const char *stats_string,
172 bool print_to_stderr);
173 pg_noreturn static pg_noinline void add_size_error(Size s1, Size s2);
174 pg_noreturn static pg_noinline void mul_size_error(Size s1, Size s2);
175
176 /*
177 * You should not do memory allocations within a critical section, because
178 * an out-of-memory error will be escalated to a PANIC. To enforce that
179 * rule, the allocation functions Assert that.
180 */
181 #define AssertNotInCriticalSection(context) \
182 Assert(CritSectionCount == 0 || (context)->allowInCritSection)
183
184 /*
185 * Call the given function in the MemoryContextMethods for the memory context
186 * type that 'pointer' belongs to.
187 */
188 #define MCXT_METHOD(pointer, method) \
189 mcxt_methods[GetMemoryChunkMethodID(pointer)].method
190
191 /*
192 * GetMemoryChunkMethodID
193 * Return the MemoryContextMethodID from the uint64 chunk header which
194 * directly precedes 'pointer'.
195 */
196 static inline MemoryContextMethodID
197 GetMemoryChunkMethodID(const void *pointer)
198 {
199 uint64 header;
200
201 /*
202 * Try to detect bogus pointers handed to us, poorly though we can.
203 * Presumably, a pointer that isn't MAXALIGNED isn't pointing at an
204 * allocated chunk.
205 */
206 Assert(pointer == (const void *) MAXALIGN(pointer));
207
208 /* Allow access to the uint64 header */
209 VALGRIND_MAKE_MEM_DEFINED((char *) pointer - sizeof(uint64), sizeof(uint64));
210
211 header = *((const uint64 *) ((const char *) pointer - sizeof(uint64)));
212
213 /* Disallow access to the uint64 header */
214 VALGRIND_MAKE_MEM_NOACCESS((char *) pointer - sizeof(uint64), sizeof(uint64));
215
216 return (MemoryContextMethodID) (header & MEMORY_CONTEXT_METHODID_MASK);
217 }
218
219 /*
220 * GetMemoryChunkHeader
221 * Return the uint64 chunk header which directly precedes 'pointer'.
222 *
223 * This is only used after GetMemoryChunkMethodID, so no need for error checks.
224 */
225 static inline uint64
226 GetMemoryChunkHeader(const void *pointer)
227 {
228 uint64 header;
229
230 /* Allow access to the uint64 header */
231 VALGRIND_MAKE_MEM_DEFINED((char *) pointer - sizeof(uint64), sizeof(uint64));
232
233 header = *((const uint64 *) ((const char *) pointer - sizeof(uint64)));
234
235 /* Disallow access to the uint64 header */
236 VALGRIND_MAKE_MEM_NOACCESS((char *) pointer - sizeof(uint64), sizeof(uint64));
237
238 return header;
239 }
240
241 /*
242 * MemoryContextTraverseNext
243 * Helper function to traverse all descendants of a memory context
244 * without recursion.
245 *
246 * Recursion could lead to out-of-stack errors with deep context hierarchies,
247 * which would be unpleasant in error cleanup code paths.
248 *
249 * To process 'context' and all its descendants, use a loop like this:
250 *
251 * <process 'context'>
252 * for (MemoryContext curr = context->firstchild;
253 * curr != NULL;
254 * curr = MemoryContextTraverseNext(curr, context))
255 * {
256 * <process 'curr'>
257 * }
258 *
259 * This visits all the contexts in pre-order, that is a node is visited
260 * before its children.
261 */
262 static MemoryContext
263 MemoryContextTraverseNext(MemoryContext curr, MemoryContext top)
264 {
265 /* After processing a node, traverse to its first child if any */
266 if (curr->firstchild != NULL)
267 return curr->firstchild;
268
269 /*
270 * After processing a childless node, traverse to its next sibling if
271 * there is one. If there isn't, traverse back up to the parent (which
272 * has already been visited, and now so have all its descendants). We're
273 * done if that is "top", otherwise traverse to its next sibling if any,
274 * otherwise repeat moving up.
275 */
276 while (curr->nextchild == NULL)
277 {
278 curr = curr->parent;
279 if (curr == top)
280 return NULL;
281 }
282 return curr->nextchild;
283 }
284
285 /*
286 * Support routines to trap use of invalid memory context method IDs
287 * (from calling pfree or the like on a bogus pointer). As a possible
288 * aid in debugging, we report the header word along with the pointer
289 * address (if we got here, there must be an accessible header word).
290 */
291 static void
292 BogusFree(void *pointer)
293 {
294 elog(ERROR, "pfree called with invalid pointer %p (header 0x%016" PRIx64 ")",
295 pointer, GetMemoryChunkHeader(pointer));
296 }
297
298 static void *
299 BogusRealloc(void *pointer, Size size, int flags)
300 {
301 elog(ERROR, "repalloc called with invalid pointer %p (header 0x%016" PRIx64 ")",
302 pointer, GetMemoryChunkHeader(pointer));
303 return NULL; /* keep compiler quiet */
304 }
305
306 static MemoryContext
307 BogusGetChunkContext(void *pointer)
308 {
309 elog(ERROR, "GetMemoryChunkContext called with invalid pointer %p (header 0x%016" PRIx64 ")",
310 pointer, GetMemoryChunkHeader(pointer));
311 return NULL; /* keep compiler quiet */
312 }
313
314 static Size
315 BogusGetChunkSpace(void *pointer)
316 {
317 elog(ERROR, "GetMemoryChunkSpace called with invalid pointer %p (header 0x%016" PRIx64 ")",
318 pointer, GetMemoryChunkHeader(pointer));
319 return 0; /* keep compiler quiet */
320 }
321
322
323 /*****************************************************************************
324 * EXPORTED ROUTINES *
325 *****************************************************************************/
326
327
328 /*
329 * MemoryContextInit
330 * Start up the memory-context subsystem.
331 *
332 * This must be called before creating contexts or allocating memory in
333 * contexts. TopMemoryContext and ErrorContext are initialized here;
334 * other contexts must be created afterwards.
335 *
336 * In normal multi-backend operation, this is called once during
337 * postmaster startup, and not at all by individual backend startup
338 * (since the backends inherit an already-initialized context subsystem
339 * by virtue of being forked off the postmaster). But in an EXEC_BACKEND
340 * build, each process must do this for itself.
341 *
342 * In a standalone backend this must be called during backend startup.
343 */
344 void
345 MemoryContextInit(void)
346 {
347 Assert(TopMemoryContext == NULL);
348
349 /*
350 * First, initialize TopMemoryContext, which is the parent of all others.
351 */
352 TopMemoryContext = AllocSetContextCreate((MemoryContext) NULL,
353 "TopMemoryContext",
354 ALLOCSET_DEFAULT_SIZES);
355
356 /*
357 * Not having any other place to point CurrentMemoryContext, make it point
358 * to TopMemoryContext. Caller should change this soon!
359 */
360 CurrentMemoryContext = TopMemoryContext;
361
362 /*
363 * Initialize ErrorContext as an AllocSetContext with slow growth rate ---
364 * we don't really expect much to be allocated in it. More to the point,
365 * require it to contain at least 8K at all times. This is the only case
366 * where retained memory in a context is *essential* --- we want to be
367 * sure ErrorContext still has some memory even if we've run out
368 * elsewhere! Also, allow allocations in ErrorContext within a critical
369 * section. Otherwise a PANIC will cause an assertion failure in the error
370 * reporting code, before printing out the real cause of the failure.
371 *
372 * This should be the last step in this function, as elog.c assumes memory
373 * management works once ErrorContext is non-null.
374 */
375 ErrorContext = AllocSetContextCreate(TopMemoryContext,
376 "ErrorContext",
377 8 * 1024,
378 8 * 1024,
379 8 * 1024);
380 MemoryContextAllowInCriticalSection(ErrorContext, true);
381 }
382
383 /*
384 * MemoryContextReset
385 * Release all space allocated within a context and delete all its
386 * descendant contexts (but not the named context itself).
387 */
388 void
389 MemoryContextReset(MemoryContext context)
390 {
391 Assert(MemoryContextIsValid(context));
392
393 /* save a function call in common case where there are no children */
394 if (context->firstchild != NULL)
395 MemoryContextDeleteChildren(context);
396
397 /* save a function call if no pallocs since startup or last reset */
398 if (!context->isReset)
399 MemoryContextResetOnly(context);
400 }
401
402 /*
403 * MemoryContextResetOnly
404 * Release all space allocated within a context.
405 * Nothing is done to the context's descendant contexts.
406 */
407 void
408 MemoryContextResetOnly(MemoryContext context)
409 {
410 Assert(MemoryContextIsValid(context));
411
412 /* Nothing to do if no pallocs since startup or last reset */
413 if (!context->isReset)
414 {
415 MemoryContextCallResetCallbacks(context);
416
417 /*
418 * If context->ident points into the context's memory, it will become
419 * a dangling pointer. We could prevent that by setting it to NULL
420 * here, but that would break valid coding patterns that keep the
421 * ident elsewhere, e.g. in a parent context. So for now we assume
422 * the programmer got it right.
423 */
424
425 context->methods->reset(context);
426 context->isReset = true;
427 VALGRIND_DESTROY_MEMPOOL(context);
428 VALGRIND_CREATE_MEMPOOL(context, 0, false);
429 }
430 }
431
432 /*
433 * MemoryContextResetChildren
434 * Release all space allocated within a context's descendants,
435 * but don't delete the contexts themselves. The named context
436 * itself is not touched.
437 */
438 void
439 MemoryContextResetChildren(MemoryContext context)
440 {
441 Assert(MemoryContextIsValid(context));
442
443 for (MemoryContext curr = context->firstchild;
444 curr != NULL;
445 curr = MemoryContextTraverseNext(curr, context))
446 {
447 MemoryContextResetOnly(curr);
448 }
449 }
450
451 /*
452 * MemoryContextDelete
453 * Delete a context and its descendants, and release all space
454 * allocated therein.
455 *
456 * The type-specific delete routine removes all storage for the context,
457 * but we have to deal with descendant nodes here.
458 */
459 void
460 MemoryContextDelete(MemoryContext context)
461 {
462 MemoryContext curr;
463
464 Assert(MemoryContextIsValid(context));
465
466 /*
467 * Delete subcontexts from the bottom up.
468 *
469 * Note: Do not use recursion here. A "stack depth limit exceeded" error
470 * would be unpleasant if we're already in the process of cleaning up from
471 * transaction abort. We also cannot use MemoryContextTraverseNext() here
472 * because we modify the tree as we go.
473 */
474 curr = context;
475 for (;;)
476 {
477 MemoryContext parent;
478
479 /* Descend down until we find a leaf context with no children */
480 while (curr->firstchild != NULL)
481 curr = curr->firstchild;
482
483 /*
484 * We're now at a leaf with no children. Free it and continue from the
485 * parent. Or if this was the original node, we're all done.
486 */
487 parent = curr->parent;
488 MemoryContextDeleteOnly(curr);
489
490 if (curr == context)
491 break;
492 curr = parent;
493 }
494 }
495
496 /*
497 * Subroutine of MemoryContextDelete,
498 * to delete a context that has no children.
499 * We must also delink the context from its parent, if it has one.
500 */
501 static void
502 MemoryContextDeleteOnly(MemoryContext context)
503 {
504 Assert(MemoryContextIsValid(context));
505 /* We had better not be deleting TopMemoryContext ... */
506 Assert(context != TopMemoryContext);
507 /* And not CurrentMemoryContext, either */
508 Assert(context != CurrentMemoryContext);
509 /* All the children should've been deleted already */
510 Assert(context->firstchild == NULL);
511
512 /*
513 * It's not entirely clear whether 'tis better to do this before or after
514 * delinking the context; but an error in a callback will likely result in
515 * leaking the whole context (if it's not a root context) if we do it
516 * after, so let's do it before.
517 */
518 MemoryContextCallResetCallbacks(context);
519
520 /*
521 * We delink the context from its parent before deleting it, so that if
522 * there's an error we won't have deleted/busted contexts still attached
523 * to the context tree. Better a leak than a crash.
524 */
525 MemoryContextSetParent(context, NULL);
526
527 /*
528 * Also reset the context's ident pointer, in case it points into the
529 * context. This would only matter if someone tries to get stats on the
530 * (already unlinked) context, which is unlikely, but let's be safe.
531 */
532 context->ident = NULL;
533
534 context->methods->delete_context(context);
535
536 VALGRIND_DESTROY_MEMPOOL(context);
537 }
538
539 /*
540 * MemoryContextDeleteChildren
541 * Delete all the descendants of the named context and release all
542 * space allocated therein. The named context itself is not touched.
543 */
544 void
545 MemoryContextDeleteChildren(MemoryContext context)
546 {
547 Assert(MemoryContextIsValid(context));
548
549 /*
550 * MemoryContextDelete will delink the child from me, so just iterate as
551 * long as there is a child.
552 */
553 while (context->firstchild != NULL)
554 MemoryContextDelete(context->firstchild);
555 }
556
557 /*
558 * MemoryContextRegisterResetCallback
559 * Register a function to be called before next context reset/delete.
560 * Such callbacks will be called in reverse order of registration.
561 *
562 * The caller is responsible for allocating a MemoryContextCallback struct
563 * to hold the info about this callback request, and for filling in the
564 * "func" and "arg" fields in the struct to show what function to call with
565 * what argument. Typically the callback struct should be allocated within
566 * the specified context, since that means it will automatically be freed
567 * when no longer needed.
568 *
569 * There is no API for deregistering a callback once registered. If you
570 * want it to not do anything anymore, adjust the state pointed to by its
571 * "arg" to indicate that.
572 */
573 void
574 MemoryContextRegisterResetCallback(MemoryContext context,
575 MemoryContextCallback *cb)
576 {
577 Assert(MemoryContextIsValid(context));
578
579 /* Push onto head so this will be called before older registrants. */
580 cb->next = context->reset_cbs;
581 context->reset_cbs = cb;
582 /* Mark the context as non-reset (it probably is already). */
583 context->isReset = false;
584 }
585
586 /*
587 * MemoryContextCallResetCallbacks
588 * Internal function to call all registered callbacks for context.
589 */
590 static void
591 MemoryContextCallResetCallbacks(MemoryContext context)
592 {
593 MemoryContextCallback *cb;
594
595 /*
596 * We pop each callback from the list before calling. That way, if an
597 * error occurs inside the callback, we won't try to call it a second time
598 * in the likely event that we reset or delete the context later.
599 */
600 while ((cb = context->reset_cbs) != NULL)
601 {
602 context->reset_cbs = cb->next;
603 cb->func(cb->arg);
604 }
605 }
606
607 /*
608 * MemoryContextSetIdentifier
609 * Set the identifier string for a memory context.
610 *
611 * An identifier can be provided to help distinguish among different contexts
612 * of the same kind in memory context stats dumps. The identifier string
613 * must live at least as long as the context it is for; typically it is
614 * allocated inside that context, so that it automatically goes away on
615 * context deletion. Pass id = NULL to forget any old identifier.
616 */
617 void
618 MemoryContextSetIdentifier(MemoryContext context, const char *id)
619 {
620 Assert(MemoryContextIsValid(context));
621 context->ident = id;
622 }
623
624 /*
625 * MemoryContextSetParent
626 * Change a context to belong to a new parent (or no parent).
627 *
628 * We provide this as an API function because it is sometimes useful to
629 * change a context's lifespan after creation. For example, a context
630 * might be created underneath a transient context, filled with data,
631 * and then reparented underneath CacheMemoryContext to make it long-lived.
632 * In this way no special effort is needed to get rid of the context in case
633 * a failure occurs before its contents are completely set up.
634 *
635 * Callers often assume that this function cannot fail, so don't put any
636 * elog(ERROR) calls in it.
637 *
638 * A possible caller error is to reparent a context under itself, creating
639 * a loop in the context graph. We assert here that context != new_parent,
640 * but checking for multi-level loops seems more trouble than it's worth.
641 */
642 void
643 MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
644 {
645 Assert(MemoryContextIsValid(context));
646 Assert(context != new_parent);
647
648 /* Fast path if it's got correct parent already */
649 if (new_parent == context->parent)
650 return;
651
652 /* Delink from existing parent, if any */
653 if (context->parent)
654 {
655 MemoryContext parent = context->parent;
656
657 if (context->prevchild != NULL)
658 context->prevchild->nextchild = context->nextchild;
659 else
660 {
661 Assert(parent->firstchild == context);
662 parent->firstchild = context->nextchild;
663 }
664
665 if (context->nextchild != NULL)
666 context->nextchild->prevchild = context->prevchild;
667 }
668
669 /* And relink */
670 if (new_parent)
671 {
672 Assert(MemoryContextIsValid(new_parent));
673 context->parent = new_parent;
674 context->prevchild = NULL;
675 context->nextchild = new_parent->firstchild;
676 if (new_parent->firstchild != NULL)
677 new_parent->firstchild->prevchild = context;
678 new_parent->firstchild = context;
679 }
680 else
681 {
682 context->parent = NULL;
683 context->prevchild = NULL;
684 context->nextchild = NULL;
685 }
686 }
687
688 /*
689 * MemoryContextAllowInCriticalSection
690 * Allow/disallow allocations in this memory context within a critical
691 * section.
692 *
693 * Normally, memory allocations are not allowed within a critical section,
694 * because a failure would lead to PANIC. There are a few exceptions to
695 * that, like allocations related to debugging code that is not supposed to
696 * be enabled in production. This function can be used to exempt specific
697 * memory contexts from the assertion in palloc().
698 */
699 void
700 MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)
701 {
702 Assert(MemoryContextIsValid(context));
703
704 context->allowInCritSection = allow;
705 }
706
707 /*
708 * GetMemoryChunkContext
709 * Given a currently-allocated chunk, determine the MemoryContext that
710 * the chunk belongs to.
711 */
712 MemoryContext
713 GetMemoryChunkContext(void *pointer)
714 {
715 return MCXT_METHOD(pointer, get_chunk_context) (pointer);
716 }
717
718 /*
719 * GetMemoryChunkSpace
720 * Given a currently-allocated chunk, determine the total space
721 * it occupies (including all memory-allocation overhead).
722 *
723 * This is useful for measuring the total space occupied by a set of
724 * allocated chunks.
725 */
726 Size
727 GetMemoryChunkSpace(void *pointer)
728 {
729 return MCXT_METHOD(pointer, get_chunk_space) (pointer);
730 }
731
732 /*
733 * MemoryContextGetParent
734 * Get the parent context (if any) of the specified context
735 */
736 MemoryContext
737 MemoryContextGetParent(MemoryContext context)
738 {
739 Assert(MemoryContextIsValid(context));
740
741 return context->parent;
742 }
743
744 /*
745 * MemoryContextIsEmpty
746 * Is a memory context empty of any allocated space?
747 */
748 bool
749 MemoryContextIsEmpty(MemoryContext context)
750 {
751 Assert(MemoryContextIsValid(context));
752
753 /*
754 * For now, we consider a memory context nonempty if it has any children;
755 * perhaps this should be changed later.
756 */
757 if (context->firstchild != NULL)
758 return false;
759 /* Otherwise use the type-specific inquiry */
760 return context->methods->is_empty(context);
761 }
762
763 /*
764 * Find the memory allocated to blocks for this memory context. If recurse is
765 * true, also include children.
766 */
767 Size
768 MemoryContextMemAllocated(MemoryContext context, bool recurse)
769 {
770 Size total = context->mem_allocated;
771
772 Assert(MemoryContextIsValid(context));
773
774 if (recurse)
775 {
776 for (MemoryContext curr = context->firstchild;
777 curr != NULL;
778 curr = MemoryContextTraverseNext(curr, context))
779 {
780 total += curr->mem_allocated;
781 }
782 }
783
784 return total;
785 }
786
787 /*
788 * Return the memory consumption statistics about the given context and its
789 * children.
790 */
791 void
792 MemoryContextMemConsumed(MemoryContext context,
793 MemoryContextCounters *consumed)
794 {
795 Assert(MemoryContextIsValid(context));
796
797 memset(consumed, 0, sizeof(*consumed));
798
799 /* Examine the context itself */
800 context->methods->stats(context, NULL, NULL, consumed, false);
801
802 /* Examine children, using iteration not recursion */
803 for (MemoryContext curr = context->firstchild;
804 curr != NULL;
805 curr = MemoryContextTraverseNext(curr, context))
806 {
807 curr->methods->stats(curr, NULL, NULL, consumed, false);
808 }
809 }
810
811 /*
812 * MemoryContextStats
813 * Print statistics about the named context and all its descendants.
814 *
815 * This is just a debugging utility, so it's not very fancy. However, we do
816 * make some effort to summarize when the output would otherwise be very long.
817 * The statistics are sent to stderr.
818 */
819 void
820 MemoryContextStats(MemoryContext context)
821 {
822 /* Hard-wired limits are usually good enough */
823 MemoryContextStatsDetail(context, 100, 100, true);
824 }
825
826 /*
827 * MemoryContextStatsDetail
828 *
829 * Entry point for use if you want to vary the number of child contexts shown.
830 *
831 * If print_to_stderr is true, print statistics about the memory contexts
832 * with fprintf(stderr), otherwise use ereport().
833 */
834 void
835 MemoryContextStatsDetail(MemoryContext context,
836 int max_level, int max_children,
837 bool print_to_stderr)
838 {
839 MemoryContextCounters grand_totals;
840
841 memset(&grand_totals, 0, sizeof(grand_totals));
842
843 MemoryContextStatsInternal(context, 1, max_level, max_children,
844 &grand_totals, print_to_stderr);
845
846 if (print_to_stderr)
847 fprintf(stderr,
848 "Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used\n",
849 grand_totals.totalspace, grand_totals.nblocks,
850 grand_totals.freespace, grand_totals.freechunks,
851 grand_totals.totalspace - grand_totals.freespace);
852 else
853 {
854 /*
855 * Use LOG_SERVER_ONLY to prevent the memory contexts from being sent
856 * to the connected client.
857 *
858 * We don't buffer the information about all memory contexts in a
859 * backend into StringInfo and log it as one message. That would
860 * require the buffer to be enlarged, risking an OOM as there could be
861 * a large number of memory contexts in a backend. Instead, we log
862 * one message per memory context.
863 */
864 ereport(LOG_SERVER_ONLY,
865 (errhidestmt(true),
866 errhidecontext(true),
867 errmsg_internal("Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used",
868 grand_totals.totalspace, grand_totals.nblocks,
869 grand_totals.freespace, grand_totals.freechunks,
870 grand_totals.totalspace - grand_totals.freespace)));
871 }
872 }
873
874 /*
875 * MemoryContextStatsInternal
876 * One recursion level for MemoryContextStats
877 *
878 * Print stats for this context if possible, but in any case accumulate counts
879 * into *totals (if not NULL).
880 */
881 static void
882 MemoryContextStatsInternal(MemoryContext context, int level,
883 int max_level, int max_children,
884 MemoryContextCounters *totals,
885 bool print_to_stderr)
886 {
887 MemoryContext child;
888 int ichild;
889
890 Assert(MemoryContextIsValid(context));
891
892 /* Examine the context itself */
893 context->methods->stats(context,
894 MemoryContextStatsPrint,
895 &level,
896 totals, print_to_stderr);
897
898 /*
899 * Examine children.
900 *
901 * If we are past the recursion depth limit or already running low on
902 * stack, do not print them explicitly but just summarize them. Similarly,
903 * if there are more than max_children of them, we do not print the rest
904 * explicitly, but just summarize them.
905 */
906 child = context->firstchild;
907 ichild = 0;
908 if (level <= max_level && !stack_is_too_deep())
909 {
910 for (; child != NULL && ichild < max_children;
911 child = child->nextchild, ichild++)
912 {
913 MemoryContextStatsInternal(child, level + 1,
914 max_level, max_children,
915 totals,
916 print_to_stderr);
917 }
918 }
919
920 if (child != NULL)
921 {
922 /* Summarize the rest of the children, avoiding recursion. */
923 MemoryContextCounters local_totals;
924
925 memset(&local_totals, 0, sizeof(local_totals));
926
927 ichild = 0;
928 while (child != NULL)
929 {
930 child->methods->stats(child, NULL, NULL, &local_totals, false);
931 ichild++;
932 child = MemoryContextTraverseNext(child, context);
933 }
934
935 if (print_to_stderr)
936 {
937 for (int i = 0; i < level; i++)
938 fprintf(stderr, " ");
939 fprintf(stderr,
940 "%d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used\n",
941 ichild,
942 local_totals.totalspace,
943 local_totals.nblocks,
944 local_totals.freespace,
945 local_totals.freechunks,
946 local_totals.totalspace - local_totals.freespace);
947 }
948 else
949 ereport(LOG_SERVER_ONLY,
950 (errhidestmt(true),
951 errhidecontext(true),
952 errmsg_internal("level: %d; %d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used",
953 level,
954 ichild,
955 local_totals.totalspace,
956 local_totals.nblocks,
957 local_totals.freespace,
958 local_totals.freechunks,
959 local_totals.totalspace - local_totals.freespace)));
960
961 if (totals)
962 {
963 totals->nblocks += local_totals.nblocks;
964 totals->freechunks += local_totals.freechunks;
965 totals->totalspace += local_totals.totalspace;
966 totals->freespace += local_totals.freespace;
967 }
968 }
969 }
970
971 /*
972 * MemoryContextStatsPrint
973 * Print callback used by MemoryContextStatsInternal
974 *
975 * For now, the passthru pointer just points to "int level"; later we might
976 * make that more complicated.
977 */
978 static void
979 MemoryContextStatsPrint(MemoryContext context, void *passthru,
980 const char *stats_string,
981 bool print_to_stderr)
982 {
983 int level = *(int *) passthru;
984 const char *name = context->name;
985 const char *ident = context->ident;
986 char truncated_ident[110];
987 int i;
988
989 /*
990 * It seems preferable to label dynahash contexts with just the hash table
991 * name. Those are already unique enough, so the "dynahash" part isn't
992 * very helpful, and this way is more consistent with pre-v11 practice.
993 */
994 if (ident && strcmp(name, "dynahash") == 0)
995 {
996 name = ident;
997 ident = NULL;
998 }
999
1000 truncated_ident[0] = '\0';
1001
1002 if (ident)
1003 {
1004 /*
1005 * Some contexts may have very long identifiers (e.g., SQL queries).
1006 * Arbitrarily truncate at 100 bytes, but be careful not to break
1007 * multibyte characters. Also, replace ASCII control characters, such
1008 * as newlines, with spaces.
1009 */
1010 int idlen = strlen(ident);
1011 bool truncated = false;
1012
1013 strcpy(truncated_ident, ": ");
1014 i = strlen(truncated_ident);
1015
1016 if (idlen > 100)
1017 {
1018 idlen = pg_mbcliplen(ident, idlen, 100);
1019 truncated = true;
1020 }
1021
1022 while (idlen-- > 0)
1023 {
1024 unsigned char c = *ident++;
1025
1026 if (c < ' ')
1027 c = ' ';
1028 truncated_ident[i++] = c;
1029 }
1030 truncated_ident[i] = '\0';
1031
1032 if (truncated)
1033 strcat(truncated_ident, "...");
1034 }
1035
1036 if (print_to_stderr)
1037 {
1038 for (i = 1; i < level; i++)
1039 fprintf(stderr, " ");
1040 fprintf(stderr, "%s: %s%s\n", name, stats_string, truncated_ident);
1041 }
1042 else
1043 ereport(LOG_SERVER_ONLY,
1044 (errhidestmt(true),
1045 errhidecontext(true),
1046 errmsg_internal("level: %d; %s: %s%s",
1047 level, name, stats_string, truncated_ident)));
1048 }
1049
1050 /*
1051 * MemoryContextCheck
1052 * Check all chunks in the named context and its children.
1053 *
1054 * This is just a debugging utility, so it's not fancy.
1055 */
1056 #ifdef MEMORY_CONTEXT_CHECKING
1057 void
1058 MemoryContextCheck(MemoryContext context)
1059 {
1060 Assert(MemoryContextIsValid(context));
1061 context->methods->check(context);
1062
1063 for (MemoryContext curr = context->firstchild;
1064 curr != NULL;
1065 curr = MemoryContextTraverseNext(curr, context))
1066 {
1067 Assert(MemoryContextIsValid(curr));
1068 curr->methods->check(curr);
1069 }
1070 }
1071 #endif
1072
1073 /*
1074 * MemoryContextCreate
1075 * Context-type-independent part of context creation.
1076 *
1077 * This is only intended to be called by context-type-specific
1078 * context creation routines, not by the unwashed masses.
1079 *
1080 * The memory context creation procedure goes like this:
1081 * 1. Context-type-specific routine makes some initial space allocation,
1082 * including enough space for the context header. If it fails,
1083 * it can ereport() with no damage done.
1084 * 2. Context-type-specific routine sets up all type-specific fields of
1085 * the header (those beyond MemoryContextData proper), as well as any
1086 * other management fields it needs to have a fully valid context.
1087 * Usually, failure in this step is impossible, but if it's possible
1088 * the initial space allocation should be freed before ereport'ing.
1089 * 3. Context-type-specific routine calls MemoryContextCreate() to fill in
1090 * the generic header fields and link the context into the context tree.
1091 * 4. We return to the context-type-specific routine, which finishes
1092 * up type-specific initialization. This routine can now do things
1093 * that might fail (like allocate more memory), so long as it's
1094 * sure the node is left in a state that delete will handle.
1095 *
1096 * node: the as-yet-uninitialized common part of the context header node.
1097 * tag: NodeTag code identifying the memory context type.
1098 * method_id: MemoryContextMethodID of the context-type being created.
1099 * parent: parent context, or NULL if this will be a top-level context.
1100 * name: name of context (must be statically allocated).
1101 *
1102 * Context routines generally assume that MemoryContextCreate can't fail,
1103 * so this can contain Assert but not elog/ereport.
1104 */
1105 void
1106 MemoryContextCreate(MemoryContext node,
1107 NodeTag tag,
1108 MemoryContextMethodID method_id,
1109 MemoryContext parent,
1110 const char *name)
1111 {
1112 /* Creating new memory contexts is not allowed in a critical section */
1113 Assert(CritSectionCount == 0);
1114
1115 /* Validate parent, to help prevent crazy context linkages */
1116 Assert(parent == NULL || MemoryContextIsValid(parent));
1117 Assert(node != parent);
1118
1119 /* Initialize all standard fields of memory context header */
1120 node->type = tag;
1121 node->isReset = true;
1122 node->methods = &mcxt_methods[method_id];
1123 node->parent = parent;
1124 node->firstchild = NULL;
1125 node->mem_allocated = 0;
1126 node->prevchild = NULL;
1127 node->name = name;
1128 node->ident = NULL;
1129 node->reset_cbs = NULL;
1130
1131 /* OK to link node into context tree */
1132 if (parent)
1133 {
1134 node->nextchild = parent->firstchild;
1135 if (parent->firstchild != NULL)
1136 parent->firstchild->prevchild = node;
1137 parent->firstchild = node;
1138 /* inherit allowInCritSection flag from parent */
1139 node->allowInCritSection = parent->allowInCritSection;
1140 }
1141 else
1142 {
1143 node->nextchild = NULL;
1144 node->allowInCritSection = false;
1145 }
1146
1147 VALGRIND_CREATE_MEMPOOL(node, 0, false);
1148 }
1149
1150 /*
1151 * MemoryContextAllocationFailure
1152 * For use by MemoryContextMethods implementations to handle when malloc
1153 * returns NULL. The behavior is specific to whether MCXT_ALLOC_NO_OOM
1154 * is in 'flags'.
1155 */
1156 void *
1157 MemoryContextAllocationFailure(MemoryContext context, Size size, int flags)
1158 {
1159 if ((flags & MCXT_ALLOC_NO_OOM) == 0)
1160 {
1161 if (TopMemoryContext)
1162 MemoryContextStats(TopMemoryContext);
1163 ereport(ERROR,
1164 (errcode(ERRCODE_OUT_OF_MEMORY),
1165 errmsg("out of memory"),
1166 errdetail("Failed on request of size %zu in memory context \"%s\".",
1167 size, context->name)));
1168 }
1169 return NULL;
1170 }
1171
1172 /*
1173 * MemoryContextSizeFailure
1174 * For use by MemoryContextMethods implementations to handle invalid
1175 * memory allocation request sizes.
1176 */
1177 void
1178 MemoryContextSizeFailure(MemoryContext context, Size size, int flags)
1179 {
1180 elog(ERROR, "invalid memory alloc request size %zu", size);
1181 }
1182
1183 /*
1184 * MemoryContextAlloc
1185 * Allocate space within the specified context.
1186 *
1187 * This could be turned into a macro, but we'd have to import
1188 * nodes/memnodes.h into postgres.h which seems a bad idea.
1189 */
1190 void *
1191 MemoryContextAlloc(MemoryContext context, Size size)
1192 {
1193 void *ret;
1194
1195 Assert(MemoryContextIsValid(context));
1196 AssertNotInCriticalSection(context);
1197
1198 context->isReset = false;
1199
1200 /*
1201 * For efficiency reasons, we purposefully offload the handling of
1202 * allocation failures to the MemoryContextMethods implementation as this
1203 * allows these checks to be performed only when an actual malloc needs to
1204 * be done to request more memory from the OS. Additionally, not having
1205 * to execute any instructions after this call allows the compiler to use
1206 * the sibling call optimization. If you're considering adding code after
1207 * this call, consider making it the responsibility of the 'alloc'
1208 * function instead.
1209 */
1210 ret = context->methods->alloc(context, size, 0);
1211
1212 VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1213
1214 return ret;
1215 }
1216
1217 /*
1218 * MemoryContextAllocZero
1219 * Like MemoryContextAlloc, but clears allocated memory
1220 *
1221 * We could just call MemoryContextAlloc then clear the memory, but this
1222 * is a very common combination, so we provide the combined operation.
1223 */
1224 void *
1225 MemoryContextAllocZero(MemoryContext context, Size size)
1226 {
1227 void *ret;
1228
1229 Assert(MemoryContextIsValid(context));
1230 AssertNotInCriticalSection(context);
1231
1232 context->isReset = false;
1233
1234 ret = context->methods->alloc(context, size, 0);
1235
1236 VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1237
1238 MemSetAligned(ret, 0, size);
1239
1240 return ret;
1241 }
1242
1243 /*
1244 * MemoryContextAllocExtended
1245 * Allocate space within the specified context using the given flags.
1246 */
1247 void *
1248 MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
1249 {
1250 void *ret;
1251
1252 Assert(MemoryContextIsValid(context));
1253 AssertNotInCriticalSection(context);
1254
1255 if (!((flags & MCXT_ALLOC_HUGE) != 0 ? AllocHugeSizeIsValid(size) :
1256 AllocSizeIsValid(size)))
1257 elog(ERROR, "invalid memory alloc request size %zu", size);
1258
1259 context->isReset = false;
1260
1261 ret = context->methods->alloc(context, size, flags);
1262 if (unlikely(ret == NULL))
1263 return NULL;
1264
1265 VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1266
1267 if ((flags & MCXT_ALLOC_ZERO) != 0)
1268 MemSetAligned(ret, 0, size);
1269
1270 return ret;
1271 }
1272
1273 /*
1274 * HandleLogMemoryContextInterrupt
1275 * Handle receipt of an interrupt indicating logging of memory
1276 * contexts.
1277 *
1278 * All the actual work is deferred to ProcessLogMemoryContextInterrupt(),
1279 * because we cannot safely emit a log message inside the signal handler.
1280 */
1281 void
1282 HandleLogMemoryContextInterrupt(void)
1283 {
1284 InterruptPending = true;
1285 LogMemoryContextPending = true;
1286 /* latch will be set by procsignal_sigusr1_handler */
1287 }
1288
1289 /*
1290 * ProcessLogMemoryContextInterrupt
1291 * Perform logging of memory contexts of this backend process.
1292 *
1293 * Any backend that participates in ProcSignal signaling must arrange
1294 * to call this function if we see LogMemoryContextPending set.
1295 * It is called from CHECK_FOR_INTERRUPTS(), which is enough because
1296 * the target process for logging of memory contexts is a backend.
1297 */
1298 void
1299 ProcessLogMemoryContextInterrupt(void)
1300 {
1301 LogMemoryContextPending = false;
1302
1303 /*
1304 * Exit immediately if memory context logging is already in progress. This
1305 * prevents recursive calls, which could occur if logging is requested
1306 * repeatedly and rapidly, potentially leading to infinite recursion and a
1307 * crash.
1308 */
1309 if (LogMemoryContextInProgress)
1310 return;
1311 LogMemoryContextInProgress = true;
1312
1313 PG_TRY();
1314 {
1315 /*
1316 * Use LOG_SERVER_ONLY to prevent this message from being sent to the
1317 * connected client.
1318 */
1319 ereport(LOG_SERVER_ONLY,
1320 (errhidestmt(true),
1321 errhidecontext(true),
1322 errmsg("logging memory contexts of PID %d", MyProcPid)));
1323
1324 /*
1325 * When a backend process is consuming huge memory, logging all its
1326 * memory contexts might overrun available disk space. To prevent
1327 * this, we limit the depth of the hierarchy, as well as the number of
1328 * child contexts to log per parent to 100.
1329 *
1330 * As with MemoryContextStats(), we suppose that practical cases where
1331 * the dump gets long will typically be huge numbers of siblings under
1332 * the same parent context; while the additional debugging value from
1333 * seeing details about individual siblings beyond 100 will not be
1334 * large.
1335 */
1336 MemoryContextStatsDetail(TopMemoryContext, 100, 100, false);
1337 }
1338 PG_FINALLY();
1339 {
1340 LogMemoryContextInProgress = false;
1341 }
1342 PG_END_TRY();
1343 }
1344
1345 void *
1346 palloc(Size size)
1347 {
1348 /* duplicates MemoryContextAlloc to avoid increased overhead */
1349 void *ret;
1350 MemoryContext context = CurrentMemoryContext;
1351
1352 Assert(MemoryContextIsValid(context));
1353 AssertNotInCriticalSection(context);
1354
1355 context->isReset = false;
1356
1357 /*
1358 * For efficiency reasons, we purposefully offload the handling of
1359 * allocation failures to the MemoryContextMethods implementation as this
1360 * allows these checks to be performed only when an actual malloc needs to
1361 * be done to request more memory from the OS. Additionally, not having
1362 * to execute any instructions after this call allows the compiler to use
1363 * the sibling call optimization. If you're considering adding code after
1364 * this call, consider making it the responsibility of the 'alloc'
1365 * function instead.
1366 */
1367 ret = context->methods->alloc(context, size, 0);
1368 /* We expect OOM to be handled by the alloc function */
1369 Assert(ret != NULL);
1370 VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1371
1372 return ret;
1373 }
1374
1375 void *
1376 palloc0(Size size)
1377 {
1378 /* duplicates MemoryContextAllocZero to avoid increased overhead */
1379 void *ret;
1380 MemoryContext context = CurrentMemoryContext;
1381
1382 Assert(MemoryContextIsValid(context));
1383 AssertNotInCriticalSection(context);
1384
1385 context->isReset = false;
1386
1387 ret = context->methods->alloc(context, size, 0);
1388 /* We expect OOM to be handled by the alloc function */
1389 Assert(ret != NULL);
1390 VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1391
1392 MemSetAligned(ret, 0, size);
1393
1394 return ret;
1395 }
1396
1397 void *
1398 palloc_extended(Size size, int flags)
1399 {
1400 /* duplicates MemoryContextAllocExtended to avoid increased overhead */
1401 void *ret;
1402 MemoryContext context = CurrentMemoryContext;
1403
1404 Assert(MemoryContextIsValid(context));
1405 AssertNotInCriticalSection(context);
1406
1407 context->isReset = false;
1408
1409 ret = context->methods->alloc(context, size, flags);
1410 if (unlikely(ret == NULL))
1411 {
1412 /* NULL can be returned only when using MCXT_ALLOC_NO_OOM */
1413 Assert(flags & MCXT_ALLOC_NO_OOM);
1414 return NULL;
1415 }
1416
1417 VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1418
1419 if ((flags & MCXT_ALLOC_ZERO) != 0)
1420 MemSetAligned(ret, 0, size);
1421
1422 return ret;
1423 }
1424
1425 /*
1426 * MemoryContextAllocAligned
1427 * Allocate 'size' bytes of memory in 'context' aligned to 'alignto'
1428 * bytes.
1429 *
1430 * Currently, we align addresses by requesting additional bytes from the
1431 * MemoryContext's standard allocator function and then aligning the returned
1432 * address by the required alignment. This means that the given MemoryContext
1433 * must support providing us with a chunk of memory that's larger than 'size'.
1434 * For allocators such as Slab, that's not going to work, as slab only allows
1435 * chunks of the size that's specified when the context is created.
1436 *
1437 * 'alignto' must be a power of 2.
1438 * 'flags' may be 0 or set the same as MemoryContextAllocExtended().
1439 */
1440 void *
1441 MemoryContextAllocAligned(MemoryContext context,
1442 Size size, Size alignto, int flags)
1443 {
1444 MemoryChunk *alignedchunk;
1445 Size alloc_size;
1446 void *unaligned;
1447 void *aligned;
1448
1449 /* wouldn't make much sense to waste that much space */
1450 Assert(alignto < (128 * 1024 * 1024));
1451
1452 /* ensure alignto is a power of 2 */
1453 Assert((alignto & (alignto - 1)) == 0);
1454
1455 /*
1456 * If the alignment requirements are less than what we already guarantee
1457 * then just use the standard allocation function.
1458 */
1459 if (unlikely(alignto <= MAXIMUM_ALIGNOF))
1460 return MemoryContextAllocExtended(context, size, flags);
1461
1462 /*
1463 * We implement aligned pointers by simply allocating enough memory for
1464 * the requested size plus the alignment and an additional "redirection"
1465 * MemoryChunk. This additional MemoryChunk is required for operations
1466 * such as pfree when used on the pointer returned by this function. We
1467 * use this redirection MemoryChunk in order to find the pointer to the
1468 * memory that was returned by the MemoryContextAllocExtended call below.
1469 * We do that by "borrowing" the block offset field and instead of using
1470 * that to find the offset into the owning block, we use it to find the
1471 * original allocated address.
1472 *
1473 * Here we must allocate enough extra memory so that we can still align
1474 * the pointer returned by MemoryContextAllocExtended and also have enough
1475 * space for the redirection MemoryChunk. Since allocations will already
1476 * be at least aligned by MAXIMUM_ALIGNOF, we can subtract that amount
1477 * from the allocation size to save a little memory.
1478 */
1479 alloc_size = size + PallocAlignedExtraBytes(alignto);
1480
1481 #ifdef MEMORY_CONTEXT_CHECKING
1482 /* ensure there's space for a sentinel byte */
1483 alloc_size += 1;
1484 #endif
1485
1486 /* perform the actual allocation */
1487 unaligned = MemoryContextAllocExtended(context, alloc_size, flags);
1488
1489 if (unlikely(unaligned == NULL))
1490 {
1491 /* NULL can be returned only when using MCXT_ALLOC_NO_OOM */
1492 Assert(flags & MCXT_ALLOC_NO_OOM);
1493 return NULL;
1494 }
1495
1496 /* set the aligned pointer */
1497 aligned = (void *) TYPEALIGN(alignto, (char *) unaligned +
1498 sizeof(MemoryChunk));
1499
1500 alignedchunk = PointerGetMemoryChunk(aligned);
1501
1502 /*
1503 * We set the redirect MemoryChunk so that the block offset calculation is
1504 * used to point back to the 'unaligned' allocated chunk. This allows us
1505 * to use MemoryChunkGetBlock() to find the unaligned chunk when we need
1506 * to perform operations such as pfree() and repalloc().
1507 *
1508 * We store 'alignto' in the MemoryChunk's 'value' so that we know what
1509 * the alignment was set to should we ever be asked to realloc this
1510 * pointer.
1511 */
1512 MemoryChunkSetHdrMask(alignedchunk, unaligned, alignto,
1513 MCTX_ALIGNED_REDIRECT_ID);
1514
1515 /* double check we produced a correctly aligned pointer */
1516 Assert((void *) TYPEALIGN(alignto, aligned) == aligned);
1517
1518 #ifdef MEMORY_CONTEXT_CHECKING
1519 alignedchunk->requested_size = size;
1520 /* set mark to catch clobber of "unused" space */
1521 set_sentinel(aligned, size);
1522 #endif
1523
1524 /* Mark the bytes before the redirection header as noaccess */
1525 VALGRIND_MAKE_MEM_NOACCESS(unaligned,
1526 (char *) alignedchunk - (char *) unaligned);
1527
1528 /* Disallow access to the redirection chunk header. */
1529 VALGRIND_MAKE_MEM_NOACCESS(alignedchunk, sizeof(MemoryChunk));
1530
1531 return aligned;
1532 }
1533
1534 /*
1535 * palloc_aligned
1536 * Allocate 'size' bytes returning a pointer that's aligned to the
1537 * 'alignto' boundary.
1538 *
1539 * Currently, we align addresses by requesting additional bytes from the
1540 * MemoryContext's standard allocator function and then aligning the returned
1541 * address by the required alignment. This means that the given MemoryContext
1542 * must support providing us with a chunk of memory that's larger than 'size'.
1543 * For allocators such as Slab, that's not going to work, as slab only allows
1544 * chunks of the size that's specified when the context is created.
1545 *
1546 * 'alignto' must be a power of 2.
1547 * 'flags' may be 0 or set the same as MemoryContextAllocExtended().
1548 */
1549 void *
1550 palloc_aligned(Size size, Size alignto, int flags)
1551 {
1552 return MemoryContextAllocAligned(CurrentMemoryContext, size, alignto, flags);
1553 }
1554
1555 /*
1556 * pfree
1557 * Release an allocated chunk.
1558 */
1559 void
1560 pfree(void *pointer)
1561 {
1562 #ifdef USE_VALGRIND
1563 MemoryContextMethodID method = GetMemoryChunkMethodID(pointer);
1564 MemoryContext context = GetMemoryChunkContext(pointer);
1565 #endif
1566
1567 MCXT_METHOD(pointer, free_p) (pointer);
1568
1569 #ifdef USE_VALGRIND
1570 if (method != MCTX_ALIGNED_REDIRECT_ID)
1571 VALGRIND_MEMPOOL_FREE(context, pointer);
1572 #endif
1573 }
1574
1575 /*
1576 * repalloc
1577 * Adjust the size of a previously allocated chunk.
1578 */
1579 void *
1580 repalloc(void *pointer, Size size)
1581 {
1582 #ifdef USE_VALGRIND
1583 MemoryContextMethodID method = GetMemoryChunkMethodID(pointer);
1584 #endif
1585 #if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
1586 MemoryContext context = GetMemoryChunkContext(pointer);
1587 #endif
1588 void *ret;
1589
1590 AssertNotInCriticalSection(context);
1591
1592 /* isReset must be false already */
1593 Assert(!context->isReset);
1594
1595 /*
1596 * For efficiency reasons, we purposefully offload the handling of
1597 * allocation failures to the MemoryContextMethods implementation as this
1598 * allows these checks to be performed only when an actual malloc needs to
1599 * be done to request more memory from the OS. Additionally, not having
1600 * to execute any instructions after this call allows the compiler to use
1601 * the sibling call optimization. If you're considering adding code after
1602 * this call, consider making it the responsibility of the 'realloc'
1603 * function instead.
1604 */
1605 ret = MCXT_METHOD(pointer, realloc) (pointer, size, 0);
1606
1607 #ifdef USE_VALGRIND
1608 if (method != MCTX_ALIGNED_REDIRECT_ID)
1609 VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size);
1610 #endif
1611
1612 return ret;
1613 }
1614
1615 /*
1616 * repalloc_extended
1617 * Adjust the size of a previously allocated chunk,
1618 * with HUGE and NO_OOM options.
1619 */
1620 void *
1621 repalloc_extended(void *pointer, Size size, int flags)
1622 {
1623 #if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
1624 MemoryContext context = GetMemoryChunkContext(pointer);
1625 #endif
1626 void *ret;
1627
1628 AssertNotInCriticalSection(context);
1629
1630 /* isReset must be false already */
1631 Assert(!context->isReset);
1632
1633 /*
1634 * For efficiency reasons, we purposefully offload the handling of
1635 * allocation failures to the MemoryContextMethods implementation as this
1636 * allows these checks to be performed only when an actual malloc needs to
1637 * be done to request more memory from the OS. Additionally, not having
1638 * to execute any instructions after this call allows the compiler to use
1639 * the sibling call optimization. If you're considering adding code after
1640 * this call, consider making it the responsibility of the 'realloc'
1641 * function instead.
1642 */
1643 ret = MCXT_METHOD(pointer, realloc) (pointer, size, flags);
1644 if (unlikely(ret == NULL))
1645 return NULL;
1646
1647 VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size);
1648
1649 return ret;
1650 }
1651
1652 /*
1653 * repalloc0
1654 * Adjust the size of a previously allocated chunk and zero out the added
1655 * space.
1656 */
1657 void *
1658 repalloc0(void *pointer, Size oldsize, Size size)
1659 {
1660 void *ret;
1661
1662 /* catch wrong argument order */
1663 if (unlikely(oldsize > size))
1664 elog(ERROR, "invalid repalloc0 call: oldsize %zu, new size %zu",
1665 oldsize, size);
1666
1667 ret = repalloc(pointer, size);
1668 memset((char *) ret + oldsize, 0, (size - oldsize));
1669 return ret;
1670 }
1671
1672 /*
1673 * Support for safe calculation of memory request sizes
1674 *
1675 * These functions perform the requested calculation, but throw error if the
1676 * result overflows.
1677 *
1678 * An important property of these functions is that if an argument was a
1679 * negative signed int before promotion (implying overflow in calculating it)
1680 * we will detect that as an error. That happens because we reject results
1681 * larger than SIZE_MAX / 2 later on, in the actual allocation step.
1682 */
1683 Size
1684 add_size(Size s1, Size s2)
1685 {
1686 Size result;
1687
1688 if (unlikely(pg_add_size_overflow(s1, s2, &result)))
1689 add_size_error(s1, s2);
1690 return result;
1691 }
1692
1693 pg_noreturn static pg_noinline void
1694 add_size_error(Size s1, Size s2)
1695 {
1696 ereport(ERROR,
1697 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1698 errmsg("invalid memory allocation request size %zu + %zu",
1699 s1, s2)));
1700 }
1701
1702 Size
1703 mul_size(Size s1, Size s2)
1704 {
1705 Size result;
1706
1707 if (unlikely(pg_mul_size_overflow(s1, s2, &result)))
1708 mul_size_error(s1, s2);
1709 return result;
1710 }
1711
1712 pg_noreturn static pg_noinline void
1713 mul_size_error(Size s1, Size s2)
1714 {
1715 ereport(ERROR,
1716 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1717 errmsg("invalid memory allocation request size %zu * %zu",
1718 s1, s2)));
1719 }
1720
1721 /*
1722 * palloc_mul
1723 * Equivalent to palloc(mul_size(s1, s2)).
1724 */
1725 void *
1726 palloc_mul(Size s1, Size s2)
1727 {
1728 /* inline mul_size() for efficiency */
1729 Size req;
1730
1731 if (unlikely(pg_mul_size_overflow(s1, s2, &req)))
1732 mul_size_error(s1, s2);
1733 return palloc(req);
1734 }
1735
1736 /*
1737 * palloc0_mul
1738 * Equivalent to palloc0(mul_size(s1, s2)).
1739 *
1740 * This is comparable to standard calloc's behavior.
1741 */
1742 void *
1743 palloc0_mul(Size s1, Size s2)
1744 {
1745 /* inline mul_size() for efficiency */
1746 Size req;
1747
1748 if (unlikely(pg_mul_size_overflow(s1, s2, &req)))
1749 mul_size_error(s1, s2);
1750 return palloc0(req);
1751 }
1752
1753 /*
1754 * palloc_mul_extended
1755 * Equivalent to palloc_extended(mul_size(s1, s2), flags).
1756 */
1757 void *
1758 palloc_mul_extended(Size s1, Size s2, int flags)
1759 {
1760 /* inline mul_size() for efficiency */
1761 Size req;
1762
1763 if (unlikely(pg_mul_size_overflow(s1, s2, &req)))
1764 mul_size_error(s1, s2);
1765 return palloc_extended(req, flags);
1766 }
1767
1768 /*
1769 * repalloc_mul
1770 * Equivalent to repalloc(p, mul_size(s1, s2)).
1771 */
1772 void *
1773 repalloc_mul(void *p, Size s1, Size s2)
1774 {
1775 /* inline mul_size() for efficiency */
1776 Size req;
1777
1778 if (unlikely(pg_mul_size_overflow(s1, s2, &req)))
1779 mul_size_error(s1, s2);
1780 return repalloc(p, req);
1781 }
1782
1783 /*
1784 * repalloc_mul_extended
1785 * Equivalent to repalloc_extended(p, mul_size(s1, s2), flags).
1786 */
1787 void *
1788 repalloc_mul_extended(void *p, Size s1, Size s2, int flags)
1789 {
1790 /* inline mul_size() for efficiency */
1791 Size req;
1792
1793 if (unlikely(pg_mul_size_overflow(s1, s2, &req)))
1794 mul_size_error(s1, s2);
1795 return repalloc_extended(p, req, flags);
1796 }
1797
1798 /*
1799 * MemoryContextAllocHuge
1800 * Allocate (possibly-expansive) space within the specified context.
1801 *
1802 * See considerations in comment at MaxAllocHugeSize.
1803 */
1804 void *
1805 MemoryContextAllocHuge(MemoryContext context, Size size)
1806 {
1807 void *ret;
1808
1809 Assert(MemoryContextIsValid(context));
1810 AssertNotInCriticalSection(context);
1811
1812 context->isReset = false;
1813
1814 /*
1815 * For efficiency reasons, we purposefully offload the handling of
1816 * allocation failures to the MemoryContextMethods implementation as this
1817 * allows these checks to be performed only when an actual malloc needs to
1818 * be done to request more memory from the OS. Additionally, not having
1819 * to execute any instructions after this call allows the compiler to use
1820 * the sibling call optimization. If you're considering adding code after
1821 * this call, consider making it the responsibility of the 'alloc'
1822 * function instead.
1823 */
1824 ret = context->methods->alloc(context, size, MCXT_ALLOC_HUGE);
1825
1826 VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1827
1828 return ret;
1829 }
1830
1831 /*
1832 * repalloc_huge
1833 * Adjust the size of a previously allocated chunk, permitting a large
1834 * value. The previous allocation need not have been "huge".
1835 */
1836 void *
1837 repalloc_huge(void *pointer, Size size)
1838 {
1839 /* this one seems not worth its own implementation */
1840 return repalloc_extended(pointer, size, MCXT_ALLOC_HUGE);
1841 }
1842
1843 /*
1844 * MemoryContextStrdup
1845 * Like strdup(), but allocate from the specified context
1846 */
1847 char *
1848 MemoryContextStrdup(MemoryContext context, const char *string)
1849 {
1850 char *nstr;
1851 Size len = strlen(string) + 1;
1852
1853 nstr = (char *) MemoryContextAlloc(context, len);
1854
1855 memcpy(nstr, string, len);
1856
1857 return nstr;
1858 }
1859
1860 char *
1861 pstrdup(const char *in)
1862 {
1863 return MemoryContextStrdup(CurrentMemoryContext, in);
1864 }
1865
1866 /*
1867 * pnstrdup
1868 * Like pstrdup(), but append null byte to a
1869 * not-necessarily-null-terminated input string.
1870 */
1871 char *
1872 pnstrdup(const char *in, Size len)
1873 {
1874 char *out;
1875
1876 len = strnlen(in, len);
1877
1878 out = palloc(len + 1);
1879 memcpy(out, in, len);
1880 out[len] = '\0';
1881
1882 return out;
1883 }
1884
1885 /*
1886 * Make copy of string with all trailing newline characters removed.
1887 */
1888 char *
1889 pchomp(const char *in)
1890 {
1891 size_t n;
1892
1893 n = strlen(in);
1894 while (n > 0 && in[n - 1] == '\n')
1895 n--;
1896 return pnstrdup(in, n);
1897 }