From f8a0fadfa1879291d68915c333657a8b686c8a66 Mon Sep 17 00:00:00 2001 From: Brandon Wright Date: Tue, 14 May 2019 15:34:25 -0500 Subject: [PATCH] GTK: Use C++ memory allocation. --- apu/resampler.h | 10 +++++ gtk/src/gtk_control.cpp | 12 +++--- gtk/src/gtk_display.cpp | 10 ++--- gtk/src/gtk_display_driver.h | 4 +- gtk/src/gtk_display_driver_gtk.cpp | 12 +++--- gtk/src/gtk_display_driver_opengl.cpp | 12 +++--- gtk/src/gtk_display_driver_xv.cpp | 14 +++--- gtk/src/gtk_sound_driver_sdl.cpp | 62 +++++++++------------------ gtk/src/gtk_sound_driver_sdl.h | 4 +- 9 files changed, 64 insertions(+), 76 deletions(-) diff --git a/apu/resampler.h b/apu/resampler.h index fa3a5696..a9173450 100644 --- a/apu/resampler.h +++ b/apu/resampler.h @@ -56,6 +56,13 @@ class Resampler return (a0 * b) + (a1 * m0) + (a2 * m1) + (a3 * c); } + Resampler() + { + this->buffer_size = 0; + buffer = NULL; + r_step = 1.0; + } + Resampler(int num_samples) { this->buffer_size = num_samples; @@ -77,6 +84,9 @@ class Resampler inline void clear(void) { + if (!buffer) + return; + start = 0; size = 0; memset(buffer, 0, buffer_size * 2); diff --git a/gtk/src/gtk_control.cpp b/gtk/src/gtk_control.cpp index 044274d4..9433c580 100644 --- a/gtk/src/gtk_control.cpp +++ b/gtk/src/gtk_control.cpp @@ -569,9 +569,9 @@ JoyDevice::JoyDevice (unsigned int device_num) num_axes = SDL_JoystickNumAxes (filedes); num_hats = SDL_JoystickNumHats (filedes); - axis = (int *) malloc (sizeof (int) * num_axes); - hat = (int *) malloc (sizeof (int) * num_hats); - calibration = (Calibration *) malloc (sizeof (Calibration) * num_axes); + axis = new int[num_axes]; + hat = new int[num_hats]; + calibration = new Calibration[num_axes]; for (int i = 0; i < num_axes; i++) { @@ -595,9 +595,9 @@ JoyDevice::~JoyDevice () if (enabled) { SDL_JoystickClose (filedes); - free (axis); - free (hat); - free (calibration); + delete[] axis; + delete[] hat; + delete[] calibration; } enabled = false; diff --git a/gtk/src/gtk_display.cpp b/gtk/src/gtk_display.cpp index 7cbc9afe..432e742f 100644 --- a/gtk/src/gtk_display.cpp +++ b/gtk/src/gtk_display.cpp @@ -1533,10 +1533,10 @@ void S9xDeinitDisplay () void S9xReinitDisplay () { - uint16 *buffer = NULL; - int width, height; + uint8_t *buffer = NULL; + int width, height; - buffer = (uint16 *) malloc (S9xDisplayDriver::image_size); + buffer = new uint8_t[S9xDisplayDriver::image_size]; memmove (buffer, driver->get_current_buffer (), S9xDisplayDriver::image_size); @@ -1551,9 +1551,9 @@ void S9xReinitDisplay () top_level->last_width = width; top_level->last_height = height; - driver->push_buffer (buffer); + driver->push_buffer ((uint16_t *)buffer); - free (buffer); + delete[] buffer; } bool8 S9xContinueUpdate (int width, int height) diff --git a/gtk/src/gtk_display_driver.h b/gtk/src/gtk_display_driver.h index 40ef760b..c3120688 100644 --- a/gtk/src/gtk_display_driver.h +++ b/gtk/src/gtk_display_driver.h @@ -49,8 +49,8 @@ class S9xDisplayDriver Snes9xWindow *window; Snes9xConfig *config; GtkWidget *drawing_area; - void *padded_buffer[4]; - void *buffer[4]; + uint8_t *padded_buffer[4]; + uint8_t *buffer[4]; }; #endif /* __GTK_DISPLAY_DRIVER_H*/ diff --git a/gtk/src/gtk_display_driver_gtk.cpp b/gtk/src/gtk_display_driver_gtk.cpp index 5e80fc4e..ebd99cdb 100644 --- a/gtk/src/gtk_display_driver_gtk.cpp +++ b/gtk/src/gtk_display_driver_gtk.cpp @@ -116,11 +116,11 @@ S9xGTKDisplayDriver::output (void *src, int S9xGTKDisplayDriver::init () { - buffer[0] = malloc (image_padded_size); - buffer[1] = malloc (scaled_padded_size); + buffer[0] = new uint8_t[image_padded_size]; + buffer[1] = new uint8_t[scaled_padded_size]; - padded_buffer[0] = (void *) (((uint8 *) buffer[0]) + image_padded_offset); - padded_buffer[1] = (void *) (((uint8 *) buffer[1]) + scaled_padded_offset); + padded_buffer[0] = &buffer[0][image_padded_offset]; + padded_buffer[1] = &buffer[1][scaled_padded_offset]; memset (buffer[0], 0, image_padded_size); memset (buffer[1], 0, scaled_padded_size); @@ -137,8 +137,8 @@ S9xGTKDisplayDriver::deinit () padded_buffer[0] = NULL; padded_buffer[1] = NULL; - free (buffer[0]); - free (buffer[1]); + delete[] buffer[0]; + delete[] buffer[1]; } void diff --git a/gtk/src/gtk_display_driver_opengl.cpp b/gtk/src/gtk_display_driver_opengl.cpp index 1be6c3bb..a82c36ff 100644 --- a/gtk/src/gtk_display_driver_opengl.cpp +++ b/gtk/src/gtk_display_driver_opengl.cpp @@ -618,13 +618,13 @@ int S9xOpenGLDisplayDriver::init () return -1; } - buffer[0] = malloc (image_padded_size); - buffer[1] = malloc (scaled_padded_size); + buffer[0] = new uint8_t[image_padded_size]; + buffer[1] = new uint8_t[scaled_padded_size]; clear_buffers (); - padded_buffer[0] = (void *) (((uint8 *) buffer[0]) + image_padded_offset); - padded_buffer[1] = (void *) (((uint8 *) buffer[1]) + scaled_padded_offset); + padded_buffer[0] = &buffer[0][image_padded_offset]; + padded_buffer[1] = &buffer[1][scaled_padded_offset]; GFX.Screen = (uint16 *) padded_buffer[0]; GFX.Pitch = image_width * image_bpp; @@ -680,8 +680,8 @@ void S9xOpenGLDisplayDriver::deinit () padded_buffer[0] = NULL; padded_buffer[1] = NULL; - free (buffer[0]); - free (buffer[1]); + delete[] buffer[0]; + delete[] buffer[1]; if (using_pbos) { diff --git a/gtk/src/gtk_display_driver_xv.cpp b/gtk/src/gtk_display_driver_xv.cpp index 86391196..b40f809d 100644 --- a/gtk/src/gtk_display_driver_xv.cpp +++ b/gtk/src/gtk_display_driver_xv.cpp @@ -448,11 +448,11 @@ S9xXVDisplayDriver::init () desired_width = scaled_max_width; desired_height = scaled_max_width; - buffer[0] = malloc (image_padded_size); - buffer[1] = malloc (scaled_padded_size); + buffer[0] = new uint8_t[image_padded_size]; + buffer[1] = new uint8_t[scaled_padded_size]; - padded_buffer[0] = (void *) (((uint8 *) buffer[0]) + image_padded_offset); - padded_buffer[1] = (void *) (((uint8 *) buffer[1]) + scaled_padded_offset); + padded_buffer[0] = &buffer[0][image_padded_offset]; + padded_buffer[1] = &buffer[1][scaled_padded_offset]; memset (buffer[0], 0, image_padded_size); memset (buffer[1], 0, scaled_padded_size); @@ -477,8 +477,8 @@ S9xXVDisplayDriver::deinit () XFreeColormap (display, xcolormap); XFree (vi); - free (buffer[0]); - free (buffer[1]); + delete[] buffer[0]; + delete[] buffer[1]; shmctl (shm.shmid, IPC_RMID, 0); shmdt (shm.shmaddr); @@ -559,7 +559,7 @@ S9xXVDisplayDriver::get_current_buffer () void S9xXVDisplayDriver::push_buffer (uint16 *src) { - memmove (GFX.Screen, src, image_size); + memmove (GFX.Screen, src, image_size * image_bpp); } void diff --git a/gtk/src/gtk_sound_driver_sdl.cpp b/gtk/src/gtk_sound_driver_sdl.cpp index 8fc694e1..c120fa36 100644 --- a/gtk/src/gtk_sound_driver_sdl.cpp +++ b/gtk/src/gtk_sound_driver_sdl.cpp @@ -25,35 +25,33 @@ void S9xSDLSoundDriver::samples_available() if (Settings.SoundSync && !Settings.TurboMode && !Settings.Mute) { mutex.lock(); - int samples = buffer->space_empty(); + int samples = buffer.space_empty(); mutex.unlock(); while (samples < snes_samples_available) { usleep(100); mutex.lock(); - samples = buffer->space_empty(); + samples = buffer.space_empty(); mutex.unlock(); } } mutex.lock(); - buffer->push(temp, snes_samples_available); + buffer.push(temp, snes_samples_available); mutex.unlock(); } void S9xSDLSoundDriver::mix(unsigned char *output, int bytes) { mutex.lock(); - if (buffer->avail() >= bytes >> 1) - buffer->read((int16_t *)output, bytes >> 1); + if (buffer.avail() >= bytes >> 1) + buffer.read((int16_t *)output, bytes >> 1); mutex.unlock(); } S9xSDLSoundDriver::S9xSDLSoundDriver() { - buffer = NULL; - audiospec = NULL; } void S9xSDLSoundDriver::init() @@ -65,16 +63,7 @@ void S9xSDLSoundDriver::init() void S9xSDLSoundDriver::terminate() { stop(); - - if (audiospec) - { - SDL_CloseAudio(); - if (buffer) - delete buffer; - free(audiospec); - audiospec = NULL; - } - + SDL_CloseAudio(); SDL_QuitSubSystem(SDL_INIT_AUDIO); } @@ -82,51 +71,40 @@ void S9xSDLSoundDriver::start() { if (!gui_config->mute_sound) { - if (audiospec) - { - SDL_PauseAudio(0); - } + SDL_PauseAudio(0); } } void S9xSDLSoundDriver::stop() { - if (audiospec) - { - SDL_PauseAudio(1); - } + SDL_PauseAudio(1); } bool S9xSDLSoundDriver::open_device() { - audiospec = (SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec)); - - audiospec->freq = Settings.SoundPlaybackRate; - audiospec->channels = 2; - audiospec->format = AUDIO_S16SYS; - audiospec->samples = (gui_config->sound_buffer_size * audiospec->freq / 1000) >> 2; - audiospec->callback = sdl_audio_callback; - audiospec->userdata = this; + audiospec = {}; + audiospec.freq = Settings.SoundPlaybackRate; + audiospec.channels = 2; + audiospec.format = AUDIO_S16SYS; + audiospec.samples = (gui_config->sound_buffer_size * audiospec.freq / 1000) >> 2; + audiospec.callback = sdl_audio_callback; + audiospec.userdata = this; printf("SDL sound driver initializing...\n"); printf(" --> (Frequency: %dhz, Latency: %dms)...", - audiospec->freq, - (audiospec->samples * 1000 / audiospec->freq)); + audiospec.freq, + (audiospec.samples * 1000 / audiospec.freq)); - if (SDL_OpenAudio(audiospec, NULL) < 0) + if (SDL_OpenAudio(&audiospec, NULL) < 0) { printf("Failed\n"); - - free(audiospec); - audiospec = NULL; - return false; } printf("OK\n"); - buffer = new Resampler(gui_config->sound_buffer_size * audiospec->freq / 500); - buffer->time_ratio(1.0); + buffer.resize(gui_config->sound_buffer_size * audiospec.freq / 500); + buffer.time_ratio(1.0); S9xSetSamplesAvailableCallback(c_samples_available, this); diff --git a/gtk/src/gtk_sound_driver_sdl.h b/gtk/src/gtk_sound_driver_sdl.h index 3b6ad068..b318e069 100644 --- a/gtk/src/gtk_sound_driver_sdl.h +++ b/gtk/src/gtk_sound_driver_sdl.h @@ -29,8 +29,8 @@ class S9xSDLSoundDriver : public S9xSoundDriver void samples_available(); private: - SDL_AudioSpec *audiospec; - Resampler *buffer; + SDL_AudioSpec audiospec; + Resampler buffer; std::mutex mutex; int16_t temp[512]; };