]>
Commit | Line | Data |
---|---|---|
2a2507e6 LP |
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 | ||
0d39fa9c | 20 | #include <fcntl.h> |
2a2507e6 LP |
21 | #include <stdlib.h> |
22 | #include <sys/mman.h> | |
23 | #include <unistd.h> | |
24 | ||
3ffd4af2 | 25 | #include "fd-util.h" |
0d39fa9c | 26 | #include "fileio.h" |
2a2507e6 | 27 | #include "macro.h" |
2a2507e6 | 28 | #include "mmap-cache.h" |
3ffd4af2 | 29 | #include "util.h" |
2a2507e6 LP |
30 | |
31 | int main(int argc, char *argv[]) { | |
32 | int x, y, z, r; | |
33 | char px[] = "/tmp/testmmapXXXXXXX", py[] = "/tmp/testmmapYXXXXXX", pz[] = "/tmp/testmmapZXXXXXX"; | |
34 | MMapCache *m; | |
35 | void *p, *q; | |
36 | ||
37 | assert_se(m = mmap_cache_new()); | |
38 | ||
2d5bdf5b | 39 | x = mkostemp_safe(px, O_RDWR|O_CLOEXEC); |
787784c4 | 40 | assert_se(x >= 0); |
2a2507e6 LP |
41 | unlink(px); |
42 | ||
2d5bdf5b | 43 | y = mkostemp_safe(py, O_RDWR|O_CLOEXEC); |
787784c4 | 44 | assert_se(y >= 0); |
2a2507e6 LP |
45 | unlink(py); |
46 | ||
2d5bdf5b | 47 | z = mkostemp_safe(pz, O_RDWR|O_CLOEXEC); |
787784c4 | 48 | assert_se(z >= 0); |
2a2507e6 LP |
49 | unlink(pz); |
50 | ||
1b8951e5 | 51 | r = mmap_cache_get(m, x, PROT_READ, 0, false, 1, 2, NULL, &p); |
787784c4 | 52 | assert_se(r >= 0); |
2a2507e6 | 53 | |
1b8951e5 | 54 | r = mmap_cache_get(m, x, PROT_READ, 0, false, 2, 2, NULL, &q); |
787784c4 | 55 | assert_se(r >= 0); |
2a2507e6 | 56 | |
787784c4 | 57 | assert_se((uint8_t*) p + 1 == (uint8_t*) q); |
2a2507e6 | 58 | |
1b8951e5 | 59 | r = mmap_cache_get(m, x, PROT_READ, 1, false, 3, 2, NULL, &q); |
787784c4 | 60 | assert_se(r >= 0); |
2a2507e6 | 61 | |
787784c4 | 62 | assert_se((uint8_t*) p + 2 == (uint8_t*) q); |
2a2507e6 | 63 | |
1b8951e5 | 64 | r = mmap_cache_get(m, x, PROT_READ, 0, false, 16ULL*1024ULL*1024ULL, 2, NULL, &p); |
787784c4 | 65 | assert_se(r >= 0); |
2a2507e6 | 66 | |
1b8951e5 | 67 | r = mmap_cache_get(m, x, PROT_READ, 1, false, 16ULL*1024ULL*1024ULL+1, 2, NULL, &q); |
787784c4 | 68 | assert_se(r >= 0); |
2a2507e6 | 69 | |
787784c4 | 70 | assert_se((uint8_t*) p + 1 == (uint8_t*) q); |
2a2507e6 LP |
71 | |
72 | mmap_cache_unref(m); | |
73 | ||
03e334a1 LP |
74 | safe_close(x); |
75 | safe_close(y); | |
76 | safe_close(z); | |
2a2507e6 LP |
77 | |
78 | return 0; | |
79 | } |