Contribs: update dvbpsi to 1.3.2
[vlc.git] / include / vlc_common.h
blobda334710afe3e63686f2fc19459d4778db1f721d
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() (assert(!"unreachable"), unreachable())
251 /* Linkage */
252 #ifdef __cplusplus
253 # define VLC_EXTERN extern "C"
254 #else
255 # define VLC_EXTERN
256 #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 #endif
267 * Exported API call annotation
269 * This macro is placed before a function declaration to indicate that the
270 * function is an API call of the LibVLC plugin API.
272 #define VLC_API VLC_EXTERN VLC_EXPORT
274 /** @} */
276 /*****************************************************************************
277 * Basic types definitions
278 *****************************************************************************/
280 * High precision date or time interval
282 * Store a high precision date or time interval. The maximum precision is the
283 * 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 * video). Dates are stored as microseconds since a common date (usually the
286 * epoch). Note that date and time intervals can be manipulated using regular
287 * arithmetic operators, and that no special functions are required.
289 typedef int64_t mtime_t;
292 * The vlc_fourcc_t type.
294 * See http://www.webartz.com/fourcc/ for a very detailed list.
296 typedef uint32_t vlc_fourcc_t;
298 #ifdef WORDS_BIGENDIAN
299 # define VLC_FOURCC( a, b, c, d ) \
300 ( ((uint32_t)d) | ( ((uint32_t)c) << 8 ) \
301 | ( ((uint32_t)b) << 16 ) | ( ((uint32_t)a) << 24 ) )
302 # define VLC_TWOCC( a, b ) \
303 ( (uint16_t)(b) | ( (uint16_t)(a) << 8 ) )
305 #else
306 # define VLC_FOURCC( a, b, c, d ) \
307 ( ((uint32_t)a) | ( ((uint32_t)b) << 8 ) \
308 | ( ((uint32_t)c) << 16 ) | ( ((uint32_t)d) << 24 ) )
309 # define VLC_TWOCC( a, b ) \
310 ( (uint16_t)(a) | ( (uint16_t)(b) << 8 ) )
312 #endif
315 * Translate a vlc_fourcc into its string representation. This function
316 * assumes there is enough room in psz_fourcc to store 4 characters in.
318 * \param fcc a vlc_fourcc_t
319 * \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 )
323 memcpy( psz_fourcc, &fcc, 4 );
326 /*****************************************************************************
327 * Classes declaration
328 *****************************************************************************/
330 /* Internal types */
331 typedef struct vlc_list_t vlc_list_t;
332 typedef struct vlc_object_t vlc_object_t;
333 typedef struct libvlc_int_t libvlc_int_t;
334 typedef struct date_t date_t;
336 /* Playlist */
338 typedef struct playlist_t playlist_t;
339 typedef struct playlist_item_t playlist_item_t;
340 typedef struct services_discovery_t services_discovery_t;
341 typedef struct vlc_renderer_discovery_t vlc_renderer_discovery_t;
342 typedef struct vlc_renderer_item_t vlc_renderer_item_t;
344 /* Modules */
345 typedef struct module_t module_t;
346 typedef struct module_config_t module_config_t;
348 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 typedef struct es_out_t es_out_t;
357 typedef struct es_out_id_t es_out_id_t;
358 typedef struct seekpoint_t seekpoint_t;
359 typedef struct info_t info_t;
360 typedef struct info_category_t info_category_t;
361 typedef struct input_attachment_t input_attachment_t;
363 /* Format */
364 typedef struct audio_format_t audio_format_t;
365 typedef struct video_format_t video_format_t;
366 typedef struct subs_format_t subs_format_t;
367 typedef struct es_format_t es_format_t;
368 typedef struct video_palette_t video_palette_t;
370 /* Audio */
371 typedef struct audio_output audio_output_t;
372 typedef audio_format_t audio_sample_format_t;
374 /* Video */
375 typedef struct vout_thread_t vout_thread_t;
376 typedef struct vlc_viewpoint_t vlc_viewpoint_t;
378 typedef video_format_t video_frame_format_t;
379 typedef struct picture_t picture_t;
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;
386 typedef struct image_handler_t image_handler_t;
388 /* Stream output */
389 typedef struct sout_instance_t sout_instance_t;
391 typedef struct sout_input_t sout_input_t;
392 typedef struct sout_packetizer_input_t sout_packetizer_input_t;
394 typedef struct sout_access_out_t sout_access_out_t;
396 typedef struct sout_mux_t sout_mux_t;
398 typedef struct sout_stream_t sout_stream_t;
400 typedef struct config_chain_t config_chain_t;
401 typedef struct session_descriptor_t session_descriptor_t;
403 /* Decoders */
404 typedef struct decoder_t decoder_t;
405 typedef struct decoder_synchro_t decoder_synchro_t;
407 /* Encoders */
408 typedef struct encoder_t encoder_t;
410 /* Filters */
411 typedef struct filter_t filter_t;
413 /* Network */
414 typedef struct vlc_url_t vlc_url_t;
416 /* Misc */
417 typedef struct iso639_lang_t iso639_lang_t;
419 /* block */
420 typedef struct block_t block_t;
421 typedef struct block_fifo_t block_fifo_t;
423 /* Hashing */
424 typedef struct md5_s md5_t;
426 /* XML */
427 typedef struct xml_t xml_t;
428 typedef struct xml_reader_t xml_reader_t;
430 /* vod server */
431 typedef struct vod_t vod_t;
432 typedef struct vod_media_t vod_media_t;
434 /* VLM */
435 typedef struct vlm_t vlm_t;
436 typedef struct vlm_message_t vlm_message_t;
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;
443 /* Update */
444 typedef struct update_t update_t;
447 * VLC value structure
449 typedef union
451 int64_t i_int;
452 bool b_bool;
453 float f_float;
454 char * psz_string;
455 void * p_address;
456 vlc_list_t * p_list;
457 struct { int32_t x; int32_t y; } coords;
459 } vlc_value_t;
462 * VLC list structure
464 struct vlc_list_t
466 int i_type;
467 int i_count;
468 vlc_value_t *p_values;
471 /*****************************************************************************
472 * Error values (shouldn't be exposed)
473 *****************************************************************************/
474 #define VLC_SUCCESS (-0) /**< No error */
475 #define VLC_EGENERIC (-1) /**< Unspecified error */
476 #define VLC_ENOMEM (-2) /**< Not enough memory */
477 #define VLC_ETIMEOUT (-3) /**< Timeout */
478 #define VLC_ENOMOD (-4) /**< Module not found */
479 #define VLC_ENOOBJ (-5) /**< Object not found */
480 #define VLC_ENOVAR (-6) /**< Variable not found */
481 #define VLC_EBADVAR (-7) /**< Bad variable value */
482 #define VLC_ENOITEM (-8) /**< Item not found */
484 /*****************************************************************************
485 * Variable callbacks: called when the value is modified
486 *****************************************************************************/
487 typedef int ( * vlc_callback_t ) ( vlc_object_t *, /* variable's object */
488 char const *, /* variable name */
489 vlc_value_t, /* old value */
490 vlc_value_t, /* new value */
491 void * ); /* callback data */
493 /*****************************************************************************
494 * List callbacks: called when elements are added/removed from the list
495 *****************************************************************************/
496 typedef int ( * vlc_list_callback_t ) ( vlc_object_t *, /* variable's object */
497 char const *, /* variable name */
498 int, /* VLC_VAR_* action */
499 vlc_value_t *, /* new/deleted value */
500 void *); /* callback data */
502 /*****************************************************************************
503 * OS-specific headers and thread types
504 *****************************************************************************/
505 #if defined( _WIN32 )
506 # include <malloc.h>
507 # ifndef PATH_MAX
508 # define PATH_MAX MAX_PATH
509 # endif
510 # include <windows.h>
511 #endif
513 #ifdef __APPLE__
514 #include <sys/syslimits.h>
515 #include <AvailabilityMacros.h>
516 #endif
518 #ifdef __OS2__
519 # define OS2EMX_PLAIN_CHAR
520 # define INCL_BASE
521 # define INCL_PM
522 # include <os2safe.h>
523 # include <os2.h>
524 #endif
526 #include "vlc_mtime.h"
527 #include "vlc_threads.h"
530 * \defgroup intops Integer operations
531 * \ingroup cext
533 * Common integer functions.
534 * @{
536 /* __MAX and __MIN: self explanatory */
537 #ifndef __MAX
538 # define __MAX(a, b) ( ((a) > (b)) ? (a) : (b) )
539 #endif
540 #ifndef __MIN
541 # define __MIN(a, b) ( ((a) < (b)) ? (a) : (b) )
542 #endif
544 /* clip v in [min, max] */
545 #define VLC_CLIP(v, min, max) __MIN(__MAX((v), (min)), (max))
547 /** Greatest common divisor */
548 VLC_USED
549 static inline int64_t GCD ( int64_t a, int64_t b )
551 while( b )
553 int64_t c = a % b;
554 a = b;
555 b = c;
557 return a;
560 /* function imported from libavutil/common.h */
561 VLC_USED
562 static inline uint8_t clip_uint8_vlc( int32_t a )
564 if( a&(~255) ) return (-a)>>31;
565 else return a;
569 * \defgroup bitops Bit operations
570 * @{
573 #define VLC_INT_FUNC(basename) \
574 VLC_INT_FUNC_TYPE(basename, unsigned, ) \
575 VLC_INT_FUNC_TYPE(basename, unsigned long, l) \
576 VLC_INT_FUNC_TYPE(basename, unsigned long long, ll)
578 #if defined (__GNUC__) || defined (__clang__)
579 # define VLC_INT_FUNC_TYPE(basename,type,suffix) \
580 VLC_USED static inline int vlc_##basename##suffix(type x) \
582 return __builtin_##basename##suffix(x); \
585 VLC_INT_FUNC(clz)
586 #else
587 VLC_USED static inline int vlc_clzll(unsigned long long x)
589 int i = sizeof (x) * 8;
591 while (x)
593 x >>= 1;
594 i--;
596 return i;
599 VLC_USED static inline int vlc_clzl(unsigned long x)
601 return vlc_clzll(x) - ((sizeof (long long) - sizeof (long)) * 8);
604 VLC_USED static inline int vlc_clz(unsigned x)
606 return vlc_clzll(x) - ((sizeof (long long) - sizeof (int)) * 8);
609 VLC_USED static inline int vlc_ctz_generic(unsigned long long x)
611 unsigned i = sizeof (x) * 8;
613 while (x)
615 x <<= 1;
616 i--;
618 return i;
621 VLC_USED static inline int vlc_parity_generic(unsigned long long x)
623 for (unsigned i = 4 * sizeof (x); i > 0; i /= 2)
624 x ^= x >> i;
625 return x & 1;
628 VLC_USED static inline int vlc_popcount_generic(unsigned long long x)
630 int count = 0;
631 while (x)
633 count += x & 1;
634 x = x >> 1;
636 return count;
639 # define VLC_INT_FUNC_TYPE(basename,type,suffix) \
640 VLC_USED static inline int vlc_##basename##suffix(type x) \
642 return vlc_##basename##_generic(x); \
644 #endif
646 VLC_INT_FUNC(ctz)
647 VLC_INT_FUNC(parity)
648 VLC_INT_FUNC(popcount)
650 #ifndef __cplusplus
651 # define VLC_INT_GENERIC(func,x) \
652 _Generic((x), \
653 unsigned char: func(x), \
654 signed char: func(x), \
655 unsigned short: func(x), \
656 signed short: func(x), \
657 unsigned int: func(x), \
658 signed int: func(x), \
659 unsigned long: func##l(x), \
660 signed long: func##l(x), \
661 unsigned long long: func##ll(x), \
662 signed long long: func##ll(x))
665 * Count leading zeroes
667 * This function counts the number of consecutive zero (clear) bits
668 * down from the highest order bit in an unsigned integer.
670 * \param x a non-zero integer
671 * \note This macro assumes that CHAR_BIT equals 8.
672 * \warning By definition, the result depends on the (width of the) type of x.
673 * \return The number of leading zero bits in x.
675 # define clz(x) \
676 _Generic((x), \
677 unsigned char: (vlc_clz(x) - (sizeof (unsigned) - 1) * 8), \
678 unsigned short: (vlc_clz(x) \
679 - (sizeof (unsigned) - sizeof (unsigned short)) * 8), \
680 unsigned: vlc_clz(x), \
681 unsigned long: vlc_clzl(x), \
682 unsigned long long: vlc_clzll(x))
685 * Count trailing zeroes
687 * This function counts the number of consecutive zero bits
688 * up from the lowest order bit in an unsigned integer.
690 * \param x a non-zero integer
691 * \note This function assumes that CHAR_BIT equals 8.
692 * \return The number of trailing zero bits in x.
694 # define ctz(x) VLC_INT_GENERIC(vlc_ctz, x)
697 * Parity
699 * This function determines the parity of an integer.
700 * \retval 0 if x has an even number of set bits.
701 * \retval 1 if x has an odd number of set bits.
703 # define parity(x) VLC_INT_GENERIC(vlc_parity, x)
706 * Bit weight / population count
708 * This function counts the number of non-zero bits in an integer.
710 * \return The count of non-zero bits.
712 # define vlc_popcount(x) \
713 _Generic((x), \
714 signed char: vlc_popcount((unsigned char)(x)), \
715 signed short: vlc_popcount((unsigned short)(x)), \
716 default: VLC_INT_GENERIC(vlc_popcount ,x))
717 #else
718 VLC_USED static inline int vlc_popcount(unsigned char x)
720 return vlc_popcount((unsigned)x);
723 VLC_USED static inline int vlc_popcount(unsigned short x)
725 return vlc_popcount((unsigned)x);
728 VLC_USED static inline int vlc_popcount(unsigned long x)
730 return vlc_popcountl(x);
733 VLC_USED static inline int vlc_popcount(unsigned long long x)
735 return vlc_popcountll(x);
737 #endif
739 /** Byte swap (16 bits) */
740 VLC_USED
741 static inline uint16_t vlc_bswap16(uint16_t x)
743 return (x << 8) | (x >> 8);
746 /** Byte swap (32 bits) */
747 VLC_USED
748 static inline uint32_t vlc_bswap32(uint32_t x)
750 #if defined (__GNUC__) || defined(__clang__)
751 return __builtin_bswap32 (x);
752 #else
753 return ((x & 0x000000FF) << 24)
754 | ((x & 0x0000FF00) << 8)
755 | ((x & 0x00FF0000) >> 8)
756 | ((x & 0xFF000000) >> 24);
757 #endif
760 /** Byte swap (64 bits) */
761 VLC_USED
762 static inline uint64_t vlc_bswap64(uint64_t x)
764 #if defined (__GNUC__) || defined(__clang__)
765 return __builtin_bswap64 (x);
766 #elif !defined (__cplusplus)
767 return ((x & 0x00000000000000FF) << 56)
768 | ((x & 0x000000000000FF00) << 40)
769 | ((x & 0x0000000000FF0000) << 24)
770 | ((x & 0x00000000FF000000) << 8)
771 | ((x & 0x000000FF00000000) >> 8)
772 | ((x & 0x0000FF0000000000) >> 24)
773 | ((x & 0x00FF000000000000) >> 40)
774 | ((x & 0xFF00000000000000) >> 56);
775 #else
776 return ((x & 0x00000000000000FFULL) << 56)
777 | ((x & 0x000000000000FF00ULL) << 40)
778 | ((x & 0x0000000000FF0000ULL) << 24)
779 | ((x & 0x00000000FF000000ULL) << 8)
780 | ((x & 0x000000FF00000000ULL) >> 8)
781 | ((x & 0x0000FF0000000000ULL) >> 24)
782 | ((x & 0x00FF000000000000ULL) >> 40)
783 | ((x & 0xFF00000000000000ULL) >> 56);
784 #endif
786 /** @} */
789 * \defgroup overflow Overflowing arithmetic
790 * @{
792 static inline bool uadd_overflow(unsigned a, unsigned b, unsigned *res)
794 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
795 return __builtin_uadd_overflow(a, b, res);
796 #else
797 *res = a + b;
798 return (a + b) < a;
799 #endif
802 static inline bool uaddl_overflow(unsigned long a, unsigned long b,
803 unsigned long *res)
805 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
806 return __builtin_uaddl_overflow(a, b, res);
807 #else
808 *res = a + b;
809 return (a + b) < a;
810 #endif
813 static inline bool uaddll_overflow(unsigned long long a, unsigned long long b,
814 unsigned long long *res)
816 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
817 return __builtin_uaddll_overflow(a, b, res);
818 #else
819 *res = a + b;
820 return (a + b) < a;
821 #endif
824 #ifndef __cplusplus
826 * Overflowing addition
828 * Converts \p a and \p b to the type of \p *r.
829 * Then computes the sum of both conversions while checking for overflow.
830 * Finally stores the result in \p *r.
832 * \param a an integer
833 * \param b an integer
834 * \param r a pointer to an integer [OUT]
835 * \retval false The sum did not overflow.
836 * \retval true The sum overflowed.
838 # define add_overflow(a,b,r) \
839 _Generic(*(r), \
840 unsigned: uadd_overflow(a, b, (unsigned *)(r)), \
841 unsigned long: uaddl_overflow(a, b, (unsigned long *)(r)), \
842 unsigned long long: uaddll_overflow(a, b, (unsigned long long *)(r)))
843 #else
844 static inline bool add_overflow(unsigned a, unsigned b, unsigned *res)
846 return uadd_overflow(a, b, res);
849 static inline bool add_overflow(unsigned long a, unsigned long b,
850 unsigned long *res)
852 return uaddl_overflow(a, b, res);
855 static inline bool add_overflow(unsigned long long a, unsigned long long b,
856 unsigned long long *res)
858 return uaddll_overflow(a, b, res);
860 #endif
862 #if !(VLC_GCC_VERSION(5,0) || defined(__clang__))
863 # include <limits.h>
864 #endif
866 static inline bool umul_overflow(unsigned a, unsigned b, unsigned *res)
868 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
869 return __builtin_umul_overflow(a, b, res);
870 #else
871 *res = a * b;
872 return b > 0 && a > (UINT_MAX / b);
873 #endif
876 static inline bool umull_overflow(unsigned long a, unsigned long b,
877 unsigned long *res)
879 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
880 return __builtin_umull_overflow(a, b, res);
881 #else
882 *res = a * b;
883 return b > 0 && a > (ULONG_MAX / b);
884 #endif
887 static inline bool umulll_overflow(unsigned long long a, unsigned long long b,
888 unsigned long long *res)
890 #if VLC_GCC_VERSION(5,0) || defined(__clang__)
891 return __builtin_umulll_overflow(a, b, res);
892 #else
893 *res = a * b;
894 return b > 0 && a > (ULLONG_MAX / b);
895 #endif
898 #ifndef __cplusplus
900 * Overflowing multiplication
902 * Converts \p a and \p b to the type of \p *r.
903 * Then computes the product of both conversions while checking for overflow.
904 * Finally stores the result in \p *r.
906 * \param a an integer
907 * \param b an integer
908 * \param r a pointer to an integer [OUT]
909 * \retval false The product did not overflow.
910 * \retval true The product overflowed.
912 #define mul_overflow(a,b,r) \
913 _Generic(*(r), \
914 unsigned: umul_overflow(a, b, (unsigned *)(r)), \
915 unsigned long: umull_overflow(a, b, (unsigned long *)(r)), \
916 unsigned long long: umulll_overflow(a, b, (unsigned long long *)(r)))
917 #else
918 static inline bool mul_overflow(unsigned a, unsigned b, unsigned *res)
920 return umul_overflow(a, b, res);
923 static inline bool mul_overflow(unsigned long a, unsigned long b,
924 unsigned long *res)
926 return umull_overflow(a, b, res);
929 static inline bool mul_overflow(unsigned long long a, unsigned long long b,
930 unsigned long long *res)
932 return umulll_overflow(a, b, res);
934 #endif
935 /** @} */
936 /** @} */
938 /* Free and set set the variable to NULL */
939 #define FREENULL(a) do { free( a ); a = NULL; } while(0)
941 #define EMPTY_STR(str) (!str || !*str)
943 VLC_API char const * vlc_error( int ) VLC_USED;
945 #include <vlc_arrays.h>
947 /* MSB (big endian)/LSB (little endian) conversions - network order is always
948 * MSB, and should be used for both network communications and files. */
950 #ifdef WORDS_BIGENDIAN
951 # define hton16(i) ((uint16_t)(i))
952 # define hton32(i) ((uint32_t)(i))
953 # define hton64(i) ((uint64_t)(i))
954 #else
955 # define hton16(i) vlc_bswap16(i)
956 # define hton32(i) vlc_bswap32(i)
957 # define hton64(i) vlc_bswap64(i)
958 #endif
959 #define ntoh16(i) hton16(i)
960 #define ntoh32(i) hton32(i)
961 #define ntoh64(i) hton64(i)
963 /** Reads 16 bits in network byte order */
964 VLC_USED
965 static inline uint16_t U16_AT (const void *p)
967 uint16_t x;
969 memcpy (&x, p, sizeof (x));
970 return ntoh16 (x);
973 /** Reads 32 bits in network byte order */
974 VLC_USED
975 static inline uint32_t U32_AT (const void *p)
977 uint32_t x;
979 memcpy (&x, p, sizeof (x));
980 return ntoh32 (x);
983 /** Reads 64 bits in network byte order */
984 VLC_USED
985 static inline uint64_t U64_AT (const void *p)
987 uint64_t x;
989 memcpy (&x, p, sizeof (x));
990 return ntoh64 (x);
993 #define GetWBE(p) U16_AT(p)
994 #define GetDWBE(p) U32_AT(p)
995 #define GetQWBE(p) U64_AT(p)
997 /** Reads 16 bits in little-endian order */
998 VLC_USED
999 static inline uint16_t GetWLE (const void *p)
1001 uint16_t x;
1003 memcpy (&x, p, sizeof (x));
1004 #ifdef WORDS_BIGENDIAN
1005 x = vlc_bswap16 (x);
1006 #endif
1007 return x;
1010 /** Reads 32 bits in little-endian order */
1011 VLC_USED
1012 static inline uint32_t GetDWLE (const void *p)
1014 uint32_t x;
1016 memcpy (&x, p, sizeof (x));
1017 #ifdef WORDS_BIGENDIAN
1018 x = vlc_bswap32 (x);
1019 #endif
1020 return x;
1023 /** Reads 64 bits in little-endian order */
1024 VLC_USED
1025 static inline uint64_t GetQWLE (const void *p)
1027 uint64_t x;
1029 memcpy (&x, p, sizeof (x));
1030 #ifdef WORDS_BIGENDIAN
1031 x = vlc_bswap64 (x);
1032 #endif
1033 return x;
1036 /** Writes 16 bits in network byte order */
1037 static inline void SetWBE (void *p, uint16_t w)
1039 w = hton16 (w);
1040 memcpy (p, &w, sizeof (w));
1043 /** Writes 32 bits in network byte order */
1044 static inline void SetDWBE (void *p, uint32_t dw)
1046 dw = hton32 (dw);
1047 memcpy (p, &dw, sizeof (dw));
1050 /** Writes 64 bits in network byte order */
1051 static inline void SetQWBE (void *p, uint64_t qw)
1053 qw = hton64 (qw);
1054 memcpy (p, &qw, sizeof (qw));
1057 /** Writes 16 bits in little endian order */
1058 static inline void SetWLE (void *p, uint16_t w)
1060 #ifdef WORDS_BIGENDIAN
1061 w = vlc_bswap16 (w);
1062 #endif
1063 memcpy (p, &w, sizeof (w));
1066 /** Writes 32 bits in little endian order */
1067 static inline void SetDWLE (void *p, uint32_t dw)
1069 #ifdef WORDS_BIGENDIAN
1070 dw = vlc_bswap32 (dw);
1071 #endif
1072 memcpy (p, &dw, sizeof (dw));
1075 /** Writes 64 bits in little endian order */
1076 static inline void SetQWLE (void *p, uint64_t qw)
1078 #ifdef WORDS_BIGENDIAN
1079 qw = vlc_bswap64 (qw);
1080 #endif
1081 memcpy (p, &qw, sizeof (qw));
1084 /* */
1085 #define VLC_UNUSED(x) (void)(x)
1087 /* Stuff defined in src/extras/libc.c */
1089 #if defined(_WIN32)
1090 /* several type definitions */
1091 # if defined( __MINGW32__ )
1092 # if !defined( _OFF_T_ )
1093 typedef long long _off_t;
1094 typedef _off_t off_t;
1095 # define _OFF_T_
1096 # else
1097 # ifdef off_t
1098 # undef off_t
1099 # endif
1100 # define off_t long long
1101 # endif
1102 # endif
1104 # ifndef O_NONBLOCK
1105 # define O_NONBLOCK 0
1106 # endif
1108 # include <tchar.h>
1109 #endif /* _WIN32 */
1111 typedef struct {
1112 unsigned num, den;
1113 } vlc_rational_t;
1115 VLC_API bool vlc_ureduce( unsigned *, unsigned *, uint64_t, uint64_t, uint64_t );
1117 #define container_of(ptr, type, member) \
1118 ((type *)(((char *)(ptr)) - offsetof(type, member)))
1120 VLC_USED VLC_MALLOC
1121 static inline void *vlc_alloc(size_t count, size_t size)
1123 return mul_overflow(count, size, &size) ? NULL : malloc(size);
1126 /*****************************************************************************
1127 * I18n stuff
1128 *****************************************************************************/
1129 VLC_API char *vlc_gettext( const char *msgid ) VLC_FORMAT_ARG(1);
1130 VLC_API char *vlc_ngettext( const char *s, const char *p, unsigned long n ) VLC_FORMAT_ARG(1) VLC_FORMAT_ARG(2);
1132 #define vlc_pgettext( ctx, id ) \
1133 vlc_pgettext_aux( ctx "\004" id, id )
1135 VLC_FORMAT_ARG(2)
1136 static inline const char *vlc_pgettext_aux( const char *ctx, const char *id )
1138 const char *tr = vlc_gettext( ctx );
1139 return (tr == ctx) ? id : tr;
1142 /*****************************************************************************
1143 * Loosy memory allocation functions. Do not use in new code.
1144 *****************************************************************************/
1145 static inline void *xmalloc(size_t len)
1147 void *ptr = malloc(len);
1148 if (unlikely(ptr == NULL && len > 0))
1149 abort();
1150 return ptr;
1153 static inline void *xrealloc(void *ptr, size_t len)
1155 void *nptr = realloc(ptr, len);
1156 if (unlikely(nptr == NULL && len > 0))
1157 abort();
1158 return nptr;
1161 static inline void *xcalloc(size_t n, size_t size)
1163 void *ptr = calloc(n, size);
1164 if (unlikely(ptr == NULL && (n > 0 || size > 0)))
1165 abort ();
1166 return ptr;
1169 static inline char *xstrdup (const char *str)
1171 char *ptr = strdup (str);
1172 if (unlikely(ptr == NULL))
1173 abort ();
1174 return ptr;
1177 /*****************************************************************************
1178 * libvlc features
1179 *****************************************************************************/
1180 VLC_API const char * VLC_CompileBy( void ) VLC_USED;
1181 VLC_API const char * VLC_CompileHost( void ) VLC_USED;
1182 VLC_API const char * VLC_Compiler( void ) VLC_USED;
1184 /*****************************************************************************
1185 * Additional vlc stuff
1186 *****************************************************************************/
1187 #include "vlc_messages.h"
1188 #include "vlc_objects.h"
1189 #include "vlc_variables.h"
1190 #include "vlc_configuration.h"
1192 #if defined( _WIN32 ) || defined( __OS2__ )
1193 # define DIR_SEP_CHAR '\\'
1194 # define DIR_SEP "\\"
1195 # define PATH_SEP_CHAR ';'
1196 # define PATH_SEP ";"
1197 #else
1198 # define DIR_SEP_CHAR '/'
1199 # define DIR_SEP "/"
1200 # define PATH_SEP_CHAR ':'
1201 # define PATH_SEP ":"
1202 #endif
1204 #define LICENSE_MSG \
1205 _("This program comes with NO WARRANTY, to the extent permitted by " \
1206 "law.\nYou may redistribute it under the terms of the GNU General " \
1207 "Public License;\nsee the file named COPYING for details.\n" \
1208 "Written by the VideoLAN team; see the AUTHORS file.\n")
1210 #endif /* !VLC_COMMON_H */