-/*****************************************************************************
- * GetUnused: find an unused variable from its name
- *****************************************************************************
- * We do i_tries tries before giving up, just in case the variable is being
- * modified and called from a callback.
- *****************************************************************************/
-static int GetUnused( vlc_object_t *p_this, const char *psz_name )
-{
- int i_var, i_tries = 0;
- vlc_object_internals_t *p_priv = p_this->p_internals;
-
- while( true )
- {
- i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
- if( i_var < 0 )
- {
- return VLC_ENOVAR;
- }
-
- if( ! p_priv->p_vars[i_var].b_incallback )
- {
- return i_var;
- }
-
- if( i_tries++ > 100 )
- {
- msg_Err( p_this, "caught in a callback deadlock?" );
- return VLC_ETIMEOUT;
- }
-
- vlc_mutex_unlock( &p_priv->var_lock );
- msleep( THREAD_SLEEP );
- vlc_mutex_lock( &p_priv->var_lock );
- }
-}
-
-/*****************************************************************************
- * HashString: our cool hash function
- *****************************************************************************
- * This function is not intended to be crypto-secure, we only want it to be
- * fast and not suck too much. This one is pretty fast and did 0 collisions
- * in wenglish's dictionary.
- *****************************************************************************/
-static uint32_t HashString( const char *psz_string )
-{
- uint32_t i_hash = 0;
-
- while( *psz_string )
- {
- i_hash += *psz_string++;
- i_hash += i_hash << 10;
- i_hash ^= i_hash >> 8;
- }
-
- return i_hash;
-}
-
-/*****************************************************************************
- * Insert: find an empty slot to insert a new variable
- *****************************************************************************
- * We use a recursive inner function indexed on the hash. This function does
- * nothing in the rare cases where a collision may occur, see Lookup()
- * to see how we handle them.
- * XXX: does this really need to be written recursively?
- *****************************************************************************/
-static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
-{
- if( i_count == 0 )
- {
- return 0;
- }
-
- return InsertInner( p_vars, i_count, HashString( psz_name ) );
-}
-
-static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
-{
- int i_middle;
-
- if( i_hash <= p_vars[0].i_hash )
- {
- return 0;
- }
-
- if( i_hash >= p_vars[i_count - 1].i_hash )
- {
- return i_count;
- }
-
- i_middle = i_count / 2;
-
- /* We know that 0 < i_middle */
- if( i_hash < p_vars[i_middle].i_hash )
- {
- return InsertInner( p_vars, i_middle, i_hash );
- }
-
- /* We know that i_middle + 1 < i_count */
- if( i_hash > p_vars[i_middle + 1].i_hash )
- {
- return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
- i_count - i_middle - 1,
- i_hash );
- }
-
- return i_middle + 1;
-}
-
-/*****************************************************************************
- * Lookup: find an existing variable given its name
- *****************************************************************************
- * We use a recursive inner function indexed on the hash. Care is taken of
- * possible hash collisions.
- * XXX: does this really need to be written recursively?
- *****************************************************************************/
-static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )