83 #define ALLOC_MINBITS 3
84 #define ALLOCSET_NUM_FREELISTS 11
85 #define ALLOC_CHUNK_LIMIT (1 << (ALLOCSET_NUM_FREELISTS-1+ALLOC_MINBITS))
87 #define ALLOC_CHUNK_FRACTION 4
104 #define ALLOC_BLOCKHDRSZ MAXALIGN(sizeof(AllocBlockData))
105 #define ALLOC_CHUNKHDRSZ sizeof(MemoryChunk)
132 #define GetFreeListLink(chkptr) \
133 (AllocFreeListLink *) ((char *) (chkptr) + ALLOC_CHUNKHDRSZ)
136 #define FreeListIdxIsValid(fidx) \
137 ((fidx) >= 0 && (fidx) < ALLOCSET_NUM_FREELISTS)
140 #define GetChunkSizeFromFreeListIdx(fidx) \
141 ((((Size) 1) << ALLOC_MINBITS) << (fidx))
194 #define AllocPointerIsValid(pointer) PointerIsValid(pointer)
200 #define AllocSetIsValid(set) \
201 (PointerIsValid(set) && IsA(set, AllocSetContext))
207 #define AllocBlockIsValid(block) \
208 (PointerIsValid(block) && AllocSetIsValid((block)->aset))
215 #define ExternalChunkGetBlock(chunk) \
216 (AllocBlock) ((char *) chunk - ALLOC_BLOCKHDRSZ)
241 #define MAX_FREE_CONTEXTS 100
244 #define KeeperBlock(set) \
245 ((AllocBlock) (((char *) set) + MAXALIGN(sizeof(AllocSetContext))))
248 #define IsKeeperBlock(set, block) ((block) == (KeeperBlock(set)))
300 #ifdef HAVE_BITSCAN_REVERSE
308 "ALLOC_CHUNK_LIMIT must be less than 64kB");
360 "sizeof(MemoryChunk) is not maxaligned");
363 "sizeof(AllocFreeListLink) larger than minimum allocation size");
378 initBlockSize >= 1024);
380 maxBlockSize >= initBlockSize &&
382 Assert(minContextSize == 0 ||
383 (minContextSize ==
MAXALIGN(minContextSize) &&
384 minContextSize >= 1024 &&
385 minContextSize <= maxBlockSize));
404 if (freeListIndex >= 0)
435 if (minContextSize != 0)
436 firstBlockSize =
Max(firstBlockSize, minContextSize);
438 firstBlockSize =
Max(firstBlockSize, initBlockSize);
450 (
errcode(ERRCODE_OUT_OF_MEMORY),
452 errdetail(
"Failed while creating memory context \"%s\".",
465 block->
endptr = ((
char *) set) + firstBlockSize;
496 "ALLOC_CHUNK_LIMIT != ALLOCSET_SEPARATE_THRESHOLD");
545 #ifdef MEMORY_CONTEXT_CHECKING
547 AllocSetCheck(context);
551 keepersize =
KeeperBlock(set)->endptr - ((
char *) set);
561 while (block != NULL)
570 #ifdef CLOBBER_FREED_MEMORY
571 wipe_mem(datastart, block->
freeptr - datastart);
585 #ifdef CLOBBER_FREED_MEMORY
586 wipe_mem(block, block->
freeptr - ((
char *) block));
615 #ifdef MEMORY_CONTEXT_CHECKING
617 AllocSetCheck(context);
621 keepersize =
KeeperBlock(set)->endptr - ((
char *) set);
666 while (block != NULL)
673 #ifdef CLOBBER_FREED_MEMORY
674 wipe_mem(block, block->
freeptr - ((
char *) block));
720 #ifdef MEMORY_CONTEXT_CHECKING
742 #ifdef MEMORY_CONTEXT_CHECKING
743 chunk->requested_size = size;
745 Assert(size < chunk_size);
748 #ifdef RANDOMIZE_ALLOCATED_MEMORY
810 #ifdef MEMORY_CONTEXT_CHECKING
811 chunk->requested_size = size;
816 #ifdef RANDOMIZE_ALLOCATED_MEMORY
835 Assert(chunk_size >= size);
841 if ((block = set->
blocks) != NULL)
886 #ifdef MEMORY_CONTEXT_CHECKING
924 while (blksize < required_size)
934 while (block == NULL && blksize > 1024 * 1024)
937 if (blksize < required_size)
949 block->
endptr = ((
char *) block) + blksize;
976 #ifdef MEMORY_CONTEXT_CHECKING
977 chunk->requested_size = size;
979 if (size < chunk_size)
982 #ifdef RANDOMIZE_ALLOCATED_MEMORY
1020 elog(
ERROR,
"could not find block containing chunk %p", chunk);
1024 #ifdef MEMORY_CONTEXT_CHECKING
1027 Assert(chunk->requested_size < (block->
endptr - (
char *) pointer));
1028 if (!sentinel_ok(pointer, chunk->requested_size))
1029 elog(
WARNING,
"detected write past chunk end in %s %p",
1044 #ifdef CLOBBER_FREED_MEMORY
1045 wipe_mem(block, block->
freeptr - ((
char *) block));
1068 #ifdef MEMORY_CONTEXT_CHECKING
1071 if (!sentinel_ok(pointer, chunk->requested_size))
1072 elog(
WARNING,
"detected write past chunk end in %s %p",
1076 #ifdef CLOBBER_FREED_MEMORY
1085 #ifdef MEMORY_CONTEXT_CHECKING
1138 elog(
ERROR,
"could not find block containing chunk %p", chunk);
1142 oldchksize = block->
endptr - (
char *) pointer;
1144 #ifdef MEMORY_CONTEXT_CHECKING
1146 Assert(chunk->requested_size < oldchksize);
1147 if (!sentinel_ok(pointer, chunk->requested_size))
1148 elog(
WARNING,
"detected write past chunk end in %s %p",
1152 #ifdef MEMORY_CONTEXT_CHECKING
1161 oldblksize = block->
endptr - ((
char *) block);
1175 block->
freeptr = block->
endptr = ((
char *) block) + blksize;
1187 #ifdef MEMORY_CONTEXT_CHECKING
1188 #ifdef RANDOMIZE_ALLOCATED_MEMORY
1194 if (size > chunk->requested_size)
1195 randomize_mem((
char *) pointer + chunk->requested_size,
1196 size - chunk->requested_size);
1208 if (
Min(size, oldchksize) > chunk->requested_size)
1210 Min(size, oldchksize) - chunk->requested_size);
1214 chunk->requested_size = size;
1217 set_sentinel(pointer, size);
1255 #ifdef MEMORY_CONTEXT_CHECKING
1257 if (chunk->requested_size < oldchksize)
1258 if (!sentinel_ok(pointer, chunk->requested_size))
1259 elog(
WARNING,
"detected write past chunk end in %s %p",
1268 if (oldchksize >= size)
1270 #ifdef MEMORY_CONTEXT_CHECKING
1271 Size oldrequest = chunk->requested_size;
1273 #ifdef RANDOMIZE_ALLOCATED_MEMORY
1275 if (size > oldrequest)
1276 randomize_mem((
char *) pointer + oldrequest,
1280 chunk->requested_size = size;
1286 if (size > oldrequest)
1294 if (size < oldchksize)
1295 set_sentinel(pointer, size);
1332 if (newPointer == NULL)
1348 #ifdef MEMORY_CONTEXT_CHECKING
1349 oldsize = chunk->requested_size;
1351 oldsize = oldchksize;
1356 memcpy(newPointer, pointer, oldsize);
1416 return block->
endptr - (
char *) chunk;
1464 Size freechunks = 0;
1475 for (block = set->
blocks; block != NULL; block = block->
next)
1478 totalspace += block->
endptr - ((
char *) block);
1486 while (chunk != NULL)
1506 char stats_string[200];
1508 snprintf(stats_string,
sizeof(stats_string),
1509 "%zu total in %zu blocks; %zu free (%zu chunks); %zu used",
1510 totalspace, nblocks, freespace, freechunks,
1511 totalspace - freespace);
1512 printfunc(context, passthru, stats_string, print_to_stderr);
1525 #ifdef MEMORY_CONTEXT_CHECKING
1542 Size total_allocated = 0;
1544 for (prevblock = NULL, block = set->
blocks;
1546 prevblock = block, block = block->
next)
1549 long blk_used = block->
freeptr - bpoz;
1552 bool has_external_chunk =
false;
1555 total_allocated += block->
endptr - ((
char *) set);
1557 total_allocated += block->
endptr - ((
char *) block);
1565 elog(
WARNING,
"problem in alloc set %s: empty block %p",
1572 if (block->
aset != set ||
1573 block->
prev != prevblock ||
1576 elog(
WARNING,
"problem in alloc set %s: corrupt header in block %p",
1582 while (bpoz < block->freeptr)
1594 has_external_chunk =
true;
1598 elog(
WARNING,
"problem in alloc set %s: bad single-chunk %p in block %p",
1599 name, chunk, block);
1606 elog(
WARNING,
"problem in alloc set %s: bad chunk size for chunk %p in block %p",
1607 name, chunk, block);
1616 elog(
WARNING,
"problem in alloc set %s: bad block offset for chunk %p in block %p",
1617 name, chunk, block);
1619 dsize = chunk->requested_size;
1623 elog(
WARNING,
"problem in alloc set %s: req size > alloc size for chunk %p in block %p",
1624 name, chunk, block);
1628 elog(
WARNING,
"problem in alloc set %s: bad size %zu for chunk %p in block %p",
1629 name, chsize, chunk, block);
1636 elog(
WARNING,
"problem in alloc set %s: detected write past chunk end in block %p, chunk %p",
1637 name, block, chunk);
1650 elog(
WARNING,
"problem in alloc set %s: found inconsistent memory block %p",
1653 if (has_external_chunk && nchunks > 1)
1654 elog(
WARNING,
"problem in alloc set %s: external chunk on non-dedicated block %p",
Datum idx(PG_FUNCTION_ARGS)
void AllocSetReset(MemoryContext context)
#define AllocSetIsValid(set)
#define AllocBlockIsValid(block)
#define IsKeeperBlock(set, block)
#define GetFreeListLink(chkptr)
#define FreeListIdxIsValid(fidx)
Size AllocSetGetChunkSpace(void *pointer)
MemoryContext AllocSetGetChunkContext(void *pointer)
void AllocSetStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
#define GetChunkSizeFromFreeListIdx(fidx)
struct AllocBlockData * AllocBlock
#define MAX_FREE_CONTEXTS
static int AllocSetFreeIndex(Size size)
bool AllocSetIsEmpty(MemoryContext context)
struct AllocBlockData AllocBlockData
void * AllocSetRealloc(void *pointer, Size size)
#define ALLOCSET_NUM_FREELISTS
struct AllocSetContext AllocSetContext
#define ALLOC_CHUNK_FRACTION
void AllocSetFree(void *pointer)
void AllocSetDelete(MemoryContext context)
struct AllocSetFreeList AllocSetFreeList
#define ALLOC_CHUNK_LIMIT
struct AllocFreeListLink AllocFreeListLink
static AllocSetFreeList context_freelists[2]
#define ExternalChunkGetBlock(chunk)
void * AllocSetAlloc(MemoryContext context, Size size)
MemoryContext AllocSetContextCreateInternal(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
AllocSetContext * AllocSet
#define PG_USED_FOR_ASSERTS_ONLY
#define MemSetAligned(start, val, len)
#define StaticAssertDecl(condition, errmessage)
#define StaticAssertStmt(condition, errmessage)
elog(ERROR, "%s: %s", p2, msg)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
Assert(fmt[strlen(fmt) - 1] !='\n')
void MemoryContextCreate(MemoryContext node, NodeTag tag, MemoryContextMethodID method_id, MemoryContext parent, const char *name)
MemoryContext TopMemoryContext
void MemoryContextStats(MemoryContext context)
void MemoryContextResetOnly(MemoryContext context)
#define VALGRIND_MAKE_MEM_DEFINED(addr, size)
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
void(* MemoryStatsPrintFunc)(MemoryContext context, void *passthru, const char *stats_string, bool print_to_stderr)
#define ALLOCSET_SMALL_MINSIZE
#define ALLOCSET_DEFAULT_MINSIZE
#define AllocHugeSizeIsValid(size)
#define ALLOCSET_SEPARATE_THRESHOLD
#define ALLOCSET_SMALL_INITSIZE
#define ALLOCSET_DEFAULT_INITSIZE
#define MEMORYCHUNK_MAX_BLOCKOFFSET
static Size MemoryChunkGetValue(MemoryChunk *chunk)
#define MemoryChunkGetPointer(c)
static bool MemoryChunkIsExternal(MemoryChunk *chunk)
static void MemoryChunkSetHdrMaskExternal(MemoryChunk *chunk, MemoryContextMethodID methodid)
static void * MemoryChunkGetBlock(MemoryChunk *chunk)
#define PointerGetMemoryChunk(p)
static void MemoryChunkSetHdrMask(MemoryChunk *chunk, void *block, Size value, MemoryContextMethodID methodid)
struct MemoryContextData * MemoryContext
static int pg_leftmost_one_pos32(uint32 word)
PGDLLIMPORT const uint8 pg_leftmost_one_pos[256]
MemoryChunk * freelist[ALLOCSET_NUM_FREELISTS]
AllocSetContext * first_free
int link(const char *src, const char *dst)