Skip to content

Commit 2fd6a44

Browse files
committed
Revert "Specialize MemoryContextMemAllocated()."
This reverts commit e00912e.
1 parent 2247a1e commit 2fd6a44

File tree

5 files changed

+28
-77
lines changed

5 files changed

+28
-77
lines changed

src/backend/utils/mmgr/aset.c

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ typedef struct AllocSetContext
132132
Size maxBlockSize; /* maximum block size */
133133
Size nextBlockSize; /* next block size to allocate */
134134
Size allocChunkLimit; /* effective chunk size limit */
135-
Size memAllocated; /* track memory allocated for this context */
136135
AllocBlock keeper; /* keep this block over resets */
137136
/* freelist this context could be put in, or -1 if not a candidate: */
138137
int freeListIndex; /* index in context_freelists[], or -1 */
@@ -273,7 +272,6 @@ static void *AllocSetRealloc(MemoryContext context, void *pointer, Size size);
273272
static void AllocSetReset(MemoryContext context);
274273
static void AllocSetDelete(MemoryContext context);
275274
static Size AllocSetGetChunkSpace(MemoryContext context, void *pointer);
276-
static Size AllocSetMemAllocated(MemoryContext context);
277275
static bool AllocSetIsEmpty(MemoryContext context);
278276
static void AllocSetStats(MemoryContext context,
279277
MemoryStatsPrintFunc printfunc, void *passthru,
@@ -293,7 +291,6 @@ static const MemoryContextMethods AllocSetMethods = {
293291
AllocSetReset,
294292
AllocSetDelete,
295293
AllocSetGetChunkSpace,
296-
AllocSetMemAllocated,
297294
AllocSetIsEmpty,
298295
AllocSetStats
299296
#ifdef MEMORY_CONTEXT_CHECKING
@@ -467,7 +464,8 @@ AllocSetContextCreateInternal(MemoryContext parent,
467464
parent,
468465
name);
469466

470-
set->memAllocated = set->keeper->endptr - ((char *) set);
467+
((MemoryContext) set)->mem_allocated =
468+
set->keeper->endptr - ((char *) set);
471469

472470
return (MemoryContext) set;
473471
}
@@ -557,7 +555,7 @@ AllocSetContextCreateInternal(MemoryContext parent,
557555
parent,
558556
name);
559557

560-
set->memAllocated = firstBlockSize;
558+
((MemoryContext) set)->mem_allocated = firstBlockSize;
561559

562560
return (MemoryContext) set;
563561
}
@@ -619,7 +617,7 @@ AllocSetReset(MemoryContext context)
619617
else
620618
{
621619
/* Normal case, release the block */
622-
set->memAllocated -= block->endptr - ((char*) block);
620+
context->mem_allocated -= block->endptr - ((char*) block);
623621

624622
#ifdef CLOBBER_FREED_MEMORY
625623
wipe_mem(block, block->freeptr - ((char *) block));
@@ -629,7 +627,7 @@ AllocSetReset(MemoryContext context)
629627
block = next;
630628
}
631629

632-
Assert(set->memAllocated == keepersize);
630+
Assert(context->mem_allocated == keepersize);
633631

634632
/* Reset block size allocation sequence, too */
635633
set->nextBlockSize = set->initBlockSize;
@@ -705,7 +703,7 @@ AllocSetDelete(MemoryContext context)
705703
AllocBlock next = block->next;
706704

707705
if (block != set->keeper)
708-
set->memAllocated -= block->endptr - ((char *) block);
706+
context->mem_allocated -= block->endptr - ((char *) block);
709707

710708
#ifdef CLOBBER_FREED_MEMORY
711709
wipe_mem(block, block->freeptr - ((char *) block));
@@ -717,7 +715,7 @@ AllocSetDelete(MemoryContext context)
717715
block = next;
718716
}
719717

720-
Assert(set->memAllocated == keepersize);
718+
Assert(context->mem_allocated == keepersize);
721719

722720
/* Finally, free the context header, including the keeper block */
723721
free(set);
@@ -760,7 +758,7 @@ AllocSetAlloc(MemoryContext context, Size size)
760758
if (block == NULL)
761759
return NULL;
762760

763-
set->memAllocated += blksize;
761+
context->mem_allocated += blksize;
764762

765763
block->aset = set;
766764
block->freeptr = block->endptr = ((char *) block) + blksize;
@@ -957,7 +955,7 @@ AllocSetAlloc(MemoryContext context, Size size)
957955
if (block == NULL)
958956
return NULL;
959957

960-
set->memAllocated += blksize;
958+
context->mem_allocated += blksize;
961959

962960
block->aset = set;
963961
block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ;
@@ -1060,7 +1058,7 @@ AllocSetFree(MemoryContext context, void *pointer)
10601058
if (block->next)
10611059
block->next->prev = block->prev;
10621060

1063-
set->memAllocated -= block->endptr - ((char*) block);
1061+
context->mem_allocated -= block->endptr - ((char*) block);
10641062

10651063
#ifdef CLOBBER_FREED_MEMORY
10661064
wipe_mem(block, block->freeptr - ((char *) block));
@@ -1163,8 +1161,8 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
11631161
}
11641162

11651163
/* updated separately, not to underflow when (oldblksize > blksize) */
1166-
set->memAllocated -= oldblksize;
1167-
set->memAllocated += blksize;
1164+
context->mem_allocated -= oldblksize;
1165+
context->mem_allocated += blksize;
11681166

11691167
block->freeptr = block->endptr = ((char *) block) + blksize;
11701168

@@ -1339,24 +1337,6 @@ AllocSetGetChunkSpace(MemoryContext context, void *pointer)
13391337
return result;
13401338
}
13411339

1342-
/*
1343-
* All memory currently allocated for this context (including fragmentation
1344-
* and freed chunks).
1345-
*
1346-
* Allocation sizes double (up to maxBlockSize), so the current block may
1347-
* represent half of the total space allocated to the context. Subtract away
1348-
* the free space at the tail of the current block, which may never have been
1349-
* touched.
1350-
*/
1351-
static Size
1352-
AllocSetMemAllocated(MemoryContext context)
1353-
{
1354-
AllocSet set = (AllocSet) context;
1355-
AllocBlock currentBlock = set->blocks;
1356-
Size tailSpace = currentBlock->endptr - currentBlock->freeptr;
1357-
return set->memAllocated - tailSpace;
1358-
}
1359-
13601340
/*
13611341
* AllocSetIsEmpty
13621342
* Is an allocset empty of any allocated space?
@@ -1558,7 +1538,7 @@ AllocSetCheck(MemoryContext context)
15581538
name, block);
15591539
}
15601540

1561-
Assert(total_allocated == set->memAllocated);
1541+
Assert(total_allocated == context->mem_allocated);
15621542
}
15631543

15641544
#endif /* MEMORY_CONTEXT_CHECKING */

src/backend/utils/mmgr/generation.c

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ typedef struct GenerationContext
6161

6262
/* Generational context parameters */
6363
Size blockSize; /* standard block size */
64-
Size memAllocated; /* track memory allocated for this context */
6564

6665
GenerationBlock *block; /* current (most recently allocated) block */
6766
dlist_head blocks; /* list of blocks */
@@ -153,7 +152,6 @@ static void *GenerationRealloc(MemoryContext context, void *pointer, Size size);
153152
static void GenerationReset(MemoryContext context);
154153
static void GenerationDelete(MemoryContext context);
155154
static Size GenerationGetChunkSpace(MemoryContext context, void *pointer);
156-
static Size GenerationMemAllocated(MemoryContext context);
157155
static bool GenerationIsEmpty(MemoryContext context);
158156
static void GenerationStats(MemoryContext context,
159157
MemoryStatsPrintFunc printfunc, void *passthru,
@@ -173,7 +171,6 @@ static const MemoryContextMethods GenerationMethods = {
173171
GenerationReset,
174172
GenerationDelete,
175173
GenerationGetChunkSpace,
176-
GenerationMemAllocated,
177174
GenerationIsEmpty,
178175
GenerationStats
179176
#ifdef MEMORY_CONTEXT_CHECKING
@@ -261,7 +258,6 @@ GenerationContextCreate(MemoryContext parent,
261258

262259
/* Fill in GenerationContext-specific header fields */
263260
set->blockSize = blockSize;
264-
set->memAllocated = 0;
265261
set->block = NULL;
266262
dlist_init(&set->blocks);
267263

@@ -301,7 +297,7 @@ GenerationReset(MemoryContext context)
301297

302298
dlist_delete(miter.cur);
303299

304-
set->memAllocated -= block->blksize;
300+
context->mem_allocated -= block->blksize;
305301

306302
#ifdef CLOBBER_FREED_MEMORY
307303
wipe_mem(block, block->blksize);
@@ -358,7 +354,7 @@ GenerationAlloc(MemoryContext context, Size size)
358354
if (block == NULL)
359355
return NULL;
360356

361-
set->memAllocated += blksize;
357+
context->mem_allocated += blksize;
362358

363359
/* block with a single (used) chunk */
364360
block->blksize = blksize;
@@ -415,7 +411,7 @@ GenerationAlloc(MemoryContext context, Size size)
415411
if (block == NULL)
416412
return NULL;
417413

418-
set->memAllocated += blksize;
414+
context->mem_allocated += blksize;
419415

420416
block->blksize = blksize;
421417
block->nchunks = 0;
@@ -532,7 +528,7 @@ GenerationFree(MemoryContext context, void *pointer)
532528
if (set->block == block)
533529
set->block = NULL;
534530

535-
set->memAllocated -= block->blksize;
531+
context->mem_allocated -= block->blksize;
536532
free(block);
537533
}
538534

@@ -670,17 +666,6 @@ GenerationGetChunkSpace(MemoryContext context, void *pointer)
670666
return result;
671667
}
672668

673-
/*
674-
* All memory currently allocated for this context (including fragmentation
675-
* and freed chunks).
676-
*/
677-
static Size
678-
GenerationMemAllocated(MemoryContext context)
679-
{
680-
GenerationContext *set = (GenerationContext *) context;
681-
return set->memAllocated;
682-
}
683-
684669
/*
685670
* GenerationIsEmpty
686671
* Is a GenerationContext empty of any allocated space?
@@ -859,7 +844,7 @@ GenerationCheck(MemoryContext context)
859844
name, nfree, block, block->nfree);
860845
}
861846

862-
Assert(total_allocated == gen->memAllocated);
847+
Assert(total_allocated == context->mem_allocated);
863848
}
864849

865850
#endif /* MEMORY_CONTEXT_CHECKING */

src/backend/utils/mmgr/mcxt.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ MemoryContextIsEmpty(MemoryContext context)
469469
Size
470470
MemoryContextMemAllocated(MemoryContext context, bool recurse)
471471
{
472-
Size total = context->methods->mem_allocated(context);
472+
Size total = context->mem_allocated;
473473

474474
AssertArg(MemoryContextIsValid(context));
475475

@@ -760,6 +760,7 @@ MemoryContextCreate(MemoryContext node,
760760
node->methods = methods;
761761
node->parent = parent;
762762
node->firstchild = NULL;
763+
node->mem_allocated = 0;
763764
node->prevchild = NULL;
764765
node->name = name;
765766
node->ident = NULL;

src/backend/utils/mmgr/slab.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ typedef struct SlabContext
6767
Size fullChunkSize; /* chunk size including header and alignment */
6868
Size blockSize; /* block size */
6969
Size headerSize; /* allocated size of context header */
70-
Size memAllocated; /* track memory allocated for this context */
7170
int chunksPerBlock; /* number of chunks per block */
7271
int minFreeChunks; /* min number of free chunks in any block */
7372
int nblocks; /* number of blocks allocated */
@@ -133,7 +132,6 @@ static void *SlabRealloc(MemoryContext context, void *pointer, Size size);
133132
static void SlabReset(MemoryContext context);
134133
static void SlabDelete(MemoryContext context);
135134
static Size SlabGetChunkSpace(MemoryContext context, void *pointer);
136-
static Size SlabMemAllocated(MemoryContext context);
137135
static bool SlabIsEmpty(MemoryContext context);
138136
static void SlabStats(MemoryContext context,
139137
MemoryStatsPrintFunc printfunc, void *passthru,
@@ -152,7 +150,6 @@ static const MemoryContextMethods SlabMethods = {
152150
SlabReset,
153151
SlabDelete,
154152
SlabGetChunkSpace,
155-
SlabMemAllocated,
156153
SlabIsEmpty,
157154
SlabStats
158155
#ifdef MEMORY_CONTEXT_CHECKING
@@ -265,7 +262,6 @@ SlabContextCreate(MemoryContext parent,
265262
slab->fullChunkSize = fullChunkSize;
266263
slab->blockSize = blockSize;
267264
slab->headerSize = headerSize;
268-
slab->memAllocated = 0;
269265
slab->chunksPerBlock = chunksPerBlock;
270266
slab->minFreeChunks = 0;
271267
slab->nblocks = 0;
@@ -290,17 +286,6 @@ SlabContextCreate(MemoryContext parent,
290286
return (MemoryContext) slab;
291287
}
292288

293-
/*
294-
* All memory currently allocated for this context (including fragmentation
295-
* and freed chunks).
296-
*/
297-
static Size
298-
SlabMemAllocated(MemoryContext context)
299-
{
300-
SlabContext *slab = (SlabContext *) context;
301-
return slab->memAllocated;
302-
}
303-
304289
/*
305290
* SlabReset
306291
* Frees all memory which is allocated in the given set.
@@ -337,14 +322,14 @@ SlabReset(MemoryContext context)
337322
#endif
338323
free(block);
339324
slab->nblocks--;
340-
slab->memAllocated -= slab->blockSize;
325+
context->mem_allocated -= slab->blockSize;
341326
}
342327
}
343328

344329
slab->minFreeChunks = 0;
345330

346331
Assert(slab->nblocks == 0);
347-
Assert(slab->memAllocated == 0);
332+
Assert(context->mem_allocated == 0);
348333
}
349334

350335
/*
@@ -422,7 +407,7 @@ SlabAlloc(MemoryContext context, Size size)
422407

423408
slab->minFreeChunks = slab->chunksPerBlock;
424409
slab->nblocks += 1;
425-
slab->memAllocated += slab->blockSize;
410+
context->mem_allocated += slab->blockSize;
426411
}
427412

428413
/* grab the block from the freelist (even the new block is there) */
@@ -516,7 +501,7 @@ SlabAlloc(MemoryContext context, Size size)
516501

517502
SlabAllocInfo(slab, chunk);
518503

519-
Assert(slab->nblocks * slab->blockSize == slab->memAllocated);
504+
Assert(slab->nblocks * slab->blockSize == context->mem_allocated);
520505

521506
return SlabChunkGetPointer(chunk);
522507
}
@@ -593,13 +578,13 @@ SlabFree(MemoryContext context, void *pointer)
593578
{
594579
free(block);
595580
slab->nblocks--;
596-
slab->memAllocated -= slab->blockSize;
581+
context->mem_allocated -= slab->blockSize;
597582
}
598583
else
599584
dlist_push_head(&slab->freelist[block->nfree], &block->node);
600585

601586
Assert(slab->nblocks >= 0);
602-
Assert(slab->nblocks * slab->blockSize == slab->memAllocated);
587+
Assert(slab->nblocks * slab->blockSize == context->mem_allocated);
603588
}
604589

605590
/*
@@ -819,7 +804,7 @@ SlabCheck(MemoryContext context)
819804
}
820805
}
821806

822-
Assert(slab->nblocks * slab->blockSize == slab->memAllocated);
807+
Assert(slab->nblocks * slab->blockSize == context->mem_allocated);
823808
}
824809

825810
#endif /* MEMORY_CONTEXT_CHECKING */

src/include/nodes/memnodes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ typedef struct MemoryContextMethods
6363
void (*reset) (MemoryContext context);
6464
void (*delete_context) (MemoryContext context);
6565
Size (*get_chunk_space) (MemoryContext context, void *pointer);
66-
Size (*mem_allocated) (MemoryContext context);
6766
bool (*is_empty) (MemoryContext context);
6867
void (*stats) (MemoryContext context,
6968
MemoryStatsPrintFunc printfunc, void *passthru,
@@ -80,6 +79,7 @@ typedef struct MemoryContextData
8079
/* these two fields are placed here to minimize alignment wastage: */
8180
bool isReset; /* T = no space alloced since last reset */
8281
bool allowInCritSection; /* allow palloc in critical section */
82+
Size mem_allocated; /* track memory allocated for this context */
8383
const MemoryContextMethods *methods; /* virtual function table */
8484
MemoryContext parent; /* NULL if no parent (toplevel context) */
8585
MemoryContext firstchild; /* head of linked list of children */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy