1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: variables.c,v 1.17 2002/12/10 18:22:01 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->i_choices = 0;
136 p_var->pp_choices = 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->pp_choices )
225 for( i = 0 ; i < p_var->i_choices ; i++ )
227 p_var->pf_free( &p_var->pp_choices[i] );
229 free( p_var->pp_choices );
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 );
313 case VLC_VAR_ADDCHOICE:
314 /* FIXME: the list is sorted, dude. Use something cleverer. */
315 for( i = p_var->i_choices ; i-- ; )
317 if( p_var->pf_cmp( p_var->pp_choices[i], *p_val ) < 0 )
323 /* The new place is i+1 */
326 if( p_var->i_default >= i )
331 INSERT_ELEM( p_var->pp_choices, p_var->i_choices, i, *p_val );
332 p_var->pf_dup( &p_var->pp_choices[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->i_choices ; i++ )
340 if( p_var->pf_cmp( p_var->pp_choices[i], *p_val ) == 0 )
346 if( i == p_var->i_choices )
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->pp_choices[i] );
363 REMOVE_ELEM( p_var->pp_choices, p_var->i_choices, 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->i_choices ; i++ )
371 if( p_var->pf_cmp( p_var->pp_choices[i], *p_val ) == 0 )
377 if( i == p_var->i_choices )
383 p_var->i_default = i;
384 CheckValue( p_var, &p_var->val );
387 case VLC_VAR_GETLIST:
388 p_val->p_address = malloc( (1 + p_var->i_choices)
389 * sizeof(vlc_value_t) );
390 ((vlc_value_t*)p_val->p_address)[0].i_int = p_var->i_choices;
391 for( i = 0 ; i < p_var->i_choices ; i++ )
393 ((vlc_value_t*)p_val->p_address)[i+1] = p_var->pp_choices[i];
394 p_var->pf_dup( &((vlc_value_t*)p_val->p_address)[i+1] );
397 case VLC_VAR_FREELIST:
398 for( i = ((vlc_value_t*)p_val->p_address)[0].i_int ; i-- ; )
400 p_var->pf_free( &((vlc_value_t*)p_val->p_address)[i+1] );
402 free( p_val->p_address );
409 vlc_mutex_unlock( &p_this->var_lock );
414 /*****************************************************************************
415 * var_Type: request a variable's type
416 *****************************************************************************
417 * This function returns the variable type if it exists, or 0 if the
418 * variable could not be found.
419 *****************************************************************************/
420 int __var_Type( vlc_object_t *p_this, const char *psz_name )
424 vlc_mutex_lock( &p_this->var_lock );
426 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
430 vlc_mutex_unlock( &p_this->var_lock );
434 i_type = p_this->p_vars[i_var].i_type;
436 vlc_mutex_unlock( &p_this->var_lock );
441 /*****************************************************************************
442 * var_Set: set a variable's value
443 *****************************************************************************
445 *****************************************************************************/
446 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
452 vlc_mutex_lock( &p_this->var_lock );
454 i_var = GetUnused( p_this, psz_name );
457 vlc_mutex_unlock( &p_this->var_lock );
461 p_var = &p_this->p_vars[i_var];
463 /* Duplicate data if needed */
464 p_var->pf_dup( &val );
466 /* Backup needed stuff */
469 /* Check boundaries and list */
470 CheckValue( p_var, &val );
472 /* Set the variable */
475 /* Deal with callbacks. Tell we're in a callback, release the lock,
476 * call stored functions, retake the lock. */
477 if( p_var->i_entries )
480 int i_entries = p_var->i_entries;
481 callback_entry_t *p_entries = p_var->p_entries;
483 p_var->b_incallback = VLC_TRUE;
484 vlc_mutex_unlock( &p_this->var_lock );
487 for( ; i_entries-- ; )
489 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
490 p_entries[i_entries].p_data );
493 vlc_mutex_lock( &p_this->var_lock );
495 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
498 msg_Err( p_this, "variable %s has disappeared" );
499 vlc_mutex_unlock( &p_this->var_lock );
503 p_var = &p_this->p_vars[i_var];
504 p_var->b_incallback = VLC_FALSE;
507 /* Free data if needed */
508 p_var->pf_free( &oldval );
510 vlc_mutex_unlock( &p_this->var_lock );
515 /*****************************************************************************
516 * var_Get: get a variable's value
517 *****************************************************************************
519 *****************************************************************************/
520 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
525 vlc_mutex_lock( &p_this->var_lock );
527 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
531 vlc_mutex_unlock( &p_this->var_lock );
535 p_var = &p_this->p_vars[i_var];
537 /* Really get the variable */
540 /* Duplicate value if needed */
541 p_var->pf_dup( p_val );
543 vlc_mutex_unlock( &p_this->var_lock );
548 /*****************************************************************************
549 * var_AddCallback: register a callback in a variable
550 *****************************************************************************
551 * We store a function pointer pf_callback that will be called upon variable
552 * modification. p_data is a generic pointer that will be passed as additional
553 * argument to the callback function.
554 *****************************************************************************/
555 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
556 vlc_callback_t pf_callback, void *p_data )
560 callback_entry_t entry;
562 entry.pf_callback = pf_callback;
563 entry.p_data = p_data;
565 vlc_mutex_lock( &p_this->var_lock );
567 i_var = GetUnused( p_this, psz_name );
570 vlc_mutex_unlock( &p_this->var_lock );
574 p_var = &p_this->p_vars[i_var];
576 INSERT_ELEM( p_var->p_entries,
581 vlc_mutex_unlock( &p_this->var_lock );
586 /*****************************************************************************
587 * var_DelCallback: remove a callback from a variable
588 *****************************************************************************
589 * pf_callback and p_data have to be given again, because different objects
590 * might have registered the same callback function.
591 *****************************************************************************/
592 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
593 vlc_callback_t pf_callback, void *p_data )
598 vlc_mutex_lock( &p_this->var_lock );
600 i_var = GetUnused( p_this, psz_name );
603 vlc_mutex_unlock( &p_this->var_lock );
607 p_var = &p_this->p_vars[i_var];
609 for( i_entry = p_var->i_entries ; i_entry-- ; )
611 if( p_var->p_entries[i_entry].pf_callback == pf_callback
612 || p_var->p_entries[i_entry].p_data == p_data )
620 vlc_mutex_unlock( &p_this->var_lock );
624 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
626 vlc_mutex_unlock( &p_this->var_lock );
631 /* Following functions are local */
633 /*****************************************************************************
634 * GetUnused: find an unused variable from its name
635 *****************************************************************************
636 * We do i_tries tries before giving up, just in case the variable is being
637 * modified and called from a callback.
638 *****************************************************************************/
639 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
641 int i_var, i_tries = 0;
645 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
651 if( ! p_this->p_vars[i_var].b_incallback )
656 if( i_tries++ > 100 )
658 msg_Err( p_this, "caught in a callback deadlock?" );
662 vlc_mutex_unlock( &p_this->var_lock );
663 msleep( THREAD_SLEEP );
664 vlc_mutex_lock( &p_this->var_lock );
668 /*****************************************************************************
669 * HashString: our cool hash function
670 *****************************************************************************
671 * This function is not intended to be crypto-secure, we only want it to be
672 * fast and not suck too much. This one is pretty fast and did 0 collisions
673 * in wenglish's dictionary.
674 *****************************************************************************/
675 static uint32_t HashString( const char *psz_string )
681 i_hash += *psz_string++;
682 i_hash += i_hash << 10;
683 i_hash ^= i_hash >> 8;
689 /*****************************************************************************
690 * Insert: find an empty slot to insert a new variable
691 *****************************************************************************
692 * We use a recursive inner function indexed on the hash. This function does
693 * nothing in the rare cases where a collision may occur, see Lookup()
694 * to see how we handle them.
695 * XXX: does this really need to be written recursively?
696 *****************************************************************************/
697 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
704 return InsertInner( p_vars, i_count, HashString( psz_name ) );
707 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
711 if( i_hash <= p_vars[0].i_hash )
716 if( i_hash >= p_vars[i_count - 1].i_hash )
721 i_middle = i_count / 2;
723 /* We know that 0 < i_middle */
724 if( i_hash < p_vars[i_middle].i_hash )
726 return InsertInner( p_vars, i_middle, i_hash );
729 /* We know that i_middle + 1 < i_count */
730 if( i_hash > p_vars[i_middle + 1].i_hash )
732 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
733 i_count - i_middle - 1,
740 /*****************************************************************************
741 * Lookup: find an existing variable given its name
742 *****************************************************************************
743 * We use a recursive inner function indexed on the hash. Care is taken of
744 * possible hash collisions.
745 * XXX: does this really need to be written recursively?
746 *****************************************************************************/
747 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
757 i_hash = HashString( psz_name );
759 i_pos = LookupInner( p_vars, i_count, i_hash );
762 if( i_hash != p_vars[i_pos].i_hash )
767 /* Hash found, entry found */
768 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
773 /* Hash collision! This should be very rare, but we cannot guarantee
774 * it will never happen. Just do an exhaustive search amongst all
775 * entries with the same hash. */
776 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
778 if( !strcmp( psz_name, p_vars[i].psz_name ) )
784 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
786 if( !strcmp( psz_name, p_vars[i].psz_name ) )
792 /* Hash found, but entry not found */
796 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
800 if( i_hash <= p_vars[0].i_hash )
805 if( i_hash >= p_vars[i_count-1].i_hash )
810 i_middle = i_count / 2;
812 /* We know that 0 < i_middle */
813 if( i_hash < p_vars[i_middle].i_hash )
815 return LookupInner( p_vars, i_middle, i_hash );
818 /* We know that i_middle + 1 < i_count */
819 if( i_hash > p_vars[i_middle].i_hash )
821 return i_middle + LookupInner( p_vars + i_middle,
829 /*****************************************************************************
830 * CheckValue: check that a value is valid wrt. a variable
831 *****************************************************************************
832 * This function checks p_val's value against p_var's limitations such as
833 * minimal and maximal value, step, in-list position, and modifies p_val if
835 *****************************************************************************/
836 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
838 /* Check that our variable is in the list */
839 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->i_choices )
843 /* FIXME: the list is sorted, dude. Use something cleverer. */
844 for( i = p_var->i_choices ; i-- ; )
846 if( p_var->pf_cmp( *p_val, p_var->pp_choices[i] ) == 0 )
852 /* If not found, change it to anything vaguely valid */
855 /* Free the old variable, get the new one, dup it */
856 p_var->pf_free( p_val );
857 *p_val = p_var->pp_choices[p_var->i_default >= 0
858 ? p_var->i_default : 0 ];
859 p_var->pf_dup( p_val );
863 /* Check that our variable is within the bounds */
864 switch( p_var->i_type & VLC_VAR_TYPE )
866 case VLC_VAR_INTEGER:
867 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
868 && (p_val->i_int % p_var->step.i_int) )
870 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
871 / p_var->step.i_int * p_var->step.i_int;
873 if( p_var->i_type & VLC_VAR_HASMIN
874 && p_val->i_int < p_var->min.i_int )
876 p_val->i_int = p_var->min.i_int;
878 if( p_var->i_type & VLC_VAR_HASMAX
879 && p_val->i_int > p_var->max.i_int )
881 p_val->i_int = p_var->max.i_int;
885 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
887 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
888 p_val->f_float / p_var->step.f_float );
889 if( p_val->f_float != f_round )
891 p_val->f_float = f_round;
894 if( p_var->i_type & VLC_VAR_HASMIN
895 && p_val->f_float < p_var->min.f_float )
897 p_val->f_float = p_var->min.f_float;
899 if( p_var->i_type & VLC_VAR_HASMAX
900 && p_val->f_float > p_var->max.f_float )
902 p_val->f_float = p_var->max.f_float;