]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-mmap-cache.c
Merge pull request #6355 from vcaputo/journal_avoid_mmap_cache_get_calls
[thirdparty/systemd.git] / src / journal / test-mmap-cache.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <sys/mman.h>
23 #include <unistd.h>
24
25 #include "fd-util.h"
26 #include "fileio.h"
27 #include "macro.h"
28 #include "mmap-cache.h"
29 #include "util.h"
30
31 int main(int argc, char *argv[]) {
32 MMapFileDescriptor *fx;
33 int x, y, z, r;
34 char px[] = "/tmp/testmmapXXXXXXX", py[] = "/tmp/testmmapYXXXXXX", pz[] = "/tmp/testmmapZXXXXXX";
35 MMapCache *m;
36 void *p, *q;
37
38 assert_se(m = mmap_cache_new());
39
40 x = mkostemp_safe(px);
41 assert_se(x >= 0);
42 unlink(px);
43
44 assert_se(fx = mmap_cache_add_fd(m, x));
45
46 y = mkostemp_safe(py);
47 assert_se(y >= 0);
48 unlink(py);
49
50 z = mkostemp_safe(pz);
51 assert_se(z >= 0);
52 unlink(pz);
53
54 r = mmap_cache_get(m, fx, PROT_READ, 0, false, 1, 2, NULL, &p, NULL);
55 assert_se(r >= 0);
56
57 r = mmap_cache_get(m, fx, PROT_READ, 0, false, 2, 2, NULL, &q, NULL);
58 assert_se(r >= 0);
59
60 assert_se((uint8_t*) p + 1 == (uint8_t*) q);
61
62 r = mmap_cache_get(m, fx, PROT_READ, 1, false, 3, 2, NULL, &q, NULL);
63 assert_se(r >= 0);
64
65 assert_se((uint8_t*) p + 2 == (uint8_t*) q);
66
67 r = mmap_cache_get(m, fx, PROT_READ, 0, false, 16ULL*1024ULL*1024ULL, 2, NULL, &p, NULL);
68 assert_se(r >= 0);
69
70 r = mmap_cache_get(m, fx, PROT_READ, 1, false, 16ULL*1024ULL*1024ULL+1, 2, NULL, &q, NULL);
71 assert_se(r >= 0);
72
73 assert_se((uint8_t*) p + 1 == (uint8_t*) q);
74
75 mmap_cache_free_fd(m, fx);
76 mmap_cache_unref(m);
77
78 safe_close(x);
79 safe_close(y);
80 safe_close(z);
81
82 return 0;
83 }