Debian Codegraph @

src/libsysprof-capture/sysprof-macros-internal.h

raw
1 /* sysprof-macros.h
2 *
3 * Copyright 2020 Endless OS Foundation
4 *
5 * Author:
6 * - Philip Withnall <[email protected]>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * Subject to the terms and conditions of this license, each copyright holder
20 * and contributor hereby grants to those receiving rights under this license
21 * a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
22 * irrevocable (except for failure to satisfy the conditions of this license)
23 * patent license to make, have made, use, offer to sell, sell, import, and
24 * otherwise transfer this software, where such license applies only to those
25 * patent claims, already acquired or hereafter acquired, licensable by such
26 * copyright holder or contributor that are necessarily infringed by:
27 *
28 * (a) their Contribution(s) (the licensed copyrights of copyright holders
29 * and non-copyrightable additions of contributors, in source or binary
30 * form) alone; or
31 *
32 * (b) combination of their Contribution(s) with the work of authorship to
33 * which such Contribution(s) was added by such copyright holder or
34 * contributor, if, at the time the Contribution is added, such addition
35 * causes such combination to be necessarily infringed. The patent license
36 * shall not apply to any other combinations which include the
37 * Contribution.
38 *
39 * Except as expressly stated above, no rights or licenses from any copyright
40 * holder or contributor is granted under this license, whether expressly, by
41 * implication, estoppel or otherwise.
42 *
43 * DISCLAIMER
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
46 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
47 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
49 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
52 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 *
57 * SPDX-License-Identifier: BSD-2-Clause-Patent
58 */
59
60 #pragma once
61
62 #include <string.h>
63
64 #ifdef __APPLE__
65 # include <libkern/OSByteOrder.h>
66 # include <architecture/byte_order.h>
67 #else
68 # include <byteswap.h>
69 # include <endian.h>
70 #endif
71
72 #define sysprof_assert_not_reached() assert (false)
73
74 #define SYSPROF_N_ELEMENTS(a) (sizeof (a) / sizeof (*a))
75
76 #define sysprof_steal_pointer(pp) __extension__ ({__typeof(*(pp)) _p = *(pp); *(pp) = NULL; _p;})
77
78 #define sysprof_clear_pointer(pp, destroy) \
79 do { \
80 __typeof((pp)) _pp = (pp); \
81 __typeof(*(pp)) _p = *_pp; \
82 *_pp = NULL; \
83 if (_p != NULL) \
84 (destroy) (_p); \
85 } while (0)
86
87 #define sysprof_strdup(s) ((s) ? strdup(s) : NULL)
88
89 #ifdef __APPLE__
90 # define bswap_16 OSSwapInt16
91 # define bswap_32 OSSwapInt32
92 # define bswap_64 OSSwapInt64
93 # define htole32 OSSwapHostToLittleInt32
94 # define __BYTE_ORDER __DARWIN_BYTE_ORDER
95 # define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
96 # define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
97 #endif