| 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 | */ |
| 16 | /* |
| 17 | * Simple linear memory allocator. |
| 18 | */ |
| 19 | #ifndef _DALVIK_LINEARALLOC |
| 20 | #define _DALVIK_LINEARALLOC |
| 21 | |
| Carl Shapiro | ae188c6 | 2011-04-08 13:11:58 -0700 | [diff] [blame^] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 26 | /* |
| 27 | * If this is set, we create additional data structures and make many |
| 28 | * additional mprotect() calls. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 29 | */ |
| 30 | #define ENFORCE_READ_ONLY false |
| 31 | |
| 32 | /* |
| 33 | * Linear allocation state. We could tuck this into the start of the |
| 34 | * allocated region, but that would prevent us from sharing the rest of |
| 35 | * that first page. |
| 36 | */ |
| 37 | typedef struct LinearAllocHdr { |
| 38 | int curOffset; /* offset where next data goes */ |
| 39 | pthread_mutex_t lock; /* controls updates to this struct */ |
| 40 | |
| 41 | char* mapAddr; /* start of mmap()ed region */ |
| 42 | int mapLength; /* length of region */ |
| 43 | int firstOffset; /* for chasing through */ |
| 44 | |
| 45 | short* writeRefCount; /* for ENFORCE_READ_ONLY */ |
| 46 | } LinearAllocHdr; |
| 47 | |
| 48 | |
| 49 | /* |
| 50 | * Create a new alloc region. |
| 51 | */ |
| 52 | LinearAllocHdr* dvmLinearAllocCreate(Object* classLoader); |
| 53 | |
| 54 | /* |
| 55 | * Destroy a region. |
| 56 | */ |
| 57 | void dvmLinearAllocDestroy(Object* classLoader); |
| 58 | |
| 59 | /* |
| 60 | * Allocate a chunk of memory. The memory will be zeroed out. |
| 61 | * |
| 62 | * For ENFORCE_READ_ONLY, call dvmLinearReadOnly on the result. |
| 63 | */ |
| 64 | void* dvmLinearAlloc(Object* classLoader, size_t size); |
| 65 | |
| 66 | /* |
| 67 | * Reallocate a chunk. The original storage is not released, but may be |
| 68 | * erased to aid debugging. |
| 69 | * |
| 70 | * For ENFORCE_READ_ONLY, call dvmLinearReadOnly on the result. Also, the |
| 71 | * caller should probably mark the "mem" argument read-only before calling. |
| 72 | */ |
| 73 | void* dvmLinearRealloc(Object* classLoader, void* mem, size_t newSize); |
| 74 | |
| 75 | /* don't call these directly */ |
| 76 | void dvmLinearSetReadOnly(Object* classLoader, void* mem); |
| 77 | void dvmLinearSetReadWrite(Object* classLoader, void* mem); |
| 78 | |
| 79 | /* |
| 80 | * Mark a chunk of memory from Alloc or Realloc as read-only. This must |
| 81 | * be done after all changes to the block of memory have been made. This |
| 82 | * actually operates on a page granularity. |
| 83 | */ |
| 84 | INLINE void dvmLinearReadOnly(Object* classLoader, void* mem) |
| 85 | { |
| 86 | if (ENFORCE_READ_ONLY && mem != NULL) |
| 87 | dvmLinearSetReadOnly(classLoader, mem); |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Make a chunk of memory writable again. |
| 92 | */ |
| 93 | INLINE void dvmLinearReadWrite(Object* classLoader, void* mem) |
| 94 | { |
| 95 | if (ENFORCE_READ_ONLY && mem != NULL) |
| 96 | dvmLinearSetReadWrite(classLoader, mem); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Free a chunk. Does not increase available storage, but the freed area |
| 101 | * may be erased to aid debugging. |
| 102 | */ |
| 103 | void dvmLinearFree(Object* classLoader, void* mem); |
| 104 | |
| 105 | /* |
| 106 | * Helper function; allocates new storage and copies "str" into it. |
| 107 | * |
| 108 | * For ENFORCE_READ_ONLY, do *not* call dvmLinearReadOnly on the result. |
| 109 | * This is done automatically. |
| 110 | */ |
| 111 | char* dvmLinearStrdup(Object* classLoader, const char* str); |
| 112 | |
| 113 | /* |
| 114 | * Dump the contents of a linear alloc area. |
| 115 | */ |
| 116 | void dvmLinearAllocDump(Object* classLoader); |
| 117 | |
| Andy McFadden | 7605a84 | 2009-07-27 14:44:22 -0700 | [diff] [blame] | 118 | /* |
| 119 | * Determine if [start, start+length) is contained in the in-use area of |
| 120 | * a single LinearAlloc. The full set of linear allocators is scanned. |
| 121 | */ |
| Bob Lee | cdacef5 | 2009-07-30 18:17:37 -0700 | [diff] [blame] | 122 | bool dvmLinearAllocContains(const void* start, size_t length); |
| Andy McFadden | 7605a84 | 2009-07-27 14:44:22 -0700 | [diff] [blame] | 123 | |
| Carl Shapiro | ae188c6 | 2011-04-08 13:11:58 -0700 | [diff] [blame^] | 124 | #ifdef __cplusplus |
| 125 | } |
| 126 | #endif |
| 127 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 128 | #endif /*_DALVIK_LINEARALLOC*/ |