]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/macros.h
Fix typos in documentation (codespell)
[apt.git] / apt-pkg / contrib / macros.h
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
578bfd0a
AL
3/* ######################################################################
4
5c0d3668 5 Macros Header - Various useful macro definitions
578bfd0a
AL
6
7 This source is placed in the Public Domain, do with it what you will
8 It was originally written by Brian C. White.
9
10 ##################################################################### */
11 /*}}}*/
12// Private header
5c0d3668
DK
13#ifndef MACROS_H
14#define MACROS_H
578bfd0a
AL
15
16// MIN_VAL(SINT16) will return -0x8000 and MAX_VAL(SINT16) = 0x7FFF
17#define MIN_VAL(t) (((t)(-1) > 0) ? (t)( 0) : (t)(((1L<<(sizeof(t)*8-1)) )))
18#define MAX_VAL(t) (((t)(-1) > 0) ? (t)(-1) : (t)(((1L<<(sizeof(t)*8-1))-1)))
19
20// Min/Max functions
1ae93c94 21#if !defined(MIN)
578bfd0a
AL
22#if defined(__HIGHC__)
23#define MIN(x,y) _min(x,y)
24#define MAX(x,y) _max(x,y)
25#endif
26
27// GNU C++ has a min/max operator <coolio>
28#if defined(__GNUG__)
29#define MIN(A,B) ((A) <? (B))
30#define MAX(A,B) ((A) >? (B))
31#endif
32
33/* Templates tend to mess up existing code that uses min/max because of the
34 strict matching requirements */
35#if !defined(MIN)
36#define MIN(A,B) ((A) < (B)?(A):(B))
37#define MAX(A,B) ((A) > (B)?(A):(B))
38#endif
1ae93c94 39#endif
578bfd0a
AL
40
41/* Bound functions, bound will return the value b within the limits a-c
42 bounv will change b so that it is within the limits of a-c. */
43#define _bound(a,b,c) MIN(c,MAX(b,a))
44#define _boundv(a,b,c) b = _bound(a,b,c)
45#define ABS(a) (((a) < (0)) ?-(a) : (a))
46
1e3f4083 47/* Useful count macro, use on an array of things and it will return the
578bfd0a
AL
48 number of items in the array */
49#define _count(a) (sizeof(a)/sizeof(a[0]))
50
51// Flag Macros
52#define FLAG(f) (1L << (f))
53#define SETFLAG(v,f) ((v) |= FLAG(f))
54#define CLRFLAG(v,f) ((v) &=~FLAG(f))
55#define CHKFLAG(v,f) ((v) & FLAG(f) ? true : false)
56
3d43e539
DK
57// some nice optional GNUC features
58#if __GNUC__ >= 3
7d15572b
DK
59 #define __must_check __attribute__ ((warn_unused_result))
60 #define __deprecated __attribute__ ((deprecated))
6dc60370 61 #define __attrib_const __attribute__ ((__const__))
7d15572b
DK
62 /* likely() and unlikely() can be used to mark boolean expressions
63 as (not) likely true which will help the compiler to optimise */
64 #define likely(x) __builtin_expect (!!(x), 1)
65 #define unlikely(x) __builtin_expect (!!(x), 0)
3d43e539 66#else
7d15572b
DK
67 #define __must_check /* no warn_unused_result */
68 #define __deprecated /* no deprecated */
6dc60370 69 #define __attrib_const /* no const attribute */
7d15572b
DK
70 #define likely(x) (x)
71 #define unlikely(x) (x)
3d43e539
DK
72#endif
73
74// cold functions are unlikely() to be called
75#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4
7d15572b 76 #define __cold __attribute__ ((__cold__))
6dc60370 77 #define __hot __attribute__ ((__hot__))
3d43e539 78#else
7d15572b 79 #define __cold /* no cold marker */
6dc60370 80 #define __hot /* no hot marker */
3d43e539
DK
81#endif
82
8f3d83ee
DK
83#ifdef __GNUG__
84// Methods have a hidden this parameter that is visible to this attribute
6dc60370 85 #define __like_printf(n) __attribute__((format(printf, n, n + 1)))
8f3d83ee 86#else
6dc60370 87 #define __like_printf(n) /* no like-printf */
8f3d83ee
DK
88#endif
89
578bfd0a 90#endif