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 : #if defined(LINUX)
28 : # ifndef __USE_BSD
29 : # define __USE_BSD /* madvise, MADV_WILLNEED */
30 : # endif
31 : #endif /* Linux */
32 :
33 : #include <sys/mman.h>
34 : #include <sys/stat.h>
35 :
36 : #include <string.h>
37 :
38 : /*
39 : * file_image_open
40 : */
41 0 : int file_image_open (file_image * image, const char * filename)
42 : {
43 0 : int result = 0;
44 : int fd;
45 : struct stat st;
46 : void * p;
47 :
48 0 : if (image == 0)
49 0 : return (EINVAL);
50 :
51 0 : image->m_base = MAP_FAILED, image->m_size = 0;
52 :
53 0 : if ((fd = open (filename, O_RDONLY)) == -1)
54 0 : return (errno);
55 :
56 0 : if (fstat (fd, &st) == -1)
57 : {
58 0 : result = errno;
59 0 : goto cleanup_and_leave;
60 : }
61 :
62 0 : p = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
63 0 : if (p == MAP_FAILED)
64 : {
65 0 : result = errno;
66 0 : goto cleanup_and_leave;
67 : }
68 :
69 0 : image->m_base = p, image->m_size = st.st_size;
70 :
71 : cleanup_and_leave:
72 0 : close (fd);
73 0 : return (result);
74 : }
75 :
76 : /*
77 : * file_image_pagein.
78 : */
79 0 : int file_image_pagein (file_image * image)
80 : {
81 : file_image w;
82 : long s;
83 : size_t k;
84 : // force touching of each page despite the optimizer
85 0 : volatile char c = 0;
86 :
87 0 : if (image == 0)
88 0 : return (EINVAL);
89 :
90 0 : if ((w.m_base = image->m_base) == 0)
91 0 : return (EINVAL);
92 0 : if ((w.m_size = image->m_size) == 0)
93 0 : return (0);
94 :
95 0 : if (madvise (w.m_base, w.m_size, MADV_WILLNEED) == -1)
96 0 : return (errno);
97 :
98 0 : if ((s = sysconf (_SC_PAGESIZE)) == -1)
99 0 : s = 0x1000;
100 :
101 0 : k = (size_t)(s);
102 0 : while (w.m_size > k)
103 : {
104 0 : c ^= ((char*)(w.m_base))[0];
105 0 : w.m_base = (char*)(w.m_base) + k;
106 0 : w.m_size -= k;
107 : }
108 0 : if (w.m_size > 0)
109 : {
110 0 : c ^= ((char*)(w.m_base))[0];
111 : }
112 :
113 0 : return (0);
114 : }
115 :
116 : /*
117 : * file_image_close
118 : */
119 0 : int file_image_close (file_image * image)
120 : {
121 0 : if (image == 0)
122 0 : return (EINVAL);
123 :
124 0 : if (munmap (image->m_base, image->m_size) == -1)
125 0 : return (errno);
126 :
127 0 : image->m_base = 0, image->m_size = 0;
128 0 : return (0);
129 : }
130 :
131 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|