variables.c 13.7 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * variables.c: routines for object variables handling
 *****************************************************************************
 * Copyright (C) 2002 VideoLAN
5
 * $Id: variables.c,v 1.3 2002/10/14 19:04:51 sam Exp $
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>

#ifdef HAVE_STDLIB_H
#   include <stdlib.h>                                          /* realloc() */
#endif

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static u32 HashString     ( const char * );
static int Insert         ( variable_t *, int, const char * );
static int InsertInner    ( variable_t *, int, u32 );
static int Lookup         ( variable_t *, int, const char * );
static int LookupInner    ( variable_t *, int, u32 );

/*****************************************************************************
 * var_Create: initialize a vlc variable
 *****************************************************************************
 * We hash the given string and insert it into the sorted list. The insertion
 * may require slow memory copies, but think about what we gain in the log(n)
 * lookup phase when setting/getting the variable value!
 *****************************************************************************/
49
int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
50
51
{
    int i_new;
52
    variable_t *p_var;
53
54
55

    vlc_mutex_lock( &p_this->var_lock );

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    /* FIXME: if the variable already exists, we don't duplicate it. But we
     * duplicate the lookups. It's not that serious, but if anyone finds some
     * time to rework Insert() so that only one lookup has to be done, feel
     * free to do so. */
    i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );

    if( i_new >= 0 )
    {
        /* If the types differ, variable creation failed. */
        if( i_type != p_this->p_vars[i_new].i_type )
        {
            vlc_mutex_unlock( &p_this->var_lock );
            return VLC_EBADVAR;
        }

        p_this->p_vars[i_new].i_usage++;
        vlc_mutex_unlock( &p_this->var_lock );
        return VLC_SUCCESS;
    }

    i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );

78
79
80
81
82
83
84
85
86
87
    if( (p_this->i_vars & 15) == 15 )
    {
        p_this->p_vars = realloc( p_this->p_vars,
                                  (p_this->i_vars+17) * sizeof(variable_t) );
    }

    memmove( p_this->p_vars + i_new + 1,
             p_this->p_vars + i_new,
             (p_this->i_vars - i_new) * sizeof(variable_t) );

88
89
90
91
    p_var = &p_this->p_vars[i_new];

    p_var->i_hash = HashString( psz_name );
    p_var->psz_name = strdup( psz_name );
92

93
94
    p_var->i_type = i_type;
    memset( &p_var->val, 0, sizeof(vlc_value_t) );
95

96
97
98
    p_var->i_usage = 1;
    p_var->b_set = VLC_FALSE;
    p_var->b_active = VLC_TRUE;
99
100
101
102

    p_this->i_vars++;

    vlc_mutex_unlock( &p_this->var_lock );
103
104

    return VLC_SUCCESS;
105
106
107
108
109
110
111
112
}

/*****************************************************************************
 * var_Destroy: destroy a vlc variable
 *****************************************************************************
 * Look for the variable and destroy it if it is found. As in var_Create we
 * do a call to memmove() but we have performance counterparts elsewhere.
 *****************************************************************************/
113
int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
114
115
{
    int i_del;
116
    variable_t *p_var;
117
118
119
120
121
122
123
124
125

    vlc_mutex_lock( &p_this->var_lock );

    i_del = Lookup( p_this->p_vars, p_this->i_vars, psz_name );

    if( i_del < 0 )
    {
        msg_Err( p_this, "variable %s was not found", psz_name );
        vlc_mutex_unlock( &p_this->var_lock );
126
        return VLC_ENOVAR;
127
128
    }

129
130
131
132
133
134
135
136
137
    p_var = &p_this->p_vars[i_del];

    if( p_var->i_usage > 1 )
    {
        p_var->i_usage--;
        vlc_mutex_unlock( &p_this->var_lock );
        return VLC_SUCCESS;
    }

138
    /* Free value if needed */
139
    switch( p_var->i_type )
140
141
142
143
    {
        case VLC_VAR_STRING:
        case VLC_VAR_MODULE:
        case VLC_VAR_FILE:
144
            if( p_var->b_set && p_var->val.psz_string )
145
            {
146
                free( p_var->val.psz_string );
147
148
149
150
            }
            break;
    }

151
    free( p_var->psz_name );
152
153
154
155
156
157
158
159
160
161
162
163
164
165

    memmove( p_this->p_vars + i_del,
             p_this->p_vars + i_del + 1,
             (p_this->i_vars - i_del - 1) * sizeof(variable_t) );

    if( (p_this->i_vars & 15) == 0 )
    {
        p_this->p_vars = realloc( p_this->p_vars,
                          (p_this->i_vars) * sizeof( variable_t ) );
    }

    p_this->i_vars--;

    vlc_mutex_unlock( &p_this->var_lock );
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

    return VLC_SUCCESS;
}

/*****************************************************************************
 * var_Type: request a variable's type, 0 if not found
 *****************************************************************************
 * 
 *****************************************************************************/
