The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | */ |
The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 16 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 17 | /* |
| 18 | * System utilities. |
| 19 | */ |
Carl Shapiro | 375fb11 | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 20 | #ifndef LIBDEX_SYSUTIL_H_ |
| 21 | #define LIBDEX_SYSUTIL_H_ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 22 | |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | /* |
Andy McFadden | 9651693 | 2009-10-28 17:39:02 -0700 | [diff] [blame] | 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 | */ |
Chris Dearman | 356839b | 2012-01-30 18:22:27 -0800 | [diff] [blame] | 34 | #ifdef PAGE_SHIFT |
| 35 | #define SYSTEM_PAGE_SIZE (1<<PAGE_SHIFT) |
| 36 | #else |
Andy McFadden | 9651693 | 2009-10-28 17:39:02 -0700 | [diff] [blame] | 37 | #define SYSTEM_PAGE_SIZE 4096 |
Chris Dearman | 356839b | 2012-01-30 18:22:27 -0800 | [diff] [blame] | 38 | #endif |
Andy McFadden | 9651693 | 2009-10-28 17:39:02 -0700 | [diff] [blame] | 39 | |
| 40 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 41 | * Use this to keep track of mapped segments. |
| 42 | */ |
Carl Shapiro | bfc9799 | 2011-04-27 14:16:08 -0700 | [diff] [blame] | 43 | struct MemMapping { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 */ |
Carl Shapiro | bfc9799 | 2011-04-27 14:16:08 -0700 | [diff] [blame] | 49 | }; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * Copy a map. |
| 53 | */ |
| 54 | void sysCopyMap(MemMapping* dst, const MemMapping* src); |
| 55 | |
| 56 | /* |
Andy McFadden | b5ebe47 | 2009-11-16 16:14:54 -0800 | [diff] [blame] | 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 | /* |
Elliott Hughes | bbe8cb5 | 2012-12-17 09:21:54 -0800 | [diff] [blame] | 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. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 69 | */ |
Andy McFadden | 80a4e24 | 2010-04-23 16:34:52 -0700 | [diff] [blame] | 70 | int sysMapFileSegmentInShmem(int fd, off_t start, size_t length, |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 71 | MemMapping* pMap); |
| 72 | |
| 73 | /* |
The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 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 | /* |
Andy McFadden | 9651693 | 2009-10-28 17:39:02 -0700 | [diff] [blame] | 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 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | |
Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 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 | |
Dan Bornstein | 650177e | 2010-12-16 13:17:30 -0800 | [diff] [blame] | 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 | |
Carl Shapiro | 375fb11 | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 109 | #endif // LIBDEX_SYSUTIL_H_ |