diff options
| author | 2026-11-10 08:29:58 +0000 | |
|---|---|---|
| committer | 2026-11-10 08:33:56 +0000 | |
| commit | 776276d5c7e67356a880f3e31076d32cdc1c1882 (patch) | |
| tree | cbde131f3d46338adee9c67ac0f988295736bdd4 /src/utils.c | |
| download | coursework-776276d5c7e67356a880f3e31076d32cdc1c1882.tar.gz | |
Diffstat (limited to 'src/utils.c')
| -rw-r--r-- | src/utils.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..ea4b936 --- /dev/null +++ b/src/utils.c | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | // utils.c | ||
| 2 | #include <stddef.h> | ||
| 3 | #include <stdarg.h> | ||
| 4 | #include <sys/stat.h> | ||
| 5 | #include <sys/mman.h> | ||
| 6 | #include <string.h> | ||
| 7 | #include <errno.h> | ||
| 8 | #include <fcntl.h> | ||
| 9 | |||
| 10 | #include "utils.h" | ||
| 11 | |||
| 12 | struct string_view | ||
| 13 | mmap_whole (int fd) | ||
| 14 | { | ||
| 15 | struct string_view sv; | ||
| 16 | |||
| 17 | struct stat st; | ||
| 18 | int ret = fstat (fd, &st); | ||
| 19 | UNUSED (ret); | ||
| 20 | ASSERT_MSG (ret != -1, "fstat: %s", strerror (errno)); | ||
| 21 | ASSERT (S_ISREG (st.st_mode)); | ||
| 22 | |||
| 23 | posix_fadvise (fd, 0, st.st_size, POSIX_FADV_SEQUENTIAL | | ||
| 24 | POSIX_FADV_WILLNEED | | ||
| 25 | POSIX_FADV_NOREUSE); | ||
| 26 | |||
| 27 | sv.begin = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE | | ||
| 28 | MAP_POPULATE | | ||
| 29 | MAP_NONBLOCK, fd, 0); | ||
| 30 | ASSERT_MSG (sv.begin != MAP_FAILED, "mmap: %s", strerror (errno)); | ||
| 31 | posix_madvise ((void *) sv.begin, st.st_size, MADV_HUGEPAGE | MADV_WILLNEED); | ||
| 32 | |||
| 33 | sv.end = sv.begin + st.st_size; | ||
| 34 | |||
| 35 | return sv; | ||
| 36 | } | ||
| 37 | |||
| 38 | struct string_view | ||
| 39 | mmap_n_from (int fd, size_t n, off_t offset) | ||
| 40 | { | ||
| 41 | struct string_view sv; | ||
| 42 | |||
| 43 | posix_fadvise (fd, offset, n, POSIX_FADV_SEQUENTIAL | | ||
| 44 | POSIX_FADV_WILLNEED | | ||
| 45 | POSIX_FADV_NOREUSE); | ||
| 46 | |||
| 47 | sv.begin = mmap (NULL, n, PROT_READ, MAP_PRIVATE | | ||
| 48 | MAP_POPULATE | | ||
| 49 | MAP_NONBLOCK, fd, offset); | ||
| 50 | ASSERT_MSG (sv.begin != MAP_FAILED, "mmap: %s", strerror (errno)); | ||
| 51 | posix_madvise ((void *) sv.begin, n, MADV_HUGEPAGE | MADV_WILLNEED); | ||
| 52 | |||
| 53 | sv.end = sv.begin + n; | ||
| 54 | |||
| 55 | return sv; | ||
| 56 | } | ||
| 57 | |||
| 58 | int | ||
| 59 | vasprintf (char **str, const char *fmt, va_list args) | ||
| 60 | { | ||
| 61 | int size = 0; | ||
| 62 | va_list tmpa; | ||
| 63 | |||
| 64 | va_copy (tmpa, args); | ||
| 65 | size = vsnprintf (NULL, 0, fmt, tmpa); | ||
| 66 | va_end (tmpa); | ||
| 67 | |||
| 68 | if (size < 0) | ||
| 69 | return -1; | ||
| 70 | |||
| 71 | *str = (char *) malloc (size + 1); | ||
| 72 | if (NULL == *str) | ||
| 73 | return -1; | ||
| 74 | |||
| 75 | size = vsprintf (*str, fmt, args); | ||
| 76 | return size; | ||
| 77 | } | ||
| 78 | |||
| 79 | int | ||
| 80 | asprintf (char **str, const char *fmt, ...) | ||
| 81 | { | ||
| 82 | int size = 0; | ||
| 83 | va_list args; | ||
| 84 | |||
| 85 | va_start (args, fmt); | ||
| 86 | size = vasprintf (str, fmt, args); | ||
| 87 | va_end (args); | ||
| 88 | |||
| 89 | return size; | ||
| 90 | } | ||
| 91 | // utils.c ends here | ||
