52 #ifdef MEMORY_CONTEXT_CHECKING
66 #ifdef MEMORY_CONTEXT_CHECKING
80 #ifdef MEMORY_CONTEXT_CHECKING
94 #ifdef MEMORY_CONTEXT_CHECKING
151 int max_level,
int max_children,
153 bool print_to_stderr);
155 const char *stats_string,
156 bool print_to_stderr);
163 #define AssertNotInCriticalSection(context) \
164 Assert(CritSectionCount == 0 || (context)->allowInCritSection)
170 #define MCXT_METHOD(pointer, method) \
171 mcxt_methods[GetMemoryChunkMethodID(pointer)].method
193 header = *((
const uint64 *) ((
const char *) pointer -
sizeof(uint64)));
215 header = *((
const uint64 *) ((
const char *) pointer -
sizeof(uint64)));
276 elog(
ERROR,
"pfree called with invalid pointer %p (header 0x%016llx)",
283 elog(
ERROR,
"repalloc called with invalid pointer %p (header 0x%016llx)",
291 elog(
ERROR,
"GetMemoryChunkContext called with invalid pointer %p (header 0x%016llx)",
299 elog(
ERROR,
"GetMemoryChunkSpace called with invalid pointer %p (header 0x%016llx)",
514 context->
ident = NULL;
582 while ((cb = context->
reset_cbs) != NULL)
628 Assert(context != new_parent);
631 if (new_parent == context->
parent)
655 context->
parent = new_parent;
697 return MCXT_METHOD(pointer, get_chunk_context) (pointer);
711 return MCXT_METHOD(pointer, get_chunk_space) (pointer);
762 total += curr->mem_allocated;
779 memset(consumed, 0,
sizeof(*consumed));
782 context->
methods->
stats(context, NULL, NULL, consumed,
false);
789 curr->methods->stats(curr, NULL, NULL, consumed,
false);
818 int max_level,
int max_children,
819 bool print_to_stderr)
823 memset(&grand_totals, 0,
sizeof(grand_totals));
826 &grand_totals, print_to_stderr);
830 "Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used\n",
849 errmsg_internal(
"Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used",
865 int max_level,
int max_children,
867 bool print_to_stderr)
878 totals, print_to_stderr);
892 for (; child != NULL && ichild < max_children;
896 max_level, max_children,
907 memset(&local_totals, 0,
sizeof(local_totals));
910 while (child != NULL)
912 child->
methods->
stats(child, NULL, NULL, &local_totals,
false);
919 for (
int i = 0;
i <= level;
i++)
922 "%d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used\n",
934 errmsg_internal(
"level: %d; %d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used",
962 const char *stats_string,
963 bool print_to_stderr)
965 int level = *(
int *) passthru;
968 char truncated_ident[110];
976 if (
ident && strcmp(
name,
"dynahash") == 0)
982 truncated_ident[0] =
'\0';
992 int idlen = strlen(
ident);
993 bool truncated =
false;
995 strcpy(truncated_ident,
": ");
996 i = strlen(truncated_ident);
1006 unsigned char c = *
ident++;
1010 truncated_ident[
i++] =
c;
1012 truncated_ident[
i] =
'\0';
1015 strcat(truncated_ident,
"...");
1018 if (print_to_stderr)
1020 for (
i = 0;
i < level;
i++)
1022 fprintf(stderr,
"%s: %s%s\n",
name, stats_string, truncated_ident);
1029 level,
name, stats_string, truncated_ident)));
1038 #ifdef MEMORY_CONTEXT_CHECKING
1043 context->
methods->check(context);
1050 curr->methods->check(curr);
1141 (
errcode(ERRCODE_OUT_OF_MEMORY),
1143 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1405 Assert(alignto < (128 * 1024 * 1024));
1408 Assert((alignto & (alignto - 1)) == 0);
1414 if (
unlikely(alignto <= MAXIMUM_ALIGNOF))
1436 #ifdef MEMORY_CONTEXT_CHECKING
1445 aligned = (
void *)
TYPEALIGN(alignto, (
char *) unaligned +
1466 #ifdef MEMORY_CONTEXT_CHECKING
1467 alignedchunk->requested_size =
size;
1469 set_sentinel(aligned,
size);
1474 (
char *) alignedchunk - (
char *) unaligned);
1533 #if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
1571 #if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
1612 elog(
ERROR,
"invalid repalloc0 call: oldsize %zu, new size %zu",
1616 memset((
char *) ret + oldsize, 0, (
size - oldsize));
1673 Size len = strlen(
string) + 1;
1677 memcpy(nstr,
string,
len);
1701 memcpy(out, in,
len);
1716 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)
void * AllocSetAlloc(MemoryContext context, Size size, int flags)
bool AllocSetIsEmpty(MemoryContext context)
void AllocSetFree(void *pointer)
void AllocSetDelete(MemoryContext context)
#define TYPEALIGN(ALIGNVAL, LEN)
#define MemSetAligned(start, val, len)
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 GenerationReset(MemoryContext context)
void * GenerationAlloc(MemoryContext context, Size size, int flags)
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 * GenerationRealloc(void *pointer, Size size, int flags)
void GenerationDelete(MemoryContext context)
volatile sig_atomic_t LogMemoryContextPending
volatile sig_atomic_t InterruptPending
volatile uint32 CritSectionCount
Assert(fmt[strlen(fmt) - 1] !='\n')
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)
char * pnstrdup(const char *in, Size len)
static void MemoryContextCallResetCallbacks(MemoryContext context)
MemoryContext MessageContext
void * MemoryContextAllocAligned(MemoryContext context, Size size, Size alignto, int flags)
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)
MemoryContext TopTransactionContext
char * pchomp(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)
char * pstrdup(const char *in)
void pfree(void *pointer)
Size GetMemoryChunkSpace(void *pointer)
void * repalloc0(void *pointer, Size oldsize, Size size)
static Size BogusGetChunkSpace(void *pointer)
void MemoryContextDeleteChildren(MemoryContext context)
MemoryContext TopMemoryContext
#define AssertNotInCriticalSection(context)
void * palloc0(Size size)
void * MemoryContextAllocZero(MemoryContext context, Size size)
MemoryContext CurTransactionContext
void * MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
MemoryContext CurrentMemoryContext
void * repalloc(void *pointer, Size size)
static MemoryContext MemoryContextTraverseNext(MemoryContext curr, MemoryContext top)
void * palloc_extended(Size size, int flags)
MemoryContext GetMemoryChunkContext(void *pointer)
void * MemoryContextAlloc(MemoryContext context, Size size)
void MemoryContextStatsDetail(MemoryContext context, int max_level, int max_children, bool print_to_stderr)
Size MemoryContextMemAllocated(MemoryContext context, bool recurse)
void MemoryContextStats(MemoryContext context)
void MemoryContextInit(void)
static void BogusFree(void *pointer)
MemoryContext PostmasterContext
void * palloc_aligned(Size size, Size alignto, int flags)
char * MemoryContextStrdup(MemoryContext context, const char *string)
static const MemoryContextMethods mcxt_methods[]
void * MemoryContextAllocationFailure(MemoryContext context, 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 MemoryContextSetIdentifier(MemoryContext context, const char *id)
void MemoryContextResetOnly(MemoryContext context)
static uint64 GetMemoryChunkHeader(const void *pointer)
MemoryContext PortalContext
void * repalloc_huge(void *pointer, Size size)
void * repalloc_extended(void *pointer, Size size, int flags)
#define MCXT_METHOD(pointer, method)
#define VALGRIND_DESTROY_MEMPOOL(context)
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
#define VALGRIND_MEMPOOL_CHANGE(context, optr, nptr, size)
#define VALGRIND_CREATE_MEMPOOL(context, redzones, zeroed)
#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_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)
bool stack_is_too_deep(void)
static pg_noinline void Size size
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 SlabStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
void SlabDelete(MemoryContext context)
void * SlabRealloc(void *pointer, Size size, int flags)
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)