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