xref: /netbsd-src/external/gpl3/binutils/dist/gprofng/src/util.h (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1 /* Copyright (C) 2021-2024 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 #ifndef O_LARGEFILE
36 #define O_LARGEFILE 0
37 #endif
38 
39 #define SWAP_ENDIAN(x)  swapByteOrder((void *) (&(x)), sizeof(x))
40 #define AppendString(len, arr, ...) len += snprintf(arr + len, sizeof(arr) - len, __VA_ARGS__)
41 #define ARR_SIZE(x)     (sizeof (x) / sizeof (*(x)))
42 
43 // Utility routines.
44 
45 //
46 // Inline functions
47 //
48 // max(a, b) - Return the maximum of two values
49 inline int
max(int a,int b)50 max (int a, int b)
51 {
52   return (a >= b) ? a : b;
53 }
54 
55 // min(a, b) - Return the minimum of two values
56 inline int
min(int a,int b)57 min (int a, int b)
58 {
59   return (a <= b) ? a : b;
60 }
61 
62 // streq(s1, s2) - Returns 1 if strings are the same, 0 otherwise
63 inline int
streq(const char * s1,const char * s2)64 streq (const char *s1, const char *s2)
65 {
66   return strcmp (s1, s2) == 0;
67 }
68 
69 // StrChr(str, ch) - Rerurn 'str' if 'ch' does not occur in 'str' or
70 // a pointer to the next symbol after the first occurrence of 'ch' in 'str'
71 inline char *
StrChr(char * str,char ch)72 StrChr (char *str, char ch)
73 {
74   char *s = strchr (str, ch);
75   return s ? (s + 1) : str;
76 }
77 
78 // StrRchr(str, ch) - Rerurn 'str' if 'ch' does not occur in 'str' or
79 // a pointer to the next symbol after the last occurrence of 'ch' in 'str'
80 inline char *
StrRchr(char * str,char ch)81 StrRchr (char *str, char ch)
82 {
83   char *s = strrchr (str, ch);
84   return s ? (s + 1) : str;
85 }
86 
87 inline char*
STR(const char * s)88 STR (const char *s)
89 {
90   return s ? (char*) s : (char*) NTXT ("NULL");
91 }
92 
93 inline char*
get_str(const char * s,const char * s1)94 get_str (const char *s, const char *s1)
95 {
96   return s ? (char*) s : (char*) s1;
97 }
98 
99 inline char *
get_basename(const char * name)100 get_basename (const char* name)
101 {
102   return StrRchr ((char*) name, '/');
103 }
104 
105 inline char *
dbe_strdup(const char * str)106 dbe_strdup (const char *str)
107 {
108   return str ? strdup (str) : NULL;
109 }
110 
111 inline long
dbe_sstrlen(const char * str)112 dbe_sstrlen (const char *str)
113 {
114   return str ? (long) strlen (str) : 0;
115 }
116 
117 inline int
dbe_strcmp(const char * s1,const char * s2)118 dbe_strcmp (const char *s1, const char *s2)
119 {
120   return s1 ? (s2 ? strcmp (s1, s2) : 1) : (s2 ? -1 : 0);
121 }
122 
123 // tstodouble(t) - Return timestruc_t in (double) seconds
124 inline double
tstodouble(timestruc_t t)125 tstodouble (timestruc_t t)
126 {
127   return (double) t.tv_sec + (double) (t.tv_nsec / 1000000000.0);
128 }
129 
130 inline void
hr2timestruc(timestruc_t * d,hrtime_t s)131 hr2timestruc (timestruc_t *d, hrtime_t s)
132 {
133   d->tv_sec = (long) (s / NANOSEC);
134   d->tv_nsec = (long) (s % NANOSEC);
135 }
136 
137 inline hrtime_t
timestruc2hr(timestruc_t * s)138 timestruc2hr (timestruc_t *s)
139 {
140   return (hrtime_t) s->tv_sec * NANOSEC + (hrtime_t) s->tv_nsec;
141 }
142 
143 #if defined(__MUSL_LIBC)
144 typedef struct stat dbe_stat_t;
145 #define fstat64 fstat
146 #define open64 open
147 #else
148 typedef struct stat64 dbe_stat_t;
149 #endif
150 
151 #if defined(__cplusplus)
152 extern "C"
153 {
154 #endif
155   //
156   // Declaration of utility functions
157   //
158   void tsadd (timestruc_t *result, timestruc_t *time);
159   void tssub (timestruc_t *result, timestruc_t *time1, timestruc_t *time2);
160   int tscmp (timestruc_t *time1, timestruc_t *time2);
161   void int_max (int *maximum, int count);
162   char *strstr_r (char *s1, const char *s2);
163   char *strrpbrk (const char *string, const char *brkset);
164   char *read_line (FILE *);
165   char *parse_qstring (char *in_str, char **endptr);
166   char *parse_fname (char *in_str, char **fcontext);
167   int get_paren (const char *name);
168 
169   uint64_t crc64 (const char *str, size_t len);
170   char *canonical_path (char *path);
171   char *get_relative_path (char *name);
172   char *get_relative_link (const char *path_to, const char *path_from);
173   char *get_prog_name (int basename);
174   char *dbe_strndup (const char *str, size_t len);
175   int dbe_stat (const char *path, dbe_stat_t *sbuf);
176   int dbe_stat_file (const char *path, dbe_stat_t *sbuf);
177   char *dbe_read_dir (const char *path, const char *format);
178   char *dbe_get_processes (const char *format);
179   char *dbe_create_directories (const char *pathname);
180   char *dbe_delete_file (const char *pathname);
181   char *dbe_xml2str (const char *s);
182   void swapByteOrder (void *p, size_t sz);
183   char *dbe_sprintf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
184   ssize_t dbe_write (int f, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
185   char *dbe_create_symlink_to_path (const char *path, const char *dir);
186   int64_t read_from_file (int fd, void *buffer, int64_t nbyte);
187   uint32_t get_cksum (const char * pathname, char ** errmsg);
188 
189 #ifdef  __cplusplus
190 }
191 int catch_out_of_memory (int (*real_main)(int, char*[]), int argc, char *argv[]);
192 #endif
193 
194 
195 #endif /* _UTIL_H */
196