(root)/
binutils-2.41/
gprofng/
src/
util.h
       1  /* Copyright (C) 2021-2023 Free Software Foundation, Inc.
       2     Contributed by Oracle.
       3  
       4     This file is part of GNU Binutils.
       5  
       6     This program is free software; you can redistribute it and/or modify
       7     it under the terms of the GNU General Public License as published by
       8     the Free Software Foundation; either version 3, or (at your option)
       9     any later version.
      10  
      11     This program is distributed in the hope that it will be useful,
      12     but WITHOUT ANY WARRANTY; without even the implied warranty of
      13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14     GNU General Public License for more details.
      15  
      16     You should have received a copy of the GNU General Public License
      17     along with this program; if not, write to the Free Software
      18     Foundation, 51 Franklin Street - Fifth Floor, Boston,
      19     MA 02110-1301, USA.  */
      20  
      21  #ifndef _PERFAN_UTIL_H
      22  #define _PERFAN_UTIL_H
      23  
      24  #include <stdio.h>
      25  #include <stdlib.h>
      26  #include <string.h>
      27  #include <sys/stat.h>
      28  #include <stdint.h>
      29  
      30  #include "gp-defs.h"
      31  #include "gp-time.h"
      32  #include "i18n.h"
      33  #include "debug.h"
      34  
      35  #define SWAP_ENDIAN(x)  swapByteOrder((void *) (&(x)), sizeof(x))
      36  #define AppendString(len, arr, ...) len += snprintf(arr + len, sizeof(arr) - len, __VA_ARGS__)
      37  #define ARR_SIZE(x)     (sizeof (x) / sizeof (*(x)))
      38  
      39  // Utility routines.
      40  
      41  //
      42  // Inline functions
      43  //
      44  // max(a, b) - Return the maximum of two values
      45  inline int
      46  max (int a, int b)
      47  {
      48    return (a >= b) ? a : b;
      49  }
      50  
      51  // min(a, b) - Return the minimum of two values
      52  inline int
      53  min (int a, int b)
      54  {
      55    return (a <= b) ? a : b;
      56  }
      57  
      58  // streq(s1, s2) - Returns 1 if strings are the same, 0 otherwise
      59  inline int
      60  streq (const char *s1, const char *s2)
      61  {
      62    return strcmp (s1, s2) == 0;
      63  }
      64  
      65  // StrChr(str, ch) - Rerurn 'str' if 'ch' does not occur in 'str' or
      66  // a pointer to the next symbol after the first occurrence of 'ch' in 'str'
      67  inline char *
      68  StrChr (char *str, char ch)
      69  {
      70    char *s = strchr (str, ch);
      71    return s ? (s + 1) : str;
      72  }
      73  
      74  // StrRchr(str, ch) - Rerurn 'str' if 'ch' does not occur in 'str' or
      75  // a pointer to the next symbol after the last occurrence of 'ch' in 'str'
      76  inline char *
      77  StrRchr (char *str, char ch)
      78  {
      79    char *s = strrchr (str, ch);
      80    return s ? (s + 1) : str;
      81  }
      82  
      83  inline char*
      84  STR (const char *s)
      85  {
      86    return s ? (char*) s : (char*) NTXT ("NULL");
      87  }
      88  
      89  inline char*
      90  get_str (const char *s, const char *s1)
      91  {
      92    return s ? (char*) s : (char*) s1;
      93  }
      94  
      95  inline char *
      96  get_basename (const char* name)
      97  {
      98    return StrRchr ((char*) name, '/');
      99  }
     100  
     101  inline char *
     102  dbe_strdup (const char *str)
     103  {
     104    return str ? strdup (str) : NULL;
     105  }
     106  
     107  inline long
     108  dbe_sstrlen (const char *str)
     109  {
     110    return str ? (long) strlen (str) : 0;
     111  }
     112  
     113  inline int
     114  dbe_strcmp (const char *s1, const char *s2)
     115  {
     116    return s1 ? (s2 ? strcmp (s1, s2) : 1) : (s2 ? -1 : 0);
     117  }
     118  
     119  // tstodouble(t) - Return timestruc_t in (double) seconds
     120  inline double
     121  tstodouble (timestruc_t t)
     122  {
     123    return (double) t.tv_sec + (double) (t.tv_nsec / 1000000000.0);
     124  }
     125  
     126  inline void
     127  hr2timestruc (timestruc_t *d, hrtime_t s)
     128  {
     129    d->tv_sec = (long) (s / NANOSEC);
     130    d->tv_nsec = (long) (s % NANOSEC);
     131  }
     132  
     133  inline hrtime_t
     134  timestruc2hr (timestruc_t *s)
     135  {
     136    return (hrtime_t) s->tv_sec * NANOSEC + (hrtime_t) s->tv_nsec;
     137  }
     138  
     139  struct stat64;
     140  
     141  #if defined(__cplusplus)
     142  extern "C"
     143  {
     144  #endif
     145    //
     146    // Declaration of utility functions
     147    //
     148    void tsadd (timestruc_t *result, timestruc_t *time);
     149    void tssub (timestruc_t *result, timestruc_t *time1, timestruc_t *time2);
     150    int tscmp (timestruc_t *time1, timestruc_t *time2);
     151    void int_max (int *maximum, int count);
     152    char *strstr_r (char *s1, const char *s2);
     153    char *strrpbrk (const char *string, const char *brkset);
     154    char *read_line (FILE *);
     155    char *parse_qstring (char *in_str, char **endptr);
     156    char *parse_fname (char *in_str, char **fcontext);
     157    int get_paren (const char *name);
     158  
     159    uint64_t crc64 (const char *str, size_t len);
     160    char *canonical_path (char *path);
     161    char *get_relative_path (char *name);
     162    char *get_relative_link (const char *path_to, const char *path_from);
     163    char *get_prog_name (int basename);
     164    char *dbe_strndup (const char *str, size_t len);
     165    int dbe_stat (const char *path, struct stat64 *sbuf);
     166    int dbe_stat_file (const char *path, struct stat64 *sbuf);
     167    char *dbe_read_dir (const char *path, const char *format);
     168    char *dbe_get_processes (const char *format);
     169    char *dbe_create_directories (const char *pathname);
     170    char *dbe_delete_file (const char *pathname);
     171    char *dbe_xml2str (const char *s);
     172    void swapByteOrder (void *p, size_t sz);
     173    char *dbe_sprintf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
     174    ssize_t dbe_write (int f, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
     175    char *dbe_create_symlink_to_path (const char *path, const char *dir);
     176    int64_t read_from_file (int fd, void *buffer, int64_t nbyte);
     177    uint32_t get_cksum (const char * pathname, char ** errmsg);
     178  
     179  #ifdef  __cplusplus
     180  }
     181  int catch_out_of_memory (int (*real_main)(int, char*[]), int argc, char *argv[]);
     182  #endif
     183  
     184  
     185  #endif /* _UTIL_H */