VLC 4.0.0-dev
vlc_arrays.h File Reference

This file defines functions, structures and macros for handling arrays in vlc. More...

Include dependency graph for vlc_arrays.h:

Go to the source code of this file.

Data Structures

struct  vlc_array_t
 
struct  vlc_dictionary_entry_t
 
struct  vlc_dictionary_t
 

Macros

#define TAB_INIT(count, tab)
 
#define TAB_CLEAN(count, tab)
 
#define TAB_APPEND_CAST(cast, count, tab, p)
 
#define TAB_APPEND(count, tab, p)    TAB_APPEND_CAST( , count, tab, p )
 
#define TAB_FIND(count, tab, p, idx)
 
#define TAB_ERASE(count, tab, index)
 
#define TAB_REMOVE(count, tab, p)
 
#define TAB_INSERT_CAST(cast, count, tab, p, index)
 
#define TAB_INSERT(count, tab, p, index)    TAB_INSERT_CAST( , count, tab, p, index )
 
#define BSEARCH(entries, count, elem, zetype, key, answer)
 Binary search in a sorted array. More...
 
#define _ARRAY_ALLOC(array, newsize)
 
#define _ARRAY_GROW1(array)
 
#define DECL_ARRAY(type)
 
#define TYPEDEF_ARRAY(type, name)   typedef DECL_ARRAY(type) name;
 
#define ARRAY_INIT(array)
 
#define ARRAY_RESET(array)
 
#define ARRAY_APPEND(array, elem)
 
#define ARRAY_INSERT(array, elem, pos)
 
#define _ARRAY_SHRINK(array)
 
#define ARRAY_FIND(array, p, idx)    TAB_FIND((array).i_size, (array).p_elems, p, idx)
 
#define ARRAY_REMOVE(array, pos)
 
#define ARRAY_VAL(array, pos)   array.p_elems[pos]
 
#define ARRAY_BSEARCH(array, elem, zetype, key, answer)    BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
 
#define ARRAY_FOREACH(item, array)
 
#define vlc_array_item_at_index(ar, idx)
 

Typedefs

typedef struct vlc_array_t vlc_array_t
 
typedef struct vlc_dictionary_entry_t vlc_dictionary_entry_t
 
typedef struct vlc_dictionary_t vlc_dictionary_t
 

Functions

static void * realloc_down (void *ptr, size_t size)
 
static void * realloc_or_free (void *p, size_t sz)
 This wrapper around realloc() will free the input pointer when realloc() returns NULL. More...
 
static void vlc_array_init (vlc_array_t *p_array)
 
static void vlc_array_clear (vlc_array_t *p_array)
 
static size_t vlc_array_count (vlc_array_t *p_array)
 
static ssize_t vlc_array_index_of_item (const vlc_array_t *ar, const void *elem)
 
static int vlc_array_insert (vlc_array_t *ar, void *elem, int idx)
 
static void vlc_array_insert_or_abort (vlc_array_t *ar, void *elem, int idx)
 
static int vlc_array_append (vlc_array_t *ar, void *elem)
 
static void vlc_array_append_or_abort (vlc_array_t *ar, void *elem)
 
static void vlc_array_remove (vlc_array_t *ar, size_t idx)
 
static uint64_t DictHash (const char *psz_string, int hashsize)
 
static void vlc_dictionary_init (vlc_dictionary_t *p_dict, int i_size)
 
static void vlc_dictionary_clear (vlc_dictionary_t *p_dict, void(*pf_free)(void *p_data, void *p_obj), void *p_obj)
 
static int vlc_dictionary_has_key (const vlc_dictionary_t *p_dict, const char *psz_key)
 
static void * vlc_dictionary_value_for_key (const vlc_dictionary_t *p_dict, const char *psz_key)
 
static int vlc_dictionary_keys_count (const vlc_dictionary_t *p_dict)
 
static bool vlc_dictionary_is_empty (const vlc_dictionary_t *p_dict)
 
