Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita801452

Browse files
committed
Allocate freechunks bitmap as part of SlabContext
The bitmap used by SlabCheck to cross-check free chunks in a block usedto be allocated for each SlabCheck call, and was never freed. The memoryleak could be fixed by simply adding a pfree call, but it's actually abad idea to do any allocations in SlabCheck at all as it assumes thestate of the memory management as a whole is sane.So instead we allocate the bitmap as part of SlabContext, which meanswe don't need to do any allocations in SlabCheck and the bitmap goesaway together with the SlabContext.Backpatch to 10, where the Slab context was introduced.Author: Tomas VondraReported-by: Andres FreundReviewed-by: Tom LaneBackpatch-through: 10Discussion:https://www.postgresql.org/message-id/20200116044119.g45f7pmgz4jmodxj%40alap3.anarazel.de
1 parent920c6ad commita801452

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

‎src/backend/utils/mmgr/slab.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ typedef struct SlabContext
7070
intchunksPerBlock;/* number of chunks per block */
7171
intminFreeChunks;/* min number of free chunks in any block */
7272
intnblocks;/* number of blocks allocated */
73+
#ifdefMEMORY_CONTEXT_CHECKING
74+
bool*freechunks;/* bitmap of free chunks in a block */
75+
#endif
7376
/* blocks with free space, grouped by number of free chunks: */
7477
dlist_headfreelist[FLEXIBLE_ARRAY_MEMBER];
7578
}SlabContext;
@@ -188,6 +191,7 @@ SlabContextCreate(MemoryContext parent,
188191
intchunksPerBlock;
189192
SizefullChunkSize;
190193
SizefreelistSize;
194+
SizeheaderSize;
191195
SlabContext*slab;
192196

193197
StaticAssertStmt(offsetof(SlabChunk,slab)+sizeof(MemoryContext)==
@@ -218,10 +222,22 @@ SlabContextCreate(MemoryContext parent,
218222
/* make sure the chunks actually fit on the block*/
219223
Assert((fullChunkSize*chunksPerBlock)+sizeof(SlabBlock) <=blockSize);
220224

225+
/* Size of the memory context header */
226+
headerSize= offsetof(SlabContext,freelist)+freelistSize;
227+
228+
#ifdefMEMORY_CONTEXT_CHECKING
229+
/*
230+
* With memory checking, we need to allocate extra space for the bitmap
231+
* of free chunks. The bitmap is an array of bools, so we don't need to
232+
* worry about alignment.
233+
*/
234+
headerSize+=chunksPerBlock*sizeof(bool);
235+
#endif
236+
221237
/* Do the type-independent part of context creation */
222238
slab= (SlabContext*)
223239
MemoryContextCreate(T_SlabContext,
224-
(offsetof(SlabContext,freelist)+freelistSize),
240+
headerSize,
225241
&SlabMethods,
226242
parent,
227243
name);
@@ -233,6 +249,12 @@ SlabContextCreate(MemoryContext parent,
233249
slab->nblocks=0;
234250
slab->minFreeChunks=0;
235251

252+
#ifdefMEMORY_CONTEXT_CHECKING
253+
/* set the freechunks pointer right after the freelists array */
254+
slab->freechunks
255+
= (bool*)slab+ offsetof(SlabContext,freelist)+freelistSize;
256+
#endif
257+
236258
return (MemoryContext)slab;
237259
}
238260

@@ -674,14 +696,10 @@ SlabCheck(MemoryContext context)
674696
inti;
675697
SlabContext*slab=castNode(SlabContext,context);
676698
char*name=slab->header.name;
677-
char*freechunks;
678699

679700
Assert(slab);
680701
Assert(slab->chunksPerBlock>0);
681702

682-
/* bitmap of free chunks on a block */
683-
freechunks=palloc(slab->chunksPerBlock*sizeof(bool));
684-
685703
/* walk all the freelists */
686704
for (i=0;i <=slab->chunksPerBlock;i++)
687705
{
@@ -704,7 +722,7 @@ SlabCheck(MemoryContext context)
704722
name,block->nfree,block,i);
705723

706724
/* reset the bitmap of free chunks for this block */
707-
memset(freechunks,0, (slab->chunksPerBlock*sizeof(bool)));
725+
memset(slab->freechunks,0, (slab->chunksPerBlock*sizeof(bool)));
708726
idx=block->firstFreeChunk;
709727

710728
/*
@@ -721,7 +739,7 @@ SlabCheck(MemoryContext context)
721739

722740
/* count the chunk as free, add it to the bitmap */
723741
nfree++;
724-
freechunks[idx]= true;
742+
slab->freechunks[idx]= true;
725743

726744
/* read index of the next free chunk */
727745
chunk=SlabBlockGetChunk(slab,block,idx);
@@ -732,7 +750,7 @@ SlabCheck(MemoryContext context)
732750
for (j=0;j<slab->chunksPerBlock;j++)
733751
{
734752
/* non-zero bit in the bitmap means chunk the chunk is used */
735-
if (!freechunks[j])
753+
if (!slab->freechunks[j])
736754
{
737755
SlabChunk*chunk=SlabBlockGetChunk(slab,block,j);
738756

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp