blob: aa33fe1fc4773d3f231d8d3761e8c4a914cf5977 [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
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.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080025 */
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 */
33typedef 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} LinearAllocHdr;
43
44
45/*
46 * Create a new alloc region.
47 */
48LinearAllocHdr* dvmLinearAllocCreate(Object* classLoader);
49
50/*
51 * Destroy a region.
52 */
53void 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 */
60void* 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 */
69void* dvmLinearRealloc(Object* classLoader, void* mem, size_t newSize);
70
71/* don't call these directly */
72void dvmLinearSetReadOnly(Object* classLoader, void* mem);
73void 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 */
80INLINE 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 */
89INLINE 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 */
99void 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 */
107char* dvmLinearStrdup(Object* classLoader, const char* str);
108
109/*
110 * Dump the contents of a linear alloc area.
111 */
112void dvmLinearAllocDump(Object* classLoader);
113
Andy McFadden7605a842009-07-27 14:44:22 -0700114/*
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 */
Bob Leecdacef52009-07-30 18:17:37 -0700118bool dvmLinearAllocContains(const void* start, size_t length);
Andy McFadden7605a842009-07-27 14:44:22 -0700119
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800120#endif /*_DALVIK_LINEARALLOC*/