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

Commita855148

Browse files
committed
Refactor aset.c and mcxt.c in preparation for Valgrind cooperation.
Move some repeated debugging code into functions and store intermediatesin variables where not presently necessary. No code-generation changesin a production build, and no functional changes. This simplifies andfocuses the main patch.
1 parent1d96bb9 commita855148

File tree

2 files changed

+73
-38
lines changed

2 files changed

+73
-38
lines changed

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

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,37 @@ AllocSetFreeIndex(Size size)
308308
returnidx;
309309
}
310310

311+
#ifdefCLOBBER_FREED_MEMORY
312+
313+
/* Wipe freed memory for debugging purposes */
314+
staticvoid
315+
wipe_mem(void*ptr,size_tsize)
316+
{
317+
memset(ptr,0x7F,size);
318+
}
319+
#endif
320+
321+
#ifdefMEMORY_CONTEXT_CHECKING
322+
staticvoid
323+
set_sentinel(void*base,Sizeoffset)
324+
{
325+
char*ptr= (char*)base+offset;
326+
327+
*ptr=0x7E;
328+
}
329+
330+
staticbool
331+
sentinel_ok(constvoid*base,Sizeoffset)
332+
{
333+
constchar*ptr= (constchar*)base+offset;
334+
boolret;
335+
336+
ret=*ptr==0x7E;
337+
338+
returnret;
339+
}
340+
#endif
341+
311342
#ifdefRANDOMIZE_ALLOCATED_MEMORY
312343

313344
/*
@@ -492,8 +523,7 @@ AllocSetReset(MemoryContext context)
492523
char*datastart= ((char*)block)+ALLOC_BLOCKHDRSZ;
493524

494525
#ifdefCLOBBER_FREED_MEMORY
495-
/* Wipe freed memory for debugging purposes */
496-
memset(datastart,0x7F,block->freeptr-datastart);
526+
wipe_mem(datastart,block->freeptr-datastart);
497527
#endif
498528
block->freeptr=datastart;
499529
block->next=NULL;
@@ -502,8 +532,7 @@ AllocSetReset(MemoryContext context)
502532
{
503533
/* Normal case, release the block */
504534
#ifdefCLOBBER_FREED_MEMORY
505-
/* Wipe freed memory for debugging purposes */
506-
memset(block,0x7F,block->freeptr- ((char*)block));
535+
wipe_mem(block,block->freeptr- ((char*)block));
507536
#endif
508537
free(block);
509538
}
@@ -545,8 +574,7 @@ AllocSetDelete(MemoryContext context)
545574
AllocBlocknext=block->next;
546575

547576
#ifdefCLOBBER_FREED_MEMORY
548-
/* Wipe freed memory for debugging purposes */
549-
memset(block,0x7F,block->freeptr- ((char*)block));
577+
wipe_mem(block,block->freeptr- ((char*)block));
550578
#endif
551579
free(block);
552580
block=next;
@@ -598,7 +626,7 @@ AllocSetAlloc(MemoryContext context, Size size)
598626
chunk->requested_size=size;
599627
/* set mark to catch clobber of "unused" space */
600628
if (size<chunk_size)
601-
((char*)AllocChunkGetPointer(chunk))[size]=0x7E;
629+
set_sentinel(AllocChunkGetPointer(chunk),size);
602630
#endif
603631
#ifdefRANDOMIZE_ALLOCATED_MEMORY
604632
/* fill the allocated space with junk */
@@ -644,7 +672,7 @@ AllocSetAlloc(MemoryContext context, Size size)
644672
chunk->requested_size=size;
645673
/* set mark to catch clobber of "unused" space */
646674
if (size<chunk->size)
647-
((char*)AllocChunkGetPointer(chunk))[size]=0x7E;
675+
set_sentinel(AllocChunkGetPointer(chunk),size);
648676
#endif
649677
#ifdefRANDOMIZE_ALLOCATED_MEMORY
650678
/* fill the allocated space with junk */
@@ -801,7 +829,7 @@ AllocSetAlloc(MemoryContext context, Size size)
801829
chunk->requested_size=size;
802830
/* set mark to catch clobber of "unused" space */
803831
if (size<chunk->size)
804-
((char*)AllocChunkGetPointer(chunk))[size]=0x7E;
832+
set_sentinel(AllocChunkGetPointer(chunk),size);
805833
#endif
806834
#ifdefRANDOMIZE_ALLOCATED_MEMORY
807835
/* fill the allocated space with junk */
@@ -827,7 +855,7 @@ AllocSetFree(MemoryContext context, void *pointer)
827855
#ifdefMEMORY_CONTEXT_CHECKING
828856
/* Test for someone scribbling on unused space in chunk */
829857
if (chunk->requested_size<chunk->size)
830-
if (((char*)pointer)[chunk->requested_size]!=0x7E)
858+
if (!sentinel_ok(pointer,chunk->requested_size))
831859
elog(WARNING,"detected write past chunk end in %s %p",
832860
set->header.name,chunk);
833861
#endif
@@ -860,8 +888,7 @@ AllocSetFree(MemoryContext context, void *pointer)
860888
else
861889
prevblock->next=block->next;
862890
#ifdefCLOBBER_FREED_MEMORY
863-
/* Wipe freed memory for debugging purposes */
864-
memset(block,0x7F,block->freeptr- ((char*)block));
891+
wipe_mem(block,block->freeptr- ((char*)block));
865892
#endif
866893
free(block);
867894
}
@@ -873,8 +900,7 @@ AllocSetFree(MemoryContext context, void *pointer)
873900
chunk->aset= (void*)set->freelist[fidx];
874901

875902
#ifdefCLOBBER_FREED_MEMORY
876-
/* Wipe freed memory for debugging purposes */
877-
memset(pointer,0x7F,chunk->size);
903+
wipe_mem(pointer,chunk->size);
878904
#endif
879905

880906
#ifdefMEMORY_CONTEXT_CHECKING
@@ -901,7 +927,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
901927
#ifdefMEMORY_CONTEXT_CHECKING
902928
/* Test for someone scribbling on unused space in chunk */
903929
if (chunk->requested_size<oldsize)
904-
if (((char*)pointer)[chunk->requested_size]!=0x7E)
930+
if (!sentinel_ok(pointer,chunk->requested_size))
905931
elog(WARNING,"detected write past chunk end in %s %p",
906932
set->header.name,chunk);
907933
#endif
@@ -924,7 +950,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
924950
chunk->requested_size=size;
925951
/* set mark to catch clobber of "unused" space */
926952
if (size<oldsize)
927-
((char*)pointer)[size]=0x7E;
953+
set_sentinel(pointer,size);
928954
#endif
929955
returnpointer;
930956
}
@@ -987,7 +1013,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
9871013
chunk->requested_size=size;
9881014
/* set mark to catch clobber of "unused" space */
9891015
if (size<chunk->size)
990-
((char*)AllocChunkGetPointer(chunk))[size]=0x7E;
1016+
set_sentinel(AllocChunkGetPointer(chunk),size);
9911017
#endif
9921018

9931019
returnAllocChunkGetPointer(chunk);
@@ -1136,11 +1162,9 @@ AllocSetCheck(MemoryContext context)
11361162
AllocChunkchunk= (AllocChunk)bpoz;
11371163
Sizechsize,
11381164
dsize;
1139-
char*chdata_end;
11401165

11411166
chsize=chunk->size;/* aligned chunk size */
11421167
dsize=chunk->requested_size;/* real data */
1143-
chdata_end= ((char*)chunk)+ (ALLOC_CHUNKHDRSZ+dsize);
11441168

11451169
/*
11461170
* Check chunk size
@@ -1170,7 +1194,8 @@ AllocSetCheck(MemoryContext context)
11701194
/*
11711195
* Check for overwrite of "unallocated" space in chunk
11721196
*/
1173-
if (dsize>0&&dsize<chsize&&*chdata_end!=0x7E)
1197+
if (dsize>0&&dsize<chsize&&
1198+
!sentinel_ok(chunk,ALLOC_CHUNKHDRSZ+dsize))
11741199
elog(WARNING,"problem in alloc set %s: detected write past chunk end in block %p, chunk %p",
11751200
name,block,chunk);
11761201

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ MemoryContextCreate(NodeTag tag, Size size,
569569
void*
570570
MemoryContextAlloc(MemoryContextcontext,Sizesize)
571571
{
572+
void*ret;
573+
572574
AssertArg(MemoryContextIsValid(context));
573575

574576
if (!AllocSizeIsValid(size))
@@ -577,7 +579,9 @@ MemoryContextAlloc(MemoryContext context, Size size)
577579

578580
context->isReset= false;
579581

580-
return (*context->methods->alloc) (context,size);
582+
ret= (*context->methods->alloc) (context,size);
583+
584+
returnret;
581585
}
582586

583587
/*
@@ -638,6 +642,8 @@ void *
638642
palloc(Sizesize)
639643
{
640644
/* duplicates MemoryContextAlloc to avoid increased overhead */
645+
void*ret;
646+
641647
AssertArg(MemoryContextIsValid(CurrentMemoryContext));
642648

643649
if (!AllocSizeIsValid(size))
@@ -646,7 +652,9 @@ palloc(Size size)
646652

647653
CurrentMemoryContext->isReset= false;
648654

649-
return (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext,size);
655+
ret= (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext,size);
656+
657+
returnret;
650658
}
651659

652660
void*
@@ -677,7 +685,7 @@ palloc0(Size size)
677685
void
678686
pfree(void*pointer)
679687
{
680-
StandardChunkHeader*header;
688+
MemoryContextcontext;
681689

682690
/*
683691
* Try to detect bogus pointers handed to us, poorly though we can.
@@ -690,12 +698,12 @@ pfree(void *pointer)
690698
/*
691699
* OK, it's probably safe to look at the chunk header.
692700
*/
693-
header= (StandardChunkHeader*)
694-
((char*)pointer-STANDARDCHUNKHEADERSIZE);
701+
context=((StandardChunkHeader*)
702+
((char*)pointer-STANDARDCHUNKHEADERSIZE))->context;
695703

696-
AssertArg(MemoryContextIsValid(header->context));
704+
AssertArg(MemoryContextIsValid(context));
697705

698-
(*header->context->methods->free_p) (header->context,pointer);
706+
(*context->methods->free_p) (context,pointer);
699707
}
700708

701709
/*
@@ -705,7 +713,12 @@ pfree(void *pointer)
705713
void*
706714
repalloc(void*pointer,Sizesize)
707715
{
708-
StandardChunkHeader*header;
716+
MemoryContextcontext;
717+
void*ret;
718+
719+
if (!AllocSizeIsValid(size))
720+
elog(ERROR,"invalid memory alloc request size %lu",
721+
(unsigned long)size);
709722

710723
/*
711724
* Try to detect bogus pointers handed to us, poorly though we can.
@@ -718,20 +731,17 @@ repalloc(void *pointer, Size size)
718731
/*
719732
* OK, it's probably safe to look at the chunk header.
720733
*/
721-
header= (StandardChunkHeader*)
722-
((char*)pointer-STANDARDCHUNKHEADERSIZE);
734+
context=((StandardChunkHeader*)
735+
((char*)pointer-STANDARDCHUNKHEADERSIZE))->context;
723736

724-
AssertArg(MemoryContextIsValid(header->context));
725-
726-
if (!AllocSizeIsValid(size))
727-
elog(ERROR,"invalid memory alloc request size %lu",
728-
(unsigned long)size);
737+
AssertArg(MemoryContextIsValid(context));
729738

730739
/* isReset must be false already */
731-
Assert(!header->context->isReset);
740+
Assert(!context->isReset);
741+
742+
ret= (*context->methods->realloc) (context,pointer,size);
732743

733-
return (*header->context->methods->realloc) (header->context,
734-
pointer,size);
744+
returnret;
735745
}
736746

737747
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp