29 #include "utils/fmgrprotos.h"
56 #ifdef MEMORY_CONTEXT_CHECKING
70 #ifdef MEMORY_CONTEXT_CHECKING
84 #ifdef MEMORY_CONTEXT_CHECKING
98 #ifdef MEMORY_CONTEXT_CHECKING
154 bool print,
int max_children,
156 bool print_to_stderr);
158 const char *stats_string,
159 bool print_to_stderr);
166 #define AssertNotInCriticalSection(context) \
167 Assert(CritSectionCount == 0 || (context)->allowInCritSection)
173 #define MCXT_METHOD(pointer, method) \
174 mcxt_methods[GetMemoryChunkMethodID(pointer)].method
196 header = *((
const uint64 *) ((
const char *) pointer -
sizeof(uint64)));
218 header = *((
const uint64 *) ((
const char *) pointer -
sizeof(uint64)));
235 elog(
ERROR,
"pfree called with invalid pointer %p (header 0x%016llx)",
242 elog(
ERROR,
"repalloc called with invalid pointer %p (header 0x%016llx)",
250 elog(
ERROR,
"GetMemoryChunkContext called with invalid pointer %p (header 0x%016llx)",
258 elog(
ERROR,
"GetMemoryChunkSpace called with invalid pointer %p (header 0x%016llx)",
435 context->
ident = NULL;
503 while ((cb = context->
reset_cbs) != NULL)
549 Assert(context != new_parent);
552 if (new_parent == context->
parent)
576 context->
parent = new_parent;
618 return MCXT_METHOD(pointer, get_chunk_context) (pointer);
632 return MCXT_METHOD(pointer, get_chunk_space) (pointer);
715 bool print_to_stderr)
719 memset(&grand_totals, 0,
sizeof(grand_totals));
725 "Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used\n",
744 errmsg_internal(
"Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used",
759 bool print,
int max_children,
761 bool print_to_stderr)
773 totals, print_to_stderr);
779 memset(&local_totals, 0,
sizeof(local_totals));
785 if (ichild < max_children)
798 if (ichild > max_children)
806 for (
i = 0;
i <= level;
i++)
809 "%d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used\n",
810 ichild - max_children,
821 errmsg_internal(
"level: %d; %d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used",
823 ichild - max_children,
850 const char *stats_string,
851 bool print_to_stderr)
853 int level = *(
int *) passthru;
856 char truncated_ident[110];
864 if (
ident && strcmp(
name,
"dynahash") == 0)
870 truncated_ident[0] =
'\0';
880 int idlen = strlen(
ident);
881 bool truncated =
false;
883 strcpy(truncated_ident,
": ");
884 i = strlen(truncated_ident);
894 unsigned char c = *
ident++;
898 truncated_ident[
i++] =
c;
900 truncated_ident[
i] =
'\0';
903 strcat(truncated_ident,
"...");
908 for (
i = 0;
i < level;
i++)
910 fprintf(stderr,
"%s: %s%s\n",
name, stats_string, truncated_ident);
917 level,
name, stats_string, truncated_ident)));
926 #ifdef MEMORY_CONTEXT_CHECKING
934 context->
methods->check(context);
936 MemoryContextCheck(child);
1029 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1045 (
errcode(ERRCODE_OUT_OF_MEMORY),
1047 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1048 size, context->
name)));
1072 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1081 (
errcode(ERRCODE_OUT_OF_MEMORY),
1083 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1084 size, context->
name)));
1110 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1119 (
errcode(ERRCODE_OUT_OF_MEMORY),
1121 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1122 size, context->
name)));
1146 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1157 (
errcode(ERRCODE_OUT_OF_MEMORY),
1159 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1160 size, context->
name)));
1236 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1245 (
errcode(ERRCODE_OUT_OF_MEMORY),
1247 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1248 size, context->
name)));
1267 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1276 (
errcode(ERRCODE_OUT_OF_MEMORY),
1278 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1279 size, context->
name)));
1301 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1312 (
errcode(ERRCODE_OUT_OF_MEMORY),
1314 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1315 size, context->
name)));
1345 Size size,
Size alignto,
int flags)
1353 Assert(alignto < (128 * 1024 * 1024));
1356 Assert((alignto & (alignto - 1)) == 0);
1362 if (
unlikely(alignto <= MAXIMUM_ALIGNOF))
1384 #ifdef MEMORY_CONTEXT_CHECKING
1393 aligned = (
void *)
TYPEALIGN(alignto, (
char *) unaligned +
1414 #ifdef MEMORY_CONTEXT_CHECKING
1415 alignedchunk->requested_size = size;
1417 set_sentinel(aligned, size);
1422 (
char *) alignedchunk - (
char *) unaligned);
1481 #if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
1487 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1501 (
errcode(ERRCODE_OUT_OF_MEMORY),
1503 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1523 #if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
1530 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1546 (
errcode(ERRCODE_OUT_OF_MEMORY),
1548 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1571 elog(
ERROR,
"invalid repalloc0 call: oldsize %zu, new size %zu",
1575 memset((
char *) ret + oldsize, 0, (size - oldsize));
1594 elog(
ERROR,
"invalid memory alloc request size %zu", size);
1603 (
errcode(ERRCODE_OUT_OF_MEMORY),
1605 errdetail(
"Failed on request of size %zu in memory context \"%s\".",
1606 size, context->
name)));
1634 Size len = strlen(
string) + 1;
1638 memcpy(nstr,
string,
len);
1662 memcpy(out, in,
len);
1677 while (n > 0 && in[n - 1] ==
'\n')
MemoryContext AlignedAllocGetChunkContext(void *pointer)
void * AlignedAllocRealloc(void *pointer, Size size)
Size AlignedAllocGetChunkSpace(void *pointer)
void AlignedAllocFree(void *pointer)
void AllocSetReset(MemoryContext context)
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 * AllocSetRealloc(void *pointer, Size size)
void AllocSetFree(void *pointer)
void AllocSetDelete(MemoryContext context)
void * AllocSetAlloc(MemoryContext context, Size size)
void print(const void *obj)
#define TYPEALIGN(ALIGNVAL, LEN)
#define MemSetAligned(start, val, len)
#define MemSetLoop(start, val, len)
elog(ERROR, "%s: %s", p2, 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 GenerationReset(MemoryContext context)
void GenerationFree(void *pointer)
MemoryContext GenerationGetChunkContext(void *pointer)
Size GenerationGetChunkSpace(void *pointer)
void * GenerationAlloc(MemoryContext context, Size size)
bool GenerationIsEmpty(MemoryContext context)
void GenerationStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
void * GenerationRealloc(void *pointer, Size size)
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)
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 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)
void MemoryContextStatsDetail(MemoryContext context, int max_children, bool print_to_stderr)
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)
void * palloc_extended(Size size, int flags)
MemoryContext GetMemoryChunkContext(void *pointer)
void * MemoryContextAlloc(MemoryContext context, Size size)
void * MemoryContextAllocZeroAligned(MemoryContext context, Size size)
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[]
static void MemoryContextStatsInternal(MemoryContext context, int level, bool print, int max_children, MemoryContextCounters *totals, bool print_to_stderr)
MemoryContext MemoryContextGetParent(MemoryContext context)
static void * BogusRealloc(void *pointer, Size size)
void ProcessLogMemoryContextInterrupt(void)
MemoryContext ErrorContext
static MemoryContext BogusGetChunkContext(void *pointer)
MemoryContext CacheMemoryContext
void * MemoryContextAllocHuge(MemoryContext context, Size size)
void MemoryContextDelete(MemoryContext context)
void MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)
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)
void * SlabAlloc(MemoryContext context, Size size)
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 * SlabRealloc(void *pointer, Size size)
void SlabDelete(MemoryContext context)
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(* reset)(MemoryContext context)
void *(* alloc)(MemoryContext context, Size size)