blob: c06d2abfade30d0201667f39648403b04a713648 [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
Carl Shapiroae188c62011-04-08 13:11:58 -070022#ifdef __cplusplus
23extern "C" {
24#endif
25
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080026/*
27 * If this is set, we create additional data structures and make many
28 * additional mprotect() calls.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080029 */
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 */
37typedef 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 */
52LinearAllocHdr* dvmLinearAllocCreate(Object* classLoader);
53
54/*
55 * Destroy a region.
56 */
57void 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 */
64void* 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 */
73void* dvmLinearRealloc(Object* classLoader, void* mem, size_t newSize);
74
75/* don't call these directly */
76void dvmLinearSetReadOnly(Object* classLoader, void* mem);
77void 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 */
84INLINE 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 */
93INLINE 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 */
103void 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 */
111char* dvmLinearStrdup(Object* classLoader, const char* str);
112
113/*
114 * Dump the contents of a linear alloc area.
115 */
116void dvmLinearAllocDump(Object* classLoader);
117
Andy McFadden7605a842009-07-27 14:44:22 -0700118/*
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 Leecdacef52009-07-30 18:17:37 -0700122bool dvmLinearAllocContains(const void* start, size_t length);
Andy McFadden7605a842009-07-27 14:44:22 -0700123
Carl Shapiroae188c62011-04-08 13:11:58 -0700124#ifdef __cplusplus
125}
126#endif
127
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800128#endif /*_DALVIK_LINEARALLOC*/