1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: variables.c,v 1.20 2003/03/09 19:25:08 gbazin Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 # include <stdlib.h> /* realloc() */
33 /*****************************************************************************
35 *****************************************************************************/
36 struct callback_entry_t
38 vlc_callback_t pf_callback;
42 /*****************************************************************************
43 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
44 *****************************************************************************/
45 static int CmpBool( vlc_value_t v, vlc_value_t w ) { return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0; }
46 static int CmpInt( vlc_value_t v, vlc_value_t w ) { return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1; }
47 static int CmpString( vlc_value_t v, vlc_value_t w ) { return strcmp( v.psz_string, w.psz_string ); }
48 static int CmpFloat( vlc_value_t v, vlc_value_t w ) { return v.f_float == w.f_float ? 0 : v.f_float > w.f_float ? 1 : -1; }
49 static int CmpAddress( vlc_value_t v, vlc_value_t w ) { return v.p_address == w.p_address ? 0 : v.p_address > w.p_address ? 1 : -1; }
51 /*****************************************************************************
52 * Local duplication functions, and local deallocation functions
53 *****************************************************************************/
54 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
55 static void DupString( vlc_value_t *p_val ) { p_val->psz_string = strdup( p_val->psz_string ); }
57 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
58 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
59 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
61 /*****************************************************************************
63 *****************************************************************************/
64 static int GetUnused ( vlc_object_t *, const char * );
65 static uint32_t HashString ( const char * );
66 static int Insert ( variable_t *, int, const char * );
67 static int InsertInner ( variable_t *, int, uint32_t );
68 static int Lookup ( variable_t *, int, const char * );
69 static int LookupInner ( variable_t *, int, uint32_t );
71 static void CheckValue ( variable_t *, vlc_value_t * );
73 /*****************************************************************************
74 * var_Create: initialize a vlc variable
75 *****************************************************************************
76 * We hash the given string and insert it into the sorted list. The insertion
77 * may require slow memory copies, but think about what we gain in the log(n)
78 * lookup phase when setting/getting the variable value!
79 *****************************************************************************/
80 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
85 vlc_mutex_lock( &p_this->var_lock );
87 /* FIXME: if the variable already exists, we don't duplicate it. But we
88 * duplicate the lookups. It's not that serious, but if anyone finds some
89 * time to rework Insert() so that only one lookup has to be done, feel
91 i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
95 /* If the types differ, variable creation failed. */
96 if( i_type != p_this->p_vars[i_new].i_type )
98 vlc_mutex_unlock( &p_this->var_lock );
102 p_this->p_vars[i_new].i_usage++;
103 vlc_mutex_unlock( &p_this->var_lock );
107 i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
109 if( (p_this->i_vars & 15) == 15 )
111 p_this->p_vars = realloc( p_this->p_vars,
112 (p_this->i_vars+17) * sizeof(variable_t) );
115 memmove( p_this->p_vars + i_new + 1,
116 p_this->p_vars + i_new,
117 (p_this->i_vars - i_new) * sizeof(variable_t) );
121 p_var = &p_this->p_vars[i_new];
123 p_var->i_hash = HashString( psz_name );
124 p_var->psz_name = strdup( psz_name );
126 p_var->i_type = i_type;
127 memset( &p_var->val, 0, sizeof(vlc_value_t) );
129 p_var->pf_dup = DupDummy;
130 p_var->pf_free = FreeDummy;
134 p_var->i_default = -1;
135 p_var->choices.i_count = 0;
136 p_var->choices.p_values = NULL;
138 p_var->b_incallback = VLC_FALSE;
139 p_var->i_entries = 0;
140 p_var->p_entries = NULL;
142 /* Always initialize the variable, even if it is a list variable; this
143 * will lead to errors if the variable is not initialized, but it will
144 * not cause crashes in the variable handling. */
145 switch( i_type & VLC_VAR_TYPE )
148 p_var->pf_cmp = CmpBool;
149 p_var->val.b_bool = VLC_FALSE;
151 case VLC_VAR_INTEGER:
152 p_var->pf_cmp = CmpInt;
153 p_var->val.i_int = 0;
158 p_var->pf_cmp = CmpString;
159 p_var->pf_dup = DupString;
160 p_var->pf_free = FreeString;
161 p_var->val.psz_string = "";
164 p_var->pf_cmp = CmpFloat;
165 p_var->val.f_float = 0.0;
170 case VLC_VAR_ADDRESS:
171 p_var->pf_cmp = CmpAddress;
172 p_var->val.p_address = NULL;
175 p_var->pf_cmp = CmpAddress;
176 p_var->pf_free = FreeMutex;
177 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
178 vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
182 /* Duplicate the default data we stored. */
183 p_var->pf_dup( &p_var->val );
185 vlc_mutex_unlock( &p_this->var_lock );
190 /*****************************************************************************
191 * var_Destroy: destroy a vlc variable
192 *****************************************************************************
193 * Look for the variable and destroy it if it is found. As in var_Create we
194 * do a call to memmove() but we have performance counterparts elsewhere.
195 *****************************************************************************/
196 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
201 vlc_mutex_lock( &p_this->var_lock );
203 i_var = GetUnused( p_this, psz_name );
206 vlc_mutex_unlock( &p_this->var_lock );
210 p_var = &p_this->p_vars[i_var];
212 if( p_var->i_usage > 1 )
215 vlc_mutex_unlock( &p_this->var_lock );
219 /* Free value if needed */
220 p_var->pf_free( &p_var->val );
222 /* Free choice list if needed */
223 if( p_var->choices.i_count )
225 for( i = 0 ; i < p_var->choices.i_count ; i++ )
227 p_var->pf_free( &p_var->choices.p_values[i] );
229 free( p_var->choices.p_values );
232 /* Free callbacks if needed */
233 if( p_var->p_entries )
235 free( p_var->p_entries );
238 free( p_var->psz_name );
240 memmove( p_this->p_vars + i_var,
241 p_this->p_vars + i_var + 1,
242 (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
244 if( (p_this->i_vars & 15) == 0 )
246 p_this->p_vars = realloc( p_this->p_vars,
247 (p_this->i_vars) * sizeof( variable_t ) );
252 vlc_mutex_unlock( &p_this->var_lock );
257 /*****************************************************************************
258 * var_Change: perform an action on a variable
259 *****************************************************************************
261 *****************************************************************************/
262 int __var_Change( vlc_object_t *p_this, const char *psz_name,
263 int i_action, vlc_value_t *p_val )
268 vlc_mutex_lock( &p_this->var_lock );
270 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
274 vlc_mutex_unlock( &p_this->var_lock );
278 p_var = &p_this->p_vars[i_var];
283 if( p_var->i_type & VLC_VAR_HASMIN )
285 p_var->pf_free( &p_var->min );
287 p_var->i_type |= VLC_VAR_HASMIN;
289 p_var->pf_dup( &p_var->min );
290 CheckValue( p_var, &p_var->val );
293 if( p_var->i_type & VLC_VAR_HASMAX )
295 p_var->pf_free( &p_var->max );
297 p_var->i_type |= VLC_VAR_HASMAX;
299 p_var->pf_dup( &p_var->max );
300 CheckValue( p_var, &p_var->val );
302 case VLC_VAR_SETSTEP:
303 if( p_var->i_type & VLC_VAR_HASSTEP )
305 p_var->pf_free( &p_var->step );
307 p_var->i_type |= VLC_VAR_HASSTEP;
308 p_var->step = *p_val;
309 p_var->pf_dup( &p_var->step );
310 CheckValue( p_var, &p_var->val );
312 case VLC_VAR_ADDCHOICE:
313 /* FIXME: the list is sorted, dude. Use something cleverer. */
314 for( i = p_var->choices.i_count ; i-- ; )
316 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
322 /* The new place is i+1 */
325 if( p_var->i_default >= i )
330 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
332 p_var->pf_dup( &p_var->choices.p_values[i] );
334 CheckValue( p_var, &p_var->val );
336 case VLC_VAR_DELCHOICE:
337 /* FIXME: the list is sorted, dude. Use something cleverer. */
338 for( i = 0 ; i < p_var->choices.i_count ; i++ )
340 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
346 if( i == p_var->choices.i_count )
349 vlc_mutex_unlock( &p_this->var_lock );
353 if( p_var->i_default > i )
357 else if( p_var->i_default == i )
359 p_var->i_default = -1;
362 p_var->pf_free( &p_var->choices.p_values[i] );
363 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
365 CheckValue( p_var, &p_var->val );
367 case VLC_VAR_SETDEFAULT:
368 /* FIXME: the list is sorted, dude. Use something cleverer. */
369 for( i = 0 ; i < p_var->choices.i_count ; i++ )
371 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
377 if( i == p_var->choices.i_count )
383 p_var->i_default = i;
384 CheckValue( p_var, &p_var->val );
387 case VLC_VAR_GETLIST:
388 p_val->p_list = malloc( sizeof(vlc_list_t) );
389 if( p_var->choices.i_count )
390 p_val->p_list->p_values = malloc( p_var->choices.i_count
391 * sizeof(vlc_value_t) );
392 p_val->p_list->i_count = p_var->choices.i_count;
393 for( i = 0 ; i < p_var->choices.i_count ; i++ )
395 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
396 p_var->pf_dup( &p_val->p_list->p_values[i] );
399 case VLC_VAR_FREELIST:
400 for( i = p_val->p_list->i_count ; i-- ; )
402 p_var->pf_free( &p_val->p_list->p_values[i] );
404 if( p_val->p_list->i_count )
405 free( p_val->p_list->p_values );
406 free( p_val->p_list );
413 vlc_mutex_unlock( &p_this->var_lock );
418 /*****************************************************************************
419 * var_Type: request a variable's type
420 *****************************************************************************
421 * This function returns the variable type if it exists, or 0 if the
422 * variable could not be found.
423 *****************************************************************************/
424 int __var_Type( vlc_object_t *p_this, const char *psz_name )
428 vlc_mutex_lock( &p_this->var_lock );
430 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
434 vlc_mutex_unlock( &p_this->var_lock );
438 i_type = p_this->p_vars[i_var].i_type;
440 vlc_mutex_unlock( &p_this->var_lock );
445 /*****************************************************************************
446 * var_Set: set a variable's value
447 *****************************************************************************
449 *****************************************************************************/
450 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
456 vlc_mutex_lock( &p_this->var_lock );
458 i_var = GetUnused( p_this, psz_name );
461 vlc_mutex_unlock( &p_this->var_lock );
465 p_var = &p_this->p_vars[i_var];
467 /* Duplicate data if needed */
468 p_var->pf_dup( &val );
470 /* Backup needed stuff */
473 /* Check boundaries and list */
474 CheckValue( p_var, &val );
476 /* Set the variable */
479 /* Deal with callbacks. Tell we're in a callback, release the lock,
480 * call stored functions, retake the lock. */
481 if( p_var->i_entries )
484 int i_entries = p_var->i_entries;
485 callback_entry_t *p_entries = p_var->p_entries;
487 p_var->b_incallback = VLC_TRUE;
488 vlc_mutex_unlock( &p_this->var_lock );
491 for( ; i_entries-- ; )
493 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
494 p_entries[i_entries].p_data );
497 vlc_mutex_lock( &p_this->var_lock );
499 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
502 msg_Err( p_this, "variable %s has disappeared", psz_name );
503 vlc_mutex_unlock( &p_this->var_lock );
507 p_var = &p_this->p_vars[i_var];
508 p_var->b_incallback = VLC_FALSE;
511 /* Free data if needed */
512 p_var->pf_free( &oldval );
514 vlc_mutex_unlock( &p_this->var_lock );
519 /*****************************************************************************
520 * var_Get: get a variable's value
521 *****************************************************************************
523 *****************************************************************************/
524 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
529 vlc_mutex_lock( &p_this->var_lock );
531 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
535 vlc_mutex_unlock( &p_this->var_lock );
539 p_var = &p_this->p_vars[i_var];
541 /* Really get the variable */
544 /* Duplicate value if needed */
545 p_var->pf_dup( p_val );
547 vlc_mutex_unlock( &p_this->var_lock );
552 /*****************************************************************************
553 * var_AddCallback: register a callback in a variable
554 *****************************************************************************
555 * We store a function pointer pf_callback that will be called upon variable
556 * modification. p_data is a generic pointer that will be passed as additional
557 * argument to the callback function.
558 *****************************************************************************/
559 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
560 vlc_callback_t pf_callback, void *p_data )
564 callback_entry_t entry;
566 entry.pf_callback = pf_callback;
567 entry.p_data = p_data;
569 vlc_mutex_lock( &p_this->var_lock );
571 i_var = GetUnused( p_this, psz_name );
574 vlc_mutex_unlock( &p_this->var_lock );
578 p_var = &p_this->p_vars[i_var];
580 INSERT_ELEM( p_var->p_entries,
585 vlc_mutex_unlock( &p_this->var_lock );
590 /*****************************************************************************
591 * var_DelCallback: remove a callback from a variable
592 *****************************************************************************
593 * pf_callback and p_data have to be given again, because different objects
594 * might have registered the same callback function.
595 *****************************************************************************/
596 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
597 vlc_callback_t pf_callback, void *p_data )
602 vlc_mutex_lock( &p_this->var_lock );
604 i_var = GetUnused( p_this, psz_name );
607 vlc_mutex_unlock( &p_this->var_lock );
611 p_var = &p_this->p_vars[i_var];
613 for( i_entry = p_var->i_entries ; i_entry-- ; )
615 if( p_var->p_entries[i_entry].pf_callback == pf_callback
616 && p_var->p_entries[i_entry].p_data == p_data )
624 vlc_mutex_unlock( &p_this->var_lock );
628 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
630 vlc_mutex_unlock( &p_this->var_lock );
635 /* Following functions are local */
637 /*****************************************************************************
638 * GetUnused: find an unused variable from its name
639 *****************************************************************************
640 * We do i_tries tries before giving up, just in case the variable is being
641 * modified and called from a callback.
642 *****************************************************************************/
643 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
645 int i_var, i_tries = 0;
649 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
655 if( ! p_this->p_vars[i_var].b_incallback )
660 if( i_tries++ > 100 )
662 msg_Err( p_this, "caught in a callback deadlock?" );
666 vlc_mutex_unlock( &p_this->var_lock );
667 msleep( THREAD_SLEEP );
668 vlc_mutex_lock( &p_this->var_lock );
672 /*****************************************************************************
673 * HashString: our cool hash function
674 *****************************************************************************
675 * This function is not intended to be crypto-secure, we only want it to be
676 * fast and not suck too much. This one is pretty fast and did 0 collisions
677 * in wenglish's dictionary.
678 *****************************************************************************/
679 static uint32_t HashString( const char *psz_string )
685 i_hash += *psz_string++;
686 i_hash += i_hash << 10;
687 i_hash ^= i_hash >> 8;
693 /*****************************************************************************
694 * Insert: find an empty slot to insert a new variable
695 *****************************************************************************
696 * We use a recursive inner function indexed on the hash. This function does
697 * nothing in the rare cases where a collision may occur, see Lookup()
698 * to see how we handle them.
699 * XXX: does this really need to be written recursively?
700 *****************************************************************************/
701 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
708 return InsertInner( p_vars, i_count, HashString( psz_name ) );
711 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
715 if( i_hash <= p_vars[0].i_hash )
720 if( i_hash >= p_vars[i_count - 1].i_hash )
725 i_middle = i_count / 2;
727 /* We know that 0 < i_middle */
728 if( i_hash < p_vars[i_middle].i_hash )
730 return InsertInner( p_vars, i_middle, i_hash );
733 /* We know that i_middle + 1 < i_count */
734 if( i_hash > p_vars[i_middle + 1].i_hash )
736 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
737 i_count - i_middle - 1,
744 /*****************************************************************************
745 * Lookup: find an existing variable given its name
746 *****************************************************************************
747 * We use a recursive inner function indexed on the hash. Care is taken of
748 * possible hash collisions.
749 * XXX: does this really need to be written recursively?
750 *****************************************************************************/
751 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
761 i_hash = HashString( psz_name );
763 i_pos = LookupInner( p_vars, i_count, i_hash );
766 if( i_hash != p_vars[i_pos].i_hash )
771 /* Hash found, entry found */
772 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
777 /* Hash collision! This should be very rare, but we cannot guarantee
778 * it will never happen. Just do an exhaustive search amongst all
779 * entries with the same hash. */
780 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
782 if( !strcmp( psz_name, p_vars[i].psz_name ) )
788 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
790 if( !strcmp( psz_name, p_vars[i].psz_name ) )
796 /* Hash found, but entry not found */
800 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
804 if( i_hash <= p_vars[0].i_hash )
809 if( i_hash >= p_vars[i_count-1].i_hash )
814 i_middle = i_count / 2;
816 /* We know that 0 < i_middle */
817 if( i_hash < p_vars[i_middle].i_hash )
819 return LookupInner( p_vars, i_middle, i_hash );
822 /* We know that i_middle + 1 < i_count */
823 if( i_hash > p_vars[i_middle].i_hash )
825 return i_middle + LookupInner( p_vars + i_middle,
833 /*****************************************************************************
834 * CheckValue: check that a value is valid wrt. a variable
835 *****************************************************************************
836 * This function checks p_val's value against p_var's limitations such as
837 * minimal and maximal value, step, in-list position, and modifies p_val if
839 *****************************************************************************/
840 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
842 /* Check that our variable is in the list */
843 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
847 /* FIXME: the list is sorted, dude. Use something cleverer. */
848 for( i = p_var->choices.i_count ; i-- ; )
850 if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
856 /* If not found, change it to anything vaguely valid */
859 /* Free the old variable, get the new one, dup it */
860 p_var->pf_free( p_val );
861 *p_val = p_var->choices.p_values[p_var->i_default >= 0
862 ? p_var->i_default : 0 ];
863 p_var->pf_dup( p_val );
867 /* Check that our variable is within the bounds */
868 switch( p_var->i_type & VLC_VAR_TYPE )
870 case VLC_VAR_INTEGER:
871 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
872 && (p_val->i_int % p_var->step.i_int) )
874 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
875 / p_var->step.i_int * p_var->step.i_int;
877 if( p_var->i_type & VLC_VAR_HASMIN
878 && p_val->i_int < p_var->min.i_int )
880 p_val->i_int = p_var->min.i_int;
882 if( p_var->i_type & VLC_VAR_HASMAX
883 && p_val->i_int > p_var->max.i_int )
885 p_val->i_int = p_var->max.i_int;
889 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
891 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
892 p_val->f_float / p_var->step.f_float );
893 if( p_val->f_float != f_round )
895 p_val->f_float = f_round;
898 if( p_var->i_type & VLC_VAR_HASMIN
899 && p_val->f_float < p_var->min.f_float )
901 p_val->f_float = p_var->min.f_float;
903 if( p_var->i_type & VLC_VAR_HASMAX
904 && p_val->f_float > p_var->max.f_float )
906 p_val->f_float = p_var->max.f_float;