[vlc-devel] [vlc-commits] array: fix up types in vlc_array_*()
Steve Lhomme
robux4 at gmail.com
Tue Mar 14 13:23:00 CET 2017
On Sun, Feb 19, 2017 at 9:59 PM, Rémi Denis-Courmont <git at videolan.org> wrote:
> vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Feb 18 16:10:02 2017 +0200| [7ecd9271afce3dc7545cecc8889f173905649ea0] | committer: Rémi Denis-Courmont
>
> array: fix up types in vlc_array_*()
>
> - use size_t, ssize_t,
> - handle const types where applicable.
>
>> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=7ecd9271afce3dc7545cecc8889f173905649ea0
> ---
>
> include/vlc_arrays.h | 99 ++++++++++++++++++----------
> include/vlc_fingerprinter.h | 2 +-
> lib/event.c | 6 +-
> lib/media_list.c | 2 +-
> modules/access/bluray.c | 6 +-
> modules/access/dsm/sd.c | 4 +-
> modules/access/zip/zipstream.c | 4 +-
> modules/access_output/livehttp.c | 2 +-
> modules/control/dbus/dbus.c | 20 +++---
> modules/gui/qt/dialogs/fingerprintdialog.cpp | 2 +-
> modules/misc/fingerprinter.c | 2 +-
> 11 files changed, 90 insertions(+), 59 deletions(-)
>
> diff --git a/include/vlc_arrays.h b/include/vlc_arrays.h
> index ec3c85c..d4c2d6e 100644
> --- a/include/vlc_arrays.h
> +++ b/include/vlc_arrays.h
> @@ -280,19 +280,20 @@ static inline void *realloc_down( void *ptr, size_t size )
> ************************************************************************/
> typedef struct vlc_array_t
> {
> - int i_count;
> + size_t i_count;
> void ** pp_elems;
> } vlc_array_t;
>
> static inline void vlc_array_init( vlc_array_t * p_array )
> {
> - memset( p_array, 0, sizeof(vlc_array_t) );
> + p_array->i_count = 0;
> + p_array->pp_elems = NULL;
> }
>
> static inline void vlc_array_clear( vlc_array_t * p_array )
> {
> free( p_array->pp_elems );
> - memset( p_array, 0, sizeof(vlc_array_t) );
> + vlc_array_init( p_array );
> }
>
> static inline vlc_array_t * vlc_array_new( void )
> @@ -306,66 +307,94 @@ static inline void vlc_array_destroy( vlc_array_t * p_array )
> {
> if( !p_array )
> return;
> - vlc_array_clear( p_array );
> + free( p_array->pp_elems );
> free( p_array );
> }
>
>
> /* Read */
> -static inline int
> -vlc_array_count( vlc_array_t * p_array )
> +static inline size_t vlc_array_count( vlc_array_t * p_array )
> {
> return p_array->i_count;
> }
>
> -static inline void *
> -vlc_array_item_at_index( vlc_array_t * p_array, int i_index )
> +#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
> +# define vlc_array_item_at_index(ar, idx) \
> + _Generic((ar), \
> + const vlc_array_t *: ((ar)->pp_elems[idx]), \
> + vlc_array_t *: ((ar)->pp_elems[idx]))
> +#elif defined (__cplusplus)
> +static inline void *vlc_array_item_at_index( vlc_array_t *ar, size_t idx )
> {
> - return p_array->pp_elems[i_index];
> + return ar->pp_elems[idx];
> }
>
> -static inline int
> -vlc_array_index_of_item( vlc_array_t * p_array, void * item )
> +static inline const void *vlc_array_item_at_index( const vlc_array_t *ar,
> + size_t idx )
> {
> - int i;
> - for( i = 0; i < p_array->i_count; i++)
> + return ar->pp_elems[idx];
> +}
Aren't you missing a #else for C99 compilers ? Or we officially don't
support those anymore ?
> +#endif
> +
> +static inline ssize_t vlc_array_index_of_item( const vlc_array_t *ar,
> + const void *elem )
> +{
> + for( size_t i = 0; i < ar->i_count; i++ )
> {
> - if( p_array->pp_elems[i] == item )
> + if( ar->pp_elems[i] == elem )
> return i;
> }
> return -1;
> }
>
> /* Write */
> -static inline void
> -vlc_array_insert( vlc_array_t * p_array, void * p_elem, int i_index )
> +static inline void vlc_array_insert( vlc_array_t *ar, void *elem, int idx )
> {
> - TAB_INSERT_CAST( (void **), p_array->i_count, p_array->pp_elems, p_elem, i_index );
> + void **pp = (void **)realloc( ar->pp_elems,
> + sizeof( void * ) * (ar->i_count + 1) );
> + if( unlikely(pp == NULL) )
> + abort();
> +
> + size_t tail = ar->i_count - idx;
> + if( tail > 0 )
> + memmove( pp + idx + 1, pp + idx, sizeof( void * ) * tail );
> +
> + pp[idx] = elem;
> + ar->i_count++;
> + ar->pp_elems = pp;
> }
>
> -static inline void
> -vlc_array_append( vlc_array_t * p_array, void * p_elem )
> +static inline void vlc_array_append( vlc_array_t *ar, void *elem )
> {
> - vlc_array_insert( p_array, p_elem, p_array->i_count );
> + void **pp = (void **)realloc( ar->pp_elems,
> + sizeof( void * ) * (ar->i_count + 1) );
> + if( unlikely(pp == NULL) )
> + abort();
> +
> + pp[ar->i_count++] = elem;
> + ar->pp_elems = pp;
> }
>
> -static inline void
> -vlc_array_remove( vlc_array_t * p_array, int i_index )
> +static inline void vlc_array_remove( vlc_array_t *ar, size_t idx )
> {
> - if( i_index >= 0 )
> + void **pp = ar->pp_elems;
> + size_t tail = ar->i_count - idx - 1;
> +
> + if( tail > 0 )
> + memmove( pp + idx, pp + idx + 1, sizeof( void * ) * tail );
> +
> + ar->i_count--;
> +
> + if( ar->i_count > 0 )
> {
> - if( p_array->i_count > 1 )
> - {
> - memmove( p_array->pp_elems + i_index,
> - p_array->pp_elems + i_index+1,
> - ( p_array->i_count - i_index - 1 ) * sizeof( void* ) );
> - }
> - p_array->i_count--;
> - if( p_array->i_count == 0 )
> - {
> - free( p_array->pp_elems );
> - p_array->pp_elems = NULL;
> - }
> + pp = (void **)realloc( pp, sizeof( void * ) * ar->i_count );
> + if( likely(pp != NULL) )
> + ar->pp_elems = pp;
> + }
> + else
> + {
> + free( pp );
> + ar->pp_elems = NULL;
> }
> }
>
> diff --git a/include/vlc_fingerprinter.h b/include/vlc_fingerprinter.h
> index 319608a..9bed2f7 100644
> --- a/include/vlc_fingerprinter.h
> +++ b/include/vlc_fingerprinter.h
> @@ -61,7 +61,7 @@ static inline void fingerprint_request_Delete( fingerprint_request_t *p_f )
> {
> vlc_gc_decref( p_f->p_item );
> free( p_f->results.psz_fingerprint );
> - for( int i = 0; i < vlc_array_count( & p_f->results.metas_array ); i++ )
> + for( size_t i = 0; i < vlc_array_count( & p_f->results.metas_array ); i++ )
> vlc_meta_Delete( (vlc_meta_t *) vlc_array_item_at_index( & p_f->results.metas_array, i ) );
> free( p_f );
> }
> diff --git a/lib/event.c b/lib/event.c
> index 6beeacd..e6ec83c 100644
> --- a/lib/event.c
> +++ b/lib/event.c
> @@ -121,7 +121,7 @@ void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
> {
> vlc_mutex_destroy(&p_em->lock);
>
> - for (int i = 0; i < vlc_array_count(&p_em->listeners); i++)
> + for (size_t i = 0; i < vlc_array_count(&p_em->listeners); i++)
> free(vlc_array_item_at_index(&p_em->listeners, i));
>
> vlc_array_clear(&p_em->listeners);
> @@ -140,7 +140,7 @@ void libvlc_event_send( libvlc_event_manager_t * p_em,
> p_event->p_obj = p_em->p_obj;
>
> vlc_mutex_lock(&p_em->lock);
> - for (int i = 0; i < vlc_array_count(&p_em->listeners); i++)
> + for (size_t i = 0; i < vlc_array_count(&p_em->listeners); i++)
> {
> libvlc_event_listener_t *listener;
>
> @@ -283,7 +283,7 @@ void libvlc_event_detach(libvlc_event_manager_t *em, libvlc_event_type_t type,
> libvlc_callback_t callback, void *opaque)
> {
> vlc_mutex_lock(&em->lock);
> - for (int i = 0; i < vlc_array_count(&em->listeners); i++)
> + for (size_t i = 0; i < vlc_array_count(&em->listeners); i++)
> {
> libvlc_event_listener_t *listener;
>
> diff --git a/lib/media_list.c b/lib/media_list.c
> index 2c5680a..d40ad1b 100644
> --- a/lib/media_list.c
> +++ b/lib/media_list.c
> @@ -204,7 +204,7 @@ void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
>
> libvlc_media_release( p_mlist->p_md );
>
> - for ( int i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
> + for( size_t i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
> {
> libvlc_media_t* p_md = vlc_array_item_at_index( &p_mlist->items, i );
> libvlc_media_release( p_md );
> diff --git a/modules/access/bluray.c b/modules/access/bluray.c
> index de8986c..6f42475 100644
> --- a/modules/access/bluray.c
> +++ b/modules/access/bluray.c
> @@ -920,7 +920,7 @@ typedef struct fmt_es_pair {
>
> static int findEsPairIndex(demux_sys_t *p_sys, int i_id)
> {
> - for (int i = 0; i < vlc_array_count(&p_sys->es); ++i)
> + for (size_t i = 0; i < vlc_array_count(&p_sys->es); ++i)
> if (((fmt_es_pair_t*)vlc_array_item_at_index(&p_sys->es, i))->i_id == i_id)
> return i;
>
> @@ -929,7 +929,7 @@ static int findEsPairIndex(demux_sys_t *p_sys, int i_id)
>
> static int findEsPairIndexByEs(demux_sys_t *p_sys, es_out_id_t *p_es)
> {
> - for (int i = 0; i < vlc_array_count(&p_sys->es); ++i)
> + for (size_t i = 0; i < vlc_array_count(&p_sys->es); ++i)
> if (((fmt_es_pair_t*)vlc_array_item_at_index(&p_sys->es, i))->p_es == p_es)
> return i;
>
> @@ -1067,7 +1067,7 @@ static int esOutControl(es_out_t *p_out, int i_query, va_list args)
>
> static void esOutDestroy(es_out_t *p_out)
> {
> - for (int i = 0; i < vlc_array_count(&p_out->p_sys->p_demux->p_sys->es); ++i)
> + for (size_t i = 0; i < vlc_array_count(&p_out->p_sys->p_demux->p_sys->es); ++i)
> free(vlc_array_item_at_index(&p_out->p_sys->p_demux->p_sys->es, i));
> vlc_array_clear(&p_out->p_sys->p_demux->p_sys->es);
> free(p_out->p_sys);
> diff --git a/modules/access/dsm/sd.c b/modules/access/dsm/sd.c
> index 2427ff8..389b39a 100644
> --- a/modules/access/dsm/sd.c
> +++ b/modules/access/dsm/sd.c
> @@ -77,7 +77,7 @@ static void entry_item_remove( services_discovery_t *p_sd,
> {
> services_discovery_sys_t *p_sys = p_sd->p_sys;
>
> - for ( int i = 0; i < vlc_array_count( p_sys->p_entry_item_list ); i++ )
> + for( size_t i = 0; i < vlc_array_count( p_sys->p_entry_item_list ); i++ )
> {
> struct entry_item *p_entry_item;
>
> @@ -176,7 +176,7 @@ void bdsm_SdClose (vlc_object_t *p_this)
>
> if( p_sys->p_entry_item_list )
> {
> - for ( int i = 0; i < vlc_array_count( p_sys->p_entry_item_list ); i++ )
> + for( size_t i = 0; i < vlc_array_count( p_sys->p_entry_item_list ); i++ )
> {
> struct entry_item *p_entry_item;
>
> diff --git a/modules/access/zip/zipstream.c b/modules/access/zip/zipstream.c
> index 4eff9f5..56a4605 100644
> --- a/modules/access/zip/zipstream.c
> +++ b/modules/access/zip/zipstream.c
> @@ -357,7 +357,7 @@ exit:
> unzClose( file );
> p_sys->zipFile = NULL;
>
> - for( int i = 0; i < vlc_array_count( p_filenames ); i++ )
> + for( size_t i = 0; i < vlc_array_count( p_filenames ); i++ )
> {
> free( vlc_array_item_at_index( p_filenames, i ) );
> }
> @@ -558,7 +558,7 @@ static int WriteXSPF( char **pp_buffer, vlc_array_t *p_filenames,
> }
>
> int i_track = 0;
> - for( int i = 0; i < vlc_array_count( p_filenames ); ++i )
> + for( size_t i = 0; i < vlc_array_count( p_filenames ); ++i )
> {
> char *psz_name = (char*) vlc_array_item_at_index( p_filenames, i );
> int i_len = strlen( psz_name );
> diff --git a/modules/access_output/livehttp.c b/modules/access_output/livehttp.c
> index e79fb9f..a0cc949 100644
> --- a/modules/access_output/livehttp.c
> +++ b/modules/access_output/livehttp.c
> @@ -522,7 +522,7 @@ static void destroySegment( output_segment_t *segment )
> static uint32_t segmentAmountNeeded( sout_access_out_sys_t *p_sys )
> {
> float duration = .0f;
> - for( unsigned index = 1; (int)index <= vlc_array_count( p_sys->segments_t ); index++ )
> + for( size_t index = 1; index <= vlc_array_count( p_sys->segments_t ); index++ )
> {
> output_segment_t* segment = vlc_array_item_at_index( p_sys->segments_t, vlc_array_count( p_sys->segments_t ) - index );
> duration += segment->f_seglength;
> diff --git a/modules/control/dbus/dbus.c b/modules/control/dbus/dbus.c
> index dd90941..ec759fd 100644
> --- a/modules/control/dbus/dbus.c
> +++ b/modules/control/dbus/dbus.c
> @@ -320,7 +320,7 @@ static void Close ( vlc_object_t *p_this )
> dbus_connection_unref( p_sys->p_conn );
>
> // Free the events array
> - for( int i = 0; i < vlc_array_count( p_sys->p_events ); i++ )
> + for( size_t i = 0; i < vlc_array_count( p_sys->p_events ); i++ )
> {
> callback_info_t* info = vlc_array_item_at_index( p_sys->p_events, i );
> free( info );
> @@ -422,7 +422,7 @@ static void process_timeouts(intf_thread_t *intf)
> {
> intf_sys_t *sys = intf->p_sys;
>
> - for (int i = 0; i < vlc_array_count(sys->p_timeouts); i++)
> + for (size_t i = 0; i < vlc_array_count(sys->p_timeouts); i++)
> {
> DBusTimeout *to = vlc_array_item_at_index(sys->p_timeouts, i);
>
> @@ -493,12 +493,13 @@ static void watch_toggled( DBusWatch *p_watch, void *p_data )
> static int GetPollFds( intf_thread_t *p_intf, struct pollfd *p_fds )
> {
> intf_sys_t *p_sys = p_intf->p_sys;
> - int i_fds = 1, i_watches = vlc_array_count( p_sys->p_watches );
> + size_t i_watches = vlc_array_count( p_sys->p_watches );
> + int i_fds = 1;
>
> p_fds[0].fd = p_sys->p_pipe_fds[PIPE_OUT];
> p_fds[0].events = POLLIN | POLLPRI;
>
> - for( int i = 0; i < i_watches; i++ )
> + for( size_t i = 0; i < i_watches; i++ )
> {
> DBusWatch *p_watch = NULL;
> p_watch = vlc_array_item_at_index( p_sys->p_watches, i );
> @@ -775,7 +776,7 @@ static void *Run( void *data )
> {
> vlc_mutex_lock( &p_sys->lock );
>
> - int i_watches = vlc_array_count( p_sys->p_watches );
> + size_t i_watches = vlc_array_count( p_sys->p_watches );
> struct pollfd fds[i_watches];
> memset(fds, 0, sizeof fds);
>
> @@ -818,20 +819,21 @@ static void *Run( void *data )
> /* Get the list of watches to process */
> i_watches = vlc_array_count( p_sys->p_watches );
> DBusWatch *p_watches[i_watches ? i_watches : 1];
> - for( int i = 0; i < i_watches; i++ )
> + for( size_t i = 0; i < i_watches; i++ )
> {
> p_watches[i] = vlc_array_item_at_index( p_sys->p_watches, i );
> }
>
> /* Get the list of events to process */
> - int i_events = vlc_array_count( p_intf->p_sys->p_events );
> + size_t i_events = vlc_array_count( p_intf->p_sys->p_events );
> callback_info_t* p_info[i_events ? i_events : 1];
> - for( int i = i_events - 1; i >= 0; i-- )
> + for( size_t i = 0; i < i_events; i++ )
> {
> p_info[i] = vlc_array_item_at_index( p_intf->p_sys->p_events, i );
> - vlc_array_remove( p_intf->p_sys->p_events, i );
> }
>
> + vlc_array_clear( p_intf->p_sys->p_events );
> +
> /* now we can release the lock and process what's pending */
> vlc_mutex_unlock( &p_intf->p_sys->lock );
>
> diff --git a/modules/gui/qt/dialogs/fingerprintdialog.cpp b/modules/gui/qt/dialogs/fingerprintdialog.cpp
> index 0c86970..a8dc7f6 100644
> --- a/modules/gui/qt/dialogs/fingerprintdialog.cpp
> +++ b/modules/gui/qt/dialogs/fingerprintdialog.cpp
> @@ -87,7 +87,7 @@ void FingerprintDialog::handleResults()
>
> ui->stackedWidget->setCurrentWidget( ui->results );
>
> - for ( int i=0; i< vlc_array_count( & p_r->results.metas_array ) ; i++ )
> + for ( size_t i = 0; i< vlc_array_count( & p_r->results.metas_array ) ; i++ )
> {
> vlc_meta_t *p_meta =
> (vlc_meta_t *) vlc_array_item_at_index( & p_r->results.metas_array, i );
> diff --git a/modules/misc/fingerprinter.c b/modules/misc/fingerprinter.c
> index 8de2ce0..1f80cb0 100644
> --- a/modules/misc/fingerprinter.c
> +++ b/modules/misc/fingerprinter.c
> @@ -91,7 +91,7 @@ static void EnqueueRequest( fingerprinter_thread_t *f, fingerprint_request_t *r
> static void QueueIncomingRequests( fingerprinter_sys_t *p_sys )
> {
> vlc_mutex_lock( &p_sys->incoming.lock );
> - int i = vlc_array_count( p_sys->incoming.queue );
> + size_t i = vlc_array_count( p_sys->incoming.queue );
> if ( i == 0 ) goto end;
> while( i )
> vlc_array_append( p_sys->processing.queue,
>
> _______________________________________________
> vlc-commits mailing list
> vlc-commits at videolan.org
> https://mailman.videolan.org/listinfo/vlc-commits
More information about the vlc-devel
mailing list