aboutsummaryrefslogtreecommitdiff
path: root/src/utils.h
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-10 08:29:58 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-10 08:33:56 +0000
commit776276d5c7e67356a880f3e31076d32cdc1c1882 (patch)
treecbde131f3d46338adee9c67ac0f988295736bdd4 /src/utils.h
downloadcoursework-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/utils.h')
-rw-r--r--src/utils.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..d8e7dda
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,95 @@
1// utils.h
2#ifndef INCLUDED_UTILS_H
3#define INCLUDED_UTILS_H
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <stdint.h>
8#include <string.h>
9#include <time.h>
10
11#include "string-view.h"
12
13#define NOP() do {} while (0)
14
15#define ARRAY_LENGTH(ARR) (sizeof (ARR)/sizeof (*(ARR)))
16
17#ifndef NDEBUG
18#include <assert.h>
19#define ASSERT assert
20#else
21#define ASSERT(...) NOP ()
22#endif
23
24#define UNUSED(EXP) ((void) EXP)
25
26#define FALLTHROUGH __attribute__ ((__fallthrough__))
27
28#define STRINGIFY(ARG) #ARG
29#define TO_STRING(ARG) STRINGIFY (ARG)
30
31#ifndef NDEBUG
32#define DEBUG(FMT, ...) \
33 fprintf (stderr, __FILE__ ":" TO_STRING (__LINE__) ": %s: " FMT "\n", \
34 __func__ __VA_OPT__ (,) __VA_ARGS__)
35#else
36#define DEBUG(...) NOP ()
37#endif
38
39#ifndef NDEBUG
40#define ASSERT_MSG(EXP, FMT, ...) do if (!(EXP)) { \
41 DEBUG (FMT __VA_OPT__ (,) __VA_ARGS__); \
42 exit (EXIT_FAILURE); \
43 } while (0)
44#else
45#define ASSERT_MSG(EXP, FMT, ...) NOP ()
46#endif
47
48#define MEASURE(FMT, ...) \
49 for (struct timespec __ts0, __ts1, *__once = NULL; \
50 clock_gettime (CLOCK_MONOTONIC, &__ts0), !__once; \
51 clock_gettime (CLOCK_MONOTONIC, &__ts1), \
52 fprintf (stderr, __FILE__ ":" TO_STRING (__LINE__) \
53 ": %s: " FMT " %.6fs\n", __func__, \
54 __VA_ARGS__ __VA_OPT__ (,) \
55 (__ts1.tv_sec - __ts0.tv_sec ) + \
56 (__ts1.tv_nsec - __ts0.tv_nsec) / 1e9), \
57 __once += 1)
58
59#define MIN(A, B) ((A) <= (B) ? (A) : (B))
60
61struct timer_scope {
62 char *fmt;
63 struct timespec start;
64};
65
66static void
67timer_end (struct timer_scope *t)
68{
69 struct timespec end;
70 clock_gettime (CLOCK_MONOTONIC, &end);
71
72 double s = ((end.tv_sec - t->start.tv_sec) +
73 (end.tv_nsec - t->start.tv_nsec)/1e9);
74
75 fprintf (stderr, t->fmt, s);
76 if (t->fmt[strlen (t->fmt) - 1] != ' ')
77 putc (' ', stderr);
78 fprintf (stderr, "took %.6fs\n", s);
79 free (t->fmt);
80}
81
82#define MEASURE_SCOPE(FMT, ...) \
83 __attribute__ ((cleanup (timer_end))) struct timer_scope __ts; \
84 asprintf (&__ts.fmt, \
85 __FILE__ ":" TO_STRING (__LINE__) ": %s: " FMT, \
86 __func__ __VA_OPT__ (,) __VA_ARGS__); \
87 clock_gettime (CLOCK_MONOTONIC, &__ts.start)
88
89struct string_view mmap_whole (int fd);
90struct string_view mmap_n_from (int fd, size_t n, off_t offset);
91
92int asprintf (char **str, const char *fmt, ...);
93
94#endif // INCLUDED_UTILS_H
95// utils.h ends here