Commit e5272103 authored by Filip Roséen's avatar Filip Roséen Committed by Jean-Baptiste Kempf

mkv: removed unnecessary dynamic memory allocations

As written in the commit title, a few uses of dynamic memory management
has been removed, effectivelly fixing a memory leak as well as making
the code cleaner/faster.
Signed-off-by: Jean-Baptiste Kempf's avatarJean-Baptiste Kempf <jb@videolan.org>
parent 19c66b86
...@@ -108,7 +108,6 @@ matroska_segment_c::~matroska_segment_c() ...@@ -108,7 +108,6 @@ matroska_segment_c::~matroska_segment_c()
void matroska_segment_c::LoadCues( KaxCues *cues ) void matroska_segment_c::LoadCues( KaxCues *cues )
{ {
bool b_invalid_cue; bool b_invalid_cue;
EbmlParser *ep;
EbmlElement *el; EbmlElement *el;
if( b_cues ) if( b_cues )
...@@ -117,9 +116,8 @@ void matroska_segment_c::LoadCues( KaxCues *cues ) ...@@ -117,9 +116,8 @@ void matroska_segment_c::LoadCues( KaxCues *cues )
return; return;
} }
ep = new EbmlParser( &es, cues, &sys.demuxer, EbmlParser eparser (&es, cues, &sys.demuxer, var_InheritBool( &sys.demuxer, "mkv-use-dummy" ) );
var_InheritBool( &sys.demuxer, "mkv-use-dummy" ) ); while( ( el = eparser.Get() ) != NULL )
while( ( el = ep->Get() ) != NULL )
{ {
if( MKV_IS_ID( el, KaxCuePoint ) ) if( MKV_IS_ID( el, KaxCuePoint ) )
{ {
...@@ -132,8 +130,8 @@ void matroska_segment_c::LoadCues( KaxCues *cues ) ...@@ -132,8 +130,8 @@ void matroska_segment_c::LoadCues( KaxCues *cues )
last_idx.i_mk_time = -1; last_idx.i_mk_time = -1;
last_idx.b_key = true; last_idx.b_key = true;
ep->Down(); eparser.Down();
while( ( el = ep->Get() ) != NULL ) while( ( el = eparser.Get() ) != NULL )
{ {
if ( MKV_CHECKED_PTR_DECL( kct_ptr, KaxCueTime, el ) ) if ( MKV_CHECKED_PTR_DECL( kct_ptr, KaxCueTime, el ) )
{ {
...@@ -157,14 +155,14 @@ void matroska_segment_c::LoadCues( KaxCues *cues ) ...@@ -157,14 +155,14 @@ void matroska_segment_c::LoadCues( KaxCues *cues )
} }
else if( MKV_IS_ID( el, KaxCueTrackPositions ) ) else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
{ {
ep->Down(); eparser.Down();
try try
{ {
while( ( el = ep->Get() ) != NULL ) while( ( el = eparser.Get() ) != NULL )
{ {
if( unlikely( !el->ValidateSize() ) ) if( unlikely( !el->ValidateSize() ) )
{ {
ep->Up(); eparser.Up();
msg_Err( &sys.demuxer, "Error %s too big, aborting", typeid(*el).name() ); msg_Err( &sys.demuxer, "Error %s too big, aborting", typeid(*el).name() );
b_invalid_cue = true; b_invalid_cue = true;
break; break;
...@@ -203,19 +201,19 @@ void matroska_segment_c::LoadCues( KaxCues *cues ) ...@@ -203,19 +201,19 @@ void matroska_segment_c::LoadCues( KaxCues *cues )
} }
catch(...) catch(...)
{ {
ep->Up(); eparser.Up();
msg_Err( &sys.demuxer, "Error while reading %s", typeid(*el).name() ); msg_Err( &sys.demuxer, "Error while reading %s", typeid(*el).name() );
b_invalid_cue = true; b_invalid_cue = true;
break; break;
} }
ep->Up(); eparser.Up();
} }
else else
{ {
msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() ); msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
} }
} }
ep->Up(); eparser.Up();
#if 0 #if 0
msg_Dbg( &sys.demuxer, " * added time=%" PRId64 " pos=%" PRId64 msg_Dbg( &sys.demuxer, " * added time=%" PRId64 " pos=%" PRId64
...@@ -230,7 +228,6 @@ void matroska_segment_c::LoadCues( KaxCues *cues ) ...@@ -230,7 +228,6 @@ void matroska_segment_c::LoadCues( KaxCues *cues )
msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() ); msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
} }
} }
delete ep;
b_cues = true; b_cues = true;
msg_Dbg( &sys.demuxer, "| - loading cues done." ); msg_Dbg( &sys.demuxer, "| - loading cues done." );
} }
...@@ -262,9 +259,8 @@ static const struct { ...@@ -262,9 +259,8 @@ static const struct {
SimpleTag * matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag, int target_type ) SimpleTag * matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag, int target_type )
{ {
EbmlParser eparser ( &es, tag, &sys.demuxer, var_InheritBool( &sys.demuxer, "mkv-use-dummy" ) );
EbmlElement *el; EbmlElement *el;
EbmlParser *ep = new EbmlParser( &es, tag, &sys.demuxer,
var_InheritBool( &sys.demuxer, "mkv-use-dummy" ) );
SimpleTag * p_simple = new (std::nothrow) SimpleTag; SimpleTag * p_simple = new (std::nothrow) SimpleTag;
size_t max_size = tag->GetSize(); size_t max_size = tag->GetSize();
size_t size = 0; size_t size = 0;
...@@ -281,7 +277,7 @@ SimpleTag * matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag, int target_t ...@@ -281,7 +277,7 @@ SimpleTag * matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag, int target_t
msg_Dbg( &sys.demuxer, "| + Simple Tag "); msg_Dbg( &sys.demuxer, "| + Simple Tag ");
try try
{ {
while( ( el = ep->Get() ) != NULL && size < max_size) while( ( el = eparser.Get() ) != NULL && size < max_size)
{ {
if( unlikely( !el->ValidateSize() ) ) if( unlikely( !el->ValidateSize() ) )
{ {
...@@ -328,7 +324,6 @@ SimpleTag * matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag, int target_t ...@@ -328,7 +324,6 @@ SimpleTag * matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag, int target_t
delete p_simple; delete p_simple;
return NULL; return NULL;
} }
delete ep;
if( !p_simple->psz_tag_name || !p_simple->p_value ) if( !p_simple->psz_tag_name || !p_simple->p_value )
{ {
...@@ -366,10 +361,10 @@ done: ...@@ -366,10 +361,10 @@ done:
void matroska_segment_c::LoadTags( KaxTags *tags ) void matroska_segment_c::LoadTags( KaxTags *tags )
{ {
/* Master elements */ /* Master elements */
EbmlParser *ep = new EbmlParser( &es, tags, &sys.demuxer, true ); EbmlParser eparser = EbmlParser( &es, tags, &sys.demuxer, true );
EbmlElement *el; EbmlElement *el;
while( ( el = ep->Get() ) != NULL ) while( ( el = eparser.Get() ) != NULL )
{ {
if( MKV_IS_ID( el, KaxTag ) ) if( MKV_IS_ID( el, KaxTag ) )
{ {
...@@ -380,15 +375,15 @@ void matroska_segment_c::LoadTags( KaxTags *tags ) ...@@ -380,15 +375,15 @@ void matroska_segment_c::LoadTags( KaxTags *tags )
continue; continue;
} }
msg_Dbg( &sys.demuxer, "+ Tag" ); msg_Dbg( &sys.demuxer, "+ Tag" );
ep->Down(); eparser.Down();
int target_type = 50; int target_type = 50;
while( ( el = ep->Get() ) != NULL ) while( ( el = eparser.Get() ) != NULL )
{ {
if( MKV_IS_ID( el, KaxTagTargets ) ) if( MKV_IS_ID( el, KaxTagTargets ) )
{ {
msg_Dbg( &sys.demuxer, "| + Targets" ); msg_Dbg( &sys.demuxer, "| + Targets" );
ep->Down(); eparser.Down();
while( ( el = ep->Get() ) != NULL ) while( ( el = eparser.Get() ) != NULL )
{ {
try try
{ {
...@@ -444,7 +439,7 @@ void matroska_segment_c::LoadTags( KaxTags *tags ) ...@@ -444,7 +439,7 @@ void matroska_segment_c::LoadTags( KaxTags *tags )
break; break;
} }
} }
ep->Up(); eparser.Up();
} }
else if( MKV_CHECKED_PTR_DECL ( kts_ptr, KaxTagSimple, el ) ) else if( MKV_CHECKED_PTR_DECL ( kts_ptr, KaxTagSimple, el ) )
{ {
...@@ -495,7 +490,7 @@ void matroska_segment_c::LoadTags( KaxTags *tags ) ...@@ -495,7 +490,7 @@ void matroska_segment_c::LoadTags( KaxTags *tags )
msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() ); msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
} }
} }
ep->Up(); eparser.Up();
this->tags.push_back(p_tag); this->tags.push_back(p_tag);
} }
else else
...@@ -503,7 +498,6 @@ void matroska_segment_c::LoadTags( KaxTags *tags ) ...@@ -503,7 +498,6 @@ void matroska_segment_c::LoadTags( KaxTags *tags )
msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() ); msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
} }
} }
delete ep;
msg_Dbg( &sys.demuxer, "loading tags done." ); msg_Dbg( &sys.demuxer, "loading tags done." );
} }
...@@ -1220,13 +1214,12 @@ void matroska_segment_c::EnsureDuration() ...@@ -1220,13 +1214,12 @@ void matroska_segment_c::EnsureDuration()
// find the last timecode in the Cluster // find the last timecode in the Cluster
if ( i_last_cluster_pos ) if ( i_last_cluster_pos )
{ {
EbmlParser *ep;
es.I_O().setFilePointer( i_last_cluster_pos, seek_beginning ); es.I_O().setFilePointer( i_last_cluster_pos, seek_beginning );
ep = new EbmlParser( &es , segment, &sys.demuxer,
var_InheritBool( &sys.demuxer, "mkv-use-dummy" ) );
KaxCluster *p_last_cluster = static_cast<KaxCluster*>( ep->Get() ); EbmlParser eparser (
&es , segment, &sys.demuxer, var_InheritBool( &sys.demuxer, "mkv-use-dummy" ) );
KaxCluster *p_last_cluster = static_cast<KaxCluster*>( eparser.Get() );
if( p_last_cluster == NULL ) if( p_last_cluster == NULL )
return; return;
ParseCluster( p_last_cluster, false, SCOPE_PARTIAL_DATA ); ParseCluster( p_last_cluster, false, SCOPE_PARTIAL_DATA );
...@@ -1265,8 +1258,6 @@ void matroska_segment_c::EnsureDuration() ...@@ -1265,8 +1258,6 @@ void matroska_segment_c::EnsureDuration()
i_duration = ( i_last_timecode - cluster->GlobalTimecode() ) / INT64_C(1000000); i_duration = ( i_last_timecode - cluster->GlobalTimecode() ) / INT64_C(1000000);
msg_Dbg( &sys.demuxer, " extracted Duration=%" PRId64, i_duration ); msg_Dbg( &sys.demuxer, " extracted Duration=%" PRId64, i_duration );
delete ep;
} }
// get back to the reading position we were at before looking for a duration // get back to the reading position we were at before looking for a duration
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment