Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame] | 1 | /* Declarations for common convenience functions. |
| 2 | Copyright (C) 2006-2011 Red Hat, Inc. |
| 3 | This file is part of elfutils. |
| 4 | |
| 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of either |
| 7 | |
| 8 | * the GNU Lesser General Public License as published by the Free |
| 9 | Software Foundation; either version 3 of the License, or (at |
| 10 | your option) any later version |
| 11 | |
| 12 | or |
| 13 | |
| 14 | * the GNU General Public License as published by the Free |
| 15 | Software Foundation; either version 2 of the License, or (at |
| 16 | your option) any later version |
| 17 | |
| 18 | or both in parallel, as here. |
| 19 | |
| 20 | elfutils is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received copies of the GNU General Public License and |
| 26 | the GNU Lesser General Public License along with this program. If |
| 27 | not, see <http://www.gnu.org/licenses/>. */ |
| 28 | |
| 29 | #ifndef LIB_SYSTEM_H |
| 30 | #define LIB_SYSTEM_H 1 |
| 31 | |
| 32 | #include <errno.h> |
| 33 | #include <stddef.h> |
| 34 | #include <stdint.h> |
| 35 | #include <sys/param.h> |
| 36 | #include <endian.h> |
| 37 | #include <byteswap.h> |
| 38 | #include <unistd.h> |
| 39 | |
| 40 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 41 | # define LE32(n) (n) |
| 42 | # define LE64(n) (n) |
| 43 | # define BE32(n) bswap_32 (n) |
| 44 | # define BE64(n) bswap_64 (n) |
| 45 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 46 | # define BE32(n) (n) |
| 47 | # define BE64(n) (n) |
| 48 | # define LE32(n) bswap_32 (n) |
| 49 | # define LE64(n) bswap_64 (n) |
| 50 | #else |
| 51 | # error "Unknown byte order" |
| 52 | #endif |
| 53 | |
| 54 | #ifndef MAX |
| 55 | #define MAX(m, n) ((m) < (n) ? (n) : (m)) |
| 56 | #endif |
| 57 | |
| 58 | #ifndef MIN |
| 59 | #define MIN(m, n) ((m) < (n) ? (m) : (n)) |
| 60 | #endif |
| 61 | |
| 62 | #if !HAVE_DECL_POWEROF2 |
| 63 | #define powerof2(x) (((x) & ((x) - 1)) == 0) |
| 64 | #endif |
| 65 | |
| 66 | #if !HAVE_DECL_MEMPCPY |
| 67 | #define mempcpy(dest, src, n) \ |
| 68 | ((void *) ((char *) memcpy (dest, src, n) + (size_t) n)) |
| 69 | #endif |
| 70 | |
| 71 | /* A special gettext function we use if the strings are too short. */ |
| 72 | #define sgettext(Str) \ |
| 73 | ({ const char *__res = strrchr (gettext (Str), '|'); \ |
| 74 | __res ? __res + 1 : Str; }) |
| 75 | |
| 76 | #define gettext_noop(Str) Str |
| 77 | |
| 78 | #ifndef TEMP_FAILURE_RETRY |
| 79 | #define TEMP_FAILURE_RETRY(expression) \ |
| 80 | ({ ssize_t __res; \ |
| 81 | do \ |
| 82 | __res = expression; \ |
| 83 | while (__res == -1 && errno == EINTR); \ |
| 84 | __res; }) |
| 85 | #endif |
| 86 | |
| 87 | static inline ssize_t __attribute__ ((unused)) |
| 88 | pwrite_retry (int fd, const void *buf, size_t len, off_t off) |
| 89 | { |
| 90 | ssize_t recvd = 0; |
| 91 | |
| 92 | do |
| 93 | { |
| 94 | ssize_t ret = TEMP_FAILURE_RETRY (pwrite (fd, buf + recvd, len - recvd, |
| 95 | off + recvd)); |
| 96 | if (ret <= 0) |
| 97 | return ret < 0 ? ret : recvd; |
| 98 | |
| 99 | recvd += ret; |
| 100 | } |
| 101 | while ((size_t) recvd < len); |
| 102 | |
| 103 | return recvd; |
| 104 | } |
| 105 | |
| 106 | static inline ssize_t __attribute__ ((unused)) |
| 107 | write_retry (int fd, const void *buf, size_t len) |
| 108 | { |
| 109 | ssize_t recvd = 0; |
| 110 | |
| 111 | do |
| 112 | { |
| 113 | ssize_t ret = TEMP_FAILURE_RETRY (write (fd, buf + recvd, len - recvd)); |
| 114 | if (ret <= 0) |
| 115 | return ret < 0 ? ret : recvd; |
| 116 | |
| 117 | recvd += ret; |
| 118 | } |
| 119 | while ((size_t) recvd < len); |
| 120 | |
| 121 | return recvd; |
| 122 | } |
| 123 | |
| 124 | static inline ssize_t __attribute__ ((unused)) |
| 125 | pread_retry (int fd, void *buf, size_t len, off_t off) |
| 126 | { |
| 127 | ssize_t recvd = 0; |
| 128 | |
| 129 | do |
| 130 | { |
| 131 | ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, buf + recvd, len - recvd, |
| 132 | off + recvd)); |
| 133 | if (ret <= 0) |
| 134 | return ret < 0 ? ret : recvd; |
| 135 | |
| 136 | recvd += ret; |
| 137 | } |
| 138 | while ((size_t) recvd < len); |
| 139 | |
| 140 | return recvd; |
| 141 | } |
| 142 | |
| 143 | /* The demangler from libstdc++. */ |
| 144 | extern char *__cxa_demangle (const char *mangled_name, char *output_buffer, |
| 145 | size_t *length, int *status); |
| 146 | |
| 147 | /* A static assertion. This will cause a compile-time error if EXPR, |
| 148 | which must be a compile-time constant, is false. */ |
| 149 | |
| 150 | #define eu_static_assert(expr) \ |
| 151 | extern int never_defined_just_used_for_checking[(expr) ? 1 : -1] \ |
| 152 | __attribute__ ((unused)) |
| 153 | |
| 154 | #endif /* system.h */ |