1*e24a5033SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e24a5033SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e24a5033SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e24a5033SAndrew Rist  * distributed with this work for additional information
6*e24a5033SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e24a5033SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e24a5033SAndrew Rist  * "License"); you may not use this file except in compliance
9*e24a5033SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e24a5033SAndrew Rist  *
11*e24a5033SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e24a5033SAndrew Rist  *
13*e24a5033SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e24a5033SAndrew Rist  * software distributed under the License is distributed on an
15*e24a5033SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e24a5033SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e24a5033SAndrew Rist  * specific language governing permissions and limitations
18*e24a5033SAndrew Rist  * under the License.
19*e24a5033SAndrew Rist  *
20*e24a5033SAndrew Rist  *************************************************************/
21*e24a5033SAndrew Rist 
22*e24a5033SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "file_image.h"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <unistd.h>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <errno.h>
29cdf0e10cSrcweir #include <fcntl.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #if defined(LINUX)
32cdf0e10cSrcweir #  ifndef __USE_BSD
33cdf0e10cSrcweir #    define __USE_BSD /* madvise, MADV_WILLNEED */
34cdf0e10cSrcweir #  endif
35cdf0e10cSrcweir #endif /* Linux */
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <sys/mman.h>
38cdf0e10cSrcweir #include <sys/stat.h>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <string.h>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir /*
43cdf0e10cSrcweir  * file_image_open
44cdf0e10cSrcweir  */
file_image_open(file_image * image,const char * filename)45cdf0e10cSrcweir int file_image_open (file_image * image, const char * filename)
46cdf0e10cSrcweir {
47cdf0e10cSrcweir     int         result = 0;
48cdf0e10cSrcweir     int         fd;
49cdf0e10cSrcweir     struct stat st;
50cdf0e10cSrcweir     void *      p;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     if (image == 0)
53cdf0e10cSrcweir         return (EINVAL);
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     image->m_base = MAP_FAILED, image->m_size = 0;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     if ((fd = open (filename, O_RDONLY)) == -1)
58cdf0e10cSrcweir         return (errno);
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     if (fstat (fd, &st) == -1)
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         result = errno;
63cdf0e10cSrcweir         goto cleanup_and_leave;
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     p = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
67cdf0e10cSrcweir 	if (p == MAP_FAILED)
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         result = errno;
70cdf0e10cSrcweir         goto cleanup_and_leave;
71cdf0e10cSrcweir     }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     image->m_base = p, image->m_size = st.st_size;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir cleanup_and_leave:
76cdf0e10cSrcweir 	close (fd);
77cdf0e10cSrcweir     return (result);
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir /*
81cdf0e10cSrcweir  * file_image_pagein.
82cdf0e10cSrcweir  */
file_image_pagein(file_image * image)83cdf0e10cSrcweir int file_image_pagein (file_image * image)
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	file_image    w;
86cdf0e10cSrcweir 	long          s;
87cdf0e10cSrcweir 	size_t        k;
88cdf0e10cSrcweir 	volatile char c = 0;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	if (image == 0)
91cdf0e10cSrcweir 		return (EINVAL);
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 	if ((w.m_base = image->m_base) == 0)
94cdf0e10cSrcweir 		return (EINVAL);
95cdf0e10cSrcweir 	if ((w.m_size = image->m_size) == 0)
96cdf0e10cSrcweir 		return (0);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	if (madvise (w.m_base, w.m_size, MADV_WILLNEED) == -1)
99cdf0e10cSrcweir 	{
100cdf0e10cSrcweir #ifndef MACOSX
101cdf0e10cSrcweir 		return (errno);
102cdf0e10cSrcweir #else
103cdf0e10cSrcweir 		/* madvise MADV_WILLNEED need not succeed here */
104cdf0e10cSrcweir 		/* but that is fine */
105cdf0e10cSrcweir #endif
106cdf0e10cSrcweir 	}
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 
109cdf0e10cSrcweir #ifndef MACOSX
110cdf0e10cSrcweir 	if ((s = sysconf (_SC_PAGESIZE)) == -1)
111cdf0e10cSrcweir 		s = 0x1000;
112cdf0e10cSrcweir #else
113cdf0e10cSrcweir 	s = getpagesize();
114cdf0e10cSrcweir #endif
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	k = (size_t)(s);
117cdf0e10cSrcweir 	while (w.m_size > k)
118cdf0e10cSrcweir 	{
119cdf0e10cSrcweir 		c ^= ((char*)(w.m_base))[0];
120cdf0e10cSrcweir 		w.m_base  = (char*)(w.m_base) + k;
121cdf0e10cSrcweir 		w.m_size -= k;
122cdf0e10cSrcweir 	}
123cdf0e10cSrcweir 	if (w.m_size > 0)
124cdf0e10cSrcweir 	{
125cdf0e10cSrcweir 		c ^= ((char*)(w.m_base))[0];
126cdf0e10cSrcweir 		w.m_base  = (char*)(w.m_base) + w.m_size;
127cdf0e10cSrcweir 		w.m_size -= w.m_size;
128cdf0e10cSrcweir 	}
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 	return (0);
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir /*
134cdf0e10cSrcweir  * file_image_close
135cdf0e10cSrcweir  */
file_image_close(file_image * image)136cdf0e10cSrcweir int file_image_close (file_image * image)
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     if (image == 0)
139cdf0e10cSrcweir         return (EINVAL);
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     if (munmap (image->m_base, image->m_size) == -1)
142cdf0e10cSrcweir 		return (errno);
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	image->m_base = 0, image->m_size = 0;
145cdf0e10cSrcweir     return (0);
146cdf0e10cSrcweir }
147