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