live555: dtsgen: reset depth on seek
[vlc.git] / include / vlc_common.h
blob4f2d0baf522257c98c83a539341ea8097bfd1821
1 /*****************************************************************************
2 * vlc_common.h: common definitions
3 * Collection of useful common types and macros definitions
4 *****************************************************************************
5 * Copyright (C) 1998-2011 VLC authors and VideoLAN
7 * Authors: Samuel Hocevar <sam@via.ecp.fr>
8 * Vincent Seguin <seguin@via.ecp.fr>
9 * Gildas Bazin <gbazin@videolan.org>
10 * RĂ©mi Denis-Courmont
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 #ifndef VLC_COMMON_H
28 # define VLC_COMMON_H 1
30 /**
31 * \defgroup vlc VLC plug-in programming interface
32 * \file
33 * \ingroup vlc
34 * This file is a collection of common definitions and types
37 /*****************************************************************************
38 * Required vlc headers
39 *****************************************************************************/
40 #include "vlc_config.h"
42 /*****************************************************************************
43 * Required system headers
44 *****************************************************************************/
45 #include <stdlib.h>
46 #include <stdarg.h>
48 #include <string.h>
49 #include <stdio.h>
50 #include <inttypes.h>
51 #include <stddef.h>
53 #ifndef __cplusplus
54 # include <stdbool.h>
55 #endif
57 /**
58 * \defgroup cext C programming language extensions
59 * \ingroup vlc
61 * This section defines a number of macros and inline functions extending the
62 * C language. Most extensions are implemented by GCC and LLVM/Clang, and have
63 * unoptimized fallbacks for other C11/C++11 conforming compilers.
64 * @{
66 #ifdef __GNUC__
67 # define VLC_GCC_VERSION(maj,min) \
68 ((__GNUC__ > (maj)) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
69 #else
70 /** GCC version check */
71 # define VLC_GCC_VERSION(maj,min) (0)
72 #endif
74 /* Try to fix format strings for all versions of mingw and mingw64 */
75 #if defined( _WIN32 ) && defined( __USE_MINGW_ANSI_STDIO )
76 #undef PRId64
77 #define PRId64 "lld"
78 #undef PRIi64
79 #define PRIi64 "lli"
80 #undef PRIu64
81 #define PRIu64 "llu"
82 #undef PRIo64
83 #define PRIo64 "llo"
84 #undef PRIx64
85 #define PRIx64 "llx"
86 #define snprintf __mingw_snprintf
87 #define vsnprintf __mingw_vsnprintf
88 #define swprintf _snwprintf
89 #endif
91 /* Function attributes for compiler warnings */
92 #ifdef __GNUC__
93 # define VLC_DEPRECATED __attribute__((deprecated))
94 # if VLC_GCC_VERSION(6,0)
95 # define VLC_DEPRECATED_ENUM __attribute__((deprecated))
96 # else
97 # define VLC_DEPRECATED_ENUM
98 # endif
100 # if defined( _WIN32 ) && !defined( __clang__ )
101 # define VLC_FORMAT(x,y) __attribute__ ((format(gnu_printf,x,y)))
102 # else
103 # define VLC_FORMAT(x,y) __attribute__ ((format(printf,x,y)))
104 # endif
105 # define VLC_FORMAT_ARG(x) __attribute__ ((format_arg(x)))
106 # define VLC_MALLOC __attribute__ ((malloc))
107 # define VLC_USED __attribute__ ((warn_unused_result))
109 #else
111 * Deprecated functions or compound members annotation
113 * Use this macro in front of a function declaration or compound member
114 * within a compound type declaration.
115 * The compiler may emit a warning every time the function or member is used.
117 * Use \ref VLC_DEPRECATED_ENUM instead for enumeration members.
119 # define VLC_DEPRECATED
122 * Deprecated enum member annotation
124 * Use this macro after an enumerated type member declaration.
125 * The compiler may emit a warning every time the enumeration member is used.
127 * See also \ref VLC_DEPRECATED.
129 # define VLC_DEPRECATED_ENUM
132 * String format function annotation
134 * Use this macro after a function prototype/declaration if the function
135 * expects a standard C format string. This helps compiler diagnostics.
137 * @param x the position (starting from 1) of the format string argument
138 * @param y the first position (also starting from 1) of the variable arguments
139 * following the format string (usually but not always \p x+1).
141 # define VLC_FORMAT(x,y)
144 * Format string translation function annotation
146 * Use this macro after a function prototype/declaration if the function
147 * expects a format string as input and returns another format string as output
148 * to another function.
150 * This is primarily intended for localization functions such as gettext().
152 # define VLC_FORMAT_ARG(x)
155 * Heap allocated result function annotation
157 * Use this macro to annotate a function that returns a pointer to memory that
158 * cannot alias any other valid pointer.
160 * This is primarily used for functions that return a pointer to heap-allocated
161 * memory, but it can be used for other applicable purposes.
163 * \warning Do not use this annotation if the returned pointer can in any way
164 * alias a valid pointer at the time the function exits. This could lead to
165 * very weird memory corruption bugs.
167 # define VLC_MALLOC
170 * Used result function annotation
172 * Use this macro to annotate a function whose result must be used.
174 * There are several cases where this is useful:
175 * - If a function has no side effects (or no useful side effects), such that
176 * the only useful purpose of calling said function is to obtain its
177 * return value.
178 * - If ignoring the function return value would lead to a resource leak
179 * (including but not limited to a memory leak).
180 * - If a function cannot be used correctly without checking its return value.
181 * For instance, if the function can fail at any time.
183 * The compiler may warn if the return value of a function call is ignored.
185 # define VLC_USED
186 #endif
188 #ifdef __ELF__
189 # define VLC_WEAK __attribute__((weak))
190 #else
192 * Weak symbol annotation
194 * Use this macro before an external identifier \b definition to mark it as a
195 * weak symbol. A weak symbol can be overriden by another symbol of the same
196 * name at the link time.
198 # define VLC_WEAK
199 #endif
201 /* Branch prediction */
202 #if defined (__GNUC__) || defined (__clang__)
203 # define likely(p) __builtin_expect(!!(p), 1)
204 # define unlikely(p) __builtin_expect(!!(p), 0)
205 # define unreachable() __builtin_unreachable()
206 #else
208 * Predicted true condition
210 * This macro indicates that the condition is expected most often true.
211 * The compiler may optimize the code assuming that this condition is usually
212 * met.
214 # define likely(p) (!!(p))
217 * Predicted false condition
219 * This macro indicates that the condition is expected most often false.
220 * The compiler may optimize the code assuming that this condition is rarely
221 * met.
223 # define unlikely(p) (!!(p))
226 * Impossible branch
228 * This macro indicates that the branch cannot be reached at run-time, and
229 * represents undefined behaviour.
230 * The compiler may optimize the code assuming that the call flow will never
231 * logically reach the point where this macro is expanded.
233 * See also \ref vlc_assert_unreachable.
235 # define unreachable() ((void)0)
236 #endif
239 * Impossible branch assertion
241 * This macro asserts that the branch cannot be reached at run-time.
243 * If the branch is reached in a debug build, it will trigger an assertion
244 * failure and abnormal program termination.
246 * If the branch is reached in a non-debug build, this macro is equivalent to
247 * \ref unreachable and the behaviour is undefined.
249 #define vlc_assert_unreachable() (vlc_assert(!"unreachable"), unreachable())
252 * Run-time assertion
254 * This macro performs a run-time assertion if C assertions are enabled
255 * and the following preprocessor symbol is defined:
256 * @verbatim __LIBVLC__ @endverbatim
257 * That restriction ensures that assertions in public header files are not
258 * unwittingly <i>leaked</i> to externally-compiled plug-ins
259 * including those header files.
261 * Within the LibVLC code base, this is exactly the same as assert(), which can
262 * and probably should be used directly instead.
264 #ifdef __LIBVLC__
265 # define vlc_assert(pred) assert(pred)
266 #else
267 # define vlc_assert(pred) ((void)0)
268 #endif
270 /* Linkage */
271 #ifdef __cplusplus
272 # define VLC_EXTERN extern "C"
273 #else
274 # define VLC_EXTERN
275 #endif
277 #if defined (_WIN32) && defined (DLL_EXPORT)
278 # define VLC_EXPORT __declspec(dllexport)
279 #elif defined (__GNUC__)
280 # define VLC_EXPORT __attribute__((visibility("default")))
281 #else
282 # define VLC_EXPORT
283 #endif
286 * Exported API call annotation
288 * This macro is placed before a function declaration to indicate that the
289 * function is an API call of the LibVLC plugin API.
291 #define VLC_API VLC_EXTERN VLC_EXPORT
293 /** @} */
295 /*****************************************************************************
296 * Basic types definitions
297 *****************************************************************************/
299 * The vlc_fourcc_t type.
301 * See http://www.webartz.com/fourcc/ for a very detailed list.
303 typedef uint32_t vlc_fourcc_t;
305 #ifdef WORDS_BIGENDIAN
306 # define VLC_FOURCC( a, b, c, d ) \
307 ( ((uint32_t)d) | ( ((uint32_t)c) << 8 ) \
308 | ( ((uint32_t)b) << 16 ) | ( ((uint32_t)a) << 24 ) )
309 # define VLC_TWOCC( a, b ) \
310 ( (uint16_t)(b) | ( (uint16_t)(a) << 8 ) )
312 #else
313 # define VLC_FOURCC( a, b, c, d ) \
314 ( ((uint32_t)a) | ( ((uint32_t)b) << 8 ) \
315 | ( ((uint32_t)c) << 16 ) | ( ((uint32_t)d) << 24 ) )
316 # define VLC_TWOCC( a, b ) \
317 ( (uint16_t)(a) | ( (uint16_t)(b) << 8 ) )
319 #endif
322 * Translate a vlc_fourcc into its string representation. This function
323 * assumes there is enough room in psz_fourcc to store 4 characters in.
325 * \param fcc a vlc_fourcc_t
326 * \param psz_fourcc string to store string representation of vlc_fourcc in
328 static inline void vlc_fourcc_to_char( vlc_fourcc_t fcc, char *psz_fourcc )
330 memcpy( psz_fourcc, &fcc, 4 );
333 /*****************************************************************************
334 * Classes declaration
335 *****************************************************************************/
337 /* Internal types */
338 typedef struct vlc_object_t vlc_object_t;
339 typedef struct libvlc_int_t libvlc_int_t;
340 typedef struct date_t date_t;
342 /* Playlist */
344 typedef struct playlist_t playlist_t;
345 typedef struct playlist_item_t playlist_item_t;
346 typedef struct services_discovery_t services_discovery_t;
347 typedef struct vlc_renderer_discovery_t vlc_renderer_discovery_t;
348 typedef struct vlc_renderer_item_t vlc_renderer_item_t;
350 /* Modules */
351 typedef struct module_t module_t;
352 typedef struct module_config_t module_config_t;
354 typedef struct config_category_t config_category_t;
356 /* Input */
357 typedef struct input_thread_t input_thread_t;
358 typedef struct input_item_t input_item_t;
359 typedef struct input_item_node_t input_item_node_t;
360 typedef struct stream_t stream_t;
361 typedef struct stream_t demux_t;
362 typedef struct es_out_t es_out_t;
363 typedef struct es_out_id_t es_out_id_t;
364 typedef struct seekpoint_t seekpoint_t;
365 typedef struct info_t info_t;
366 typedef struct info_category_t info_category_t;
367 typedef struct input_attachment_t input_attachment_t;
369 /* Format */
370 typedef struct audio_format_t audio_format_t;
371 typedef struct video_format_t video_format_t;
372 typedef struct subs_format_t subs_format_t;
373 typedef struct es_format_t es_format_t;
374 typedef struct video_palette_t video_palette_t;
376 /* Audio */
377 typedef struct audio_output audio_output_t;
378 typedef audio_format_t audio_sample_format_t;
380 /* Video */
381 typedef struct vout_thread_t vout_thread_t;
382 typedef struct vlc_viewpoint_t vlc_viewpoint_t;
384 typedef video_format_t video_frame_format_t;
385 typedef struct picture_t picture_t;
387 /* Subpictures */
388 typedef struct spu_t spu_t;
389 typedef struct subpicture_t subpicture_t;
390 typedef struct subpicture_region_t subpicture_region_t;
392 typedef struct image_handler_t image_handler_t;
394 /* Stream output */
395 typedef struct sout_instance_t sout_instance_t;
397 typedef struct sout_input_t sout_input_t;
398 typedef struct sout_packetizer_input_t sout_packetizer_input_t;
400 typedef struct sout_access_out_t sout_access_out_t;
402 typedef struct sout_mux_t sout_mux_t;
404 typedef struct sout_stream_t sout_stream_t;
406 typedef struct config_chain_t config_chain_t;
407 typedef struct session_descriptor_t session_descriptor_t;
409 /* Decoders */
410 typedef struct decoder_t decoder_t;
411 typedef struct decoder_synchro_t decoder_synchro_t;
413 /* Encoders */
414 typedef struct encoder_t encoder_t;
416 /* Filters */
417 typedef struct filter_t filter_t;
419 /* Network */
420 typedef struct vlc_url_t vlc_url_t;
422 /* Misc */
423 typedef struct iso639_lang_t iso639_lang_t;
425 /* block */
426 typedef struct block_t block_t;
427 typedef struct block_fifo_t block_fifo_t;
429 /* Hashing */
430 typedef struct md5_s md5_t;
432 /* XML */
433 typedef struct xml_t xml_t;
434 typedef struct xml_reader_t xml_reader_t;
436 /* vod server */
437 typedef struct vod_t vod_t;
438 typedef struct vod_media_t vod_media_t;
440 /* VLM */
441 typedef struct vlm_t vlm_t;
442 typedef struct vlm_message_t vlm_message_t;
444 /* misc */
445 typedef struct vlc_meta_t vlc_meta_t;
446 typedef struct input_stats_t input_stats_t;
447 typedef struct addon_entry_t addon_entry_t;
449 /* Update */
450 typedef struct update_t update_t;
453 * VLC value structure
455 typedef union
457 int64_t i_int;
458 bool b_bool;
459 float f_float;
460 char * psz_string;
461 void * p_address;
462 struct { int32_t x; int32_t y; } coords;
464 } vlc_value_t;
466 /*****************************************************************************
467 * Error values (shouldn't be exposed)
468 *****************************************************************************/
469 #define VLC_SUCCESS (-0) /**< No error */
470 #define VLC_EGENERIC (-1) /**< Unspecified error */
471 #define VLC_ENOMEM (-2) /**< Not enough memory */
472 #define VLC_ETIMEOUT (-3) /**< Timeout */
473 #define VLC_ENOMOD (-4) /**< Module not found */
474 #define VLC_ENOOBJ (-5) /**< Object not found */
475 #define VLC_ENOVAR (-6) /**< Variable not found */
476 #define VLC_EBADVAR (-7) /**< Bad variable value */
477 #define VLC_ENOITEM (-8) /**< Item not found */
479 /*****************************************************************************
480 * Variable callbacks: called when the value is modified
481 *****************************************************************************/
482 typedef int ( * vlc_callback_t ) ( vlc_object_t *, /* variable's object */
483 char const *, /* variable name */
484 vlc_value_t, /* old value */
485 vlc_value_t, /* new value */
486 void * ); /* callback data */
488 /*****************************************************************************
489 * List callbacks: called when elements are added/removed from the list
490 *****************************************************************************/
491 typedef int ( * vlc_list_callback_t ) ( vlc_object_t *, /* variable's object */
492 char const *, /* variable name */
493 int, /* VLC_VAR_* action */
494 vlc_value_t *, /* new/deleted value */
495 void *); /* callback data */
497 /*****************************************************************************
498 * OS-specific headers and thread types
499 *****************************************************************************/
500 #if defined( _WIN32 )
501 # include <malloc.h>
502 # ifndef PATH_MAX
503 # define PATH_MAX MAX_PATH
504 # endif
505 # include <windows.h>
506 #endif
508 #ifdef __APPLE__
509 #include <sys/syslimits.h>
510 #include <AvailabilityMacros.h>
511 #endif
513 #ifdef __OS2__
514 # define OS2EMX_PLAIN_CHAR
515 # define INCL_BASE
516 # define INCL_PM
517 # include <os2safe.h>
518 # include <os2.h>
519 #endif
521 #include "vlc_tick.h"
522 #include "vlc_threads.h"
525 * \defgroup intops Integer operations
526 * \ingroup cext
528 * Common integer functions.
529 * @{
531 /* __MAX and __MIN: self explanatory */
532 #ifndef __MAX
533 # define __MAX(a, b) ( ((a) > (b)) ? (a) : (b) )
534 #endif
535 #ifndef __MIN
536 # define __MIN(a, b) ( ((a) < (b)) ? (a) : (b) )
537 #endif
539 /* clip v in [min, max] */
540 #define VLC_CLIP(v, min, max) __MIN(__MAX((v), (min)), (max))
542 /** Greatest common divisor */
543 VLC_USED
544 static inline int64_t GCD ( int64_t a, int64_t b )
546 while( b )
548 int64_t c = a % b;
549 a = b;
550 b = c;
552 return a;
555 /* function imported from libavutil/common.h */
556 VLC_USED
557 static inline uint8_t clip_uint8_vlc( int32_t a )
559 if( a&(~255) ) return (-a)>>31;
560 else return a;
564 * \defgroup bitops Bit operations
565 * @{
568 #define VLC_INT_FUNC(basename) \
569 VLC_INT_FUNC_TYPE(basename, unsigned, ) \
570 VLC_INT_FUNC_TYPE(basename, unsigned long, l) \
571 VLC_INT_FUNC_TYPE(basename, unsigned long long, ll)
573 #if defined (__GNUC__) || defined (__clang__)
574 # define VLC_INT_FUNC_TYPE(basename,type,suffix) \
575 VLC_USED static inline int vlc_##basename##suffix(type x) \
577 return __builtin_##basename##suffix(x); \
580 VLC_INT_FUNC(clz)
581 #else
582 VLC_USED static inline int vlc_clzll(unsigned long long x)
584 int i = sizeof (x) * 8;
586 while (x)
588 x >>= 1;
589 i--;
591 return i;
594 VLC_USED static inline int vlc_clzl(unsigned long x)
596 return vlc_clzll(x) - ((sizeof (long long) - sizeof (long)) * 8);
599 VLC_USED static inline int vlc_clz(unsigned x)
601 return vlc_clzll(x) - ((sizeof (long long) - sizeof (int)) * 8);
604 VLC_USED static inline int vlc_ctz_generic(unsigned long long x)
606 unsigned i = sizeof (x) * 8;
608 while (x)
610 x <<= 1;
611 i--;
613 return i;
616 VLC_USED static inline int vlc_parity_generic(unsigned long long x)
618 for (unsigned i = 4 * sizeof (x); i > 0; i /= 2)
619 x ^= x >> i;
620 return x & 1;
623 VLC_USED static inline int vlc_popcount_generic(unsigned long long x)
625 int count = 0;
626 while (x)
628 count += x & 1;
629 x = x >> 1;
631 return count;
634 # define VLC_INT_FUNC_TYPE(basename,type,suffix) \
635 VLC_USED static inline int vlc_##basename##suffix(type x) \
637 return vlc_##basename##_generic(x); \
639 #endif
641 VLC_INT_FUNC(ctz)
642 VLC_INT_FUNC(parity)
643 VLC_INT_FUNC(popcount)
645 #ifndef __cplusplus
646 # define VLC_INT_GENERIC(func,x) \
647 _Generic((x), \
648 unsigned char: func(x), \
649 signed char: func(x), \
650 unsigned short: func(x), \
651 signed short: func(x), \
652 unsigned int: func(x), \
653 signed int: func(x), \
654 unsigned long: func##l(x), \
655 signed long: func##l(x), \
656 unsigned long long: func##ll(x), \
657 signed long long: func##ll(x))
660 * Count leading zeroes
662 * This function counts the number of consecutive zero (clear) bits
663 * down from the highest order bit in an unsigned integer.
665 * \param x a non-zero integer
666 * \note This macro assumes that CHAR_BIT equals 8.
667 * \warning By definition, the result depends on the (width of the) type of x.
668 * \return The number of leading zero bits in x.
670 # define clz(x) \
671 _Generic((x), \
672 unsigned char: (vlc_clz(x) - (sizeof (unsigned) - 1) * 8), \
673 unsigned short: (vlc_clz(x) \
674 - (sizeof (unsigned) - sizeof (unsigned short)) * 8), \
675 unsigned: vlc_clz(x), \
676 unsigned long: vlc_clzl(x), \
677 unsigned long long: vlc_clzll(x))
680 * Count trailing zeroes
682 * This function counts the number of consecutive zero bits
683 * up from the lowest order bit in an unsigned integer.
685 * \param x a non-zero integer
686 * \note This function assumes that CHAR_BIT equals 8.
687 * \return The number of trailing zero bits in x.
689 # define ctz(x) VLC_INT_GENERIC(vlc_ctz, x)
692 * Parity
694 * This function determines the parity of an integer.
695 * \retval 0 if x has an even number of set bits.
696 * \retval 1 if x has an odd number of set bits.
698 # define parity(x) VLC_INT_GENERIC(vlc_parity, x)
701 * Bit weight / population count
703 * This function counts the number of non-zero bits in an integer.
705 * \return The count of non-zero bits.
707 # define vlc_popcount(x) \
708 _Generic((x), \
709 signed char: vlc_popcount((unsigned char)(x)), \
710 signed short: vlc_popcount((unsigned short)(x)), \
711 default: VLC_INT_GENERIC(vlc_popcount ,x))
712 #else
713 VLC_USED static inline int vlc_popcount(unsigned char x)
715 return vlc_popcount((unsigned)x);
718 VLC_USED static inline int vlc_popcount(unsigned short x)
720 return vlc_popcount((unsigned)x);
723 VLC_USED static inline int vlc_popcount(unsigned long x)
725 return vlc_popcountl(x);
728 VLC_USED static inline int vlc_popcount(unsigned long long x)
730 return vlc_popcountll(x);
732 #endif
734 /** Byte swap (16 bits) */
735 VLC_USED
736 static inline uint16_t vlc_bswap16(uint16_t x)
738 return (x << 8) | (x >> 8);
741 /** Byte swap (32 bits) */
742 VLC_USED
743 static inline uint32_t vlc_bswap32(uint32_t x)
745 #if defined (__GNUC__) || defined(__clang__)
746 return __builtin_bswap32 (x);
747 #else
748 return ((x & 0x000000FF) << 24)
749 | ((x & 0x0000FF00) << 8)
750 | ((x & 0x00FF0000) >> 8)
751 | ((x & 0xFF000000) >> 24);
752 #endif
755 /** Byte swap (64 bits) */
756 VLC_USED
757 static inline uint64_t vlc_bswap64(uint64_t x)
759 #if defined (__GNUC__) || defined(__clang__)
760 return __builtin_bswap64 (x);
761 #elif !defined (__cplusplus)
762 return ((x & 0x00000000000000FF) << 56)
763 | ((x & 0x000000000000FF00) << 40)
764 | ((x & 0x0000000000FF0000) << 24)
765 | ((x & 0x00000000FF000000) << 8)
766 | ((x & 0x000000FF00000000) >> 8)
767 | ((x & 0x0000FF0000000000) >> 24)
768 | ((x & 0x00FF000000000000) >> 40)
769 | ((x & 0xFF00000000000000) >> 56);
770 #else
771 return ((x & 0x00000000000000FFULL) << 56)
772 | ((x & 0x000000000000FF00ULL) << 40)
773 | ((x & 0x0000000000FF0000ULL) << 24)
774 | ((x & 0x00000000FF000000ULL) << 8)
775 | ((x & 0x000000FF00000000ULL) >> 8)
776 | ((x & 0x0000FF0000000000ULL) >> 24)
777 | ((x & 0x00FF000000000000ULL) >> 40)
778 | ((x & 0xFF00000000000000ULL) >> 56);
779 #endif
781 /** @} */
784 * \defgroup overflow Overflowing arithmetic
785 * @{
787 static inline bool uadd_overflow(unsigned a, unsigned b, unsigned *res)
789 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
790 return __builtin_uadd_overflow(a, b, res);
791 #else
792 *res = a + b;
793 return (a + b) < a;
794 #endif
797 static inline bool uaddl_overflow(unsigned long a, unsigned long b,
798 unsigned long *res)
800 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
801 return __builtin_uaddl_overflow(a, b, res);
802 #else
803 *res = a + b;
804 return (a + b) < a;
805 #endif
808 static inline bool uaddll_overflow(unsigned long long a, unsigned long long b,
809 unsigned long long *res)
811 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
812 return __builtin_uaddll_overflow(a, b, res);
813 #else
814 *res = a + b;
815 return (a + b) < a;
816 #endif
819 #ifndef __cplusplus
821 * Overflowing addition
823 * Converts \p a and \p b to the type of \p *r.
824 * Then computes the sum of both conversions while checking for overflow.
825 * Finally stores the result in \p *r.
827 * \param a an integer
828 * \param b an integer
829 * \param r a pointer to an integer [OUT]
830 * \retval false The sum did not overflow.
831 * \retval true The sum overflowed.
833 # define add_overflow(a,b,r) \
834 _Generic(*(r), \
835 unsigned: uadd_overflow(a, b, (unsigned *)(r)), \
836 unsigned long: uaddl_overflow(a, b, (unsigned long *)(r)), \
837 unsigned long long: uaddll_overflow(a, b, (unsigned long long *)(r)))
838 #else
839 static inline bool add_overflow(unsigned a, unsigned b, unsigned *res)
841 return uadd_overflow(a, b, res);
844 static inline bool add_overflow(unsigned long a, unsigned long b,
845 unsigned long *res)
847 return uaddl_overflow(a, b, res);
850 static inline bool add_overflow(unsigned long long a, unsigned long long b,
851 unsigned long long *res)
853 return uaddll_overflow(a, b, res);
855 #endif
857 #if !(VLC_GCC_VERSION(5,0) || defined(__clang__))
858 # include <limits.h>
859 #endif
861 static inline bool umul_overflow(unsigned a, unsigned b, unsigned *res)
863 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
864 return __builtin_umul_overflow(a, b, res);
865 #else
866 *res = a * b;
867 return b > 0 && a > (UINT_MAX / b);
868 #endif
871 static inline bool umull_overflow(unsigned long a, unsigned long b,
872 unsigned long *res)
874 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
875 return __builtin_umull_overflow(a, b, res);
876 #else
877 *res = a * b;
878 return b > 0 && a > (ULONG_MAX / b);
879 #endif
882 static inline bool umulll_overflow(unsigned long long a, unsigned long long b,
883 unsigned long long *res)
885 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
886 return __builtin_umulll_overflow(a, b, res);
887 #else
888 *res = a * b;
889 return b > 0 && a > (ULLONG_MAX / b);
890 #endif
893 #ifndef __cplusplus
895 * Overflowing multiplication
897 * Converts \p a and \p b to the type of \p *r.
898 * Then computes the product of both conversions while checking for overflow.
899 * Finally stores the result in \p *r.
901 * \param a an integer
902 * \param b an integer
903 * \param r a pointer to an integer [OUT]
904 * \retval false The product did not overflow.
905 * \retval true The product overflowed.
907 #define mul_overflow(a,b,r) \
908 _Generic(*(r), \
909 unsigned: umul_overflow(a, b, (unsigned *)(r)), \
910 unsigned long: umull_overflow(a, b, (unsigned long *)(r)), \
911 unsigned long long: umulll_overflow(a, b, (unsigned long long *)(r)))
912 #else
913 static inline bool mul_overflow(unsigned a, unsigned b, unsigned *res)
915 return umul_overflow(a, b, res);
918 static inline bool mul_overflow(unsigned long a, unsigned long b,
919 unsigned long *res)
921 return umull_overflow(a, b, res);
924 static inline bool mul_overflow(unsigned long long a, unsigned long long b,
925 unsigned long long *res)
927 return umulll_overflow(a, b, res);
929 #endif
930 /** @} */
931 /** @} */
933 /* Free and set set the variable to NULL */
934 #define FREENULL(a) do { free( a ); a = NULL; } while(0)
936 #define EMPTY_STR(str) (!str || !*str)
938 VLC_API char const * vlc_error( int ) VLC_USED;
940 #include <vlc_arrays.h>
942 /* MSB (big endian)/LSB (little endian) conversions - network order is always
943 * MSB, and should be used for both network communications and files. */
945 #ifdef WORDS_BIGENDIAN
946 # define hton16(i) ((uint16_t)(i))
947 # define hton32(i) ((uint32_t)(i))
948 # define hton64(i) ((uint64_t)(i))
949 #else
950 # define hton16(i) vlc_bswap16(i)
951 # define hton32(i) vlc_bswap32(i)
952 # define hton64(i) vlc_bswap64(i)
953 #endif
954 #define ntoh16(i) hton16(i)
955 #define ntoh32(i) hton32(i)
956 #define ntoh64(i) hton64(i)
958 /** Reads 16 bits in network byte order */
959 VLC_USED
960 static inline uint16_t U16_AT (const void *p)
962 uint16_t x;
964 memcpy (&x, p, sizeof (x));
965 return ntoh16 (x);
968 /** Reads 32 bits in network byte order */
969 VLC_USED
970 static inline uint32_t U32_AT (const void *p)
972 uint32_t x;
974 memcpy (&x, p, sizeof (x));
975 return ntoh32 (x);
978 /** Reads 64 bits in network byte order */
979 VLC_USED
980 static inline uint64_t U64_AT (const void *p)
982 uint64_t x;
984 memcpy (&x, p, sizeof (x));
985 return ntoh64 (x);
988 #define GetWBE(p) U16_AT(p)
989 #define GetDWBE(p) U32_AT(p)
990 #define GetQWBE(p) U64_AT(p)
992 /** Reads 16 bits in little-endian order */
993 VLC_USED
994 static inline uint16_t GetWLE (const void *p)
996 uint16_t x;
998 memcpy (&x, p, sizeof (x));
999 #ifdef WORDS_BIGENDIAN
1000 x = vlc_bswap16 (x);
1001 #endif
1002 return x;
1005 /** Reads 32 bits in little-endian order */
1006 VLC_USED
1007 static inline uint32_t GetDWLE (const void *p)
1009 uint32_t x;
1011 memcpy (&x, p, sizeof (x));
1012 #ifdef WORDS_BIGENDIAN
1013 x = vlc_bswap32 (x);
1014 #endif
1015 return x;
1018 /** Reads 64 bits in little-endian order */
1019 VLC_USED
1020 static inline uint64_t GetQWLE (const void *p)
1022 uint64_t x;
1024 memcpy (&x, p, sizeof (x));
1025 #ifdef WORDS_BIGENDIAN
1026 x = vlc_bswap64 (x);
1027 #endif
1028 return x;
1031 /** Writes 16 bits in network byte order */
1032 static inline void SetWBE (void *p, uint16_t w)
1034 w = hton16 (w);
1035 memcpy (p, &w, sizeof (w));
1038 /** Writes 32 bits in network byte order */
1039 static inline void SetDWBE (void *p, uint32_t dw)
1041 dw = hton32 (dw);
1042 memcpy (p, &dw, sizeof (dw));
1045 /** Writes 64 bits in network byte order */
1046 static inline void SetQWBE (void *p, uint64_t qw)
1048 qw = hton64 (qw);
1049 memcpy (p, &qw, sizeof (qw));
1052 /** Writes 16 bits in little endian order */
1053 static inline void SetWLE (void *p, uint16_t w)
1055 #ifdef WORDS_BIGENDIAN
1056 w = vlc_bswap16 (w);
1057 #endif
1058 memcpy (p, &w, sizeof (w));
1061 /** Writes 32 bits in little endian order */
1062 static inline void SetDWLE (void *p, uint32_t dw)
1064 #ifdef WORDS_BIGENDIAN
1065 dw = vlc_bswap32 (dw);
1066 #endif
1067 memcpy (p, &dw, sizeof (dw));
1070 /** Writes 64 bits in little endian order */
1071 static inline void SetQWLE (void *p, uint64_t qw)
1073 #ifdef WORDS_BIGENDIAN
1074 qw = vlc_bswap64 (qw);
1075 #endif
1076 memcpy (p, &qw, sizeof (qw));
1079 /* */
1080 #define VLC_UNUSED(x) (void)(x)
1082 /* Stuff defined in src/extras/libc.c */
1084 #if defined(_WIN32)
1085 /* several type definitions */
1086 # if defined( __MINGW32__ )
1087 # if !defined( _OFF_T_ )
1088 typedef long long _off_t;
1089 typedef _off_t off_t;
1090 # define _OFF_T_
1091 # else
1092 # ifdef off_t
1093 # undef off_t
1094 # endif
1095 # define off_t long long
1096 # endif
1097 # endif
1099 # ifndef O_NONBLOCK
1100 # define O_NONBLOCK 0
1101 # endif
1103 # include <tchar.h>
1104 #endif /* _WIN32 */
1106 typedef struct {
1107 unsigned num, den;
1108 } vlc_rational_t;
1110 VLC_API bool vlc_ureduce( unsigned *, unsigned *, uint64_t, uint64_t, uint64_t );
1112 #define container_of(ptr, type, member) \
1113 ((type *)(((char *)(ptr)) - offsetof(type, member)))
1115 VLC_USED VLC_MALLOC
1116 static inline void *vlc_alloc(size_t count, size_t size)
1118 return mul_overflow(count, size, &size) ? NULL : malloc(size);
1121 /*****************************************************************************
1122 * I18n stuff
1123 *****************************************************************************/
1124 VLC_API const char *vlc_gettext(const char *msgid) VLC_FORMAT_ARG(1);
1125 VLC_API const char *vlc_ngettext(const char *s, const char *p, unsigned long n)
1126 VLC_FORMAT_ARG(1) VLC_FORMAT_ARG(2);
1128 #define vlc_pgettext( ctx, id ) \
1129 vlc_pgettext_aux( ctx "\004" id, id )
1131 VLC_FORMAT_ARG(2)
1132 static inline const char *vlc_pgettext_aux( const char *ctx, const char *id )
1134 const char *tr = vlc_gettext( ctx );
1135 return (tr == ctx) ? id : tr;
1138 /*****************************************************************************
1139 * Loosy memory allocation functions. Do not use in new code.
1140 *****************************************************************************/
1141 static inline void *xmalloc(size_t len)
1143 void *ptr = malloc(len);
1144 if (unlikely(ptr == NULL && len > 0))
1145 abort();
1146 return ptr;
1149 static inline void *xrealloc(void *ptr, size_t len)
1151 void *nptr = realloc(ptr, len);
1152 if (unlikely(nptr == NULL && len > 0))
1153 abort();
1154 return nptr;
1157 static inline void *xcalloc(size_t n, size_t size)
1159 void *ptr = calloc(n, size);
1160 if (unlikely(ptr == NULL && (n > 0 || size > 0)))
1161 abort ();
1162 return ptr;
1165 static inline char *xstrdup (const char *str)
1167 char *ptr = strdup (str);
1168 if (unlikely(ptr == NULL))
1169 abort ();
1170 return ptr;
1173 /*****************************************************************************
1174 * libvlc features
1175 *****************************************************************************/
1176 VLC_API const char * VLC_CompileBy( void ) VLC_USED;
1177 VLC_API const char * VLC_CompileHost( void ) VLC_USED;
1178 VLC_API const char * VLC_Compiler( void ) VLC_USED;
1180 /*****************************************************************************
1181 * Additional vlc stuff
1182 *****************************************************************************/
1183 #include "vlc_messages.h"
1184 #include "vlc_objects.h"
1185 #include "vlc_variables.h"
1186 #include "vlc_configuration.h"
1188 #if defined( _WIN32 ) || defined( __OS2__ )
1189 # define DIR_SEP_CHAR '\\'
1190 # define DIR_SEP "\\"
1191 # define PATH_SEP_CHAR ';'
1192 # define PATH_SEP ";"
1193 #else
1194 # define DIR_SEP_CHAR '/'
1195 # define DIR_SEP "/"
1196 # define PATH_SEP_CHAR ':'
1197 # define PATH_SEP ":"
1198 #endif
1200 #define LICENSE_MSG \
1201 _("This program comes with NO WARRANTY, to the extent permitted by " \
1202 "law.\nYou may redistribute it under the terms of the GNU General " \
1203 "Public License;\nsee the file named COPYING for details.\n" \
1204 "Written by the VideoLAN team; see the AUTHORS file.\n")
1206 #endif /* !VLC_COMMON_H */