aboutsummaryrefslogtreecommitdiff
path: root/gprofng/src/util.h
blob: cbbc2f7e118fca09849fbc97df04c0ece0da83d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Copyright (C) 2021-2024 Free Software Foundation, Inc.
   Contributed by Oracle.

   This file is part of GNU Binutils.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */

#ifndef _PERFAN_UTIL_H
#define _PERFAN_UTIL_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <stdint.h>

#include "gp-defs.h"
#include "gp-time.h"
#include "i18n.h"
#include "debug.h"

#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif

#define SWAP_ENDIAN(x)  swapByteOrder((void *) (&(x)), sizeof(x))
#define AppendString(len, arr, ...) len += snprintf(arr + len, sizeof(arr) - len, __VA_ARGS__)
#define ARR_SIZE(x)     (sizeof (x) / sizeof (*(x)))

// Utility routines.

//
// Inline functions
//
// max(a, b) - Return the maximum of two values
inline int
max (int a, int b)
{
  return (a >= b) ? a : b;
}

// min(a, b) - Return the minimum of two values
inline int
min (int a, int b)
{
  return (a <= b) ? a : b;
}

// streq(s1, s2) - Returns 1 if strings are the same, 0 otherwise
inline int
streq (const char *s1, const char *s2)
{
  return strcmp (s1, s2) == 0;
}

// StrChr(str, ch) - Rerurn 'str' if 'ch' does not occur in 'str' or
// a pointer to the next symbol after the first occurrence of 'ch' in 'str'
inline char *
StrChr (char *str, char ch)
{
  char *s = strchr (str, ch);
  return s ? (s + 1) : str;
}

// StrRchr(str, ch) - Rerurn 'str' if 'ch' does not occur in 'str' or
// a pointer to the next symbol after the last occurrence of 'ch' in 'str'
inline char *
StrRchr (char *str, char ch)
{
  char *s = strrchr (str, ch);
  return s ? (s + 1) : str;
}

inline char*
STR (const char *s)
{
  return s ? (char*) s : (char*) NTXT ("NULL");
}

inline char*
get_str (const char *s, const char *s1)
{
  return s ? (char*) s : (char*) s1;
}

inline char *
get_basename (const char* name)
{
  return StrRchr ((char*) name, '/');
}

inline char *
dbe_strdup (const char *str)
{
  return str ? strdup (str) : NULL;
}

inline long
dbe_sstrlen (const char *str)
{
  return str ? (long) strlen (str) : 0;
}

inline int
dbe_strcmp (const char *s1, const char *s2)
{
  return s1 ? (s2 ? strcmp (s1, s2) : 1) : (s2 ? -1 : 0);
}

// tstodouble(t) - Return timestruc_t in (double) seconds
inline double
tstodouble (timestruc_t t)
{
  return (double) t.tv_sec + (double) (t.tv_nsec / 1000000000.0);
}

inline void
hr2timestruc (timestruc_t *d, hrtime_t s)
{
  d->tv_sec = (long) (s / NANOSEC);
  d->tv_nsec = (long) (s % NANOSEC);
}

inline hrtime_t
timestruc2hr (timestruc_t *s)
{
  return (hrtime_t) s->tv_sec * NANOSEC + (hrtime_t) s->tv_nsec;
}

#if defined(__MUSL_LIBC)
typedef struct stat dbe_stat_t;
#define fstat64 fstat
#define open64 open
#else
typedef struct stat64 dbe_stat_t;
#endif

#if defined(__cplusplus)
extern "C"
{
#endif
  //
  // Declaration of utility functions
  //
  void tsadd (timestruc_t *result, timestruc_t *time);
  void tssub (timestruc_t *result, timestruc_t *time1, timestruc_t *time2);
  int tscmp (timestruc_t *time1, timestruc_t *time2);
  void int_max (int *maximum, int count);
  char *strstr_r (char *s1, const char *s2);
  char *strrpbrk (const char *string, const char *brkset);
  char *read_line (FILE *);
  char *parse_qstring (char *in_str, char **endptr);
  char *parse_fname (char *in_str, char **fcontext);
  int get_paren (const char *name);

  uint64_t crc64 (const char *str, size_t len);
  char *canonical_path (char *path);
  char *get_relative_path (char *name);
  char *get_relative_link (const char *path_to, const char *path_from);
  char *get_prog_name (int basename);
  char *dbe_strndup (const char *str, size_t len);
  int dbe_stat (const char *path, dbe_stat_t *sbuf);
  int dbe_stat_file (const char *path, dbe_stat_t *sbuf);
  char *dbe_read_dir (const char *path, const char *format);
  char *dbe_get_processes (const char *format);
  char *dbe_create_directories (const char *pathname);
  char *dbe_delete_file (const char *pathname);
  char *dbe_xml2str (const char *s);
  void swapByteOrder (void *p, size_t sz);
  char *dbe_sprintf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
  ssize_t dbe_write (int f, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
  char *dbe_create_symlink_to_path (const char *path, const char *dir);
  int64_t read_from_file (int fd, void *buffer, int64_t nbyte);
  uint32_t get_cksum (const char * pathname, char ** errmsg);

#ifdef  __cplusplus
}
int catch_out_of_memory (int (*real_main)(int, char*[]), int argc, char *argv[]);
#endif


#endif /* _UTIL_H */