static char ** vlc_dictionary_all_keys (const vlc_dictionary_t *p_dict)
 
static void vlc_dictionary_insert_impl_ (vlc_dictionary_t *p_dict, const char *psz_key, void *p_value, bool rebuild)
 
static void vlc_dictionary_insert (vlc_dictionary_t *p_dict, const char *psz_key, void *p_value)
 
static void vlc_dictionary_remove_value_for_key (const vlc_dictionary_t *p_dict, const char *psz_key, void(*pf_free)(void *p_data, void *p_obj), void *p_obj)
 

Variables

static void *const kVLCDictionaryNotFound = NULL
 

Detailed Description

This file defines functions, structures and macros for handling arrays in vlc.

Macro Definition Documentation

◆ _ARRAY_ALLOC

#define _ARRAY_ALLOC (   array,
  newsize 
)
Value:
{ \
(array).i_alloc = newsize; \
(array).p_elems = realloc( (array).p_elems, (array).i_alloc * \
sizeof(*(array).p_elems) ); \
if( !(array).p_elems ) abort(); \
}

◆ _ARRAY_GROW1

#define _ARRAY_GROW1 (   array)
Value:
{ \
if( (array).i_alloc < 10 ) \
_ARRAY_ALLOC(array, 10 ) \
else if( (array).i_alloc == (array).i_size ) \
_ARRAY_ALLOC(array, (int)((array).i_alloc * 1.5) ) \
}

◆ _ARRAY_SHRINK

#define _ARRAY_SHRINK (   array)
Value:
{ \
if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
_ARRAY_ALLOC(array, (array).i_size + 5); \
} \
}

◆ ARRAY_APPEND

#define ARRAY_APPEND (   array,
  elem 
)
Value:
do { \
_ARRAY_GROW1(array); \
(array).p_elems[(array).i_size] = elem; \
(array).i_size++; \
} while(0)

◆ ARRAY_BSEARCH

#define ARRAY_BSEARCH (   array,
  elem,
  zetype,
  key,
  answer 
)     BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)

◆ ARRAY_FIND

#define ARRAY_FIND (   array,
  p,
  idx 
)     TAB_FIND((array).i_size, (array).p_elems, p, idx)

◆ ARRAY_FOREACH

#define ARRAY_FOREACH (   item,
  array 
)
Value:
for (int array_index_##item = 0; \
array_index_##item < (array).i_size && \
((item) = (array).p_elems[array_index_##item], 1); \
++array_index_##item)

◆ ARRAY_INIT

#define ARRAY_INIT (   array)
Value:
do { \
(array).i_alloc = 0; \
(array).i_size = 0; \
(array).p_elems = NULL; \
} while(0)

◆ ARRAY_INSERT

#define ARRAY_INSERT (   array,
  elem,
  pos 
)
Value:
do { \
_ARRAY_GROW1(array); \
if( (array).i_size - (pos) ) { \
memmove( (array).p_elems + (pos) + 1, (array).p_elems + (pos), \
((array).i_size-(pos)) * sizeof(*(array).p_elems) ); \
} \
(array).p_elems[pos] = elem; \
(array).i_size++; \
} while(0)

◆ ARRAY_REMOVE

#define ARRAY_REMOVE (   array,
  pos 
)
Value:
do { \
if( (array).i_size - (pos) - 1 ) \
{ \
memmove( (array).p_elems + (pos), (array).p_elems + (pos) + 1, \
( (array).i_size - (pos) - 1 ) *sizeof(*(array).p_elems) );\
} \
(array).i_size--; \
_ARRAY_SHRINK(array); \
} while(0)

◆ ARRAY_RESET

#define ARRAY_RESET (   array)
Value:
do { \
(array).i_alloc = 0; \
(array).i_size = 0; \
free( (array).p_elems ); (array).p_elems = NULL; \
} while(0)

◆ ARRAY_VAL

#define ARRAY_VAL (   array,
  pos 
)    array.p_elems[pos]

◆ BSEARCH

#define BSEARCH (   entries,
  count,
  elem,
  zetype,
  key,
  answer 
)
Value:
do { \
int low = 0, high = count - 1; \
answer = -1; \
while( low <= high ) {\
int mid = ((unsigned int)low + (unsigned int)high) >> 1;\
zetype mid_val = entries[mid] elem;\
if( mid_val < key ) \
low = mid + 1; \
else if ( mid_val > key ) \
high = mid -1; \
else \
{ \
answer = mid; break; \
}\
} \
} while(0)
size_t count
Definition: core.c:403

Binary search in a sorted array.

The key must be comparable by < and >

Parameters
entriesarray of entries
countnumber of entries
elemkey to check within an entry (like .id, or ->i_id)
zetypetype of the key
keyvalue of the key
answerindex of answer within the array. -1 if not found

◆ DECL_ARRAY

#define DECL_ARRAY (   type)
Value:
struct { \
int i_alloc; \
int i_size; \
type *p_elems; \
}

◆ TAB_APPEND

#define TAB_APPEND (   count,
  tab,
  p 
)     TAB_APPEND_CAST( , count, tab, p )

◆ TAB_APPEND_CAST

#define TAB_APPEND_CAST (   cast,
  count,
  tab,
  p 
)
Value:
do { \
if( (count) > 0 ) \
(tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
else \
(tab) = cast malloc( sizeof( *(tab) ) ); \
if( !(tab) ) abort(); \
(tab)[count] = (p); \
(count)++; \
} while(0)
#define p(t)

◆ TAB_CLEAN

#define TAB_CLEAN (   count,
  tab 
)
Value:
do { \
free( tab ); \
(count)= 0; \
(tab)= NULL; \
} while(0)

◆ TAB_ERASE

#define TAB_ERASE (   count,
  tab,
  index 
)
Value:
do { \
if( (count) > 1 ) \
memmove( (tab) + (index), \
(tab) + (index) + 1, \
((count) - (index) - 1 ) * sizeof( *(tab) ) );\
(count)--; \
if( (count) == 0 ) \
{ \
free( tab ); \
(tab) = NULL; \
} \
} while(0)

◆ TAB_FIND

#define TAB_FIND (   count,
  tab,
  p,
  idx 
)
Value:
do { \
for( (idx) = 0; (idx) < (count); (idx)++ ) \
if( (tab)[(idx)] == (p) ) \
break; \
if( (idx) >= (count) ) \
(idx) = -1; \
} while(0)

◆ TAB_INIT

#define TAB_INIT (   count,
  tab 
)
Value:
do { \
(count) = 0; \
(tab) = NULL; \
} while(0)

◆ TAB_INSERT

#define TAB_INSERT (   count,
  tab,
  p,
  index 
)     TAB_INSERT_CAST( , count, tab, p, index )

◆ TAB_INSERT_CAST

#define TAB_INSERT_CAST (   cast,
  count,
  tab,
  p,
  index 
)
Value:
do { \
if( (count) > 0 ) \
(tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
else \
(tab) = cast malloc( sizeof( *(tab) ) ); \
if( !(tab) ) abort(); \
if( (count) - (index) > 0 ) \
memmove( (tab) + (index) + 1, \
(tab) + (index), \
((count) - (index)) * sizeof( *(tab) ) );\
(tab)[(index)] = (p); \
(count)++; \
} while(0)

◆ TAB_REMOVE

#define TAB_REMOVE (   count,
  tab,
  p 
)
Value:
do { \
int i_index; \
TAB_FIND( count, tab, p, i_index ); \
if( i_index >= 0 ) \
TAB_ERASE( count, tab, i_index ); \
} while(0)

◆ TYPEDEF_ARRAY

#define TYPEDEF_ARRAY (   type,
  name 
)    typedef DECL_ARRAY(type) name;

◆ vlc_array_item_at_index

#define vlc_array_item_at_index (   ar,
  idx 
)
Value:
_Generic((ar), \
const vlc_array_t *: ((ar)->pp_elems[idx]), \
vlc_array_t *: ((ar)->pp_elems[idx]))
Definition: vlc_arrays.h:257

Typedef Documentation

◆ vlc_array_t

typedef struct vlc_array_t vlc_array_t

◆ vlc_dictionary_entry_t

◆ vlc_dictionary_t

Function Documentation

◆ DictHash()

static uint64_t DictHash ( const char *  psz_string,
int  hashsize 
)
inlinestatic

◆ realloc_down()

static void * realloc_down ( void *  ptr,
size_t  size 
)
inlinestatic

◆ realloc_or_free()

static void * realloc_or_free ( void *  p,
size_t  sz 
)
inlinestatic

This wrapper around realloc() will free the input pointer when realloc() returns NULL.

The use case ptr = realloc(ptr, newsize) will cause a memory leak when ptr pointed to a heap allocation before, leaving the buffer allocated but unreferenced. vlc_realloc() is a drop-in replacement for that use case (and only that use case).

References p.

Referenced by vlc_config_create().

◆ vlc_array_append()

static int vlc_array_append ( vlc_array_t ar,
void *  elem 
)
inlinestatic

◆ vlc_array_append_or_abort()

static void vlc_array_append_or_abort ( vlc_array_t ar,
void *  elem 
)
inlinestatic

References vlc_array_append().

Referenced by ChangeFilters(), and sout_StreamChainNew().

◆ vlc_array_clear()

◆ vlc_array_count()

◆ vlc_array_index_of_item()

static ssize_t vlc_array_index_of_item ( const vlc_array_t ar,
const void *  elem 
)
inlinestatic

◆ vlc_array_init()

◆ vlc_array_insert()

static int vlc_array_insert ( vlc_array_t ar,
void *  elem,
int  idx 
)
inlinestatic

◆ vlc_array_insert_or_abort()

static void vlc_array_insert_or_abort ( vlc_array_t ar,
void *  elem,
int  idx 
)
inlinestatic

References vlc_array_insert().

◆ vlc_array_remove()

static void vlc_array_remove ( vlc_array_t ar,
size_t  idx 
)
inlinestatic

◆ vlc_dictionary_all_keys()

◆ vlc_dictionary_clear()

static void vlc_dictionary_clear ( vlc_dictionary_t p_dict,
void(*)(void *p_data, void *p_obj)  pf_free,
void *  p_obj 
)
inlinestatic

◆ vlc_dictionary_has_key()

static int vlc_dictionary_has_key ( const vlc_dictionary_t p_dict,
const char *  psz_key 
)
inlinestatic

◆ vlc_dictionary_init()

static void vlc_dictionary_init ( vlc_dictionary_t p_dict,
int  i_size 
)
inlinestatic

◆ vlc_dictionary_insert()

static void vlc_dictionary_insert ( vlc_dictionary_t p_dict,
const char *  psz_key,
void *  p_value 
)
inlinestatic

◆ vlc_dictionary_insert_impl_()

◆ vlc_dictionary_is_empty()

static bool vlc_dictionary_is_empty ( const vlc_dictionary_t p_dict)
inlinestatic

◆ vlc_dictionary_keys_count()

static int vlc_dictionary_keys_count ( const vlc_dictionary_t p_dict)
inlinestatic

◆ vlc_dictionary_remove_value_for_key()

static void vlc_dictionary_remove_value_for_key ( const vlc_dictionary_t p_dict,
const char *  psz_key,
void(*)(void *p_data, void *p_obj)  pf_free,
void *  p_obj 
)
inlinestatic

◆ vlc_dictionary_value_for_key()

Variable Documentation

◆ kVLCDictionaryNotFound

void* const kVLCDictionaryNotFound = NULL
static