1 /******************************************************************************
2  *
3  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
4  * Use is subject to license terms.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation;
9  * version 2.1 of the License.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "xc_private.h"
21 
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <malloc.h>
25 
26 /* Optionally flush file to disk and discard page cache */
discard_file_cache(xc_interface * xch,int fd,int flush)27 void discard_file_cache(xc_interface *xch, int fd, int flush)
28 {
29     off_t cur = 0;
30     int saved_errno = errno;
31 
32     if ( flush && (fsync(fd) < 0) )
33     {
34         /*PERROR("Failed to flush file: %s", strerror(errno));*/
35         goto out;
36     }
37 
38     /*
39      * Calculate last page boundry of amount written so far
40      * unless we are flushing in which case entire cache
41      * is discarded.
42      */
43     if ( !flush )
44     {
45         if ( ( cur = lseek(fd, 0, SEEK_CUR)) == (off_t)-1 )
46             cur = 0;
47         cur &= ~(PAGE_SIZE - 1);
48     }
49 
50     /* Discard from the buffer cache. */
51     if ( posix_fadvise(fd, 0, cur, POSIX_FADV_DONTNEED) < 0 )
52     {
53         /*PERROR("Failed to discard cache: %s", strerror(errno));*/
54         goto out;
55     }
56 
57  out:
58     errno = saved_errno;
59 }
60 
xc_memalign(xc_interface * xch,size_t alignment,size_t size)61 void *xc_memalign(xc_interface *xch, size_t alignment, size_t size)
62 {
63     return valloc(size);
64 }
65 
66 /*
67  * Local variables:
68  * mode: C
69  * c-file-style: "BSD"
70  * c-basic-offset: 4
71  * tab-width: 4
72  * indent-tabs-mode: nil
73  * End:
74  */
75