aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
blob: 67d0a8481b9402869698b21c8c028392e462a10c (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
// src/utils.c
#include <stddef.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#include "utils.h"

const char *progname;

struct sv
mmap_whole (int fd)
{
  struct sv sv;

  struct stat st;
  int ret = fstat (fd, &st);
  UNUSED (ret);
  ASSERT_MSG (ret != -1, "fstat: %s", strerror (errno));
  ASSERT (S_ISREG (st.st_mode));

  posix_fadvise (fd, 0, st.st_size, POSIX_FADV_SEQUENTIAL |
                                    POSIX_FADV_WILLNEED |
                                    POSIX_FADV_NOREUSE);

  sv.beg = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE |
                                              MAP_POPULATE |
                                              MAP_NONBLOCK, fd, 0);
  ASSERT_MSG (sv.beg != MAP_FAILED, "mmap: %s", strerror (errno));
  posix_madvise ((void *) sv.beg, st.st_size, MADV_HUGEPAGE | MADV_WILLNEED);

  sv.end = sv.beg + st.st_size;

  return sv;
}

struct sv
read_until_eof (struct arena *a, int fd)
{
  struct arena_checkpoint cp = arena_checkpoint (a, 1);
  size_t reserve = 4096;

  size_t read_total = 0;
  for (;;) {
    arena_reserve (a, reserve, 1);
    ssize_t bytes_read =
      read (fd, arena_next_free (a, 1), arena_room (a, 1));

    if (bytes_read < 0) {
      DEBUG ("read: %s", strerror (errno));
      arena_rollback_to (a, cp);
      return (struct sv) {0};
    }

    if (bytes_read == 0)
      break;

    read_total += bytes_read;

    arena_allocate_fast (a, bytes_read, 1);
    reserve *= 2;
  }

  if (a->tail->free == cp.free)
    return (struct sv) {0};

  char *ptr = arena_coalesce_from_fast (a, cp, read_total);
  return (struct sv) {
    .beg = ptr,
    .end = ptr + read_total,
  };
}

struct location
find_location (struct sv haystack, struct sv needle)
{
  ASSERT (needle.beg >= haystack.beg);
  ASSERT (needle.end <= haystack.end);

  struct location loc = { .beg = { 1, 1 } };

  const char *head;
  for (head = haystack.beg; head < needle.beg; ++head) {
    loc.beg.column += 1;
    if (*head == '\n') {
      loc.beg.line += 1;
      loc.beg.column = 1;
    }
  }

  loc.end.line   = loc.beg.line;
  loc.end.column = loc.beg.column - 1;

  for (; head < needle.end; ++head) {
    loc.end.column += 1;
    if (*head == '\n') {
      loc.end.line += 1;
      loc.end.column = 1;
    }
  }

  return loc;
}

void
print_location (const char *filename, struct location loc,
                int alignment, const char *format, ...)
{
  int printed = 0;

  printed += printf ("%s:", filename);
  if (loc.end.line > loc.beg.line) {
    printed += printf ("%d-%d", loc.beg.line, loc.end.line);
  } else {
    printed += printf ("%d:%d-%d", loc.beg.line, loc.beg.column,
            loc.end.column);
  } printed += printf (": info: ");

  while (printed < alignment) {
    putchar (' ');
    printed += 1;
  }

  va_list ap;
  va_start (ap, format);
  vprintf (format, ap);
  va_end (ap);
  printf ("\n");
}

// src/utils.c ends here