summaryrefslogtreecommitdiff
path: root/cbits/mmap.c
blob: 9d6be17d94dcdfab3f45d49d936ec9aa6b0b3169 (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
/* Copyright (c) Tom Smeding, 2026
 * Licensed under the GNU Affero General Public Licence v3 or later; see COPYING.
 */
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>


void* tirclogv_mmap(int fd, size_t *lengthp) {
  struct stat statbuf;
  int ret = fstat(fd, &statbuf);
  if (ret < 0) {
    perror("stat");
    return NULL;
  }
  void *addr = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED | MAP_POPULATE, fd, 0);
  if (addr == NULL) {
    perror("mmap");
  }
  if (lengthp != NULL) *lengthp = statbuf.st_size;
  return addr;
}

void tirclogv_munmap(void *addr, size_t length) {
  int ret = munmap(addr, length);
  if (ret < 0) {
    perror("munmap");
  }
}