46 #define Generation_BLOCKHDRSZ MAXALIGN(sizeof(GenerationBlock)) 47 #define Generation_CHUNKHDRSZ sizeof(GenerationChunk) 107 #ifdef MEMORY_CONTEXT_CHECKING 112 #define GENERATIONCHUNK_RAWSIZE (SIZEOF_SIZE_T * 2 + SIZEOF_VOID_P * 2) 114 #define GENERATIONCHUNK_RAWSIZE (SIZEOF_SIZE_T + SIZEOF_VOID_P * 2) 118 #if (GENERATIONCHUNK_RAWSIZE % MAXIMUM_ALIGNOF) != 0 133 #define GENERATIONCHUNK_PRIVATE_LEN offsetof(GenerationChunk, context) 139 #define GenerationIsValid(set) PointerIsValid(set) 141 #define GenerationPointerGetChunk(ptr) \ 142 ((GenerationChunk *)(((char *)(ptr)) - Generation_CHUNKHDRSZ)) 143 #define GenerationChunkGetPointer(chk) \ 144 ((GenerationPointer *)(((char *)(chk)) + Generation_CHUNKHDRSZ)) 160 #ifdef MEMORY_CONTEXT_CHECKING 176 #ifdef MEMORY_CONTEXT_CHECKING 204 "sizeof(GenerationChunk) is not maxaligned");
207 "padding calculation in GenerationChunk is wrong");
215 if (blockSize !=
MAXALIGN(blockSize) ||
218 elog(
ERROR,
"invalid blockSize for memory context: %zu",
232 (
errcode(ERRCODE_OUT_OF_MEMORY),
234 errdetail(
"Failed while creating memory context \"%s\".",
273 #ifdef MEMORY_CONTEXT_CHECKING 275 GenerationCheck(context);
286 #ifdef CLOBBER_FREED_MEMORY 287 wipe_mem(block, block->
blksize);
333 if (chunk_size > set->blockSize / 8)
354 chunk->
size = chunk_size;
356 #ifdef MEMORY_CONTEXT_CHECKING 357 chunk->requested_size = size;
359 if (size < chunk_size)
362 #ifdef RANDOMIZE_ALLOCATED_MEMORY 386 if ((block == NULL) ||
389 Size blksize =
set->blockSize;
403 block->
endptr = ((
char *) block) + blksize;
432 chunk->
size = chunk_size;
434 #ifdef MEMORY_CONTEXT_CHECKING 435 chunk->requested_size = size;
437 if (size < chunk->size)
440 #ifdef RANDOMIZE_ALLOCATED_MEMORY 470 block = chunk->
block;
472 #ifdef MEMORY_CONTEXT_CHECKING 474 if (chunk->requested_size < chunk->
size)
475 if (!sentinel_ok(pointer, chunk->requested_size))
476 elog(
WARNING,
"detected write past chunk end in %s %p",
480 #ifdef CLOBBER_FREED_MEMORY 481 wipe_mem(pointer, chunk->
size);
487 #ifdef MEMORY_CONTEXT_CHECKING 489 chunk->requested_size = 0;
508 if (set->block == block)
532 oldsize = chunk->
size;
534 #ifdef MEMORY_CONTEXT_CHECKING 536 if (chunk->requested_size < oldsize)
537 if (!sentinel_ok(pointer, chunk->requested_size))
538 elog(
WARNING,
"detected write past chunk end in %s %p",
554 #ifdef MEMORY_CONTEXT_CHECKING 555 Size oldrequest = chunk->requested_size;
557 #ifdef RANDOMIZE_ALLOCATED_MEMORY 559 if (size > oldrequest)
560 randomize_mem((
char *) pointer + oldrequest,
564 chunk->requested_size = size;
570 if (size > oldrequest)
579 set_sentinel(pointer, size);
601 if (newPointer == NULL)
617 #ifdef MEMORY_CONTEXT_CHECKING 618 oldsize = chunk->requested_size;
624 memcpy(newPointer, pointer, oldsize);
680 Size nfreechunks = 0;
694 nfreechunks += block->
nfree;
701 char stats_string[200];
703 snprintf(stats_string,
sizeof(stats_string),
704 "%zu total in %zd blocks (%zd chunks); %zu free (%zd chunks); %zu used",
705 totalspace, nblocks, nchunks, freespace,
706 nfreechunks, totalspace - freespace);
707 printfunc(context, passthru, stats_string);
720 #ifdef MEMORY_CONTEXT_CHECKING 736 Size total_allocated = 0;
746 total_allocated += block->
blksize;
753 elog(
WARNING,
"problem in Generation %s: number of free chunks %d in block %p exceeds %d allocated",
761 while (ptr < block->freeptr)
774 if (chunk->
block != block)
775 elog(
WARNING,
"problem in Generation %s: bogus block link in block %p, chunk %p",
783 if ((chunk->requested_size > 0 && chunk->
context != gen) ||
785 elog(
WARNING,
"problem in Generation %s: bogus context link in block %p, chunk %p",
789 if (chunk->
size < chunk->requested_size ||
791 elog(
WARNING,
"problem in Generation %s: bogus chunk size in block %p, chunk %p",
798 if (chunk->requested_size < chunk->
size &&
800 elog(
WARNING,
"problem in Generation %s: detected write past chunk end in block %p, chunk %p",
819 elog(
WARNING,
"problem in Generation %s: number of allocated chunks %d in block %p does not match header %d",
820 name, nchunks, block, block->
nchunks);
822 if (nfree != block->
nfree)
823 elog(
WARNING,
"problem in Generation %s: number of free chunks %d in block %p does not match header %d",
824 name, nfree, block, block->
nfree);
static void GenerationDelete(MemoryContext context)
#define GenerationPointerGetChunk(ptr)
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
void(* MemoryStatsPrintFunc)(MemoryContext context, void *passthru, const char *stats_string)
#define dlist_foreach_modify(iter, lhead)
static void GenerationStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals)
static void dlist_push_head(dlist_head *head, dlist_node *node)
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
#define dlist_foreach(iter, lhead)
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
static void GenerationReset(MemoryContext context)
#define GENERATIONCHUNK_RAWSIZE
int errcode(int sqlerrcode)
#define GENERATIONCHUNK_PRIVATE_LEN
#define StaticAssertStmt(condition, errmessage)
#define dlist_container(type, membername, ptr)
#define GenerationIsValid(set)
void MemoryContextStats(MemoryContext context)
GenerationContext * context
int errdetail(const char *fmt,...)
static Size GenerationGetChunkSpace(MemoryContext context, void *pointer)
static const MemoryContextMethods GenerationMethods
static void dlist_delete(dlist_node *node)
static void GenerationFree(MemoryContext context, void *pointer)
void MemoryContextCreate(MemoryContext node, NodeTag tag, const MemoryContextMethods *methods, MemoryContext parent, const char *name)
#define AssertArg(condition)
MemoryContext TopMemoryContext
#define GenerationChunkGetPointer(chk)
MemoryContext GenerationContextCreate(MemoryContext parent, const char *name, Size blockSize)
struct GenerationContext GenerationContext
static void dlist_init(dlist_head *head)
#define ereport(elevel,...)
#define Assert(condition)
static bool dlist_is_empty(dlist_head *head)
static void * GenerationRealloc(MemoryContext context, void *pointer, Size size)
#define AllocHugeSizeIsValid(size)
int errmsg(const char *fmt,...)
static bool GenerationIsEmpty(MemoryContext context)
#define Generation_CHUNKHDRSZ
static void * GenerationAlloc(MemoryContext context, Size size)
#define Generation_BLOCKHDRSZ
#define offsetof(type, field)