57#define BOGUS_MCTX(id) \
58 [id].free_p = BogusFree, \
59 [id].realloc = BogusRealloc, \
60 [id].get_chunk_context = BogusGetChunkContext, \
61 [id].get_chunk_space = BogusGetChunkSpace
74#ifdef MEMORY_CONTEXT_CHECKING
88#ifdef MEMORY_CONTEXT_CHECKING
102#ifdef MEMORY_CONTEXT_CHECKING
116#ifdef MEMORY_CONTEXT_CHECKING
130#ifdef MEMORY_CONTEXT_CHECKING
180 int max_level,
int max_children,
182 bool print_to_stderr);
184 const char *stats_string,
185 bool print_to_stderr);
192#define AssertNotInCriticalSection(context) \
193 Assert(CritSectionCount == 0 || (context)->allowInCritSection)
199#define MCXT_METHOD(pointer, method) \
200 mcxt_methods[GetMemoryChunkMethodID(pointer)].method
222 header = *((
const uint64 *) ((
const char *) pointer -
sizeof(
uint64)));
244 header = *((
const uint64 *) ((
const char *) pointer -
sizeof(
uint64)));
305 elog(
ERROR,
"pfree called with invalid pointer %p (header 0x%016" PRIx64
")",
312 elog(
ERROR,
"repalloc called with invalid pointer %p (header 0x%016" PRIx64
")",
320 elog(
ERROR,
"GetMemoryChunkContext called with invalid pointer %p (header 0x%016" PRIx64
")",
328 elog(
ERROR,
"GetMemoryChunkSpace called with invalid pointer %p (header 0x%016" PRIx64
")",
541 context->
ident = NULL;
640 while ((cb = context->
reset_cbs) != NULL)
686 Assert(context != new_parent);
689 if (new_parent == context->
parent)
713 context->
parent = new_parent;
755 return MCXT_METHOD(pointer, get_chunk_context) (pointer);
769 return MCXT_METHOD(pointer, get_chunk_space) (pointer);
820 total += curr->mem_allocated;
837 memset(consumed, 0,
sizeof(*consumed));
840 context->
methods->
stats(context, NULL, NULL, consumed,
false);
847 curr->methods->stats(curr, NULL, NULL, consumed,
false);
876 int max_level,
int max_children,
877 bool print_to_stderr)
881 memset(&grand_totals, 0,
sizeof(grand_totals));
884 &grand_totals, print_to_stderr);
888 "Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used\n",
907 errmsg_internal(
"Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used",
923 int max_level,
int max_children,
925 bool print_to_stderr)
936 totals, print_to_stderr);
950 for (; child != NULL && ichild < max_children;
954 max_level, max_children,
965 memset(&local_totals, 0,
sizeof(local_totals));
968 while (child != NULL)
970 child->
methods->
stats(child, NULL, NULL, &local_totals,
false);
977 for (
int i = 0;
i < level;
i++)
980 "%d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used\n",
992 errmsg_internal(
"level: %d; %d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used",
1020 const char *stats_string,
1021 bool print_to_stderr)
1023 int level = *(
int *) passthru;
1026 char truncated_ident[110];
1034 if (
ident && strcmp(
name,
"dynahash") == 0)
1040 truncated_ident[0] =
'\0';
1050 int idlen = strlen(
ident);
1051 bool truncated =
false;
1053 strcpy(truncated_ident,
": ");
1054 i = strlen(truncated_ident);
1064 unsigned char c = *
ident++;
1068 truncated_ident[
i++] =
c;
1070 truncated_ident[
i] =
'\0';
1073 strcat(truncated_ident,
"...");
1076 if (print_to_stderr)
1078 for (
i = 1;
i < level;
i++)
1080 fprintf(stderr,
"%s: %s%s\n",
name, stats_string, truncated_ident);
1087 level,
name, stats_string, truncated_ident)));
1096#ifdef MEMORY_CONTEXT_CHECKING
1101 context->
methods->check(context);
1108 curr->methods->check(curr);
1202 (
errcode(ERRCODE_OUT_OF_MEMORY),
1204 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1205 size, context->
name)));
1218 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1295 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1461 Size size,
Size alignto,
int flags)
1475 Assert(alignto < (128 * 1024 * 1024));
1478 Assert((alignto & (alignto - 1)) == 0);
1484 if (
unlikely(alignto <= MAXIMUM_ALIGNOF))
1506#ifdef MEMORY_CONTEXT_CHECKING
1520 aligned = (
void *)
TYPEALIGN(alignto, (
char *) unaligned +
1541#ifdef MEMORY_CONTEXT_CHECKING
1542 alignedchunk->requested_size = size;
1544 set_sentinel(aligned, size);
1612#if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
1647#if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
1688 elog(
ERROR,
"invalid repalloc0 call: oldsize %zu, new size %zu",
1692 memset((
char *) ret + oldsize, 0, (size - oldsize));
1749 Size len = strlen(
string) + 1;
1753 memcpy(nstr,
string,
len);
1777 memcpy(out, in,
len);
1792 while (n > 0 && in[n - 1] ==
'\n')
MemoryContext AlignedAllocGetChunkContext(void *pointer)
void * AlignedAllocRealloc(void *pointer, Size size, int flags)
Size AlignedAllocGetChunkSpace(void *pointer)
void AlignedAllocFree(void *pointer)
void AllocSetReset(MemoryContext context)
void * AllocSetRealloc(void *pointer, Size size, int flags)
Size AllocSetGetChunkSpace(void *pointer)
MemoryContext AllocSetGetChunkContext(void *pointer)
void AllocSetStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
bool AllocSetIsEmpty(MemoryContext context)
void * AllocSetAlloc(MemoryContext context, Size size, int flags)
void AllocSetFree(void *pointer)
void AllocSetDelete(MemoryContext context)
void BumpFree(void *pointer)
void BumpDelete(MemoryContext context)
Size BumpGetChunkSpace(void *pointer)
void BumpStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
MemoryContext BumpGetChunkContext(void *pointer)
void BumpReset(MemoryContext context)
bool BumpIsEmpty(MemoryContext context)
void * BumpRealloc(void *pointer, Size size, int flags)
void * BumpAlloc(MemoryContext context, Size size, int flags)
#define TYPEALIGN(ALIGNVAL, LEN)
#define MemSetAligned(start, val, len)
#define fprintf(file, fmt, msg)
int errmsg_internal(const char *fmt,...)
int errhidestmt(bool hide_stmt)
int errdetail(const char *fmt,...)
int errhidecontext(bool hide_ctx)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define MCXT_ALLOC_NO_OOM
void * GenerationRealloc(void *pointer, Size size, int flags)
void GenerationReset(MemoryContext context)
void GenerationFree(void *pointer)
MemoryContext GenerationGetChunkContext(void *pointer)
Size GenerationGetChunkSpace(void *pointer)
bool GenerationIsEmpty(MemoryContext context)
void GenerationStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
void GenerationDelete(MemoryContext context)
void * GenerationAlloc(MemoryContext context, Size size, int flags)
volatile sig_atomic_t LogMemoryContextPending
volatile sig_atomic_t InterruptPending
volatile uint32 CritSectionCount
Assert(PointerIsAligned(start, uint64))
int pg_mbcliplen(const char *mbstr, int len, int limit)
static void MemoryContextStatsInternal(MemoryContext context, int level, int max_level, int max_children, MemoryContextCounters *totals, bool print_to_stderr)
void MemoryContextUnregisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
void * repalloc0(void *pointer, Size oldsize, Size size)
static void MemoryContextCallResetCallbacks(MemoryContext context)
char * MemoryContextStrdup(MemoryContext context, const char *string)
void * MemoryContextAlloc(MemoryContext context, Size size)
MemoryContext MessageContext
bool MemoryContextIsEmpty(MemoryContext context)
void MemoryContextMemConsumed(MemoryContext context, MemoryContextCounters *consumed)
void MemoryContextReset(MemoryContext context)
void MemoryContextCreate(MemoryContext node, NodeTag tag, MemoryContextMethodID method_id, MemoryContext parent, const char *name)
void * MemoryContextAllocZero(MemoryContext context, Size size)
MemoryContext TopTransactionContext
char * pstrdup(const char *in)
void HandleLogMemoryContextInterrupt(void)
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
static MemoryContextMethodID GetMemoryChunkMethodID(const void *pointer)
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
static void * BogusRealloc(void *pointer, Size size, int flags)
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
Size GetMemoryChunkSpace(void *pointer)
void * palloc0(Size size)
static Size BogusGetChunkSpace(void *pointer)
void * MemoryContextAllocAligned(MemoryContext context, Size size, Size alignto, int flags)
void MemoryContextDeleteChildren(MemoryContext context)
MemoryContext TopMemoryContext
char * pchomp(const char *in)
#define AssertNotInCriticalSection(context)
MemoryContext CurTransactionContext
MemoryContext CurrentMemoryContext
static MemoryContext MemoryContextTraverseNext(MemoryContext curr, MemoryContext top)
MemoryContext GetMemoryChunkContext(void *pointer)
void * MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
void MemoryContextStatsDetail(MemoryContext context, int max_level, int max_children, bool print_to_stderr)
Size MemoryContextMemAllocated(MemoryContext context, bool recurse)
char * pnstrdup(const char *in, Size len)
void MemoryContextStats(MemoryContext context)
void MemoryContextInit(void)
static void BogusFree(void *pointer)
void * palloc_extended(Size size, int flags)
MemoryContext PostmasterContext
void * MemoryContextAllocationFailure(MemoryContext context, Size size, int flags)
static const MemoryContextMethods mcxt_methods[]
void * repalloc_extended(void *pointer, Size size, int flags)
MemoryContext MemoryContextGetParent(MemoryContext context)
void ProcessLogMemoryContextInterrupt(void)
MemoryContext ErrorContext
static MemoryContext BogusGetChunkContext(void *pointer)
MemoryContext CacheMemoryContext
void MemoryContextSizeFailure(MemoryContext context, Size size, int flags)
void * MemoryContextAllocHuge(MemoryContext context, Size size)
void MemoryContextDelete(MemoryContext context)
void MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)
static void MemoryContextDeleteOnly(MemoryContext context)
void MemoryContextResetChildren(MemoryContext context)
static void MemoryContextStatsPrint(MemoryContext context, void *passthru, const char *stats_string, bool print_to_stderr)
void * repalloc_huge(void *pointer, Size size)
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
void MemoryContextResetOnly(MemoryContext context)
static uint64 GetMemoryChunkHeader(const void *pointer)
MemoryContext PortalContext
void * palloc_aligned(Size size, Size alignto, int flags)
#define MCXT_METHOD(pointer, method)
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
#define VALGRIND_MEMPOOL_CHANGE(context, optr, nptr, size)
#define VALGRIND_MEMPOOL_ALLOC(context, addr, size)
#define VALGRIND_MEMPOOL_FREE(context, addr)
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
#define MemoryContextIsValid(context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define AllocHugeSizeIsValid(size)
#define AllocSizeIsValid(size)
#define MEMORY_CONTEXT_METHODID_MASK
#define PallocAlignedExtraBytes(alignto)
@ MCTX_15_RESERVED_WIPEDMEM_ID
@ MCTX_1_RESERVED_GLIBC_ID
@ MCTX_0_RESERVED_UNUSEDMEM_ID
@ MCTX_2_RESERVED_GLIBC_ID
@ MCTX_ALIGNED_REDIRECT_ID
struct MemoryChunk MemoryChunk
#define PointerGetMemoryChunk(p)
static void MemoryChunkSetHdrMask(MemoryChunk *chunk, void *block, Size value, MemoryContextMethodID methodid)
size_t strnlen(const char *str, size_t maxlen)
void * SlabAlloc(MemoryContext context, Size size, int flags)
void SlabFree(void *pointer)
void SlabReset(MemoryContext context)
Size SlabGetChunkSpace(void *pointer)
bool SlabIsEmpty(MemoryContext context)
MemoryContext SlabGetChunkContext(void *pointer)
void * SlabRealloc(void *pointer, Size size, int flags)
void SlabStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
void SlabDelete(MemoryContext context)
bool stack_is_too_deep(void)
struct MemoryContextCallback * next
MemoryContextCallbackFunction func
MemoryContextCallback * reset_cbs
const MemoryContextMethods * methods
void(* delete_context)(MemoryContext context)
void(* stats)(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
bool(* is_empty)(MemoryContext context)
void *(* alloc)(MemoryContext context, Size size, int flags)
void(* reset)(MemoryContext context)