From bfe17fd33e68ea4aed301147fd4e090fc2f6abb6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9mi=20Denis-Courmont?= Date: Mon, 24 Mar 2014 19:45:43 +0200 Subject: [PATCH] lua: vector out files descriptable table code --- modules/lua/intf.c | 26 ++++------- modules/lua/libs.h | 1 - modules/lua/libs/net.c | 104 ++++++++++++++++++++++++----------------- modules/lua/vlc.h | 26 +++++++---- 4 files changed, 87 insertions(+), 70 deletions(-) diff --git a/modules/lua/intf.c b/modules/lua/intf.c index 9dacd8d2b9..61943549ab 100644 --- a/modules/lua/intf.c +++ b/modules/lua/intf.c @@ -223,8 +223,6 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name ) } p_intf->p_sys = p_sys; - vlclua_fd_init( p_sys ); - p_sys->psz_filename = vlclua_find_file( "intf", name ); if( !p_sys->psz_filename ) { @@ -255,7 +253,11 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name ) luaopen_input( L ); luaopen_msg( L ); luaopen_misc( L ); - luaopen_net_intf( L ); + if( vlclua_fd_init( L, &p_sys->dtable ) ) + { + lua_close( L ); + goto error; + } luaopen_object( L ); luaopen_osd( L ); luaopen_playlist( L ); @@ -364,20 +366,9 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name ) p_sys->L = L; -#ifndef _WIN32 - if( vlc_pipe( p_sys->fd ) ) - { - lua_close( p_sys->L ); - goto error; - } -#else -# define close(fd) (void)0 -#endif - if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) ) { - close( p_sys->fd[1] ); - close( p_sys->fd[0] ); + vlclua_fd_cleanup( &p_sys->dtable ); lua_close( p_sys->L ); goto error; } @@ -396,12 +387,11 @@ void Close_LuaIntf( vlc_object_t *p_this ) intf_thread_t *p_intf = (intf_thread_t*)p_this; intf_sys_t *p_sys = p_intf->p_sys; - close( p_sys->fd[1] ); + vlclua_fd_interrupt( &p_sys->dtable ); vlc_join( p_sys->thread, NULL ); + vlclua_fd_cleanup( &p_sys->dtable ); lua_close( p_sys->L ); - close( p_sys->fd[0] ); - vlclua_fd_destroy( p_sys ); free( p_sys->psz_filename ); free( p_sys ); } diff --git a/modules/lua/libs.h b/modules/lua/libs.h index 1951cf8f4f..ffac4dc76e 100644 --- a/modules/lua/libs.h +++ b/modules/lua/libs.h @@ -31,7 +31,6 @@ void luaopen_input( lua_State * ); void luaopen_msg( lua_State * ); void luaopen_misc( lua_State * ); void luaopen_net_generic( lua_State * ); -void luaopen_net_intf( lua_State * ); void luaopen_object( lua_State * ); void luaopen_osd( lua_State * ); void luaopen_playlist( lua_State * ); diff --git a/modules/lua/libs/net.c b/modules/lua/libs/net.c index d4fe299ff4..fe2e37f0a1 100644 --- a/modules/lua/libs/net.c +++ b/modules/lua/libs/net.c @@ -42,50 +42,40 @@ #include #include #include -#include #include "../vlc.h" #include "../libs.h" +#include "misc.h" -void vlclua_fd_init( intf_sys_t *sys ) +static vlclua_dtable_t *vlclua_get_dtable( lua_State *L ) { - sys->fdv = NULL; - sys->fdc = 0; -} - -/** Releases all (leaked) VLC Lua file descriptors. */ -void vlclua_fd_destroy( intf_sys_t *sys ) -{ - for( unsigned i = 0; i < sys->fdc; i++ ) - net_Close( sys->fdv[i] ); - free( sys->fdv ); + return vlclua_get_object( L, vlclua_get_dtable ); } /** Maps an OS file descriptor to a VLC Lua file descriptor */ static int vlclua_fd_map( lua_State *L, int fd ) { - intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L ); - intf_sys_t *sys = intf->p_sys; + vlclua_dtable_t *dt = vlclua_get_dtable( L ); if( (unsigned)fd < 3u ) return -1; #ifndef NDEBUG - for( unsigned i = 0; i < sys->fdc; i++ ) - assert( sys->fdv[i] != fd ); + for( unsigned i = 0; i < dt->fdc; i++ ) + assert( dt->fdv[i] != fd ); #endif - if( sys->fdc >= 64 ) + if( dt->fdc >= 64 ) return -1; - int *fdv = realloc( sys->fdv, (sys->fdc + 1) * sizeof (sys->fdv[0]) ); + int *fdv = realloc( dt->fdv, (dt->fdc + 1) * sizeof (dt->fdv[0]) ); if( unlikely(fdv == NULL) ) return -1; - sys->fdv = fdv; - sys->fdv[sys->fdc] = fd; - fd = 3 + sys->fdc; - sys->fdc++; + dt->fdv = fdv; + dt->fdv[dt->fdc] = fd; + fd = 3 + dt->fdc; + dt->fdc++; return fd; } @@ -100,25 +90,23 @@ static int vlclua_fd_map_safe( lua_State *L, int fd ) /** Gets the OS file descriptor mapped to a VLC Lua file descriptor */ static int vlclua_fd_get( lua_State *L, unsigned idx ) { - intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L ); - intf_sys_t *sys = intf->p_sys; + vlclua_dtable_t *dt = vlclua_get_dtable( L ); if( idx < 3u ) return idx; idx -= 3; - return (idx < sys->fdc) ? sys->fdv[idx] : -1; + return (idx < dt->fdc) ? dt->fdv[idx] : -1; } /** Gets the VLC Lua file descriptor mapped from an OS file descriptor */ static int vlclua_fd_get_lua( lua_State *L, int fd ) { - intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L ); - intf_sys_t *sys = intf->p_sys; + vlclua_dtable_t *dt = vlclua_get_dtable( L ); if( (unsigned)fd < 3u ) return fd; - for( unsigned i = 0; i < sys->fdc; i++ ) - if( sys->fdv[i] == fd ) + for( unsigned i = 0; i < dt->fdc; i++ ) + if( dt->fdv[i] == fd ) return 3 + i; return -1; } @@ -126,25 +114,24 @@ static int vlclua_fd_get_lua( lua_State *L, int fd ) /** Unmaps an OS file descriptor from VLC Lua */ static void vlclua_fd_unmap( lua_State *L, unsigned idx ) { - intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L ); - intf_sys_t *sys = intf->p_sys; + vlclua_dtable_t *dt = vlclua_get_dtable( L ); int fd = -1; if( idx < 3u ) return; /* Never close stdin/stdout/stderr. */ idx -= 3; - if( idx >= sys->fdc ) + if( idx >= dt->fdc ) return; - fd = sys->fdv[idx]; - sys->fdc--; - memmove( sys->fdv + idx, sys->fdv + idx + 1, - (sys->fdc - idx) * sizeof (sys->fdv[0]) ); + fd = dt->fdv[idx]; + dt->fdc--; + memmove( dt->fdv + idx, dt->fdv + idx + 1, + (dt->fdc - idx) * sizeof (dt->fdv[0]) ); /* realloc() not really needed */ #ifndef NDEBUG - for( unsigned i = 0; i < sys->fdc; i++ ) - assert( sys->fdv[i] != fd ); + for( unsigned i = 0; i < dt->fdc; i++ ) + assert( dt->fdv[i] != fd ); #endif } @@ -322,8 +309,7 @@ static int vlclua_net_recv( lua_State *L ) /* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */ static int vlclua_net_poll( lua_State *L ) { - intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L ); - intf_sys_t *sys = intf->p_sys; + vlclua_dtable_t *dt = vlclua_get_dtable( L ); luaL_checktype( L, 1, LUA_TTABLE ); @@ -340,7 +326,7 @@ static int vlclua_net_poll( lua_State *L ) lua_pushnil( L ); int i = 1; - p_fds[0].fd = sys->fd[0]; + p_fds[0].fd = dt->fd[0]; p_fds[0].events = POLLIN; while( lua_next( L, 1 ) ) { @@ -509,7 +495,7 @@ static const luaL_Reg vlclua_net_intf_reg[] = { { NULL, NULL } }; -void luaopen_net_intf( lua_State *L ) +static void luaopen_net_intf( lua_State *L ) { lua_newtable( L ); luaL_register( L, NULL, vlclua_net_intf_reg ); @@ -525,6 +511,40 @@ void luaopen_net_intf( lua_State *L ) lua_setfield( L, -2, "net" ); } +int vlclua_fd_init( lua_State *L, vlclua_dtable_t *dt ) +{ +#ifndef _WIN32 + if( vlc_pipe( dt->fd ) ) + return -1; +#endif + dt->fdv = NULL; + dt->fdc = 0; + vlclua_set_object( L, vlclua_get_dtable, dt ); + luaopen_net_intf( L ); + return 0; +} + +void vlclua_fd_interrupt( vlclua_dtable_t *dt ) +{ +#ifndef _WIN32 + close( dt->fd[1] ); + dt->fd[1] = -1; +#endif +} + +/** Releases all (leaked) VLC Lua file descriptors. */ +void vlclua_fd_cleanup( vlclua_dtable_t *dt ) +{ + for( unsigned i = 0; i < dt->fdc; i++ ) + net_Close( dt->fdv[i] ); + free( dt->fdv ); +#ifndef _WIN32 + if( dt->fd[1] != -1 ) + close( dt->fd[1] ); + close( dt->fd[0] ); +#endif +} + static const luaL_Reg vlclua_net_generic_reg[] = { { "url_parse", vlclua_url_parse }, { "stat", vlclua_stat }, /* Not really "net" */ diff --git a/modules/lua/vlc.h b/modules/lua/vlc.h index 16bea84530..d54a5d3506 100644 --- a/modules/lua/vlc.h +++ b/modules/lua/vlc.h @@ -155,23 +155,31 @@ int vlclua_playlist_add_internal( vlc_object_t *, lua_State *, playlist_t *, int vlclua_add_modules_path( lua_State *, const char *psz_filename ); /** - * Per-interface private state + * File descriptors table */ -struct intf_sys_t +typedef struct { - char *psz_filename; - lua_State *L; + int *fdv; + unsigned fdc; #ifndef _WIN32 int fd[2]; #endif - int *fdv; - unsigned fdc; +} vlclua_dtable_t; + +int vlclua_fd_init( lua_State *, vlclua_dtable_t * ); +void vlclua_fd_interrupt( vlclua_dtable_t * ); +void vlclua_fd_cleanup( vlclua_dtable_t * ); +/** + * Per-interface private state + */ +struct intf_sys_t +{ + char *psz_filename; + lua_State *L; vlc_thread_t thread; + vlclua_dtable_t dtable; }; -void vlclua_fd_init( struct intf_sys_t * ); -void vlclua_fd_destroy( struct intf_sys_t * ); - #endif /* VLC_LUA_H */ -- 2.39.2