| 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 | */ |
| 20 | #ifndef _LIBDEX_SYSUTIL |
| 21 | #define _LIBDEX_SYSUTIL |
| 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 | */ |
| 34 | #define SYSTEM_PAGE_SIZE 4096 |
| 35 | |
| 36 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 37 | * Use this to keep track of mapped segments. |
| 38 | */ |
| 39 | typedef struct MemMapping { |
| 40 | void* addr; /* start of data */ |
| 41 | size_t length; /* length of data */ |
| 42 | |
| 43 | void* baseAddr; /* page-aligned base address */ |
| 44 | size_t baseLength; /* length of mapping */ |
| 45 | } MemMapping; |
| 46 | |
| 47 | /* |
| 48 | * Copy a map. |
| 49 | */ |
| 50 | void sysCopyMap(MemMapping* dst, const MemMapping* src); |
| 51 | |
| 52 | /* |
| 53 | * Load a file into a new shared memory segment. All data from the current |
| 54 | * offset to the end of the file is pulled in. |
| 55 | * |
| 56 | * The segment is read-write, allowing VM fixups. (It should be modified |
| 57 | * to support .gz/.zip compressed data.) |
| 58 | * |
| 59 | * On success, "pMap" is filled in, and zero is returned. |
| 60 | */ |
| 61 | int sysLoadFileInShmem(int fd, MemMapping* pMap); |
| 62 | |
| 63 | /* |
| 64 | * Map a file (from fd's current offset) into a shared, |
| 65 | * read-only memory segment. |
| 66 | * |
| 67 | * On success, "pMap" is filled in, and zero is returned. |
| 68 | */ |
| Andy McFadden | b5ebe47 | 2009-11-16 16:14:54 -0800 | [diff] [blame] | 69 | int sysMapFileInShmemReadOnly(int fd, MemMapping* pMap); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 70 | |
| 71 | /* |
| Andy McFadden | b5ebe47 | 2009-11-16 16:14:54 -0800 | [diff] [blame] | 72 | * Map a file (from fd's current offset) into a shared, read-only memory |
| 73 | * segment that can be made writable. (In some cases, such as when |
| 74 | * mapping a file on a FAT filesystem, the result may be fully writable.) |
| 75 | * |
| 76 | * On success, "pMap" is filled in, and zero is returned. |
| 77 | */ |
| 78 | int sysMapFileInShmemWritableReadOnly(int fd, MemMapping* pMap); |
| 79 | |
| 80 | /* |
| 81 | * Like sysMapFileInShmemReadOnly, but on only part of a file. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 82 | */ |
| Andy McFadden | 80a4e24 | 2010-04-23 16:34:52 -0700 | [diff] [blame] | 83 | 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] | 84 | MemMapping* pMap); |
| 85 | |
| 86 | /* |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 87 | * Create a private anonymous mapping, useful for large allocations. |
| 88 | * |
| 89 | * On success, "pMap" is filled in, and zero is returned. |
| 90 | */ |
| 91 | int sysCreatePrivateMap(size_t length, MemMapping* pMap); |
| 92 | |
| 93 | /* |
| Andy McFadden | 9651693 | 2009-10-28 17:39:02 -0700 | [diff] [blame] | 94 | * Change the access rights on one or more pages. If "wantReadWrite" is |
| 95 | * zero, the pages will be made read-only; otherwise they will be read-write. |
| 96 | * |
| 97 | * Returns 0 on success. |
| 98 | */ |
| 99 | int sysChangeMapAccess(void* addr, size_t length, int wantReadWrite, |
| 100 | MemMapping* pmap); |
| 101 | |
| 102 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 103 | * Release the pages associated with a shared memory segment. |
| 104 | * |
| 105 | * This does not free "pMap"; it just releases the memory. |
| 106 | */ |
| 107 | void sysReleaseShmem(MemMapping* pMap); |
| 108 | |
| Brian Carlstrom | fbdcfb9 | 2010-05-28 15:42:12 -0700 | [diff] [blame] | 109 | /* |
| 110 | * Write until all bytes have been written. |
| 111 | * |
| 112 | * Returns 0 on success, or an errno value on failure. |
| 113 | */ |
| 114 | int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg); |
| 115 | |
| Dan Bornstein | 650177e | 2010-12-16 13:17:30 -0800 | [diff] [blame] | 116 | /* |
| 117 | * Copy the given number of bytes from one fd to another. Returns |
| 118 | * 0 on success, -1 on failure. |
| 119 | */ |
| 120 | int sysCopyFileToFile(int outFd, int inFd, size_t count); |
| 121 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 122 | #endif /*_DALVIK_SYSUTIL*/ |