aboutsummaryrefslogtreecommitdiff
path: root/src/utils.h
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 22:28:13 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 22:47:34 +0000
commitcb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch)
treec7d90c0abe9533c87d51b5196871846855efa1ba /src/utils.h
downloadthesis-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/utils.h')
-rw-r--r--src/utils.h132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..b6c38d5
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,132 @@
1// src/utils.h
2
3#ifndef INCLUDED_UTILS_H
4#define INCLUDED_UTILS_H
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <stdint.h>
9#include <string.h>
10#include <time.h>
11
12#include "sv.h"
13#include "arena.h"
14
15#define NOP() do {} while (0)
16
17#define ARRAY_LENGTH(ARR) (sizeof (ARR)/sizeof (*(ARR)))
18
19#ifndef NDEBUG
20#include <assert.h>
21#define ASSERT assert
22#else
23#define ASSERT(...) NOP ()
24#endif
25
26#define MALLOC malloc
27#define REALLOC realloc
28#define FREE free
29
30#define UNUSED(EXP) ((void) EXP)
31
32#define FALLTHROUGH __attribute__ ((__fallthrough__))
33
34#define STRINGIFY(ARG) #ARG
35#define TO_STRING(ARG) STRINGIFY (ARG)
36
37#ifndef NDEBUG
38#define DEBUG(FMT, ...) \
39 fprintf (stderr, __FILE__ ":" TO_STRING (__LINE__) ": %s: " FMT "\n", \
40 __func__, ##__VA_ARGS__)
41#define LOG(FMT, ...) \
42 fprintf (stderr, __FILE__ ":" TO_STRING (__LINE__) ": %s: " FMT "\n", \
43 __func__, ##__VA_ARGS__)
44#else
45#define DEBUG(...) NOP ()
46#define LOG(FMT, ...) \
47 fprintf (stderr, "%s: " FMT "\n", progname, ##__VA_ARGS__)
48#endif
49
50#define FATAL(FMT, ...) LOG ("fatal: " FMT, ##__VA_ARGS__)
51#define ERROR(FMT, ...) LOG ("error: " FMT, ##__VA_ARGS__)
52#define WARNING(FMT, ...) LOG ("warning: " FMT, ##__VA_ARGS__)
53#define NOTICE(FMT, ...) LOG ("notice: " FMT, ##__VA_ARGS__)
54#define INFO(FMT, ...) LOG ("info: " FMT, ##__VA_ARGS__)
55
56#define DIE(FMT, ...) do { \
57 FATAL (FMT, ##__VA_ARGS__); \
58 exit (EXIT_FAILURE); \
59 } while (0)
60
61#ifndef NDEBUG
62#define ASSERT_MSG(EXP, FMT, ...) do if (!(EXP)) { \
63 DEBUG (FMT, ##__VA_ARGS__); \
64 exit (EXIT_FAILURE); \
65 } while (0)
66#else
67#define ASSERT_MSG(EXP, FMT, ...) NOP ()
68#endif
69
70#define MEASURE(FMT, ...) \
71 for (struct timespec _ts0, _ts1, *_once = NULL; \
72 clock_gettime (CLOCK_MONOTONIC, &_ts0), !_once; \
73 clock_gettime (CLOCK_MONOTONIC, &_ts1), \
74 DEBUG (FMT " %.6fs\n", ##__VA_ARGS__, \
75 (_ts1.tv_sec - _ts0.tv_sec ) + \
76 (_ts1.tv_nsec - _ts0.tv_nsec) / 1e9), \
77 _once += 1)
78
79#define MIN(A, B) ((A) <= (B) ? (A) : (B))
80
81struct timer_scope {
82 char *fmt;
83 struct timespec start;
84};
85
86static void
87timer_end (struct timer_scope *t)
88{
89 struct timespec end;
90 clock_gettime (CLOCK_MONOTONIC, &end);
91
92 double s = ((end.tv_sec - t->start.tv_sec) +
93 (end.tv_nsec - t->start.tv_nsec)/1e9);
94
95 fprintf (stderr, t->fmt, s);
96 if (t->fmt[strlen (t->fmt) - 1] != ' ')
97 putc (' ', stderr);
98 fprintf (stderr, "took %.6fs\n", s);
99 free (t->fmt);
100}
101
102#define MEASURE_SCOPE(FMT, ...) \
103 __attribute__ ((cleanup (timer_end))) struct timer_scope __ts; \
104 asprintf (&__ts.fmt, \
105 __FILE__ ":" TO_STRING (__LINE__) ": %s: " FMT, \
106 __func__, ##__VA_ARGS__); \
107 clock_gettime (CLOCK_MONOTONIC, &__ts.start)
108
109struct location {
110 struct {
111 int line;
112 int column;
113 } beg;
114 struct {
115 int line;
116 int column;
117 } end;
118};
119
120extern const char *progname;
121
122struct sv mmap_whole (int fd);
123struct sv read_until_eof (struct arena *, int fd);
124
125struct location find_location (struct sv haystack, struct sv needle);
126void print_location (const char *filename, struct location, int alignment, const char *format, ...);
127
128void print_back_trace (int skip);
129
130#endif // INCLUDED_UTILS_H
131
132// src/utils.h ends here