From: <zu...@us...> - 2015-03-02 12:10:33
|
Revision: 5118 http://sourceforge.net/p/fuse-emulator/code/5118 Author: zubzero Date: 2015-03-02 12:10:25 +0000 (Mon, 02 Mar 2015) Log Message: ----------- Add and use ARRAY_SIZE macro Modified Paths: -------------- trunk/libspectrum/hacking/ChangeLog trunk/libspectrum/internals.h trunk/libspectrum/pzx_read.c trunk/libspectrum/szx.c trunk/libspectrum/test/test.c trunk/libspectrum/zxs.c Modified: trunk/libspectrum/hacking/ChangeLog =================================================================== --- trunk/libspectrum/hacking/ChangeLog 2015-03-01 11:25:31 UTC (rev 5117) +++ trunk/libspectrum/hacking/ChangeLog 2015-03-02 12:10:25 UTC (rev 5118) @@ -972,3 +972,5 @@ (Sergio). 20150225 pzx_read.c,szx.c,tzx_read.c,warajevo_read.c: avoid (undefined) reliance on pointer overflow in buffer length checks (Stuart). +20150302 internals.h,pzx_read.c,szx.c,test/test.c,zxs.c: add and use ARRAY_SIZE + macro (Stuart). Modified: trunk/libspectrum/internals.h =================================================================== --- trunk/libspectrum/internals.h 2015-03-01 11:25:31 UTC (rev 5117) +++ trunk/libspectrum/internals.h 2015-03-02 12:10:25 UTC (rev 5118) @@ -51,6 +51,32 @@ #endif /* _MSC_VER > 1200 */ #endif /* #ifdef _MSC_VER */ +#if defined(__GNUC__) && defined(__GNUC_MINOR__) + #define GNUC_VERSION \ + (__GNUC__ << 16) + __GNUC_MINOR__ + #define GNUC_PREREQ(maj, min) \ + (GNUC_VERSION >= ((maj) << 16) + (min)) +#else + #define GNUC_PREREQ(maj, min) 0 +#endif + +#define BUILD_BUG_ON_ZERO(e) \ + (sizeof(struct { int:-!!(e) * 1234; })) + +#if GNUC_PREREQ(3, 1) + #define SAME_TYPE(a, b) \ + __builtin_types_compatible_p(typeof(a), typeof(b)) + #define MUST_BE_ARRAY(a) \ + BUILD_BUG_ON_ZERO(SAME_TYPE((a), &(*a))) +#else + #define MUST_BE_ARRAY(a) \ + BUILD_BUG_ON_ZERO(sizeof(a) % sizeof(*a)) +#endif + +#define ARRAY_SIZE(a) ( \ + (sizeof(a) / sizeof(*a)) \ + + MUST_BE_ARRAY(a)) + /* VC6 lacks M_LN2, and VS2003+ require _USE_MATH_DEFINES defined before math.h */ #ifndef M_LN2 Modified: trunk/libspectrum/pzx_read.c =================================================================== --- trunk/libspectrum/pzx_read.c 2015-03-01 11:25:31 UTC (rev 5117) +++ trunk/libspectrum/pzx_read.c 2015-03-02 12:10:25 UTC (rev 5118) @@ -69,9 +69,6 @@ }; -static size_t info_ids_count = - sizeof( info_ids ) / sizeof( struct info_t ); - #define PZX_PULSE "PULS" #define PZX_DATA "DATA" @@ -118,7 +115,7 @@ static int get_id_byte( char *info_tag ) { struct info_t *info = - (struct info_t*)bsearch( info_tag, info_ids, info_ids_count, + (struct info_t*)bsearch( info_tag, info_ids, ARRAY_SIZE( info_ids ), sizeof( struct info_t ), info_t_compar ); return info == NULL ? -1 : info->archive_info_id; } @@ -514,9 +511,6 @@ }; -static size_t read_blocks_count = - sizeof( read_blocks ) / sizeof( struct read_block_t ); - static libspectrum_error read_block_header( char *id, libspectrum_dword *data_length, const libspectrum_byte **buffer, @@ -558,7 +552,7 @@ done = 0; - for( i = 0; !done && i < read_blocks_count; i++ ) { + for( i = 0; !done && i < ARRAY_SIZE( read_blocks ); i++ ) { if( !memcmp( id, read_blocks[i].id, 4 ) ) { error = read_blocks[i].function( tape, buffer, end, data_length, ctx ); Modified: trunk/libspectrum/szx.c =================================================================== --- trunk/libspectrum/szx.c 2015-03-01 11:25:31 UTC (rev 5117) +++ trunk/libspectrum/szx.c 2015-03-02 12:10:25 UTC (rev 5118) @@ -2211,9 +2211,6 @@ }; -static size_t read_chunks_count = - sizeof( read_chunks ) / sizeof( struct read_chunk_t ); - static libspectrum_error read_chunk_header( char *id, libspectrum_dword *data_length, const libspectrum_byte **buffer, @@ -2256,7 +2253,7 @@ done = 0; - for( i = 0; !done && i < read_chunks_count; i++ ) { + for( i = 0; !done && i < ARRAY_SIZE( read_chunks ); i++ ) { if( !memcmp( id, read_chunks[i].id, 4 ) ) { error = read_chunks[i].function( snap, version, buffer, end, Modified: trunk/libspectrum/test/test.c =================================================================== --- trunk/libspectrum/test/test.c 2015-03-01 11:25:31 UTC (rev 5117) +++ trunk/libspectrum/test/test.c 2015-03-02 12:10:25 UTC (rev 5118) @@ -6,6 +6,7 @@ #include <sys/types.h> #include <unistd.h> +#include "internals.h" #include "test.h" const char *progname; @@ -770,7 +771,7 @@ { test_27, "Reading old SZX file", 0 }, }; -static size_t test_count = sizeof( tests ) / sizeof( tests[0] ); +static size_t test_count = ARRAY_SIZE( tests ); static void parse_test_specs( char **specs, int count ) Modified: trunk/libspectrum/zxs.c =================================================================== --- trunk/libspectrum/zxs.c 2015-03-01 11:25:31 UTC (rev 5117) +++ trunk/libspectrum/zxs.c 2015-03-02 12:10:25 UTC (rev 5118) @@ -427,9 +427,6 @@ }; -static size_t read_chunks_count = - sizeof( read_chunks ) / sizeof( struct read_chunk_t ); - static libspectrum_error read_chunk_header( char *id, libspectrum_dword *data_length, const libspectrum_byte **buffer, @@ -472,7 +469,7 @@ done = 0; - for( i = 0; !done && i < read_chunks_count; i++ ) { + for( i = 0; !done && i < ARRAY_SIZE( read_chunks ); i++ ) { if( !strcmp( id, read_chunks[i].id ) ) { error = read_chunks[i].function( snap, &compression, buffer, end, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |