Use unique_ptr for MVEFILE::cur_chunk

This commit is contained in:
Kp 2015-01-28 03:42:52 +00:00
parent cd14d2163b
commit a3edc2b4f8
2 changed files with 16 additions and 18 deletions

View file

@ -101,7 +101,7 @@ static void mvefile_reset(MVEFILE *file)
static bool have_segment_header(const MVEFILE *movie)
{
/* if nothing is cached, fail */
if (movie->cur_chunk == NULL || movie->next_segment >= movie->cur_fill)
if (!movie->cur_chunk || movie->next_segment >= movie->cur_fill)
return false;
/* if we don't have enough data to get a segment, fail */
if (movie->cur_fill - movie->next_segment < 4)
@ -261,7 +261,6 @@ int mve_play_next_chunk(MVESTREAM *movie)
*/
MVEFILE::MVEFILE() :
stream(nullptr),
cur_chunk(nullptr),
buf_size(0),
cur_fill(0),
next_segment(0)
@ -273,9 +272,6 @@ MVEFILE::MVEFILE() :
*/
MVEFILE::~MVEFILE()
{
/* free the buffer */
if (cur_chunk)
mve_free(cur_chunk);
}
/*
@ -348,17 +344,11 @@ static void _mvefile_set_buffer_size(MVEFILE *movie, int buf_size)
/* copy old data */
if (movie->cur_chunk && movie->cur_fill)
memcpy(new_buffer, movie->cur_chunk, movie->cur_fill);
memcpy(new_buffer, movie->cur_chunk.get(), movie->cur_fill);
/* free old buffer */
if (movie->cur_chunk)
{
mve_free(movie->cur_chunk);
movie->cur_chunk = 0;
}
/* install new buffer */
movie->cur_chunk = new_buffer;
movie->cur_chunk.reset(new_buffer);
movie->buf_size = new_len;
}

View file

@ -4,8 +4,8 @@
* project's Git history. See COPYING.txt at the top level for license
* terms and a link to the Git history.
*/
#ifndef INCLUDED_MVELIB_H
#define INCLUDED_MVELIB_H
#pragma once
#include <stdio.h>
#include <stdlib.h>
@ -14,6 +14,7 @@
#ifdef __cplusplus
#include <cstdint>
#include <memory>
#include "dxxsconf.h"
#include "compiler-array.h"
@ -23,6 +24,15 @@ extern mve_cb_Free mve_free;
extern mve_cb_ShowFrame mve_showframe;
extern mve_cb_SetPalette mve_setpalette;
class MVE_chunk_deleter
{
public:
void operator()(uint8_t *p) const
{
mve_free(p);
}
};
/*
* structure for maintaining info on a MVEFILE stream
*/
@ -31,7 +41,7 @@ struct MVEFILE
MVEFILE();
~MVEFILE();
void *stream;
unsigned char *cur_chunk;
std::unique_ptr<uint8_t[], MVE_chunk_deleter> cur_chunk;
int buf_size;
int cur_fill;
int next_segment;
@ -116,5 +126,3 @@ void mve_set_handler_context(MVESTREAM *movie, void *context);
int mve_play_next_chunk(MVESTREAM *movie);
#endif
#endif /* INCLUDED_MVELIB_H */