From a6cb18708c5ae6d8a403871a96177b0465982cb8 Mon Sep 17 00:00:00 2001 From: Pierre Wendling Date: Sat, 3 Jun 2023 21:15:14 -0400 Subject: [PATCH 1/3] Fix building on MSVC. - `__attribute__((unused))` does not exist. - VLAs are not supported, use a std::string instead. --- libgambatte/src/file/unzip/crypt.h | 8 +++++++- libgambatte/src/file/unzip/ioapi.c | 20 ++++++++++++-------- libgambatte/src/statesaver.cpp | 7 ++++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/libgambatte/src/file/unzip/crypt.h b/libgambatte/src/file/unzip/crypt.h index 7d8597c2..d081b148 100644 --- a/libgambatte/src/file/unzip/crypt.h +++ b/libgambatte/src/file/unzip/crypt.h @@ -29,10 +29,16 @@ #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8)) +#ifdef _MSC_VER +#define ATTRIBUTE_UNUSED +#else +#define ATTRIBUTE_UNUSED __attribute__((unused)) +#endif + /*********************************************************************** * Return the next byte in the pseudo-random sequence */ -static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab __attribute__((unused))) +static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab ATTRIBUTE_UNUSED) { unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an * unpredictable manner on 16-bit systems; not a problem diff --git a/libgambatte/src/file/unzip/ioapi.c b/libgambatte/src/file/unzip/ioapi.c index 84795e34..04237674 100644 --- a/libgambatte/src/file/unzip/ioapi.c +++ b/libgambatte/src/file/unzip/ioapi.c @@ -13,7 +13,11 @@ #include #include "ioapi.h" - +#ifdef _MSC_VER +#define ATTRIBUTE_UNUSED +#else +#define ATTRIBUTE_UNUSED __attribute__((unused)) +#endif /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ @@ -66,7 +70,7 @@ int ZCALLBACK ferror_file_func OF(( voidpf ZCALLBACK fopen_file_func (opaque, filename, mode) - voidpf opaque __attribute__((unused)); + voidpf opaque ATTRIBUTE_UNUSED; const char* filename; int mode; { @@ -88,7 +92,7 @@ voidpf ZCALLBACK fopen_file_func (opaque, filename, mode) uLong ZCALLBACK fread_file_func (opaque, stream, buf, size) - voidpf opaque __attribute__((unused)); + voidpf opaque ATTRIBUTE_UNUSED; voidpf stream; void* buf; uLong size; @@ -100,7 +104,7 @@ uLong ZCALLBACK fread_file_func (opaque, stream, buf, size) uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size) - voidpf opaque __attribute__((unused)); + voidpf opaque ATTRIBUTE_UNUSED; voidpf stream; const void* buf; uLong size; @@ -111,7 +115,7 @@ uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size) } long ZCALLBACK ftell_file_func (opaque, stream) - voidpf opaque __attribute__((unused)); + voidpf opaque ATTRIBUTE_UNUSED; voidpf stream; { long ret; @@ -120,7 +124,7 @@ long ZCALLBACK ftell_file_func (opaque, stream) } long ZCALLBACK fseek_file_func (opaque, stream, offset, origin) - voidpf opaque __attribute__((unused)); + voidpf opaque ATTRIBUTE_UNUSED; voidpf stream; uLong offset; int origin; @@ -146,7 +150,7 @@ long ZCALLBACK fseek_file_func (opaque, stream, offset, origin) } int ZCALLBACK fclose_file_func (opaque, stream) - voidpf opaque __attribute__((unused)); + voidpf opaque ATTRIBUTE_UNUSED; voidpf stream; { int ret; @@ -155,7 +159,7 @@ int ZCALLBACK fclose_file_func (opaque, stream) } int ZCALLBACK ferror_file_func (opaque, stream) - voidpf opaque __attribute__((unused)); + voidpf opaque ATTRIBUTE_UNUSED; voidpf stream; { int ret; diff --git a/libgambatte/src/statesaver.cpp b/libgambatte/src/statesaver.cpp index 501f5c42..25558f78 100644 --- a/libgambatte/src/statesaver.cpp +++ b/libgambatte/src/statesaver.cpp @@ -550,10 +550,11 @@ bool StateSaver::saveState(SaveState const &state, return false; std::size_t size = saveState(state, videoBuf, pitch, NULL, mode); - char stateBuf[size]; + std::string stateBuf; + stateBuf.reserve(size); - saveState(state, videoBuf, pitch, stateBuf, mode); - file.write(stateBuf, size); + saveState(state, videoBuf, pitch, stateBuf.data(), mode); + file << stateBuf; return !file.fail(); } From 7925c74de7a5535b89ebc0d934fb3b91a968dbef Mon Sep 17 00:00:00 2001 From: Pierre Wendling Date: Sat, 3 Jun 2023 22:46:22 -0400 Subject: [PATCH 2/3] Use unique_ptr to manage the stateBuf. --- libgambatte/src/statesaver.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libgambatte/src/statesaver.cpp b/libgambatte/src/statesaver.cpp index 25558f78..78524fcb 100644 --- a/libgambatte/src/statesaver.cpp +++ b/libgambatte/src/statesaver.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -550,11 +551,10 @@ bool StateSaver::saveState(SaveState const &state, return false; std::size_t size = saveState(state, videoBuf, pitch, NULL, mode); - std::string stateBuf; - stateBuf.reserve(size); + std::unique_ptr stateBuf {new char[size]}; - saveState(state, videoBuf, pitch, stateBuf.data(), mode); - file << stateBuf; + saveState(state, videoBuf, pitch, stateBuf.get(), mode); + file.write(stateBuf.get(), size); return !file.fail(); } From f8d1f967d5fd3d0be84192de6c5e9ff52994b512 Mon Sep 17 00:00:00 2001 From: Pierre Wendling Date: Sat, 3 Jun 2023 22:53:46 -0400 Subject: [PATCH 3/3] Prefer using `(void)` instead of `__attribute__`. --- libgambatte/src/file/unzip/crypt.h | 9 ++---- libgambatte/src/file/unzip/ioapi.c | 51 +++++++++--------------------- 2 files changed, 17 insertions(+), 43 deletions(-) diff --git a/libgambatte/src/file/unzip/crypt.h b/libgambatte/src/file/unzip/crypt.h index d081b148..4141ae99 100644 --- a/libgambatte/src/file/unzip/crypt.h +++ b/libgambatte/src/file/unzip/crypt.h @@ -29,17 +29,12 @@ #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8)) -#ifdef _MSC_VER -#define ATTRIBUTE_UNUSED -#else -#define ATTRIBUTE_UNUSED __attribute__((unused)) -#endif - /*********************************************************************** * Return the next byte in the pseudo-random sequence */ -static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab ATTRIBUTE_UNUSED) +static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab) { + (void)pcrc_32_tab; unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an * unpredictable manner on 16-bit systems; not a problem * with any known compiler so far, though */ diff --git a/libgambatte/src/file/unzip/ioapi.c b/libgambatte/src/file/unzip/ioapi.c index 04237674..3ada75e8 100644 --- a/libgambatte/src/file/unzip/ioapi.c +++ b/libgambatte/src/file/unzip/ioapi.c @@ -13,12 +13,6 @@ #include #include "ioapi.h" -#ifdef _MSC_VER -#define ATTRIBUTE_UNUSED -#else -#define ATTRIBUTE_UNUSED __attribute__((unused)) -#endif - /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ #ifndef SEEK_CUR @@ -69,11 +63,9 @@ int ZCALLBACK ferror_file_func OF(( voidpf stream)); -voidpf ZCALLBACK fopen_file_func (opaque, filename, mode) - voidpf opaque ATTRIBUTE_UNUSED; - const char* filename; - int mode; +voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode) { + (void)opaque; FILE* file = NULL; const char* mode_fopen = NULL; if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) @@ -91,44 +83,34 @@ voidpf ZCALLBACK fopen_file_func (opaque, filename, mode) } -uLong ZCALLBACK fread_file_func (opaque, stream, buf, size) - voidpf opaque ATTRIBUTE_UNUSED; - voidpf stream; - void* buf; - uLong size; +uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size) { + (void)opaque; uLong ret; ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream); return ret; } -uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size) - voidpf opaque ATTRIBUTE_UNUSED; - voidpf stream; - const void* buf; - uLong size; +uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size) { + (void)opaque; uLong ret; ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream); return ret; } -long ZCALLBACK ftell_file_func (opaque, stream) - voidpf opaque ATTRIBUTE_UNUSED; - voidpf stream; +long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream) { + (void)opaque; long ret; ret = ftell((FILE *)stream); return ret; } -long ZCALLBACK fseek_file_func (opaque, stream, offset, origin) - voidpf opaque ATTRIBUTE_UNUSED; - voidpf stream; - uLong offset; - int origin; +long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin) { + (void)opaque; int fseek_origin=0; long ret; switch (origin) @@ -149,26 +131,23 @@ long ZCALLBACK fseek_file_func (opaque, stream, offset, origin) return ret; } -int ZCALLBACK fclose_file_func (opaque, stream) - voidpf opaque ATTRIBUTE_UNUSED; - voidpf stream; +int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream) { + (void)opaque; int ret; ret = fclose((FILE *)stream); return ret; } -int ZCALLBACK ferror_file_func (opaque, stream) - voidpf opaque ATTRIBUTE_UNUSED; - voidpf stream; +int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream) { + (void)opaque; int ret; ret = ferror((FILE *)stream); return ret; } -void fill_fopen_filefunc (pzlib_filefunc_def) - zlib_filefunc_def* pzlib_filefunc_def; +void fill_fopen_filefunc (zlib_filefunc_def* pzlib_filefunc_def) { pzlib_filefunc_def->zopen_file = fopen_file_func; pzlib_filefunc_def->zread_file = fread_file_func;