vlc_common.h 32.8 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * vlc_common.h: common definitions
3 4
 * Collection of useful common types and macros definitions
 *****************************************************************************
5
 * Copyright (C) 1998-2011 VLC authors and VideoLAN
6 7 8
 *
 * Authors: Samuel Hocevar <sam@via.ecp.fr>
 *          Vincent Seguin <seguin@via.ecp.fr>
9
 *          Gildas Bazin <gbazin@videolan.org>
10
 *          Rémi Denis-Courmont
11
 *
12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15
 * (at your option) any later version.
16
 *
17 18
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 26
 *****************************************************************************/

27 28 29
#ifndef VLC_COMMON_H
# define VLC_COMMON_H 1

Clément Stenac's avatar
Clément Stenac committed
30
/**
31
 * \defgroup vlc VLC plug-in programming interface
Sam Hocevar's avatar
Sam Hocevar committed
32
 * \file
33
 * \ingroup vlc
Clément Stenac's avatar
Clément Stenac committed
34 35 36
 * This file is a collection of common definitions and types
 */

37
/*****************************************************************************
38
 * Required vlc headers
39
 *****************************************************************************/
40 41
#include "vlc_config.h"

42 43 44
/*****************************************************************************
 * Required system headers
 *****************************************************************************/
45
#include <stdlib.h>
46 47
#include <stdarg.h>

48
#include <string.h>
49
#include <stdio.h>
50
#include <inttypes.h>
51
#include <stddef.h>
52

53 54 55 56
#ifndef __cplusplus
# include <stdbool.h>
#endif

57 58
/**
 * \defgroup cext C programming language extensions
59
 * \ingroup vlc
60 61 62 63 64 65
 *
 * This section defines a number of macros and inline functions extending the
 * C language. Most extensions are implemented by GCC and LLVM/Clang, and have
 * unoptimized fallbacks for other C11/C++11 conforming compilers.
 * @{
 */
66 67 68 69
#ifdef __GNUC__
# define VLC_GCC_VERSION(maj,min) \
    ((__GNUC__ > (maj)) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
#else
70
/** GCC version check */
71 72 73
# define VLC_GCC_VERSION(maj,min) (0)
#endif

74 75 76 77 78 79 80 81 82 83 84 85
/* Try to fix format strings for all versions of mingw and mingw64 */
#if defined( _WIN32 ) && defined( __USE_MINGW_ANSI_STDIO )
 #undef PRId64
 #define PRId64 "lld"
 #undef PRIi64
 #define PRIi64 "lli"
 #undef PRIu64
 #define PRIu64 "llu"
 #undef PRIo64
 #define PRIo64 "llo"
 #undef PRIx64
 #define PRIx64 "llx"
86 87
 #define snprintf __mingw_snprintf
 #define vsnprintf __mingw_vsnprintf
88
 #define swprintf _snwprintf
89 90
#endif

91
/* Function attributes for compiler warnings */
92
#ifdef __GNUC__
93
# define VLC_DEPRECATED __attribute__((deprecated))
94 95 96 97 98
# if VLC_GCC_VERSION(6,0)
#  define VLC_DEPRECATED_ENUM __attribute__((deprecated))
# else
#  define VLC_DEPRECATED_ENUM
# endif
99

100
# if defined( _WIN32 ) && !defined( __clang__ )
101 102 103 104 105 106
#  define VLC_FORMAT(x,y) __attribute__ ((format(gnu_printf,x,y)))
# else
#  define VLC_FORMAT(x,y) __attribute__ ((format(printf,x,y)))
# endif
# define VLC_FORMAT_ARG(x) __attribute__ ((format_arg(x)))
# define VLC_MALLOC __attribute__ ((malloc))
107
# define VLC_USED __attribute__ ((warn_unused_result))
108

109
#else
110 111 112 113 114 115 116 117 118
/**
 * Deprecated functions or compound members annotation
 *
 * Use this macro in front of a function declaration or compound member
 * within a compound type declaration.
 * The compiler may emit a warning every time the function or member is used.
 *
 * Use \ref VLC_DEPRECATED_ENUM instead for enumeration members.
 */
119
# define VLC_DEPRECATED
120 121 122 123 124 125 126 127 128

/**
 * Deprecated enum member annotation
 *
 * Use this macro after an enumerated type member declaration.
 * The compiler may emit a warning every time the enumeration member is used.
 *
 * See also \ref VLC_DEPRECATED.
 */
129
# define VLC_DEPRECATED_ENUM
130 131 132 133 134 135 136 137 138 139 140

/**
 * String format function annotation
 *
 * Use this macro after a function prototype/declaration if the function
 * expects a standard C format string. This helps compiler diagnostics.
 *
 * @param x the position (starting from 1) of the format string argument
 * @param y the first position (also starting from 1) of the variable arguments
 *          following the format string (usually but not always \p x+1).
 */
141
# define VLC_FORMAT(x,y)
142 143 144 145 146 147 148 149 150 151

/**
 * Format string translation function annotation
 *
 * Use this macro after a function prototype/declaration if the function
 * expects a format string as input and returns another format string as output
 * to another function.
 *
 * This is primarily intended for localization functions such as gettext().
 */
152
# define VLC_FORMAT_ARG(x)
153 154 155 156 157 158 159 160 161 162 163 164 165 166

/**
 * Heap allocated result function annotation
 *
 * Use this macro to annotate a function that returns a pointer to memory that
 * cannot alias any other valid pointer.
 *
 * This is primarily used for functions that return a pointer to heap-allocated
 * memory, but it can be used for other applicable purposes.
 *
 * \warning Do not use this annotation if the returned pointer can in any way
 * alias a valid pointer at the time the function exits. This could lead to
 * very weird memory corruption bugs.
 */
167
# define VLC_MALLOC
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

/**
 * Used result function annotation
 *
 * Use this macro to annotate a function whose result must be used.
 *
 * There are several cases where this is useful:
 * - If a function has no side effects (or no useful side effects), such that
 *   the only useful purpose of calling said function is to obtain its
 *   return value.
 * - If ignoring the function return value would lead to a resource leak
 *   (including but not limited to a memory leak).
 * - If a function cannot be used correctly without checking its return value.
 *   For instance, if the function can fail at any time.
 *
 * The compiler may warn if the return value of a function call is ignored.
 */
185
# define VLC_USED
186 187
#endif

188 189 190 191 192 193 194 195 196 197 198 199
#ifdef __ELF__
# define VLC_WEAK __attribute__((weak))
#else
/**
 * Weak symbol annotation
 *
 * Use this macro before an external identifier \b definition to mark it as a
 * weak symbol. A weak symbol can be overriden by another symbol of the same
 * name at the link time.
 */
# define VLC_WEAK
#endif
200

201
/* Branch prediction */
202
#if defined (__GNUC__) || defined (__clang__)
203 204 205
# define likely(p)     __builtin_expect(!!(p), 1)
# define unlikely(p)   __builtin_expect(!!(p), 0)
# define unreachable() __builtin_unreachable()
206
#else
207 208 209 210 211 212 213
/**
 * Predicted true condition
 *
 * This macro indicates that the condition is expected most often true.
 * The compiler may optimize the code assuming that this condition is usually
 * met.
 */
214
# define likely(p)     (!!(p))
215 216 217 218 219 220 221 222

/**
 * Predicted false condition
 *
 * This macro indicates that the condition is expected most often false.
 * The compiler may optimize the code assuming that this condition is rarely
 * met.
 */
223
# define unlikely(p)   (!!(p))
224 225 226 227 228 229 230 231 232 233 234

/**
 * Impossible branch
 *
 * This macro indicates that the branch cannot be reached at run-time, and
 * represents undefined behaviour.
 * The compiler may optimize the code assuming that the call flow will never
 * logically reach the point where this macro is expanded.
 *
 * See also \ref vlc_assert_unreachable.
 */
235
# define unreachable() ((void)0)
236 237
#endif

238 239 240 241 242 243 244 245 246 247 248
/**
 * Impossible branch assertion
 *
 * This macro asserts that the branch cannot be reached at run-time.
 *
 * If the branch is reached in a debug build, it will trigger an assertion
 * failure and abnormal program termination.
 *
 * If the branch is reached in a non-debug build, this macro is equivalent to
 * \ref unreachable and the behaviour is undefined.
 */
249 250
#define vlc_assert_unreachable() (assert(!"unreachable"), unreachable())

251 252
/* Linkage */
#ifdef __cplusplus
253
# define VLC_EXTERN extern "C"
254
#else
255
# define VLC_EXTERN
256 257
#endif

258
#if defined (_WIN32) && defined (DLL_EXPORT)
259
# define VLC_EXPORT __declspec(dllexport)
260
#elif defined (__GNUC__)
261
# define VLC_EXPORT __attribute__((visibility("default")))
262
#else
263
# define VLC_EXPORT
264 265
#endif

266 267 268 269 270 271
/**
 * Exported API call annotation
 *
 * This macro is placed before a function declaration to indicate that the
 * function is an API call of the LibVLC plugin API.
 */
272
#define VLC_API VLC_EXTERN VLC_EXPORT
273

274
/** @} */
275

276 277 278
/*****************************************************************************
 * Basic types definitions
 *****************************************************************************/
279 280 281
/**
 * High precision date or time interval
 *
282 283
 * Store a high precision date or time interval. The maximum precision is the
 * microsecond, and a 64 bits integer is used to avoid overflows (maximum
284
 * time interval is then 292271 years, which should be long enough for any
285 286
 * video). Dates are stored as microseconds since a common date (usually the
 * epoch). Note that date and time intervals can be manipulated using regular
287
 * arithmetic operators, and that no special functions are required.
288
 */
289
typedef int64_t mtime_t;
290

291
/**
292
 * The vlc_fourcc_t type.
293
 *
294
 * See http://www.webartz.com/fourcc/ for a very detailed list.
295
 */
296
typedef uint32_t vlc_fourcc_t;
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
#ifdef WORDS_BIGENDIAN
#   define VLC_FOURCC( a, b, c, d ) \
        ( ((uint32_t)d) | ( ((uint32_t)c) << 8 ) \
           | ( ((uint32_t)b) << 16 ) | ( ((uint32_t)a) << 24 ) )
#   define VLC_TWOCC( a, b ) \
        ( (uint16_t)(b) | ( (uint16_t)(a) << 8 ) )

#else
#   define VLC_FOURCC( a, b, c, d ) \
        ( ((uint32_t)a) | ( ((uint32_t)b) << 8 ) \
           | ( ((uint32_t)c) << 16 ) | ( ((uint32_t)d) << 24 ) )
#   define VLC_TWOCC( a, b ) \
        ( (uint16_t)(a) | ( (uint16_t)(b) << 8 ) )

#endif

314 315 316 317 318 319 320
/**
 * Translate a vlc_fourcc into its string representation. This function
 * assumes there is enough room in psz_fourcc to store 4 characters in.
 *
 * \param fcc a vlc_fourcc_t
 * \param psz_fourcc string to store string representation of vlc_fourcc in
 */
321
static inline void vlc_fourcc_to_char( vlc_fourcc_t fcc, char *psz_fourcc )
322
{
323
    memcpy( psz_fourcc, &fcc, 4 );
324 325
}

326 327 328 329
/*****************************************************************************
 * Classes declaration
 *****************************************************************************/

330
/* Internal types */
331 332
typedef struct vlc_list_t vlc_list_t;
typedef struct vlc_object_t vlc_object_t;
333
typedef struct libvlc_int_t libvlc_int_t;
334
typedef struct date_t date_t;
335

336
/* Playlist */
337

338 339
typedef struct playlist_t playlist_t;
typedef struct playlist_item_t playlist_item_t;
340
typedef struct services_discovery_t services_discovery_t;
341 342
typedef struct vlc_renderer_discovery_t vlc_renderer_discovery_t;
typedef struct vlc_renderer_item_t vlc_renderer_item_t;
343

344
/* Modules */
345 346
typedef struct module_t module_t;
typedef struct module_config_t module_config_t;
347

348 349
typedef struct config_category_t config_category_t;

350
/* Input */
351
typedef struct input_thread_t input_thread_t;
352
typedef struct input_item_t input_item_t;
353
typedef struct input_item_node_t input_item_node_t;
354
typedef struct stream_t     stream_t;
355
typedef struct stream_t demux_t;
356 357
typedef struct es_out_t     es_out_t;
typedef struct es_out_id_t  es_out_id_t;
Gildas Bazin's avatar
Gildas Bazin committed
358
typedef struct seekpoint_t seekpoint_t;
359 360
typedef struct info_t info_t;
typedef struct info_category_t info_category_t;
361
typedef struct input_attachment_t input_attachment_t;
362

363 364 365 366
/* Format */
typedef struct audio_format_t audio_format_t;
typedef struct video_format_t video_format_t;
typedef struct subs_format_t subs_format_t;
367 368
typedef struct es_format_t es_format_t;
typedef struct video_palette_t video_palette_t;
369

370
/* Audio */
371
typedef struct audio_output audio_output_t;
372
typedef audio_format_t audio_sample_format_t;
373 374

/* Video */
375
typedef struct vout_thread_t vout_thread_t;
376
typedef struct vlc_viewpoint_t vlc_viewpoint_t;
377

378
typedef video_format_t video_frame_format_t;
379
typedef struct picture_t picture_t;
380

381
/* Subpictures */
382
typedef struct spu_t spu_t;
383
typedef struct subpicture_t subpicture_t;
384
typedef struct subpicture_region_t subpicture_region_t;
385

386 387
typedef struct image_handler_t image_handler_t;

388 389
/* Stream output */
typedef struct sout_instance_t sout_instance_t;
390

391
typedef struct sout_input_t sout_input_t;
392
typedef struct sout_packetizer_input_t sout_packetizer_input_t;
393

394
typedef struct sout_access_out_t sout_access_out_t;
395

396
typedef struct sout_mux_t sout_mux_t;
397

398
typedef struct sout_stream_t    sout_stream_t;
399

400
typedef struct config_chain_t       config_chain_t;
401
typedef struct session_descriptor_t session_descriptor_t;
402

403
/* Decoders */
Laurent Aimar's avatar
Laurent Aimar committed
404 405
typedef struct decoder_t         decoder_t;
typedef struct decoder_synchro_t decoder_synchro_t;
406

407 408 409
/* Encoders */
typedef struct encoder_t      encoder_t;

410 411 412
/* Filters */
typedef struct filter_t filter_t;

413
/* Network */
414
typedef struct vlc_url_t vlc_url_t;
415

416 417 418
/* Misc */
typedef struct iso639_lang_t iso639_lang_t;

419 420 421 422
/* block */
typedef struct block_t      block_t;
typedef struct block_fifo_t block_fifo_t;

423 424 425
/* Hashing */
typedef struct md5_s md5_t;

426 427 428 429
/* XML */
typedef struct xml_t xml_t;
typedef struct xml_reader_t xml_reader_t;

430 431
/* vod server */
typedef struct vod_t     vod_t;
432
typedef struct vod_media_t vod_media_t;
433

434
/* VLM */
435 436
typedef struct vlm_t         vlm_t;
typedef struct vlm_message_t vlm_message_t;
437

438
/* misc */
439
typedef struct vlc_meta_t    vlc_meta_t;
440
typedef struct input_stats_t input_stats_t;
441
typedef struct addon_entry_t addon_entry_t;
442

443 444
/* Update */
typedef struct update_t update_t;
445

446 447 448 449 450
/**
 * VLC value structure
 */
typedef union
{
451
    int64_t         i_int;
452
    bool            b_bool;
453 454 455
    float           f_float;
    char *          psz_string;
    void *          p_address;
456
    struct { int32_t x; int32_t y; } coords;
457 458 459 460 461 462 463 464

} vlc_value_t;

/**
 * VLC list structure
 */
struct vlc_list_t
{
465 466 467
    int          i_type;
    int          i_count;
    vlc_value_t *p_values;
468 469 470 471 472
};

/*****************************************************************************
 * Error values (shouldn't be exposed)
 *****************************************************************************/
473 474 475 476 477 478 479 480 481
#define VLC_SUCCESS        (-0) /**< No error */
#define VLC_EGENERIC       (-1) /**< Unspecified error */
#define VLC_ENOMEM         (-2) /**< Not enough memory */
#define VLC_ETIMEOUT       (-3) /**< Timeout */
#define VLC_ENOMOD         (-4) /**< Module not found */
#define VLC_ENOOBJ         (-5) /**< Object not found */
#define VLC_ENOVAR         (-6) /**< Variable not found */
#define VLC_EBADVAR        (-7) /**< Bad variable value */
#define VLC_ENOITEM        (-8) /**< Item not found */
482

483
/*****************************************************************************
484
 * Variable callbacks: called when the value is modified
485 486 487 488 489 490 491
 *****************************************************************************/
typedef int ( * vlc_callback_t ) ( vlc_object_t *,      /* variable's object */
                                   char const *,            /* variable name */
                                   vlc_value_t,                 /* old value */
                                   vlc_value_t,                 /* new value */
                                   void * );                /* callback data */

492 493 494 495 496 497 498 499 500
/*****************************************************************************
 * List callbacks: called when elements are added/removed from the list
 *****************************************************************************/
typedef int ( * vlc_list_callback_t ) ( vlc_object_t *,      /* variable's object */
                                        char const *,            /* variable name */
                                        int,                  /* VLC_VAR_* action */
                                        vlc_value_t *,      /* new/deleted value  */
                                        void *);                 /* callback data */

501 502 503
/*****************************************************************************
 * OS-specific headers and thread types
 *****************************************************************************/
504
#if defined( _WIN32 )
505 506 507 508 509 510 511
#   include <malloc.h>
#   ifndef PATH_MAX
#       define PATH_MAX MAX_PATH
#   endif
#   include <windows.h>
#endif

512 513 514 515 516
#ifdef __APPLE__
#include <sys/syslimits.h>
#include <AvailabilityMacros.h>
#endif

517 518 519 520
#ifdef __OS2__
#   define OS2EMX_PLAIN_CHAR
#   define INCL_BASE
#   define INCL_PM
521
#   include <os2safe.h>
522 523 524
#   include <os2.h>
#endif

525
#include "vlc_mtime.h"
526 527
#include "vlc_threads.h"

528 529
/**
 * \defgroup intops Integer operations
530
 * \ingroup cext
531 532 533 534
 *
 * Common integer functions.
 * @{
 */
535 536 537
/* __MAX and __MIN: self explanatory */
#ifndef __MAX
#   define __MAX(a, b)   ( ((a) > (b)) ? (a) : (b) )
538
#endif
539 540
#ifndef __MIN
#   define __MIN(a, b)   ( ((a) < (b)) ? (a) : (b) )
541 542
#endif

543 544 545
/* clip v in [min, max] */
#define VLC_CLIP(v, min, max)    __MIN(__MAX((v), (min)), (max))

546
/** Greatest common divisor */
547
VLC_USED
548
static inline int64_t GCD ( int64_t a, int64_t b )
549
{
550 551 552 553 554 555 556
    while( b )
    {
        int64_t c = a % b;
        a = b;
        b = c;
    }
    return a;
557 558
}

559
/* function imported from libavutil/common.h */
560
VLC_USED
561 562 563 564 565 566
static inline uint8_t clip_uint8_vlc( int32_t a )
{
    if( a&(~255) ) return (-a)>>31;
    else           return a;
}

567 568 569 570
/**
 * \defgroup bitops Bit operations
 * @{
 */
571

572 573 574 575
#define VLC_INT_FUNC(basename) \
        VLC_INT_FUNC_TYPE(basename, unsigned, ) \
        VLC_INT_FUNC_TYPE(basename, unsigned long, l) \
        VLC_INT_FUNC_TYPE(basename, unsigned long long, ll)
576

577 578 579 580 581
#if defined (__GNUC__) || defined (__clang__)
# define VLC_INT_FUNC_TYPE(basename,type,suffix) \
VLC_USED static inline int vlc_##basename##suffix(type x) \
{ \
    return __builtin_##basename##suffix(x); \
582 583
}

584 585 586
VLC_INT_FUNC(clz)
#else
VLC_USED static inline int vlc_clzll(unsigned long long x)
587
{
588
    int i = sizeof (x) * 8;
589

590 591 592 593 594 595
    while (x)
    {
        x >>= 1;
        i--;
    }
    return i;
596 597
}

598
VLC_USED static inline int vlc_clzl(unsigned long x)
599
{
600
    return vlc_clzll(x) - ((sizeof (long long) - sizeof (long)) * 8);
601 602
}

603
VLC_USED static inline int vlc_clz(unsigned x)
604
{
605
    return vlc_clzll(x) - ((sizeof (long long) - sizeof (int)) * 8);
606 607
}

608
VLC_USED static inline int vlc_ctz_generic(unsigned long long x)
609
{
610
    unsigned i = sizeof (x) * 8;
611

612 613 614 615 616 617
    while (x)
    {
        x <<= 1;
        i--;
    }
    return i;
618
}
619

620
VLC_USED static inline int vlc_parity_generic(unsigned long long x)
621
{
622 623 624
    for (unsigned i = 4 * sizeof (x); i > 0; i /= 2)
        x ^= x >> i;
    return x & 1;
625 626
}

627
VLC_USED static inline int vlc_popcount_generic(unsigned long long x)
628
{
629 630 631 632 633 634 635
    int count = 0;
    while (x)
    {
        count += x & 1;
        x = x >> 1;
    }
    return count;
636 637
}

638 639 640 641
# define VLC_INT_FUNC_TYPE(basename,type,suffix) \
VLC_USED static inline int vlc_##basename##suffix(type x) \
{ \
    return vlc_##basename##_generic(x); \
642
}
643
#endif
644

645 646 647
VLC_INT_FUNC(ctz)
VLC_INT_FUNC(parity)
VLC_INT_FUNC(popcount)
648

649 650 651 652 653 654 655 656 657 658 659 660 661
#ifndef __cplusplus
# define VLC_INT_GENERIC(func,x) \
    _Generic((x), \
        unsigned char:      func(x), \
          signed char:      func(x), \
        unsigned short:     func(x), \
          signed short:     func(x), \
        unsigned int:       func(x), \
          signed int:       func(x), \
        unsigned long:      func##l(x), \
          signed long:      func##l(x), \
        unsigned long long: func##ll(x), \
          signed long long: func##ll(x))
662

663 664 665 666 667 668 669 670 671 672 673
/**
 * Count leading zeroes
 *
 * This function counts the number of consecutive zero (clear) bits
 * down from the highest order bit in an unsigned integer.
 *
 * \param x a non-zero integer
 * \note This macro assumes that CHAR_BIT equals 8.
 * \warning By definition, the result depends on the (width of the) type of x.
 * \return The number of leading zero bits in x.
 */
674
# define clz(x) \
675
    _Generic((x), \
676 677 678 679 680 681
        unsigned char: (vlc_clz(x) - (sizeof (unsigned) - 1) * 8), \
        unsigned short: (vlc_clz(x) \
		- (sizeof (unsigned) - sizeof (unsigned short)) * 8), \
        unsigned: vlc_clz(x), \
        unsigned long: vlc_clzl(x), \
        unsigned long long: vlc_clzll(x))
682

683 684 685 686 687 688 689 690 691 692
/**
 * Count trailing zeroes
 *
 * This function counts the number of consecutive zero bits
 * up from the lowest order bit in an unsigned integer.
 *
 * \param x a non-zero integer
 * \note This function assumes that CHAR_BIT equals 8.
 * \return The number of trailing zero bits in x.
 */
693
# define ctz(x) VLC_INT_GENERIC(vlc_ctz, x)
694

695 696 697 698 699 700 701 702
/**
 * Parity
 *
 * This function determines the parity of an integer.
 * \retval 0 if x has an even number of set bits.
 * \retval 1 if x has an odd number of set bits.
 */
# define parity(x) VLC_INT_GENERIC(vlc_parity, x)
703

704 705 706 707 708 709 710
/**
 * Bit weight / population count
 *
 * This function counts the number of non-zero bits in an integer.
 *
 * \return The count of non-zero bits.
 */
711
# define vlc_popcount(x) \
712
    _Generic((x), \
713 714 715 716 717 718 719 720
        signed char:  vlc_popcount((unsigned char)(x)), \
        signed short: vlc_popcount((unsigned short)(x)), \
        default: VLC_INT_GENERIC(vlc_popcount ,x))
#else
VLC_USED static inline int vlc_popcount(unsigned char x)
{
    return vlc_popcount((unsigned)x);
}
721

722
VLC_USED static inline int vlc_popcount(unsigned short x)
723
{
724
    return vlc_popcount((unsigned)x);
725 726
}

727 728 729 730 731 732 733 734 735 736
VLC_USED static inline int vlc_popcount(unsigned long x)
{
    return vlc_popcountl(x);
}

VLC_USED static inline int vlc_popcount(unsigned long long x)
{
    return vlc_popcountll(x);
}
#endif
737

738 739
/** Byte swap (16 bits) */
VLC_USED
740
static inline uint16_t vlc_bswap16(uint16_t x)
741 742 743 744 745 746
{
    return (x << 8) | (x >> 8);
}

/** Byte swap (32 bits) */
VLC_USED
747
static inline uint32_t vlc_bswap32(uint32_t x)
748
{
749
#if defined (__GNUC__) || defined(__clang__)
750 751 752 753 754 755 756 757 758 759 760
    return __builtin_bswap32 (x);
#else
    return ((x & 0x000000FF) << 24)
         | ((x & 0x0000FF00) <<  8)
         | ((x & 0x00FF0000) >>  8)
         | ((x & 0xFF000000) >> 24);
#endif
}

/** Byte swap (64 bits) */
VLC_USED
761
static inline uint64_t vlc_bswap64(uint64_t x)
762
{
763
#if defined (__GNUC__) || defined(__clang__)
764
    return __builtin_bswap64 (x);
765
#elif !defined (__cplusplus)
766 767 768 769 770 771 772 773
    return ((x & 0x00000000000000FF) << 56)
         | ((x & 0x000000000000FF00) << 40)
         | ((x & 0x0000000000FF0000) << 24)
         | ((x & 0x00000000FF000000) <<  8)
         | ((x & 0x000000FF00000000) >>  8)
         | ((x & 0x0000FF0000000000) >> 24)
         | ((x & 0x00FF000000000000) >> 40)
         | ((x & 0xFF00000000000000) >> 56);
774
#else
775 776 777 778 779 780 781 782
    return ((x & 0x00000000000000FFULL) << 56)
         | ((x & 0x000000000000FF00ULL) << 40)
         | ((x & 0x0000000000FF0000ULL) << 24)
         | ((x & 0x00000000FF000000ULL) <<  8)
         | ((x & 0x000000FF00000000ULL) >>  8)
         | ((x & 0x0000FF0000000000ULL) >> 24)
         | ((x & 0x00FF000000000000ULL) >> 40)
         | ((x & 0xFF00000000000000ULL) >> 56);
783 784
#endif
}
785
/** @} */
786

787 788 789 790
/**
 * \defgroup overflow Overflowing arithmetic
 * @{
 */
791 792
static inline bool uadd_overflow(unsigned a, unsigned b, unsigned *res)
{
793
#if VLC_GCC_VERSION(5,0) || defined(__clang__)
794 795 796 797 798 799 800 801 802 803
     return __builtin_uadd_overflow(a, b, res);
#else
     *res = a + b;
     return (a + b) < a;
#endif
}

static inline bool uaddl_overflow(unsigned long a, unsigned long b,
                                  unsigned long *res)
{
804
#if VLC_GCC_VERSION(5,0) || defined(__clang__)
805 806 807 808 809 810 811 812 813 814
     return __builtin_uaddl_overflow(a, b, res);
#else
     *res = a + b;
     return (a + b) < a;
#endif
}

static inline bool uaddll_overflow(unsigned long long a, unsigned long long b,
                                   unsigned long long *res)
{
815
#if VLC_GCC_VERSION(5,0) || defined(__clang__)
816 817 818 819 820 821 822 823
     return __builtin_uaddll_overflow(a, b, res);
#else
     *res = a + b;
     return (a + b) < a;
#endif
}

#ifndef __cplusplus
824 825 826 827 828 829 830 831 832 833 834 835 836
/**
 * Overflowing addition
 *
 * Converts \p a and \p b to the type of \p *r.
 * Then computes the sum of both conversions while checking for overflow.
 * Finally stores the result in \p *r.
 *
 * \param a an integer
 * \param b an integer
 * \param r a pointer to an integer [OUT]
 * \retval false The sum did not overflow.
 * \retval true The sum overflowed.
 */
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
# define add_overflow(a,b,r) \
    _Generic(*(r), \
        unsigned: uadd_overflow(a, b, (unsigned *)(r)), \
        unsigned long: uaddl_overflow(a, b, (unsigned long *)(r)), \
        unsigned long long: uaddll_overflow(a, b, (unsigned long long *)(r)))
#else
static inline bool add_overflow(unsigned a, unsigned b, unsigned *res)
{
    return uadd_overflow(a, b, res);
}

static inline bool add_overflow(unsigned long a, unsigned long b,
                                unsigned long *res)
{
    return uaddl_overflow(a, b, res);
}

static inline bool add_overflow(unsigned long long a, unsigned long long b,
                                unsigned long long *res)
{
    return uaddll_overflow(a, b, res);
}
#endif

861 862 863 864
#if !(VLC_GCC_VERSION(5,0) || defined(__clang__))
# include <limits.h>
#endif

865 866
static inline bool umul_overflow(unsigned a, unsigned b, unsigned *res)
{
867
#if VLC_GCC_VERSION(5,0) || defined(__clang__)
868 869 870 871 872 873 874 875 876 877
     return __builtin_umul_overflow(a, b, res);
#else
     *res = a * b;
     return b > 0 && a > (UINT_MAX / b);
#endif
}

static inline bool umull_overflow(unsigned long a, unsigned long b,
                                  unsigned long *res)
{
878
#if VLC_GCC_VERSION(5,0) || defined(__clang__)
879 880 881 882 883 884 885 886 887 888
     return __builtin_umull_overflow(a, b, res);
#else
     *res = a * b;
     return b > 0 && a > (ULONG_MAX / b);
#endif
}

static inline bool umulll_overflow(unsigned long long a, unsigned long long b,
                                   unsigned long long *res)
{
889
#if VLC_GCC_VERSION(5,0) || defined(__clang__)
890 891 892 893 894 895 896 897
     return __builtin_umulll_overflow(a, b, res);
#else
     *res = a * b;
     return b > 0 && a > (ULLONG_MAX / b);
#endif
}

#ifndef __cplusplus
898 899 900 901 902 903 904 905 906 907 908 909 910
/**
 * Overflowing multiplication
 *
 * Converts \p a and \p b to the type of \p *r.
 * Then computes the product of both conversions while checking for overflow.
 * Finally stores the result in \p *r.
 *
 * \param a an integer
 * \param b an integer
 * \param r a pointer to an integer [OUT]
 * \retval false The product did not overflow.
 * \retval true The product overflowed.
 */
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
#define mul_overflow(a,b,r) \
    _Generic(*(r), \
        unsigned: umul_overflow(a, b, (unsigned *)(r)), \
        unsigned long: umull_overflow(a, b, (unsigned long *)(r)), \
        unsigned long long: umulll_overflow(a, b, (unsigned long long *)(r)))
#else
static inline bool mul_overflow(unsigned a, unsigned b, unsigned *res)
{
    return umul_overflow(a, b, res);
}

static inline bool mul_overflow(unsigned long a, unsigned long b,
                                unsigned long *res)
{
    return umull_overflow(a, b, res);
}

static inline bool mul_overflow(unsigned long long a, unsigned long long b,
                                unsigned long long *res)
{
    return umulll_overflow(a, b, res);
}
#endif
934 935
/** @} */
/** @} */
936

937
/* Free and set set the variable to NULL */
938
#define FREENULL(a) do { free( a ); a = NULL; } while(0)
939

940 941
#define EMPTY_STR(str) (!str || !*str)

942
VLC_API char const * vlc_error( int ) VLC_USED;
943

944
#include <vlc_arrays.h>
945

946
/* MSB (big endian)/LSB (little endian) conversions - network order is always
947
 * MSB, and should be used for both network communications and files. */
948 949 950 951

#ifdef WORDS_BIGENDIAN
# define hton16(i) ((uint16_t)(i))
# define hton32(i) ((uint32_t)(i))
952
# define hton64(i) ((uint64_t)(i))
953
#else
954 955 956
# define hton16(i) vlc_bswap16(i)
# define hton32(i) vlc_bswap32(i)
# define hton64(i) vlc_bswap64(i)
957 958 959 960 961 962
#endif
#define ntoh16(i) hton16(i)
#define ntoh32(i) hton32(i)
#define ntoh64(i) hton64(i)

/** Reads 16 bits in network byte order */
963
VLC_USED
964
static inline uint16_t U16_AT (const void *p)
965
{
966 967 968 969
    uint16_t x;

    memcpy (&x, p, sizeof (x));
    return ntoh16 (x);
970
}
971

972
/** Reads 32 bits in network byte order */
973
VLC_USED
974
static inline uint32_t U32_AT (const void *p)
975
{
976 977 978 979
    uint32_t x;

    memcpy (&x, p, sizeof (x));
    return ntoh32 (x);
980
}
981

982
/** Reads 64 bits in network byte order */
983
VLC_USED
984
static inline uint64_t U64_AT (const void *p)
985
{
986 987 988 989
    uint64_t x;

    memcpy (&x, p, sizeof (x));
    return ntoh64 (x);
990
}
991

992 993 994 995 996
#define GetWBE(p)  U16_AT(p)
#define GetDWBE(p) U32_AT(p)
#define GetQWBE(p) U64_AT(p)

/** Reads 16 bits in little-endian order */
997
VLC_USED
998
static inline uint16_t GetWLE (const void *p)
999
{
1000 1001 1002 1003
    uint16_t x;

    memcpy (&x, p, sizeof (x));
#ifdef WORDS_BIGENDIAN
1004
    x = vlc_bswap16 (x);
1005 1006
#endif
    return x;
1007
}
1008

1009
/** Reads 32 bits in little-endian order */
1010
VLC_USED
1011
static inline uint32_t GetDWLE (const void *p)
1012
{
1013 1014 1015 1016
    uint32_t x;

    memcpy (&x, p, sizeof (x));
#ifdef WORDS_BIGENDIAN
1017
    x = vlc_bswap32 (x);
1018 1019
#endif
    return x;
1020
}
1021

1022
/** Reads 64 bits in little-endian order */
1023
VLC_USED
1024
static inline uint64_t GetQWLE (const void *p)
1025
{
1026
    uint64_t x;
1027

1028 1029
    memcpy (&x, p, sizeof (x));
#ifdef WORDS_BIGENDIAN
1030
    x = vlc_bswap64 (x);
1031 1032
#endif
    return x;
1033 1034
}

1035
/** Writes 16 bits in network byte order */
1036
static inline void SetWBE (void *p, uint16_t w)
1037
{
1038 1039
    w = hton16 (w);
    memcpy (p, &w, sizeof (w));
1040
}
1041 1042

/** Writes 32 bits in network byte order */
1043
static inline void SetDWBE (void *p, uint32_t dw)
1044
{
1045 1046
    dw = hton32 (dw);
    memcpy (p, &dw, sizeof (dw));
1047
}
1048 1049

/** Writes 64 bits in network byte order */
1050
static inline void SetQWBE (void *p, uint64_t qw)
1051
{
1052 1053
    qw = hton64 (qw);
    memcpy (p, &qw, sizeof (qw));
1054 1055
}

1056
/** Writes 16 bits in little endian order */
1057
static inline void SetWLE (void *p, uint16_t w)
1058
{
1059
#ifdef WORDS_BIGENDIAN
1060
    w = vlc_bswap16 (w);
1061 1062
#endif
    memcpy (p, &w, sizeof (w));
1063
}
1064 1065

/** Writes 32 bits in little endian order */
1066
static inline void SetDWLE (void *p, uint32_t dw)
1067
{
1068
#ifdef WORDS_BIGENDIAN
1069
    dw = vlc_bswap32 (dw);
1070 1071
#endif
    memcpy (p, &dw, sizeof (dw));
1072
}
1073

1074
/** Writes 64 bits in little endian order */
1075
static inline void SetQWLE (void *p, uint64_t qw)
1076
{
1077
#ifdef WORDS_BIGENDIAN
1078
    qw = vlc_bswap64 (qw);
1079 1080
#endif
    memcpy (p, &qw, sizeof (qw));
1081
}
1082

1083 1084 1085
/* */
#define VLC_UNUSED(x) (void)(x)

1086
/* Stuff defined in src/extras/libc.c */
1087

1088
#if defined(_WIN32)
1089 1090 1091
/* several type definitions */
#   if defined( __MINGW32__ )
#       if !defined( _OFF_T_ )
1092 1093
            typedef long long _off_t;
            typedef _off_t off_t;
1094 1095
#           define _OFF_T_
#       else
1096 1097 1098
#           ifdef off_t
#               undef off_t
#           endif
1099 1100 1101 1102
#           define off_t long long
#       endif
#   endif

1103 1104
#   ifndef O_NONBLOCK
#       define O_NONBLOCK 0
1105 1106
#   endif

1107
#   include <tchar.h>
1108
#endif /* _WIN32 */
1109

1110 1111 1112 1113
typedef struct {
    unsigned num, den;
} vlc_rational_t;

1114
VLC_API bool vlc_ureduce( unsigned *, unsigned *, uint64_t, uint64_t, uint64_t );
1115

1116 1117 1118
#define container_of(ptr, type, member) \
    ((type *)(((char *)(ptr)) - offsetof(type, member)))

1119 1120 1121
VLC_USED VLC_MALLOC
static inline void *vlc_alloc(size_t count, size_t size)
{
1122
    return mul_overflow(count, size, &size) ? NULL : malloc(size);
1123 1124
}

1125 1126 1127
/*****************************************************************************
 * I18n stuff
 *****************************************************************************/
1128 1129 1130
VLC_API const char *vlc_gettext(const char *msgid) VLC_FORMAT_ARG(1);
VLC_API const char *vlc_ngettext(const char *s, const char *p, unsigned long n)
VLC_FORMAT_ARG(1) VLC_FORMAT_ARG(2);
1131

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1132 1133 1134
#define vlc_pgettext( ctx, id ) \
        vlc_pgettext_aux( ctx "\004" id, id )

1135
VLC_FORMAT_ARG(2)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1136
static inline const char *vlc_pgettext_aux( const char *ctx, const char *id )
1137
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1138
    const char *tr = vlc_gettext( ctx );
1139 1140 1141
    return (tr == ctx) ? id : tr;
}

1142 1143 1144
/*****************************************************************************
 * Loosy memory allocation functions. Do not use in new code.
 *****************************************************************************/
1145
static inline void *xmalloc(size_t len)
1146
{
1147 1148 1149
    void *ptr = malloc(len);
    if (unlikely(ptr == NULL && len > 0))
        abort();
1150 1151 1152
    return ptr;
}

1153
static inline void *xrealloc(void *ptr, size_t len)
1154
{
1155 1156 1157
    void *nptr = realloc(ptr, len);
    if (unlikely(nptr == NULL && len > 0))
        abort();
1158 1159 1160
    return nptr;
}

1161
static inline void *xcalloc(size_t n, size_t size)
1162
{
1163 1164
    void *ptr = calloc(n, size);
    if (unlikely(ptr == NULL && (n > 0 || size > 0)))
1165 1166 1167 1168
        abort ();
    return ptr;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1169 1170 1171 1172 1173 1174 1175 1176
static inline char *xstrdup (const char *str)
{
    char *ptr = strdup (str);
    if (unlikely(ptr == NULL))
        abort ();
    return ptr;
}

1177 1178 1179
/*****************************************************************************
 * libvlc features
 *****************************************************************************/
1180 1181 1182
VLC_API const char * VLC_CompileBy( void ) VLC_USED;
VLC_API const char * VLC_CompileHost( void ) VLC_USED;
VLC_API const char * VLC_Compiler( void ) VLC_USED;
1183

1184
/*****************************************************************************
1185
 * Additional vlc stuff
1186
 *****************************************************************************/
1187
#include "vlc_messages.h"
Sam Hocevar's avatar
Sam Hocevar committed
1188
#include "vlc_objects.h"
1189
#include "vlc_variables.h"
1190
#include "vlc_configuration.h"
1191

1192
#if defined( _WIN32 ) || defined( __OS2__ )
1193
#   define DIR_SEP_CHAR '\\'
1194
#   define DIR_SEP "\\"
1195 1196
#   define PATH_SEP_CHAR ';'
#   define PATH_SEP ";"
1197
#else
1198
#   define DIR_SEP_CHAR '/'
1199
#   define DIR_SEP "/"
1200 1201
#   define PATH_SEP_CHAR ':'
#   define PATH_SEP ":"
1202
#endif
1203

1204 1205 1206 1207 1208 1209
#define LICENSE_MSG \
  _("This program comes with NO WARRANTY, to the extent permitted by " \
    "law.\nYou may redistribute it under the terms of the GNU General " \
    "Public License;\nsee the file named COPYING for details.\n" \
    "Written by the VideoLAN team; see the AUTHORS file.\n")

1210
#endif /* !VLC_COMMON_H */