@@ -308,6 +308,37 @@ AllocSetFreeIndex(Size size)
308308return idx ;
309309}
310310
311+ #ifdef CLOBBER_FREED_MEMORY
312+
313+ /* Wipe freed memory for debugging purposes */
314+ static void
315+ wipe_mem (void * ptr ,size_t size )
316+ {
317+ memset (ptr ,0x7F ,size );
318+ }
319+ #endif
320+
321+ #ifdef MEMORY_CONTEXT_CHECKING
322+ static void
323+ set_sentinel (void * base ,Size offset )
324+ {
325+ char * ptr = (char * )base + offset ;
326+
327+ * ptr = 0x7E ;
328+ }
329+
330+ static bool
331+ sentinel_ok (const void * base ,Size offset )
332+ {
333+ const char * ptr = (const char * )base + offset ;
334+ bool ret ;
335+
336+ ret = * ptr == 0x7E ;
337+
338+ return ret ;
339+ }
340+ #endif
341+
311342#ifdef RANDOMIZE_ALLOCATED_MEMORY
312343
313344/*
@@ -492,8 +523,7 @@ AllocSetReset(MemoryContext context)
492523char * datastart = ((char * )block )+ ALLOC_BLOCKHDRSZ ;
493524
494525#ifdef CLOBBER_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
498528block -> freeptr = datastart ;
499529block -> next = NULL ;
@@ -502,8 +532,7 @@ AllocSetReset(MemoryContext context)
502532{
503533/* Normal case, release the block */
504534#ifdef CLOBBER_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
508537free (block );
509538}
@@ -545,8 +574,7 @@ AllocSetDelete(MemoryContext context)
545574AllocBlock next = block -> next ;
546575
547576#ifdef CLOBBER_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
551579free (block );
552580block = next ;
@@ -598,7 +626,7 @@ AllocSetAlloc(MemoryContext context, Size size)
598626chunk -> requested_size = size ;
599627/* set mark to catch clobber of "unused" space */
600628if (size < chunk_size )
601- (( char * ) AllocChunkGetPointer (chunk ))[ size ] = 0x7E ;
629+ set_sentinel ( AllocChunkGetPointer (chunk ), size ) ;
602630#endif
603631#ifdef RANDOMIZE_ALLOCATED_MEMORY
604632/* fill the allocated space with junk */
@@ -644,7 +672,7 @@ AllocSetAlloc(MemoryContext context, Size size)
644672chunk -> requested_size = size ;
645673/* set mark to catch clobber of "unused" space */
646674if (size < chunk -> size )
647- (( char * ) AllocChunkGetPointer (chunk ))[ size ] = 0x7E ;
675+ set_sentinel ( AllocChunkGetPointer (chunk ), size ) ;
648676#endif
649677#ifdef RANDOMIZE_ALLOCATED_MEMORY
650678/* fill the allocated space with junk */
@@ -801,7 +829,7 @@ AllocSetAlloc(MemoryContext context, Size size)
801829chunk -> requested_size = size ;
802830/* set mark to catch clobber of "unused" space */
803831if (size < chunk -> size )
804- (( char * ) AllocChunkGetPointer (chunk ))[ size ] = 0x7E ;
832+ set_sentinel ( AllocChunkGetPointer (chunk ), size ) ;
805833#endif
806834#ifdef RANDOMIZE_ALLOCATED_MEMORY
807835/* fill the allocated space with junk */
@@ -827,7 +855,7 @@ AllocSetFree(MemoryContext context, void *pointer)
827855#ifdef MEMORY_CONTEXT_CHECKING
828856/* Test for someone scribbling on unused space in chunk */
829857if (chunk -> requested_size < chunk -> size )
830- if ((( char * ) pointer )[ chunk -> requested_size ] != 0x7E )
858+ if (! sentinel_ok ( pointer , chunk -> requested_size ) )
831859elog (WARNING ,"detected write past chunk end in %s %p" ,
832860set -> header .name ,chunk );
833861#endif
@@ -860,8 +888,7 @@ AllocSetFree(MemoryContext context, void *pointer)
860888else
861889prevblock -> next = block -> next ;
862890#ifdef CLOBBER_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
866893free (block );
867894}
@@ -873,8 +900,7 @@ AllocSetFree(MemoryContext context, void *pointer)
873900chunk -> aset = (void * )set -> freelist [fidx ];
874901
875902#ifdef CLOBBER_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#ifdef MEMORY_CONTEXT_CHECKING
@@ -901,7 +927,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
901927#ifdef MEMORY_CONTEXT_CHECKING
902928/* Test for someone scribbling on unused space in chunk */
903929if (chunk -> requested_size < oldsize )
904- if ((( char * ) pointer )[ chunk -> requested_size ] != 0x7E )
930+ if (! sentinel_ok ( pointer , chunk -> requested_size ) )
905931elog (WARNING ,"detected write past chunk end in %s %p" ,
906932set -> header .name ,chunk );
907933#endif
@@ -924,7 +950,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
924950chunk -> requested_size = size ;
925951/* set mark to catch clobber of "unused" space */
926952if (size < oldsize )
927- (( char * ) pointer )[ size ] = 0x7E ;
953+ set_sentinel ( pointer , size ) ;
928954#endif
929955return pointer ;
930956}
@@ -987,7 +1013,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
9871013chunk -> requested_size = size ;
9881014/* set mark to catch clobber of "unused" space */
9891015if (size < chunk -> size )
990- (( char * ) AllocChunkGetPointer (chunk ))[ size ] = 0x7E ;
1016+ set_sentinel ( AllocChunkGetPointer (chunk ), size ) ;
9911017#endif
9921018
9931019return AllocChunkGetPointer (chunk );
@@ -1136,11 +1162,9 @@ AllocSetCheck(MemoryContext context)
11361162AllocChunk chunk = (AllocChunk )bpoz ;
11371163Size chsize ,
11381164dsize ;
1139- char * chdata_end ;
11401165
11411166chsize = chunk -> size ;/* aligned chunk size */
11421167dsize = 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 ))
11741199elog (WARNING ,"problem in alloc set %s: detected write past chunk end in block %p, chunk %p" ,
11751200name ,block ,chunk );
11761201