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