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 * Load a file into a new shared memory segment. All data from the current 58 * offset to the end of the file is pulled in. 59 * 60 * The segment is read-write, allowing VM fixups. (It should be modified 61 * to support .gz/.zip compressed data.) 62 * 63 * On success, "pMap" is filled in, and zero is returned. 64 */ 65 int sysLoadFileInShmem(int fd, MemMapping* pMap); 66 67 /* 68 * Map a file (from fd's current offset) into a shared, 69 * read-only memory segment. 70 * 71 * On success, "pMap" is filled in, and zero is returned. 72 */ 73 int sysMapFileInShmemReadOnly(int fd, MemMapping* pMap); 74 75 /* 76 * Map a file (from fd's current offset) into a shared, read-only memory 77 * segment that can be made writable. (In some cases, such as when 78 * mapping a file on a FAT filesystem, the result may be fully writable.) 79 * 80 * On success, "pMap" is filled in, and zero is returned. 81 */ 82 int sysMapFileInShmemWritableReadOnly(int fd, MemMapping* pMap); 83 84 /* 85 * Like sysMapFileInShmemReadOnly, but on only part of a file. 86 */ 87 int sysMapFileSegmentInShmem(int fd, off_t start, size_t length, 88 MemMapping* pMap); 89 90 /* 91 * Create a private anonymous mapping, useful for large allocations. 92 * 93 * On success, "pMap" is filled in, and zero is returned. 94 */ 95 int sysCreatePrivateMap(size_t length, MemMapping* pMap); 96 97 /* 98 * Change the access rights on one or more pages. If "wantReadWrite" is 99 * zero, the pages will be made read-only; otherwise they will be read-write. 100 * 101 * Returns 0 on success. 102 */ 103 int sysChangeMapAccess(void* addr, size_t length, int wantReadWrite, 104 MemMapping* pmap); 105 106 /* 107 * Release the pages associated with a shared memory segment. 108 * 109 * This does not free "pMap"; it just releases the memory. 110 */ 111 void sysReleaseShmem(MemMapping* pMap); 112 113 /* 114 * Write until all bytes have been written. 115 * 116 * Returns 0 on success, or an errno value on failure. 117 */ 118 int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg); 119 120 /* 121 * Copy the given number of bytes from one fd to another. Returns 122 * 0 on success, -1 on failure. 123 */ 124 int sysCopyFileToFile(int outFd, int inFd, size_t count); 125 126 #endif // LIBDEX_SYSUTIL_H_ 127