From a42e7c3db1a374f3e1bd75a1f46c384215f0240d Mon Sep 17 00:00:00 2001 From: Kp Date: Sat, 1 Nov 2014 15:00:48 +0000 Subject: [PATCH] Use unique_ptr for MVESTREAM --- d2x-rebirth/include/libmve.h | 4 +-- d2x-rebirth/libmve/mvelib.cpp | 57 +++++++++------------------------- d2x-rebirth/libmve/mvelib.h | 11 +++---- d2x-rebirth/libmve/mveplay.cpp | 5 +-- 4 files changed, 23 insertions(+), 54 deletions(-) diff --git a/d2x-rebirth/include/libmve.h b/d2x-rebirth/include/libmve.h index 6bde2e2ea..83af9c748 100644 --- a/d2x-rebirth/include/libmve.h +++ b/d2x-rebirth/include/libmve.h @@ -24,13 +24,13 @@ struct MVE_videoSpec { int MVE_rmStepMovie(MVESTREAM *mve); void MVE_rmHoldMovie(); -void MVE_rmEndMovie(MVESTREAM *&mve); +void MVE_rmEndMovie(std::unique_ptr mve); struct MVESTREAM_deleter_t { void operator()(MVESTREAM *p) const { - MVE_rmEndMovie(p); + MVE_rmEndMovie(std::unique_ptr(p)); } }; diff --git a/d2x-rebirth/libmve/mvelib.cpp b/d2x-rebirth/libmve/mvelib.cpp index b7405ba75..4a7737cb4 100644 --- a/d2x-rebirth/libmve/mvelib.cpp +++ b/d2x-rebirth/libmve/mvelib.cpp @@ -14,6 +14,8 @@ #include "mvelib.h" +#include "compiler-make_unique.h" + static const char MVE_HEADER[] = "Interplay MVE File\x1A"; static const short MVE_HDRCONST1 = 0x001A; static const short MVE_HDRCONST2 = 0x0100; @@ -45,8 +47,6 @@ static int _mvefile_fetch_next_chunk(MVEFILE *movie); /* * private functions for mvestream */ -static MVESTREAM *_mvestream_alloc(void); -static void _mvestream_free(MVESTREAM *movie); static int _mvestream_open(MVESTREAM *movie, void *stream); static void _mvestream_reset(MVESTREAM *movie); @@ -200,26 +200,15 @@ int mvefile_fetch_next_chunk(MVEFILE *movie) */ MVESTREAM_ptr_t mve_open(void *stream) { - MVESTREAM *movie; - /* allocate */ - movie = _mvestream_alloc(); + auto movie = make_unique(); /* open */ - if (! _mvestream_open(movie, stream)) + if (! _mvestream_open(movie.get(), stream)) { - _mvestream_free(movie); - return NULL; + return nullptr; } - return MVESTREAM_ptr_t(movie); -} - -/* - * close an MVE stream - */ -void mve_close(MVESTREAM *movie) -{ - _mvestream_free(movie); + return MVESTREAM_ptr_t(movie.release()); } /* @@ -260,7 +249,7 @@ int mve_play_next_chunk(MVESTREAM *movie) while (major != 0xff) { /* check whether to handle the segment */ - if (major < 32 && movie->handlers[major] != NULL) + if (major < movie->handlers.size() && movie->handlers[major]) { minor = mvefile_get_next_segment_minor(movie->movie); len = mvefile_get_next_segment_size(movie->movie); @@ -452,35 +441,19 @@ static uint16_t _mve_get_ushort(const unsigned char *data) /* * allocate an MVESTREAM */ -static MVESTREAM *_mvestream_alloc(void) -{ - MVESTREAM *movie; - +MVESTREAM::MVESTREAM() : /* allocate and zero-initialize everything */ - movie = (MVESTREAM *)mve_alloc(sizeof(MVESTREAM)); - movie->movie = NULL; - movie->context = 0; - memset(movie->handlers, 0, sizeof(movie->handlers)); - - return movie; + movie(nullptr), + context(0), + handlers{} +{ } -/* - * free an MVESTREAM - */ -static void _mvestream_free(MVESTREAM *movie) +MVESTREAM::~MVESTREAM() { /* close MVEFILE */ - if (movie->movie) - mvefile_close(movie->movie); - movie->movie = NULL; - - /* clear context and handlers */ - movie->context = NULL; - memset(movie->handlers, 0, sizeof(movie->handlers)); - - /* free the struct */ - mve_free(movie); + if (movie) + mvefile_close(movie); } /* diff --git a/d2x-rebirth/libmve/mvelib.h b/d2x-rebirth/libmve/mvelib.h index 3ed01ea29..2186a3615 100644 --- a/d2x-rebirth/libmve/mvelib.h +++ b/d2x-rebirth/libmve/mvelib.h @@ -14,6 +14,8 @@ #ifdef __cplusplus #include +#include "dxxsconf.h" +#include "compiler-array.h" extern mve_cb_Read mve_read; extern mve_cb_Alloc mve_alloc; @@ -84,9 +86,11 @@ typedef int (*MVESEGMENTHANDLER)(unsigned char major, unsigned char minor, const */ struct MVESTREAM { + MVESTREAM(); + ~MVESTREAM(); MVEFILE *movie; void *context; - MVESEGMENTHANDLER handlers[32]; + array handlers; }; /* @@ -94,11 +98,6 @@ struct MVESTREAM */ MVESTREAM_ptr_t mve_open(void *stream); -/* - * close an MVE stream - */ -void mve_close(MVESTREAM *movie); - /* * reset an MVE stream */ diff --git a/d2x-rebirth/libmve/mveplay.cpp b/d2x-rebirth/libmve/mveplay.cpp index 400a183a7..f08e9ad2e 100644 --- a/d2x-rebirth/libmve/mveplay.cpp +++ b/d2x-rebirth/libmve/mveplay.cpp @@ -753,7 +753,7 @@ int MVE_rmStepMovie(MVESTREAM *const mve) return 0; } -void MVE_rmEndMovie(MVESTREAM *&mve) +void MVE_rmEndMovie(std::unique_ptr pMovie) { timer_stop(); timer_created = 0; @@ -791,9 +791,6 @@ void MVE_rmEndMovie(MVESTREAM *&mve) g_nMapLength=0; videobuf_created = 0; video_initialized = 0; - - mve_close(mve); - mve = NULL; }