Changeset 266fb28cd7fdd0db6926f807bf4f9bcdd1b29010

Show
Ignore:
Timestamp:
19/10/06 23:00:14 (2 years ago)
Author:
Clément Stenac <zorglub@videolan.org>
git-committer:
Clément Stenac <zorglub@videolan.org> 1161291614 +0000
git-parent:

[531bbc546238a0067f76869d123fae139d3f2157]

git-author:
Clément Stenac <zorglub@videolan.org> 1161291614 +0000
Message:

Dynamic array with log allocation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • include/vlc_common.h

    r62fffad r266fb28  
    634634#define FREE(a) if( a ) { free( a ); } 
    635635 
    636 /* Dynamic array handling: realloc array, move data, increment position */ 
    637 #if defined( _MSC_VER ) && _MSC_VER < 1300 && !defined( UNDER_CE ) 
    638 #   define VLCCVP (void**) /* Work-around for broken compiler */ 
    639 #else 
    640 #   define VLCCVP 
    641 #endif 
    642 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem )                           \ 
    643     do                                                                        \ 
    644     {                                                                         \ 
    645         if( !i_oldsize ) (p_ar) = NULL;                                       \ 
    646         (p_ar) = VLCCVP realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \ 
    647         if( (i_oldsize) - (i_pos) )                                           \ 
    648         {                                                                     \ 
    649             memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos),                  \ 
    650                      ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) );           \ 
    651         }                                                                     \ 
    652         (p_ar)[i_pos] = elem;                                                 \ 
    653         (i_oldsize)++;                                                        \ 
    654     }                                                                         \ 
    655     while( 0 ) 
    656  
    657 #define REMOVE_ELEM( p_ar, i_oldsize, i_pos )                                 \ 
    658     do                                                                        \ 
    659     {                                                                         \ 
    660         if( (i_oldsize) - (i_pos) - 1 )                                       \ 
    661         {                                                                     \ 
    662             memmove( (p_ar) + (i_pos),                                        \ 
    663                      (p_ar) + (i_pos) + 1,                                    \ 
    664                      ((i_oldsize) - (i_pos) - 1) * sizeof( *(p_ar) ) );       \ 
    665         }                                                                     \ 
    666         if( i_oldsize > 1 )                                                   \ 
    667         {                                                                     \ 
    668             (p_ar) = realloc( p_ar, ((i_oldsize) - 1) * sizeof( *(p_ar) ) );  \ 
    669         }                                                                     \ 
    670         else                                                                  \ 
    671         {                                                                     \ 
    672             free( p_ar );                                                     \ 
    673             (p_ar) = NULL;                                                    \ 
    674         }                                                                     \ 
    675         (i_oldsize)--;                                                        \ 
    676     }                                                                         \ 
    677     while( 0 ) 
    678  
    679  
    680 #define TAB_APPEND( count, tab, p )             \ 
    681     if( (count) > 0 )                           \ 
    682     {                                           \ 
    683         (tab) = realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \ 
    684     }                                           \ 
    685     else                                        \ 
    686     {                                           \ 
    687         (tab) = malloc( sizeof( void ** ) );    \ 
    688     }                                           \ 
    689     (tab)[count] = (p);        \ 
    690     (count)++ 
    691  
    692 #define TAB_FIND( count, tab, p, index )        \ 
    693     {                                           \ 
    694         int _i_;                                \ 
    695         (index) = -1;                           \ 
    696         for( _i_ = 0; _i_ < (count); _i_++ )    \ 
    697         {                                       \ 
    698             if( (tab)[_i_] == (p) )  \ 
    699             {                                   \ 
    700                 (index) = _i_;                  \ 
    701                 break;                          \ 
    702             }                                   \ 
    703         }                                       \ 
    704     } 
    705  
    706 #define TAB_REMOVE( count, tab, p )             \ 
    707     {                                           \ 
    708         int _i_index_;                          \ 
    709         TAB_FIND( count, tab, p, _i_index_ );   \ 
    710         if( _i_index_ >= 0 )                    \ 
    711         {                                       \ 
    712             if( (count) > 1 )                     \ 
    713             {                                   \ 
    714                 memmove( ((void**)(tab) + _i_index_),    \ 
    715                          ((void**)(tab) + _i_index_+1),  \ 
    716                          ( (count) - _i_index_ - 1 ) * sizeof( void* ) );\ 
    717             }                                   \ 
    718             (count)--;                          \ 
    719             if( (count) == 0 )                  \ 
    720             {                                   \ 
    721                 free( tab );                    \ 
    722                 (tab) = NULL;                   \ 
    723             }                                   \ 
    724         }                                       \ 
    725     } 
    726  
    727 /* Binary search in an array */ 
    728 #define BSEARCH( entries, count, elem, zetype, key, answer ) {  \ 
    729     int low = 0, high = count - 1;   \ 
    730     answer = -1; \ 
    731     while( low <= high ) {\ 
    732         int mid = (low + high ) / 2; /* Just don't care about 2^30 tables */ \ 
    733         zetype mid_val = entries[mid] elem;\ 
    734         if( mid_val < key ) \ 
    735             low = mid + 1; \ 
    736         else if ( mid_val > key ) \ 
    737             high = mid -1;  \ 
    738         else    \ 
    739         {   \ 
    740             answer = mid;  break;   \ 
    741         }\ 
    742     } \ 
    743 
    744  
    745 /* Dictionnary handling */ 
    746 struct dict_entry_t 
    747 
    748     int       i_int; 
    749     char     *psz_string; 
    750     uint64_t  i_hash; 
    751     void     *p_data; 
    752 }; 
    753  
    754 struct dict_t 
    755 
    756     dict_entry_t *p_entries; 
    757     int i_entries; 
    758 }; 
    759  
    760 VLC_EXPORT( dict_t *, vlc_DictNew, (void) ); 
    761 VLC_EXPORT( void, vlc_DictClear, (dict_t * ) ); 
    762 VLC_EXPORT( void, vlc_DictInsert, (dict_t *, int, const char *, void * ) ); 
    763 VLC_EXPORT( void*, vlc_DictGet, (dict_t *, int, const char * ) ); 
    764 VLC_EXPORT( int, vlc_DictLookup, (dict_t *, int, const char * ) ); 
     636#include <vlc_arrays.h> 
    765637 
    766638/* MSB (big endian)/LSB (little endian) conversions - network order is always 
  • test/NativeAlgoTest.py

    r0fbf13c r266fb28  
    44 
    55class NativeAlgoTestCase( unittest.TestCase ): 
     6    def test_arrays( self ): 
     7        """[Algo] Check dynamic arrays""" 
     8        native_libvlc_test.arrays_test() 
    69    def test_bsearch_direct( self ): 
    710        """[Algo] Check Bsearch with simple types""" 
  • test/native/algo.c

    r0fbf13c r266fb28  
    2424 
    2525/********************************************************************** 
     26 * Arrays 
     27 *********************************************************************/ 
     28 
     29TYPEDEF_ARRAY(long,long_array_t); 
     30 
     31PyObject *arrays_test( PyObject *self, PyObject *args ) 
     32{ 
     33    mtime_t one, two; 
     34    int number = 1000000; 
     35    int number2 = 50000; /* For slow with memmove */ 
     36    printf("\n"); 
     37    { 
     38        int i_items = 0; 
     39        int *p_items = NULL; 
     40        int i; 
     41        one = mdate(); 
     42        for( i = 0 ; i<number;i++) { 
     43            INSERT_ELEM(p_items,i_items, i_items, i+50); 
     44        } 
     45        two = mdate(); 
     46        printf( " Std array %i items appended in "I64Fi" µs\n", number, 
     47                (two-one) ); 
     48        for( i = number-1 ; i>=0; i--) { 
     49            REMOVE_ELEM( p_items, i_items, i ); 
     50        } 
     51        one = mdate(); 
     52        printf( " Std array %i items removed in  "I64Fi" µs\n", number, 
     53                (one-two) ); 
     54 
     55        for( i = 0 ; i<number2;i++) { 
     56            int pos = i_items == 0  ? 0 : rand() % i_items; 
     57            INSERT_ELEM(p_items, i_items, pos, pos + 50); 
     58        } 
     59        two = mdate(); 
     60        printf( " Std array %i items inserted in  "I64Fi" µs\n", number2, 
     61                (two-one) ); 
     62    } 
     63    { 
     64        DECL_ARRAY(int) int_array; 
     65        int i = 0; 
     66        ARRAY_INIT(int_array); 
     67        ASSERT(int_array.i_size == 0, "" ); 
     68        ASSERT(int_array.i_alloc == 0, "" ); 
     69        ASSERT(int_array.p_elems == 0, "" ); 
     70 
     71        ARRAY_APPEND(int_array, 42 ); 
     72        ASSERT(int_array.i_size == 1, "" ); 
     73        ASSERT(int_array.i_alloc > 1, "" ); 
     74        ASSERT(int_array.p_elems[0] == 42, "" ); 
     75        ARRAY_REMOVE(int_array,0); 
     76        ASSERT(int_array.i_size == 0, "" ); 
     77 
     78        one = mdate(); 
     79        for( i = 0 ; i<number;i++) { 
     80            ARRAY_APPEND(int_array, i+50); 
     81        } 
     82        two = mdate(); 
     83        printf( " New array %i items appended in "I64Fi" µs\n", number, 
     84                (two-one) ); 
     85        ASSERT(int_array.p_elems[1242] == 1292 , ""); 
     86        for( i = number-1 ; i>=0; i--) { 
     87            ARRAY_REMOVE(int_array,i); 
     88        } 
     89        one = mdate(); 
     90        printf( " New array %i items removed in  "I64Fi" µs\n", number, 
     91                (one-two) ); 
     92 
     93        /* Now random inserts */ 
     94        for( i = 0 ; i<number2;i++) { 
     95            int pos = int_array.i_size == 0  ? 0 : rand() % int_array.i_size; 
     96            ARRAY_INSERT(int_array, pos+50, pos); 
     97        } 
     98        two = mdate(); 
     99        printf( " New array %i items inserted in  "I64Fi" µs\n", number2, 
     100                (two-one) ); 
     101    } 
     102    { 
     103        long_array_t larray; 
     104        ARRAY_INIT(larray); 
     105    } 
     106    Py_INCREF( Py_None); 
     107    return Py_None; 
     108} 
     109 
     110/********************************************************************** 
    26111 * Binary search 
    27112 *********************************************************************/ 
     
    67152    struct bsearch_tester array[] = 
    68153    { 
    69         { 0, 12 }, { 1, 22 } , { 2, 33 } , { 3, 68 } , { 4, 56 }  
     154        { 0, 12 }, { 1, 22 } , { 2, 33 } , { 3, 68 } , { 4, 56 } 
    70155    }; 
    71156#define MEMBCHECK( checked, expected ) { \ 
  • test/native/init.c

    rf834305 r266fb28  
    2121   DEF_METHOD( gui_chains_test, "Test interactions between chains and GUI" ) 
    2222   DEF_METHOD( psz_chains_test, "Test building of chain strings" ) 
     23   DEF_METHOD( arrays_test, "Test arrays") 
    2324   DEF_METHOD( bsearch_direct_test, "Test Bsearch without structure" ) 
    2425   DEF_METHOD( bsearch_member_test, "Test Bsearch with structure" ) 
  • test/native/tests.h

    rf834305 r266fb28  
    2020 
    2121/* Algo */ 
     22PyObject *arrays_test( PyObject *self, PyObject *args ); 
    2223PyObject *bsearch_direct_test( PyObject *self, PyObject *args ); 
    2324PyObject *bsearch_member_test( PyObject *self, PyObject *args ); 
  • test/test.sh

    re85a5a6 r266fb28  
    1313ulimit -c unlimited 
    1414 
    15 python test/test.py -v 2>&1|perl  -e \ 
     15if [ "x$1" = "xdebug" ] 
     16then 
     17  gdb python "test/test.sh" 
     18else 
     19  python test/test.py -v 2>&1|perl  -e \ 
    1620'$bold = "\033[1m"; 
    1721$grey  = "\033[37m"; 
     
    4347     } 
    4448}' 
    45  
     49fi