1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * System utilities.
19  */
20 #ifndef LIBDEX_SYSUTIL_H_
21 #define LIBDEX_SYSUTIL_H_
22 
23 #include <sys/types.h>
24 
25 /*
26  * System page size.  Normally you're expected to get this from
27  * sysconf(_SC_PAGESIZE) or some system-specific define (usually PAGESIZE
28  * or PAGE_SIZE).  If we use a simple #define the compiler can generate
29  * appropriate masks directly, so we define it here and verify it as the
30  * VM is starting up.
31  *
32  * Must be a power of 2.
33  */
34 #ifdef PAGE_SHIFT
35 #define SYSTEM_PAGE_SIZE        (1<<PAGE_SHIFT)
36 #else
37 #define SYSTEM_PAGE_SIZE        4096
38 #endif
39 
40 /*
41  * Use this to keep track of mapped segments.
42  */
43 struct MemMapping {
44     void*   addr;           /* start of data */
45     size_t  length;         /* length of data */
46 
47     void*   baseAddr;       /* page-aligned base address */
48     size_t  baseLength;     /* length of mapping */
49 };
50 
51 /*
52  * Copy a map.
53  */
54 void sysCopyMap(MemMapping* dst, const MemMapping* src);
55 
56 /*
57  * Map a file (from fd's current offset) into a shared, read-only memory
58  * segment that can be made writable.  (In some cases, such as when
59  * mapping a file on a FAT filesystem, the result may be fully writable.)
60  *
61  * On success, "pMap" is filled in, and zero is returned.
62  */
63 int sysMapFileInShmemWritableReadOnly(int fd, MemMapping* pMap);
64 
65 /*
66  * Map part of a file into a shared, read-only memory segment.
67  *
68  * On success, "pMap" is filled in, and zero is returned.
69  */
70 int sysMapFileSegmentInShmem(int fd, off_t start, size_t length,
71     MemMapping* pMap);
72 
73 /*
74  * Create a private anonymous mapping, useful for large allocations.
75  *
76  * On success, "pMap" is filled in, and zero is returned.
77  */
78 int sysCreatePrivateMap(size_t length, MemMapping* pMap);
79 
80 /*
81  * Change the access rights on one or more pages.  If "wantReadWrite" is
82  * zero, the pages will be made read-only; otherwise they will be read-write.
83  *
84  * Returns 0 on success.
85  */
86 int sysChangeMapAccess(void* addr, size_t length, int wantReadWrite,
87     MemMapping* pmap);
88 
89 /*
90  * Release the pages associated with a shared memory segment.
91  *
92  * This does not free "pMap"; it just releases the memory.
93  */
94 void sysReleaseShmem(MemMapping* pMap);
95 
96 /*
97  * Write until all bytes have been written.
98  *
99  * Returns 0 on success, or an errno value on failure.
100  */
101 int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg);
102 
103 /*
104  * Copy the given number of bytes from one fd to another. Returns
105  * 0 on success, -1 on failure.
106  */
107 int sysCopyFileToFile(int outFd, int inFd, size_t count);
108 
109 #endif  // LIBDEX_SYSUTIL_H_
110