/* * The default life map is an stl map< uint64_t, uint64_t >. * You may map row and col to this map_key however you want. * It is up to you to ensure there are no collisions here. */
/* * If using the default life map, we can call the DecrementLife * function which will check if the map_key already exists. If so, * the life value is decremented (write count incremented). Otherwise * the map_key is inserted with a write count of 1. */ uint64_t rowSize; uint64_t wordSize; uint64_t currentBit; uint64_t flipPartitions; uint64_t rowPartitions; int *modifyCount;
/* * Count the number of bits that are modified. If it is more than half, then we will invert the data then write. */ for( uint64_t i = 0; i < flipPartitions; i++ ) modifyCount[i] = 0;
currentBit = 0;
/* Get what is currently in the memory (i.e., if it was previously flipped, get the flipped data. */ for( uint64_t i = 0; i < flipPartitions; i++ ) { uint64_t curAddr = row * rowPartitions + col * flipPartitions + i;
/* Check each byte to see if it was modified */ for( uint64_t i = 0; i < wordSize; ++i ) { /* * If no bytes have changed we can just continue. Yes, I know this will check the byte 8 times, but i'd rather not change the iter. */ uint8_t oldByte, newByte;
oldByte = oldData.GetByte( i ); newByte = newData.GetByte( i );
/* * If the bytes are different, then at least one bit has changed. check each bit individually. */ for( int j = 0; j < 8; j++ ) { uint8_t oldBit, newBit;
/* * Flip any partitions as needed and mark them as inverted or not. */ for( uint64_t i = 0; i < flipPartitions; i++ ) { bitCompareSwapWrites += modifyCount[i];
/* Invert if more than half of the bits are modified. */ if( modifyCount[i] > (fpSize / 2) ) { InvertData( newData, i*fpSize, (i+1)*fpSize );
bitsFlipped += (fpSize - modifyCount[i]);
/* * Mark this address as flipped. If the data was already inverted, it * should remain as inverted for the new data. */ if( !flippedAddresses.count( curAddr ) ) { flippedAddresses.insert( curAddr ); } } else { /* * This data is not inverted and should not be marked as such. */ if( flippedAddresses.count( curAddr ) ) { flippedAddresses.erase( curAddr ); }