1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <sys/mman.h>
6 #include <unistd.h>
7
8 #include "fd-util.h"
9 #include "macro.h"
10 #include "mmap-cache.h"
11 #include "tmpfile-util.h"
12 #include "util.h"
13
main(int argc,char * argv[])14 int main(int argc, char *argv[]) {
15 MMapFileDescriptor *fx;
16 int x, y, z, r;
17 char px[] = "/tmp/testmmapXXXXXXX", py[] = "/tmp/testmmapYXXXXXX", pz[] = "/tmp/testmmapZXXXXXX";
18 MMapCache *m;
19 void *p, *q;
20
21 assert_se(m = mmap_cache_new());
22
23 x = mkostemp_safe(px);
24 assert_se(x >= 0);
25 unlink(px);
26
27 assert_se(fx = mmap_cache_add_fd(m, x, PROT_READ));
28
29 y = mkostemp_safe(py);
30 assert_se(y >= 0);
31 unlink(py);
32
33 z = mkostemp_safe(pz);
34 assert_se(z >= 0);
35 unlink(pz);
36
37 r = mmap_cache_fd_get(fx, 0, false, 1, 2, NULL, &p);
38 assert_se(r >= 0);
39
40 r = mmap_cache_fd_get(fx, 0, false, 2, 2, NULL, &q);
41 assert_se(r >= 0);
42
43 assert_se((uint8_t*) p + 1 == (uint8_t*) q);
44
45 r = mmap_cache_fd_get(fx, 1, false, 3, 2, NULL, &q);
46 assert_se(r >= 0);
47
48 assert_se((uint8_t*) p + 2 == (uint8_t*) q);
49
50 r = mmap_cache_fd_get(fx, 0, false, 16ULL*1024ULL*1024ULL, 2, NULL, &p);
51 assert_se(r >= 0);
52
53 r = mmap_cache_fd_get(fx, 1, false, 16ULL*1024ULL*1024ULL+1, 2, NULL, &q);
54 assert_se(r >= 0);
55
56 assert_se((uint8_t*) p + 1 == (uint8_t*) q);
57
58 mmap_cache_fd_free(fx);
59 mmap_cache_unref(m);
60
61 safe_close(x);
62 safe_close(y);
63 safe_close(z);
64
65 return 0;
66 }
67