mirror of
https://github.com/postgres/postgres.git
synced 2025-05-03 22:24:49 +03:00
Use Size instead of int64 to track allocated memory
Commit 5dd7fc1519 added block-level memory accounting, but used int64 variable to track the amount of allocated memory. That is incorrect, because we have Size for exactly these purposes, but it was mostly harmless until c477f3e449 which changed how we handle with repalloc() when downsizing the chunk. Previously we've ignored these cases and just kept using the original chunk, but now we need to update the accounting, and the code was doing this: context->mem_allocated += blksize - oldblksize; Both blksize and oldblksize are Size (so unsigned) which means the subtraction underflows, producing a very high positive value. On 64-bit platforms (where Size has the same size as mem_alllocated) this happens to work because the result wraps to the right value, but on (some) 32-bit platforms this fails. This fixes two things - it changes mem_allocated (and related variables) to Size, and it splits the update to two separate steps, to prevent any underflows. Discussion: https://www.postgresql.org/message-id/15151.1570163761%40sss.pgh.pa.us
This commit is contained in:
parent
967e276e9f
commit
f2369bc610
@ -1154,7 +1154,9 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
context->mem_allocated += blksize - oldblksize;
|
/* updated separately, not to underflow when (oldblksize > blksize) */
|
||||||
|
context->mem_allocated -= oldblksize;
|
||||||
|
context->mem_allocated += blksize;
|
||||||
|
|
||||||
block->freeptr = block->endptr = ((char *) block) + blksize;
|
block->freeptr = block->endptr = ((char *) block) + blksize;
|
||||||
|
|
||||||
@ -1427,7 +1429,7 @@ AllocSetCheck(MemoryContext context)
|
|||||||
const char *name = set->header.name;
|
const char *name = set->header.name;
|
||||||
AllocBlock prevblock;
|
AllocBlock prevblock;
|
||||||
AllocBlock block;
|
AllocBlock block;
|
||||||
int64 total_allocated = 0;
|
Size total_allocated = 0;
|
||||||
|
|
||||||
for (prevblock = NULL, block = set->blocks;
|
for (prevblock = NULL, block = set->blocks;
|
||||||
block != NULL;
|
block != NULL;
|
||||||
|
@ -753,7 +753,7 @@ GenerationCheck(MemoryContext context)
|
|||||||
GenerationContext *gen = (GenerationContext *) context;
|
GenerationContext *gen = (GenerationContext *) context;
|
||||||
const char *name = context->name;
|
const char *name = context->name;
|
||||||
dlist_iter iter;
|
dlist_iter iter;
|
||||||
int64 total_allocated = 0;
|
Size total_allocated = 0;
|
||||||
|
|
||||||
/* walk all blocks in this context */
|
/* walk all blocks in this context */
|
||||||
dlist_foreach(iter, &gen->blocks)
|
dlist_foreach(iter, &gen->blocks)
|
||||||
|
@ -79,7 +79,7 @@ typedef struct MemoryContextData
|
|||||||
/* these two fields are placed here to minimize alignment wastage: */
|
/* these two fields are placed here to minimize alignment wastage: */
|
||||||
bool isReset; /* T = no space alloced since last reset */
|
bool isReset; /* T = no space alloced since last reset */
|
||||||
bool allowInCritSection; /* allow palloc in critical section */
|
bool allowInCritSection; /* allow palloc in critical section */
|
||||||
int64 mem_allocated; /* track memory allocated for this context */
|
Size mem_allocated; /* track memory allocated for this context */
|
||||||
const MemoryContextMethods *methods; /* virtual function table */
|
const MemoryContextMethods *methods; /* virtual function table */
|
||||||
MemoryContext parent; /* NULL if no parent (toplevel context) */
|
MemoryContext parent; /* NULL if no parent (toplevel context) */
|
||||||
MemoryContext firstchild; /* head of linked list of children */
|
MemoryContext firstchild; /* head of linked list of children */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user