int __var_Type( vlc_object_t *p_this, const char *psz_name )
{
    int i_var, i_type;

    vlc_mutex_lock( &p_this->var_lock );

    i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );

    if( i_var < 0 )
    {
        vlc_mutex_unlock( &p_this->var_lock );
        return 0;
    }

    i_type = p_this->p_vars[i_var].i_type;

    vlc_mutex_unlock( &p_this->var_lock );

    return i_type;
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
}

/*****************************************************************************
 * var_Set: set a variable's value
 *****************************************************************************
 *
 *****************************************************************************/
int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
{
    int i_var;

    vlc_mutex_lock( &p_this->var_lock );

    i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );

    if( i_var < 0 )
    {
        vlc_mutex_unlock( &p_this->var_lock );
212
        return VLC_ENOVAR;
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
    }

    /* Duplicate value if needed */
    switch( p_this->p_vars[i_var].i_type )
    {
        case VLC_VAR_STRING:
        case VLC_VAR_MODULE:
        case VLC_VAR_FILE:
            if( p_this->p_vars[i_var].b_set
                 && p_this->p_vars[i_var].val.psz_string )
            {
                free( p_this->p_vars[i_var].val.psz_string );
            }
            if( val.psz_string )
            {
                val.psz_string = strdup( val.psz_string );
            }
            break;
    }

    p_this->p_vars[i_var].val = val;
    p_this->p_vars[i_var].b_set = VLC_TRUE;

    /* XXX: callback stuff will go here */

    vlc_mutex_unlock( &p_this->var_lock );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * var_Get: get a variable's value
 *****************************************************************************
 *
 *****************************************************************************/
248
int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
249
250
251
252
253
254
255
256
257
258
{
    int i_var;

    vlc_mutex_lock( &p_this->var_lock );

    i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );

    if( i_var < 0 )
    {
        vlc_mutex_unlock( &p_this->var_lock );
259
        return VLC_ENOVAR;
260
261
262
263
264
    }

    if( !p_this->p_vars[i_var].b_set )
    {
        vlc_mutex_unlock( &p_this->var_lock );
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
        return VLC_EBADVAR;
    }

    /* Some variables trigger special behaviour. */
    switch( p_this->p_vars[i_var].i_type )
    {
        case VLC_VAR_COMMAND:
            if( p_this->p_vars[i_var].b_set )
            {
                int i_ret = ((int (*) (vlc_object_t *, char *, char *))
                                p_this->p_vars[i_var].val.p_address) (
                                    p_this,
                                    p_this->p_vars[i_var].psz_name,
                                    p_val->psz_string );
                vlc_mutex_unlock( &p_this->var_lock );
                return i_ret;
            }
            break;
283
284
    }

285
286
    /* Really set the variable */
    *p_val = p_this->p_vars[i_var].val;
287
288
289
290
291
292
293

    /* Duplicate value if needed */
    switch( p_this->p_vars[i_var].i_type )
    {
        case VLC_VAR_STRING:
        case VLC_VAR_MODULE:
        case VLC_VAR_FILE:
294
            if( p_val->psz_string )
295
            {
296
                p_val->psz_string = strdup( p_val->psz_string );
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
            }
            break;
    }

    vlc_mutex_unlock( &p_this->var_lock );

    return VLC_SUCCESS;
}

/* Following functions are local */

/*****************************************************************************
 * 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 u32 HashString( const char *psz_string )
{
    u32 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, u32 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 )
{
    u32 i_hash;
    int i, i_pos;

    if( i_count == 0 )
    {
        return -1;
    }

    i_hash = HashString( psz_name );

    i_pos = LookupInner( p_vars, i_count, i_hash );

    /* Hash not found */
    if( i_hash != p_vars[i_pos].i_hash )
    {
        return -1;
    }

    /* Hash found, entry found */
    if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
    {
        return i_pos;
    }

    /* Hash collision! This should be very rare, but we cannot guarantee
     * it will never happen. Just do an exhaustive search amongst all
     * entries with the same hash. */
    for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
    {
        if( !strcmp( psz_name, p_vars[i].psz_name ) )
        {
            return i;
        }
    }

    for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
    {
        if( !strcmp( psz_name, p_vars[i].psz_name ) )
        {
            return i;
        }
    }

    /* Hash found, but entry not found */
    return -1;
}

static int LookupInner( variable_t *p_vars, int i_count, u32 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 - 1;
    }

    i_middle = i_count / 2;

    /* We know that 0 < i_middle */
    if( i_hash < p_vars[i_middle].i_hash )
    {
        return LookupInner( p_vars, i_middle, i_hash );
    }

    /* We know that i_middle + 1 < i_count */
    if( i_hash > p_vars[i_middle].i_hash )
    {
        return i_middle + LookupInner( p_vars + i_middle,
                                       i_count - i_middle,
                                       i_hash );
    }

    return i_middle;
}