update credits
[LibreOffice.git] / desktop / unx / source / file_image_unx.c
blob89df91e1314ac4f789b7bb4775e771af40069765
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "file_image.h"
22 #include <unistd.h>
24 #include <errno.h>
25 #include <fcntl.h>
27 #if defined(LINUX)
28 # ifndef __USE_BSD
29 # define __USE_BSD /* madvise, MADV_WILLNEED */
30 # endif
31 #endif /* Linux */
33 #include <sys/mman.h>
34 #include <sys/stat.h>
36 #include <string.h>
39 * file_image_open
41 int file_image_open (file_image * image, const char * filename)
43 int result = 0;
44 int fd;
45 struct stat st;
46 void * p;
48 if (image == 0)
49 return (EINVAL);
51 image->m_base = MAP_FAILED, image->m_size = 0;
53 if ((fd = open (filename, O_RDONLY)) == -1)
54 return (errno);
56 if (fstat (fd, &st) == -1)
58 result = errno;
59 goto cleanup_and_leave;
62 p = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
63 if (p == MAP_FAILED)
65 result = errno;
66 goto cleanup_and_leave;
69 image->m_base = p, image->m_size = st.st_size;
71 cleanup_and_leave:
72 close (fd);
73 return (result);
77 * file_image_pagein.
79 int file_image_pagein (file_image * image)
81 file_image w;
82 long s;
83 size_t k;
84 // force touching of each page despite the optimizer
85 volatile char c = 0;
87 if (image == 0)
88 return (EINVAL);
90 if ((w.m_base = image->m_base) == 0)
91 return (EINVAL);
92 if ((w.m_size = image->m_size) == 0)
93 return (0);
95 if (madvise (w.m_base, w.m_size, MADV_WILLNEED) == -1)
96 return (errno);
98 if ((s = sysconf (_SC_PAGESIZE)) == -1)
99 s = 0x1000;
101 k = (size_t)(s);
102 while (w.m_size > k)
104 c ^= ((char*)(w.m_base))[0];
105 w.m_base = (char*)(w.m_base) + k;
106 w.m_size -= k;
108 if (w.m_size > 0)
110 c ^= ((char*)(w.m_base))[0];
113 return (0);
117 * file_image_close
119 int file_image_close (file_image * image)
121 if (image == 0)
122 return (EINVAL);
124 if (munmap (image->m_base, image->m_size) == -1)
125 return (errno);
127 image->m_base = 0, image->m_size = 0;
128 return (0);
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */