1
0
Fork 0

Add asserts to prevent a potential bug

Use C++11 static_assert to assure, that code behaves correctly; if
Chip or Channel structs will be changed to non-std layout or a different
first fields will be introduced, that will invalidate the offset
calculation.

Additionally, add asserts to prevent possibility of introducing a bug
when offset stored in one the tables is 0.
This commit is contained in:
Patryk Obara 2019-11-12 10:45:46 +01:00 committed by Patryk Obara
parent 1e95311081
commit 4e65c8f69f
2 changed files with 18 additions and 5 deletions

View file

@ -27,6 +27,7 @@
#include "pic.h"
#include "hardware.h"
#include <cmath>
namespace Adlib {

View file

@ -32,15 +32,14 @@
//DUNNO Keyon in 4op, switch to 2op without keyoff.
*/
#include "dbopl.h"
#include <cassert>
#include <cstddef>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "dosbox.h"
#include "dbopl.h"
#include <type_traits>
#ifndef PI
#define PI 3.14159265358979323846
@ -1450,6 +1449,13 @@ void InitTables( void ) {
//Add back the bits for highest ones
if ( i >= 16 )
index += 9;
static_assert(std::is_standard_layout<Chip>::value,
"struct Chip is not a standard layout type");
static_assert(offsetof(Chip, chan) == 0,
"offset table stores values relative to the start of struct");
// values stored in offset tables are artificially increased by 1
// to keep macros REGCHAN and REGOP working correctly
ChanOffsetTable[i] = 1+(Bit16u)(index*sizeof(DBOPL::Channel));
}
//Same for operators
@ -1463,7 +1469,13 @@ void InitTables( void ) {
if ( chNum >= 12 )
chNum += 16 - 12;
Bitu opNum = ( i % 8 ) / 3;
static_assert(std::is_standard_layout<Channel>::value,
"struct Channel is not a standard layout type");
static_assert(offsetof(Channel, op) == 0,
"offset table stores values relative to the start of struct");
OpOffsetTable[i] = ChanOffsetTable[chNum]+(Bit16u)(opNum*sizeof(DBOPL::Operator));
assert(OpOffsetTable[i] > 0); // needs to be non-zero; see REGOP macro
}
#if 0
DBOPL::Chip* chip = 0;