From 88ce545cdda64659894b87fc0b98e1b044eea3e0 Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Mon, 18 Nov 2019 05:08:36 +1100 Subject: misc cleanup: * Got rid of some more simple_list in core debugger code * Fixed a buffer overrun in wavwrite (buffer half requried size) * Slightly reduced dependencies and overhead in wavwrite * Made new disassembly windows in Qt debugger default to current CPU --- src/lib/util/corealloc.h | 2 - src/lib/util/wavwrite.cpp | 185 +++++++++++++++++++++++----------------------- src/lib/util/wavwrite.h | 13 ++-- 3 files changed, 99 insertions(+), 101 deletions(-) (limited to 'src/lib') diff --git a/src/lib/util/corealloc.h b/src/lib/util/corealloc.h index 12dea2fce67..222b1428307 100644 --- a/src/lib/util/corealloc.h +++ b/src/lib/util/corealloc.h @@ -32,9 +32,7 @@ // global allocation helpers -- use these instead of new and delete #define global_alloc(Type) new Type -#define global_alloc_nothrow(Type) new (std::nothrow) Type #define global_alloc_array(Type, Num) new Type[Num] -#define global_alloc_array_nothrow(Type, Num) new (std::nothrow) Type[Num] #define global_free(Ptr) do { delete Ptr; } while (0) #define global_free_array(Ptr) do { delete[] Ptr; } while (0) diff --git a/src/lib/util/wavwrite.cpp b/src/lib/util/wavwrite.cpp index 0667e9dcaff..93b4b0a6ca7 100644 --- a/src/lib/util/wavwrite.cpp +++ b/src/lib/util/wavwrite.cpp @@ -1,88 +1,91 @@ // license:BSD-3-Clause // copyright-holders:Aaron Giles -#include "osdcomm.h" -#include "corealloc.h" -#include #include "wavwrite.h" +#include "osdcomm.h" + +#include +#include +#include + + struct wav_file { - FILE *file; - uint32_t total_offs; - uint32_t data_offs; + FILE *file = nullptr; + std::uint32_t total_offs = 0U; + std::uint32_t data_offs = 0U; }; wav_file *wav_open(const char *filename, int sample_rate, int channels) { - wav_file *wav; - uint32_t bps, temp32; - uint16_t align, temp16; + std::uint32_t temp32; + std::uint16_t temp16; - /* allocate memory for the wav struct */ - wav = global_alloc_nothrow(wav_file); + // allocate memory for the wav struct + wav_file *const wav = new (std::nothrow) wav_file; if (!wav) return nullptr; - /* create the file */ - wav->file = fopen(filename, "wb"); + // create the file */ + wav->file = std::fopen(filename, "wb"); if (!wav->file) { - global_free(wav); + delete wav; return nullptr; } - /* write the 'RIFF' header */ - fwrite("RIFF", 1, 4, wav->file); + // write the 'RIFF' header + std::fwrite("RIFF", 1, 4, wav->file); - /* write the total size */ + // write the total size temp32 = 0; - wav->total_offs = ftell(wav->file); - fwrite(&temp32, 1, 4, wav->file); + wav->total_offs = std::ftell(wav->file); + std::fwrite(&temp32, 1, 4, wav->file); - /* write the 'WAVE' type */ - fwrite("WAVE", 1, 4, wav->file); + // write the 'WAVE' type + std::fwrite("WAVE", 1, 4, wav->file); - /* write the 'fmt ' tag */ - fwrite("fmt ", 1, 4, wav->file); + // write the 'fmt ' tag + std::fwrite("fmt ", 1, 4, wav->file); - /* write the format length */ + // write the format length temp32 = little_endianize_int32(16); - fwrite(&temp32, 1, 4, wav->file); + std::fwrite(&temp32, 1, 4, wav->file); - /* write the format (PCM) */ + // write the format (PCM) temp16 = little_endianize_int16(1); - fwrite(&temp16, 1, 2, wav->file); + std::fwrite(&temp16, 1, 2, wav->file); - /* write the channels */ + // write the channels temp16 = little_endianize_int16(channels); - fwrite(&temp16, 1, 2, wav->file); + std::fwrite(&temp16, 1, 2, wav->file); - /* write the sample rate */ + // write the sample rate temp32 = little_endianize_int32(sample_rate); - fwrite(&temp32, 1, 4, wav->file); + std::fwrite(&temp32, 1, 4, wav->file); - /* write the bytes/second */ - bps = sample_rate * 2 * channels; + // write the bytes/second + std::uint32_t const bps = sample_rate * 2 * channels; temp32 = little_endianize_int32(bps); - fwrite(&temp32, 1, 4, wav->file); + std::fwrite(&temp32, 1, 4, wav->file); - /* write the block align */ - align = 2 * channels; + // write the block align + std::uint16_t const align = 2 * channels; temp16 = little_endianize_int16(align); - fwrite(&temp16, 1, 2, wav->file); + std::fwrite(&temp16, 1, 2, wav->file); - /* write the bits/sample */ + // write the bits/sample temp16 = little_endianize_int16(16); - fwrite(&temp16, 1, 2, wav->file); + std::fwrite(&temp16, 1, 2, wav->file); - /* write the 'data' tag */ - fwrite("data", 1, 4, wav->file); + // write the 'data' tag + std::fwrite("data", 1, 4, wav->file); - /* write the data length */ + // write the data length temp32 = 0; - wav->data_offs = ftell(wav->file); - fwrite(&temp32, 1, 4, wav->file); + wav->data_offs = std::ftell(wav->file); + std::fwrite(&temp32, 1, 4, wav->file); return wav; } @@ -90,102 +93,96 @@ wav_file *wav_open(const char *filename, int sample_rate, int channels) void wav_close(wav_file *wav) { - uint32_t total; - uint32_t temp32; - - if (!wav) return; + if (!wav) + return; - total = ftell(wav->file); + std::uint32_t temp32; + std::uint32_t const total = std::ftell(wav->file); - /* update the total file size */ - fseek(wav->file, wav->total_offs, SEEK_SET); + // update the total file size + std::fseek(wav->file, wav->total_offs, SEEK_SET); temp32 = total - (wav->total_offs + 4); temp32 = little_endianize_int32(temp32); - fwrite(&temp32, 1, 4, wav->file); + std::fwrite(&temp32, 1, 4, wav->file); - /* update the data size */ - fseek(wav->file, wav->data_offs, SEEK_SET); + // update the data size + std::fseek(wav->file, wav->data_offs, SEEK_SET); temp32 = total - (wav->data_offs + 4); temp32 = little_endianize_int32(temp32); - fwrite(&temp32, 1, 4, wav->file); + std::fwrite(&temp32, 1, 4, wav->file); - fclose(wav->file); - global_free(wav); + std::fclose(wav->file); + delete wav; } void wav_add_data_16(wav_file *wav, int16_t *data, int samples) { - if (!wav) return; + if (!wav) + return; - /* just write and flush the data */ - fwrite(data, 2, samples, wav->file); - fflush(wav->file); + // just write and flush the data + std::fwrite(data, 2, samples, wav->file); + std::fflush(wav->file); } void wav_add_data_32(wav_file *wav, int32_t *data, int samples, int shift) { - std::vector temp; - int i; - - if (!wav || !samples) return; + if (!wav || !samples) + return; - /* resize dynamic array */ - temp.resize(samples); + // resize dynamic array + std::unique_ptr temp(new int16_t [samples]); - /* clamp */ - for (i = 0; i < samples; i++) + // clamp + for (int i = 0; i < samples; i++) { int val = data[i] >> shift; temp[i] = (val < -32768) ? -32768 : (val > 32767) ? 32767 : val; } - /* write and flush */ - fwrite(&temp[0], 2, samples, wav->file); - fflush(wav->file); + // write and flush + std::fwrite(&temp[0], 2, samples, wav->file); + std::fflush(wav->file); } void wav_add_data_16lr(wav_file *wav, int16_t *left, int16_t *right, int samples) { - std::vector temp; - int i; + if (!wav || !samples) + return; - if (!wav || !samples) return; + // resize dynamic array + std::unique_ptr temp(new int16_t [samples * 2]); - /* resize dynamic array */ - temp.resize(samples * 2); - - /* interleave */ - for (i = 0; i < samples * 2; i++) + // interleave + for (int i = 0; i < samples * 2; i++) temp[i] = (i & 1) ? right[i / 2] : left[i / 2]; - /* write and flush */ - fwrite(&temp[0], 4, samples, wav->file); - fflush(wav->file); + // write and flush + std::fwrite(&temp[0], 4, samples, wav->file); + std::fflush(wav->file); } void wav_add_data_32lr(wav_file *wav, int32_t *left, int32_t *right, int samples, int shift) { - std::vector temp; - int i; - - if (!wav || !samples) return; + if (!wav || !samples) + return; - /* resize dynamic array */ - temp.resize(samples); + // resize dynamic array + std::unique_ptr temp(new int16_t [samples * 2]); - /* interleave */ - for (i = 0; i < samples * 2; i++) + // interleave + for (int i = 0; i < samples * 2; i++) { int val = (i & 1) ? right[i / 2] : left[i / 2]; val >>= shift; temp[i] = (val < -32768) ? -32768 : (val > 32767) ? 32767 : val; } - /* write and flush */ - fwrite(&temp[0], 4, samples, wav->file); - fflush(wav->file); + // write and flush + std::fwrite(&temp[0], 4, samples, wav->file); + std::fflush(wav->file); } diff --git a/src/lib/util/wavwrite.h b/src/lib/util/wavwrite.h index 8ceb6ad2a0d..cdf6f4d0d78 100644 --- a/src/lib/util/wavwrite.h +++ b/src/lib/util/wavwrite.h @@ -5,14 +5,17 @@ #pragma once +#include + + struct wav_file; wav_file *wav_open(const char *filename, int sample_rate, int channels); -void wav_close(wav_file*wavptr); +void wav_close(wav_file *wavptr); -void wav_add_data_16(wav_file *wavptr, int16_t *data, int samples); -void wav_add_data_32(wav_file *wavptr, int32_t *data, int samples, int shift); -void wav_add_data_16lr(wav_file *wavptr, int16_t *left, int16_t *right, int samples); -void wav_add_data_32lr(wav_file *wavptr, int32_t *left, int32_t *right, int samples, int shift); +void wav_add_data_16(wav_file *wavptr, std::int16_t *data, int samples); +void wav_add_data_32(wav_file *wavptr, std::int32_t *data, int samples, int shift); +void wav_add_data_16lr(wav_file *wavptr, std::int16_t *left, std::int16_t *right, int samples); +void wav_add_data_32lr(wav_file *wavptr, std::int32_t *left, std::int32_t *right, int samples, int shift); #endif // MAME_UTIL_WAVWRITE_H -- cgit v1.2.3