Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "file_image.h"
21 :
22 : #include <unistd.h>
23 :
24 : #include <errno.h>
25 : #include <fcntl.h>
26 :
27 : #include <sys/mman.h>
28 : #include <sys/stat.h>
29 :
30 : #include <string.h>
31 :
32 : /*
33 : * file_image_open
34 : */
35 3402 : int file_image_open (file_image * image, const char * filename)
36 : {
37 3402 : int result = 0;
38 : int fd;
39 : struct stat st;
40 : void * p;
41 :
42 3402 : if (image == 0)
43 0 : return EINVAL;
44 :
45 3402 : image->m_base = MAP_FAILED, image->m_size = 0;
46 :
47 3402 : if ((fd = open (filename, O_RDONLY)) == -1)
48 0 : return errno;
49 :
50 3402 : if (fstat (fd, &st) == -1)
51 : {
52 0 : result = errno;
53 0 : goto cleanup_and_leave;
54 : }
55 :
56 3402 : p = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
57 3402 : if (p == MAP_FAILED)
58 : {
59 0 : result = errno;
60 0 : goto cleanup_and_leave;
61 : }
62 :
63 3402 : image->m_base = p, image->m_size = st.st_size;
64 :
65 : cleanup_and_leave:
66 3402 : close (fd);
67 3402 : return result;
68 : }
69 :
70 : /*
71 : * file_image_pagein.
72 : */
73 3402 : int file_image_pagein (file_image * image)
74 : {
75 : file_image w;
76 : long s;
77 : size_t k;
78 : // force touching of each page despite the optimizer
79 3402 : volatile char c =0;
80 :
81 3402 : if (image == 0)
82 0 : return EINVAL;
83 :
84 3402 : if ((w.m_base = image->m_base) == 0)
85 0 : return EINVAL;
86 3402 : if ((w.m_size = image->m_size) == 0)
87 0 : return 0;
88 :
89 3402 : if (madvise (w.m_base, w.m_size, MADV_WILLNEED) == -1)
90 0 : return errno;
91 :
92 3402 : if ((s = sysconf (_SC_PAGESIZE)) == -1)
93 0 : s = 0x1000;
94 :
95 3402 : k = (size_t)(s);
96 6203736 : while (w.m_size > k)
97 : {
98 6196932 : c ^= ((char*)(w.m_base))[0];
99 6196932 : w.m_base = (char*)(w.m_base) + k;
100 6196932 : w.m_size -= k;
101 : }
102 3402 : if (w.m_size > 0)
103 : {
104 3402 : c ^= ((char*)(w.m_base))[0];
105 : }
106 :
107 3402 : return 0;
108 : }
109 :
110 : /*
111 : * file_image_close
112 : */
113 3402 : int file_image_close (file_image * image)
114 : {
115 3402 : if (image == 0)
116 0 : return EINVAL;
117 :
118 3402 : if (munmap (image->m_base, image->m_size) == -1)
119 0 : return errno;
120 :
121 3402 : image->m_base = 0, image->m_size = 0;
122 3402 : return 0;
123 : }
124 :
125 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|