40 #define BOGUS_MCTX(id) \
41 [id].free_p = BogusFree, \
42 [id].realloc = BogusRealloc, \
43 [id].get_chunk_context = BogusGetChunkContext, \
44 [id].get_chunk_space = BogusGetChunkSpace
57 #ifdef MEMORY_CONTEXT_CHECKING
71 #ifdef MEMORY_CONTEXT_CHECKING
85 #ifdef MEMORY_CONTEXT_CHECKING
99 #ifdef MEMORY_CONTEXT_CHECKING
113 #ifdef MEMORY_CONTEXT_CHECKING
163 int max_level,
int max_children,
165 bool print_to_stderr);
167 const char *stats_string,
168 bool print_to_stderr);
175 #define AssertNotInCriticalSection(context) \
176 Assert(CritSectionCount == 0 || (context)->allowInCritSection)
182 #define MCXT_METHOD(pointer, method) \
183 mcxt_methods[GetMemoryChunkMethodID(pointer)].method
205 header = *((
const uint64 *) ((
const char *) pointer -
sizeof(
uint64)));
227 header = *((
const uint64 *) ((
const char *) pointer -
sizeof(
uint64)));
288 elog(
ERROR,
"pfree called with invalid pointer %p (header 0x%016llx)",
295 elog(
ERROR,
"repalloc called with invalid pointer %p (header 0x%016llx)",
303 elog(
ERROR,
"GetMemoryChunkContext called with invalid pointer %p (header 0x%016llx)",
311 elog(
ERROR,
"GetMemoryChunkSpace called with invalid pointer %p (header 0x%016llx)",
388 if (
context->firstchild != NULL)
547 while (
context->firstchild != NULL)
594 while ((cb =
context->reset_cbs) != NULL)
643 if (new_parent ==
context->parent)
651 if (
context->prevchild != NULL)
659 if (
context->nextchild != NULL)
698 context->allowInCritSection = allow;
709 return MCXT_METHOD(pointer, get_chunk_context) (pointer);
723 return MCXT_METHOD(pointer, get_chunk_space) (pointer);
751 if (
context->firstchild != NULL)
774 total += curr->mem_allocated;
791 memset(consumed, 0,
sizeof(*consumed));
801 curr->methods->stats(curr, NULL, NULL, consumed,
false);
830 int max_level,
int max_children,
831 bool print_to_stderr)
835 memset(&grand_totals, 0,
sizeof(grand_totals));
838 &grand_totals, print_to_stderr);
842 "Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used\n",
861 errmsg_internal(
"Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used",
877 int max_level,
int max_children,
879 bool print_to_stderr)
890 totals, print_to_stderr);
904 for (; child != NULL && ichild < max_children;
908 max_level, max_children,
919 memset(&local_totals, 0,
sizeof(local_totals));
922 while (child != NULL)
924 child->
methods->
stats(child, NULL, NULL, &local_totals,
false);
931 for (
int i = 0;
i <= level;
i++)
934 "%d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used\n",
946 errmsg_internal(
"level: %d; %d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used",
974 const char *stats_string,
975 bool print_to_stderr)
977 int level = *(
int *) passthru;
980 char truncated_ident[110];
988 if (
ident && strcmp(
name,
"dynahash") == 0)
994 truncated_ident[0] =
'\0';
1004 int idlen = strlen(
ident);
1005 bool truncated =
false;
1007 strcpy(truncated_ident,
": ");
1008 i = strlen(truncated_ident);
1018 unsigned char c = *
ident++;
1022 truncated_ident[
i++] =
c;
1024 truncated_ident[
i] =
'\0';
1027 strcat(truncated_ident,
"...");
1030 if (print_to_stderr)
1032 for (
i = 0;
i < level;
i++)
1034 fprintf(stderr,
"%s: %s%s\n",
name, stats_string, truncated_ident);
1041 level,
name, stats_string, truncated_ident)));
1050 #ifdef MEMORY_CONTEXT_CHECKING
1062 curr->methods->check(curr);
1154 (
errcode(ERRCODE_OUT_OF_MEMORY),
1156 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1418 Assert(alignto < (128 * 1024 * 1024));
1421 Assert((alignto & (alignto - 1)) == 0);
1427 if (
unlikely(alignto <= MAXIMUM_ALIGNOF))
1449 #ifdef MEMORY_CONTEXT_CHECKING
1458 aligned = (
void *)
TYPEALIGN(alignto, (
char *) unaligned +
1479 #ifdef MEMORY_CONTEXT_CHECKING
1480 alignedchunk->requested_size =
size;
1482 set_sentinel(aligned,
size);
1487 (
char *) alignedchunk - (
char *) unaligned);
1546 #if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
1584 #if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
1625 elog(
ERROR,
"invalid repalloc0 call: oldsize %zu, new size %zu",
1629 memset((
char *) ret + oldsize, 0, (
size - oldsize));
1686 Size len = strlen(
string) + 1;
1690 memcpy(nstr,
string,
len);
1714 memcpy(out, in,
len);
1729 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)
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 * BumpRealloc(void *pointer, Size size, int flags)
void * BumpAlloc(MemoryContext context, Size size, int flags)
void BumpReset(MemoryContext context)
bool BumpIsEmpty(MemoryContext context)
#define TYPEALIGN(ALIGNVAL, LEN)
#define Assert(condition)
#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
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_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)
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(* stats)(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
void *(* alloc)(MemoryContext context, Size size, int flags)