Skip to content

Commit

Permalink
Merge branch 'faster-checkcleanbuffer' into 'master'
Browse files Browse the repository at this point in the history
improve check for clean buffer by use of memcmp() on memory chunks

See merge request integer/scip!3607
  • Loading branch information
svigerske committed Dec 1, 2024
2 parents 3fd15d1 + 7e875b3 commit 1cc8137
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/blockmemshell/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,40 @@ void checkChkmem(
#define checkChkmem(chkmem) /**/
#endif

#ifdef CHECKCLEANBUFFER
#define CHECKCLEANBUFFER_TESTSIZE 1048576 /**< size of test block */

/** a memory block that will be initialized to all zero, to be used in memcmp() */
static uint8_t checkcleanbuffer_testblock[CHECKCLEANBUFFER_TESTSIZE];

/** whether checkcleanbuffer_testblock has been initialized to be all zero */
static SCIP_Bool checkcleanbuffer_testblockinit = FALSE;

/** check whether a memory block has all zero values */
static
void checkCleanmem(
void* mem, /**< memory block to check */
int size /**< size of memory block */
)
{
uint8_t* startptr;
uint8_t* endptr;

if( !checkcleanbuffer_testblockinit )
{
BMSclearMemorySize(checkcleanbuffer_testblock, CHECKCLEANBUFFER_TESTSIZE);
checkcleanbuffer_testblockinit = TRUE;
}

for( startptr = (uint8_t*)mem, endptr = (uint8_t*)(mem) + size;
startptr + CHECKCLEANBUFFER_TESTSIZE < endptr;
startptr += CHECKCLEANBUFFER_TESTSIZE )
{
assert(memcmp(startptr, checkcleanbuffer_testblock, CHECKCLEANBUFFER_TESTSIZE) == 0);
}
assert(memcmp(startptr, checkcleanbuffer_testblock, endptr - startptr) == 0);
}
#endif

/** links chunk to the block's chunk array, sort it by store pointer;
* returns TRUE if successful, FALSE otherwise
Expand Down Expand Up @@ -2753,14 +2787,7 @@ void* BMSallocBufferMemory_work(
#ifdef CHECKCLEANBUFFER
/* check that the memory is cleared */
if( buffer->clean )
{
char* tmpptr = (char*)(buffer->data[bufnum]);
unsigned int inc = buffer->size[bufnum] / sizeof(*tmpptr);
tmpptr += inc;

while( --tmpptr >= (char*)(buffer->data[bufnum]) )
assert(*tmpptr == '\0');
}
checkCleanmem(buffer->data[bufnum], buffer->size[bufnum]);
#endif

ptr = buffer->data[bufnum];
Expand Down Expand Up @@ -3060,13 +3087,7 @@ void BMSfreeBufferMemory_work(
#ifdef CHECKCLEANBUFFER
/* check that the memory is cleared */
if( buffer->clean )
{
size_t i;
uint8_t* tmpptr = (uint8_t*)(buffer->data[bufnum]);

for( i = 0; i < buffer->size[bufnum]; ++i )
assert(tmpptr[i] == 0);
}
checkCleanmem(buffer->data[bufnum], buffer->size[bufnum]);
#endif

assert( buffer->data[bufnum] == *ptr );
Expand Down

0 comments on commit 1cc8137

Please sign in to comment.