lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / desktop / unx / source / file_image_unx.c
blobec229f95ff98ac650e5e39e84fbb1d1977b6a471
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 #include <sys/mman.h>
28 #include <sys/stat.h>
30 #include <string.h>
33 * file_image_open
35 int file_image_open (file_image * image, const char * filename)
37 int result = 0;
38 int fd;
39 struct stat st;
40 void * p;
42 if (image == NULL)
43 return EINVAL;
45 image->m_base = MAP_FAILED;
46 image->m_size = 0;
48 if ((fd = open (filename, O_RDONLY)) == -1)
49 return errno;
51 if (fstat (fd, &st) == -1)
53 result = errno;
54 goto cleanup_and_leave;
57 p = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
58 if (p == MAP_FAILED)
60 result = errno;
61 goto cleanup_and_leave;
64 image->m_base = p;
65 image->m_size = st.st_size;
67 cleanup_and_leave:
68 close (fd);
69 return result;
73 * file_image_pagein.
75 int file_image_pagein (file_image * image)
77 long s = -1;
78 volatile char c =0;
79 size_t idx;
81 if (image == NULL)
82 return EINVAL;
83 if (image->m_base == NULL)
84 return EINVAL;
85 if (image->m_size == 0)
86 return 0;
88 if (madvise (image->m_base, image->m_size, MADV_WILLNEED) == -1)
89 return errno;
91 s = sysconf (_SC_PAGESIZE);
92 if (s == -1)
93 s = 0x1000;
94 // force touching of each page despite the optimizer
95 for(idx = 0; idx < image->m_size; idx += (size_t)s)
97 c ^= ((volatile const char*)(image->m_base))[idx];
99 c ^= ((volatile const char*)(image->m_base))[image->m_size-1];
101 return 0;
105 * file_image_close
107 int file_image_close (file_image * image)
109 if (image == NULL)
110 return EINVAL;
112 if (munmap (image->m_base, image->m_size) == -1)
113 return errno;
115 image->m_base = NULL;
116 image->m_size = 0;
117 return 0;
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */