2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * Simple linear memory allocator.
19 #ifndef _DALVIK_LINEARALLOC
20 #define _DALVIK_LINEARALLOC
23 * If this is set, we create additional data structures and make many
24 * additional mprotect() calls.
26 #define ENFORCE_READ_ONLY false
29 * Linear allocation state. We could tuck this into the start of the
30 * allocated region, but that would prevent us from sharing the rest of
33 typedef struct LinearAllocHdr {
34 int curOffset; /* offset where next data goes */
35 pthread_mutex_t lock; /* controls updates to this struct */
37 char* mapAddr; /* start of mmap()ed region */
38 int mapLength; /* length of region */
39 int firstOffset; /* for chasing through */
41 short* writeRefCount; /* for ENFORCE_READ_ONLY */
46 * Create a new alloc region.
48 LinearAllocHdr* dvmLinearAllocCreate(Object* classLoader);
53 void dvmLinearAllocDestroy(Object* classLoader);
56 * Allocate a chunk of memory. The memory will be zeroed out.
58 * For ENFORCE_READ_ONLY, call dvmLinearReadOnly on the result.
60 void* dvmLinearAlloc(Object* classLoader, size_t size);
63 * Reallocate a chunk. The original storage is not released, but may be
64 * erased to aid debugging.
66 * For ENFORCE_READ_ONLY, call dvmLinearReadOnly on the result. Also, the
67 * caller should probably mark the "mem" argument read-only before calling.
69 void* dvmLinearRealloc(Object* classLoader, void* mem, size_t newSize);
71 /* don't call these directly */
72 void dvmLinearSetReadOnly(Object* classLoader, void* mem);
73 void dvmLinearSetReadWrite(Object* classLoader, void* mem);
76 * Mark a chunk of memory from Alloc or Realloc as read-only. This must
77 * be done after all changes to the block of memory have been made. This
78 * actually operates on a page granularity.
80 INLINE void dvmLinearReadOnly(Object* classLoader, void* mem)
82 if (ENFORCE_READ_ONLY && mem != NULL)
83 dvmLinearSetReadOnly(classLoader, mem);
87 * Make a chunk of memory writable again.
89 INLINE void dvmLinearReadWrite(Object* classLoader, void* mem)
91 if (ENFORCE_READ_ONLY && mem != NULL)
92 dvmLinearSetReadWrite(classLoader, mem);
96 * Free a chunk. Does not increase available storage, but the freed area
97 * may be erased to aid debugging.
99 void dvmLinearFree(Object* classLoader, void* mem);
102 * Helper function; allocates new storage and copies "str" into it.
104 * For ENFORCE_READ_ONLY, do *not* call dvmLinearReadOnly on the result.
105 * This is done automatically.
107 char* dvmLinearStrdup(Object* classLoader, const char* str);
110 * Dump the contents of a linear alloc area.
112 void dvmLinearAllocDump(Object* classLoader);
115 * Determine if [start, start+length) is contained in the in-use area of
116 * a single LinearAlloc. The full set of linear allocators is scanned.
118 bool dvmLinearAllocContains(const void* start, size_t length);
120 #endif /*_DALVIK_LINEARALLOC*/