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 <error.h> 34 #include <stddef.h> 35 #include <stdint.h> 36 #include <sys/param.h> 37 #include <endian.h> 38 #include <byteswap.h> 39 #include <unistd.h> 40 41 #if __BYTE_ORDER == __LITTLE_ENDIAN 42 # define LE32(n) (n) 43 # define LE64(n) (n) 44 # define BE32(n) bswap_32 (n) 45 # define BE64(n) bswap_64 (n) 46 #elif __BYTE_ORDER == __BIG_ENDIAN 47 # define BE32(n) (n) 48 # define BE64(n) (n) 49 # define LE32(n) bswap_32 (n) 50 # define LE64(n) bswap_64 (n) 51 #else 52 # error "Unknown byte order" 53 #endif 54 55 #ifndef MAX 56 #define MAX(m, n) ((m) < (n) ? (n) : (m)) 57 #endif 58 59 #ifndef MIN 60 #define MIN(m, n) ((m) < (n) ? (m) : (n)) 61 #endif 62 63 #if !HAVE_DECL_POWEROF2 64 #define powerof2(x) (((x) & ((x) - 1)) == 0) 65 #endif 66 67 #if !HAVE_DECL_MEMPCPY 68 #define mempcpy(dest, src, n) \ 69 ((void *) ((char *) memcpy (dest, src, n) + (size_t) n)) 70 #endif 71 72 /* A special gettext function we use if the strings are too short. */ 73 #define sgettext(Str) \ 74 ({ const char *__res = strrchr (gettext (Str), '|'); \ 75 __res ? __res + 1 : Str; }) 76 77 #define gettext_noop(Str) Str 78 79 #ifndef TEMP_FAILURE_RETRY 80 #define TEMP_FAILURE_RETRY(expression) \ 81 ({ ssize_t __res; \ 82 do \ 83 __res = expression; \ 84 while (__res == -1 && errno == EINTR); \ 85 __res; }) 86 #endif 87 88 static inline ssize_t __attribute__ ((unused)) 89 pwrite_retry (int fd, const void *buf, size_t len, off_t off) 90 { 91 ssize_t recvd = 0; 92 93 do 94 { 95 ssize_t ret = TEMP_FAILURE_RETRY (pwrite (fd, buf + recvd, len - recvd, 96 off + recvd)); 97 if (ret <= 0) 98 return ret < 0 ? ret : recvd; 99 100 recvd += ret; 101 } 102 while ((size_t) recvd < len); 103 104 return recvd; 105 } 106 107 static inline ssize_t __attribute__ ((unused)) 108 write_retry (int fd, const void *buf, size_t len) 109 { 110 ssize_t recvd = 0; 111 112 do 113 { 114 ssize_t ret = TEMP_FAILURE_RETRY (write (fd, buf + recvd, len - recvd)); 115 if (ret <= 0) 116 return ret < 0 ? ret : recvd; 117 118 recvd += ret; 119 } 120 while ((size_t) recvd < len); 121 122 return recvd; 123 } 124 125 static inline ssize_t __attribute__ ((unused)) 126 pread_retry (int fd, void *buf, size_t len, off_t off) 127 { 128 ssize_t recvd = 0; 129 130 do 131 { 132 ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, buf + recvd, len - recvd, 133 off + recvd)); 134 if (ret <= 0) 135 return ret < 0 ? ret : recvd; 136 137 recvd += ret; 138 } 139 while ((size_t) recvd < len); 140 141 return recvd; 142 } 143 144 /* The demangler from libstdc++. */ 145 extern char *__cxa_demangle (const char *mangled_name, char *output_buffer, 146 size_t *length, int *status); 147 148 /* A static assertion. This will cause a compile-time error if EXPR, 149 which must be a compile-time constant, is false. */ 150 151 #define eu_static_assert(expr) \ 152 extern int never_defined_just_used_for_checking[(expr) ? 1 : -1] \ 153 __attribute__ ((unused)) 154 155 #endif /* system.h */ 156