LCOV - code coverage report
Current view: top level - elfutils/lib - system.h (source / functions) Hit Total Coverage
Test: lcov.out Lines: 20 21 95.2 %
Date: 2013-03-08 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 14 30 46.7 %

           Branch data     Line data    Source code
       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 <argp.h>
      33                 :            : #include <stddef.h>
      34                 :            : #include <stdint.h>
      35                 :            : #include <endian.h>
      36                 :            : #include <byteswap.h>
      37                 :            : #include <unistd.h>
      38                 :            : 
      39                 :            : #if __BYTE_ORDER == __LITTLE_ENDIAN
      40                 :            : # define LE32(n)        (n)
      41                 :            : # define BE32(n)        bswap_32 (n)
      42                 :            : #elif __BYTE_ORDER == __BIG_ENDIAN
      43                 :            : # define BE32(n)        (n)
      44                 :            : # define LE32(n)        bswap_32 (n)
      45                 :            : #else
      46                 :            : # error "Unknown byte order"
      47                 :            : #endif
      48                 :            : 
      49                 :            : extern void *xmalloc (size_t) __attribute__ ((__malloc__));
      50                 :            : extern void *xcalloc (size_t, size_t) __attribute__ ((__malloc__));
      51                 :            : extern void *xrealloc (void *, size_t) __attribute__ ((__malloc__));
      52                 :            : 
      53                 :            : extern char *xstrdup (const char *) __attribute__ ((__malloc__));
      54                 :            : extern char *xstrndup (const char *, size_t) __attribute__ ((__malloc__));
      55                 :            : 
      56                 :            : 
      57                 :            : extern uint32_t crc32 (uint32_t crc, unsigned char *buf, size_t len);
      58                 :            : extern int crc32_file (int fd, uint32_t *resp);
      59                 :            : 
      60                 :            : /* A special gettext function we use if the strings are too short.  */
      61                 :            : #define sgettext(Str) \
      62                 :            :   ({ const char *__res = strrchr (gettext (Str), '|');                        \
      63                 :            :      __res ? __res + 1 : Str; })
      64                 :            : 
      65                 :            : #define gettext_noop(Str) Str
      66                 :            : 
      67                 :            : 
      68                 :            : static inline ssize_t __attribute__ ((unused))
      69                 :        453 : pwrite_retry (int fd, const void *buf, size_t len, off_t off)
      70                 :            : {
      71                 :        453 :   ssize_t recvd = 0;
      72                 :            : 
      73                 :            :   do
      74                 :            :     {
      75 [ -  + ][ #  # ]:        453 :       ssize_t ret = TEMP_FAILURE_RETRY (pwrite (fd, buf + recvd, len - recvd,
      76                 :            :                                                 off + recvd));
      77         [ +  + ]:        453 :       if (ret <= 0)
      78         [ +  - ]:          7 :         return ret < 0 ? ret : recvd;
      79                 :            : 
      80                 :        446 :       recvd += ret;
      81                 :            :     }
      82         [ -  + ]:        453 :   while ((size_t) recvd < len);
      83                 :            : 
      84                 :            :   return recvd;
      85                 :            : }
      86                 :            : 
      87                 :            : static inline ssize_t __attribute__ ((unused))
      88                 :         13 : write_retry (int fd, const void *buf, size_t len)
      89                 :            : {
      90                 :         13 :   ssize_t recvd = 0;
      91                 :            : 
      92                 :            :   do
      93                 :            :     {
      94 [ -  + ][ #  # ]:         13 :       ssize_t ret = TEMP_FAILURE_RETRY (write (fd, buf + recvd, len - recvd));
      95         [ -  + ]:         13 :       if (ret <= 0)
      96         [ #  # ]:          0 :         return ret < 0 ? ret : recvd;
      97                 :            : 
      98                 :         13 :       recvd += ret;
      99                 :            :     }
     100         [ -  + ]:         13 :   while ((size_t) recvd < len);
     101                 :            : 
     102                 :            :   return recvd;
     103                 :            : }
     104                 :            : 
     105                 :            : static inline ssize_t __attribute__ ((unused))
     106                 :      19670 : pread_retry (int fd, void *buf, size_t len, off_t off)
     107                 :            : {
     108                 :      24674 :   ssize_t recvd = 0;
     109                 :            : 
     110                 :            :   do
     111                 :            :     {
     112 [ -  + ][ #  # ]:      24674 :       ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, buf + recvd, len - recvd,
     113                 :            :                                                off + recvd));
     114         [ +  + ]:      24674 :       if (ret <= 0)
     115         [ +  - ]:       5004 :         return ret < 0 ? ret : recvd;
     116                 :            : 
     117                 :      19670 :       recvd += ret;
     118                 :            :     }
     119         [ +  + ]:      24674 :   while ((size_t) recvd < len);
     120                 :            : 
     121                 :            :   return recvd;
     122                 :            : }
     123                 :            : 
     124                 :            : 
     125                 :            : /* We need define two variables, argp_program_version_hook and
     126                 :            :    argp_program_bug_address, in all programs.  argp.h declares these
     127                 :            :    variables as non-const (which is correct in general).  But we can
     128                 :            :    do better, it is not going to change.  So we want to move them into
     129                 :            :    the .rodata section.  Define macros to do the trick.  */
     130                 :            : #define ARGP_PROGRAM_VERSION_HOOK_DEF \
     131                 :            :   void (*const apvh) (FILE *, struct argp_state *) \
     132                 :            :    __asm ("argp_program_version_hook")
     133                 :            : #define ARGP_PROGRAM_BUG_ADDRESS_DEF \
     134                 :            :   const char *const apba__ __asm ("argp_program_bug_address")
     135                 :            : 
     136                 :            : 
     137                 :            : /* The demangler from libstdc++.  */
     138                 :            : extern char *__cxa_demangle (const char *mangled_name, char *output_buffer,
     139                 :            :                              size_t *length, int *status);
     140                 :            : 
     141                 :            : 
     142                 :            : 
     143                 :            : /* Color handling.  */
     144                 :            : 
     145                 :            : /* Command line parser.  */
     146                 :            : extern const struct argp color_argp;
     147                 :            : 
     148                 :            : /* Coloring mode.  */
     149                 :            : enum color_enum
     150                 :            :   {
     151                 :            :     color_never = 0,
     152                 :            :     color_always,
     153                 :            :     color_auto
     154                 :            :   } __attribute__ ((packed));
     155                 :            : extern enum color_enum color_mode;
     156                 :            : 
     157                 :            : /* Colors to use for the various components.  */
     158                 :            : extern char *color_address;
     159                 :            : extern char *color_bytes;
     160                 :            : extern char *color_mnemonic;
     161                 :            : extern char *color_operand1;
     162                 :            : extern char *color_operand2;
     163                 :            : extern char *color_operand3;
     164                 :            : extern char *color_label;
     165                 :            : extern char *color_undef;
     166                 :            : extern char *color_undef_tls;
     167                 :            : extern char *color_undef_weak;
     168                 :            : extern char *color_symbol;
     169                 :            : extern char *color_tls;
     170                 :            : extern char *color_weak;
     171                 :            : 
     172                 :            : extern const char color_off[];
     173                 :            : 
     174                 :            : /* A static assertion.  This will cause a compile-time error if EXPR,
     175                 :            :    which must be a compile-time constant, is false.  */
     176                 :            : 
     177                 :            : #define eu_static_assert(expr)                                          \
     178                 :            :   extern int never_defined_just_used_for_checking[(expr) ? 1 : -1]      \
     179                 :            :     __attribute__ ((unused))
     180                 :            : 
     181                 :            : #endif /* system.h */

Generated by: LCOV version 1.9