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 4992 : int file_image_open (file_image * image, const char * filename)
36 : {
37 4992 : int result = 0;
38 : int fd;
39 : struct stat st;
40 : void * p;
41 :
42 4992 : if (image == 0)
43 0 : return (EINVAL);
44 :
45 4992 : image->m_base = MAP_FAILED, image->m_size = 0;
46 :
47 4992 : if ((fd = open (filename, O_RDONLY)) == -1)
48 192 : return (errno);
49 :
50 4800 : if (fstat (fd, &st) == -1)
51 : {
52 0 : result = errno;
53 0 : goto cleanup_and_leave;
54 : }
55 :
56 4800 : p = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
57 4800 : if (p == MAP_FAILED)
58 : {
59 0 : result = errno;
60 0 : goto cleanup_and_leave;
61 : }
62 :
63 4800 : image->m_base = p, image->m_size = st.st_size;
64 :
65 : cleanup_and_leave:
66 4800 : close (fd);
67 4800 : return (result);
68 : }
69 :
70 : /*
71 : * file_image_pagein.
72 : */
73 4800 : 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 4800 : volatile char c = 0;
80 :
81 4800 : if (image == 0)
82 0 : return (EINVAL);
83 :
84 4800 : if ((w.m_base = image->m_base) == 0)
85 0 : return (EINVAL);
86 4800 : if ((w.m_size = image->m_size) == 0)
87 0 : return (0);
88 :
89 4800 : if (madvise (w.m_base, w.m_size, MADV_WILLNEED) == -1)
90 0 : return (errno);
91 :
92 4800 : if ((s = sysconf (_SC_PAGESIZE)) == -1)
93 0 : s = 0x1000;
94 :
95 4800 : k = (size_t)(s);
96 9191136 : while (w.m_size > k)
97 : {
98 9181536 : c ^= ((char*)(w.m_base))[0];
99 9181536 : w.m_base = (char*)(w.m_base) + k;
100 9181536 : w.m_size -= k;
101 : }
102 4800 : if (w.m_size > 0)
103 : {
104 4800 : c ^= ((char*)(w.m_base))[0];
105 : }
106 :
107 4800 : return (0);
108 : }
109 :
110 : /*
111 : * file_image_close
112 : */
113 4800 : int file_image_close (file_image * image)
114 : {
115 4800 : if (image == 0)
116 0 : return (EINVAL);
117 :
118 4800 : if (munmap (image->m_base, image->m_size) == -1)
119 0 : return (errno);
120 :
121 4800 : image->m_base = 0, image->m_size = 0;
122 4800 : return (0);
123 : }
124 :
125 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|