aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
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.c
downloadthesis-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..67d0a84
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,135 @@
1// src/utils.c
2#include <stddef.h>
3#include <stdarg.h>
4#include <unistd.h>
5#include <sys/stat.h>
6#include <sys/mman.h>
7#include <string.h>
8#include <errno.h>
9#include <fcntl.h>
10
11#include "utils.h"
12
13const char *progname;
14
15struct sv
16mmap_whole (int fd)
17{
18 struct sv sv;
19
20 struct stat st;
21 int ret = fstat (fd, &st);
22 UNUSED (ret);
23 ASSERT_MSG (ret != -1, "fstat: %s", strerror (errno));
24 ASSERT (S_ISREG (st.st_mode));
25
26 posix_fadvise (fd, 0, st.st_size, POSIX_FADV_SEQUENTIAL |
27 POSIX_FADV_WILLNEED |
28 POSIX_FADV_NOREUSE);
29
30 sv.beg = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE |
31 MAP_POPULATE |
32 MAP_NONBLOCK, fd, 0);
33 ASSERT_MSG (sv.beg != MAP_FAILED, "mmap: %s", strerror (errno));
34 posix_madvise ((void *) sv.beg, st.st_size, MADV_HUGEPAGE | MADV_WILLNEED);
35
36 sv.end = sv.beg + st.st_size;
37
38 return sv;
39}
40
41struct sv
42read_until_eof (struct arena *a, int fd)
43{
44 struct arena_checkpoint cp = arena_checkpoint (a, 1);
45 size_t reserve = 4096;
46
47 size_t read_total = 0;
48 for (;;) {
49 arena_reserve (a, reserve, 1);
50 ssize_t bytes_read =
51 read (fd, arena_next_free (a, 1), arena_room (a, 1));
52
53 if (bytes_read < 0) {
54 DEBUG ("read: %s", strerror (errno));
55 arena_rollback_to (a, cp);
56 return (struct sv) {0};
57 }
58
59 if (bytes_read == 0)
60 break;
61
62 read_total += bytes_read;
63
64 arena_allocate_fast (a, bytes_read, 1);
65 reserve *= 2;
66 }
67
68 if (a->tail->free == cp.free)
69 return (struct sv) {0};
70
71 char *ptr = arena_coalesce_from_fast (a, cp, read_total);
72 return (struct sv) {
73 .beg = ptr,
74 .end = ptr + read_total,
75 };
76}
77
78struct location
79find_location (struct sv haystack, struct sv needle)
80{
81 ASSERT (needle.beg >= haystack.beg);
82 ASSERT (needle.end <= haystack.end);
83
84 struct location loc = { .beg = { 1, 1 } };
85
86 const char *head;
87 for (head = haystack.beg; head < needle.beg; ++head) {
88 loc.beg.column += 1;
89 if (*head == '\n') {
90 loc.beg.line += 1;
91 loc.beg.column = 1;
92 }
93 }
94
95 loc.end.line = loc.beg.line;
96 loc.end.column = loc.beg.column - 1;
97
98 for (; head < needle.end; ++head) {
99 loc.end.column += 1;
100 if (*head == '\n') {
101 loc.end.line += 1;
102 loc.end.column = 1;
103 }
104 }
105
106 return loc;
107}
108
109void
110print_location (const char *filename, struct location loc,
111 int alignment, const char *format, ...)
112{
113 int printed = 0;
114
115 printed += printf ("%s:", filename);
116 if (loc.end.line > loc.beg.line) {
117 printed += printf ("%d-%d", loc.beg.line, loc.end.line);
118 } else {
119 printed += printf ("%d:%d-%d", loc.beg.line, loc.beg.column,
120 loc.end.column);
121 } printed += printf (": info: ");
122
123 while (printed < alignment) {
124 putchar (' ');
125 printed += 1;
126 }
127
128 va_list ap;
129 va_start (ap, format);
130 vprintf (format, ap);
131 va_end (ap);
132 printf ("\n");
133}
134
135// src/utils.c ends here