blob: 8244ce7bea459ead24a0544f1b1b8bb4982c0e5a [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 * Garbage-collecting allocator.
18 */
19#ifndef _DALVIK_ALLOC_ALLOC
20#define _DALVIK_ALLOC_ALLOC
21
Carl Shapiroad9400f2010-05-01 20:08:57 -070022#include <stddef.h>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080023
24/*
25 * Initialization.
26 */
27bool dvmGcStartup(void);
Andy McFadden7fc3ce82009-07-14 15:57:23 -070028bool dvmCreateStockExceptions(void);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080029bool dvmGcStartupAfterZygote(void);
30void dvmGcShutdown(void);
Carl Shapiroec805ea2010-06-28 16:28:26 -070031void dvmGcThreadShutdown(void);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080032
33/*
34 * Do any last-minute preparation before we call fork() for the first time.
35 */
36bool dvmGcPreZygoteFork(void);
37
38/*
39 * Basic allocation function.
40 *
41 * The new object will be added to the "tracked alloc" table unless
Barry Hayesd4f78d32010-06-08 09:34:42 -070042 * flags is ALLOC_DONT_TRACK.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080043 *
44 * Returns NULL and throws an exception on failure.
45 */
46void* dvmMalloc(size_t size, int flags);
47
48/*
49 * Allocate a new object.
50 *
51 * The new object will be added to the "tracked alloc" table unless
Barry Hayesd4f78d32010-06-08 09:34:42 -070052 * flags is ALLOC_DONT_TRACK.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080053 *
54 * Returns NULL and throws an exception on failure.
55 */
56Object* dvmAllocObject(ClassObject* clazz, int flags);
57
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080058/* flags for dvmMalloc */
59enum {
60 ALLOC_DEFAULT = 0x00,
Barry Hayesd4f78d32010-06-08 09:34:42 -070061 ALLOC_DONT_TRACK = 0x01, /* don't add to internal tracking list */
62 ALLOC_FINALIZABLE = 0x02, /* call finalize() before freeing */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080063};
64
65/*
66 * Call when a request is so far off that we can't call dvmMalloc(). Throws
67 * an exception with the specified message.
68 */
69void dvmThrowBadAllocException(const char* msg);
70
71/*
72 * Track an object reference that is currently only visible internally.
73 * This is called automatically by dvmMalloc() unless ALLOC_DONT_TRACK
74 * is set.
75 *
76 * The "self" argument is allowed as an optimization; it may be NULL.
77 */
78void dvmAddTrackedAlloc(Object* obj, Thread* self);
79
80/*
81 * Remove an object from the internal tracking list.
82 *
83 * Does nothing if "obj" is NULL.
84 *
85 * The "self" argument is allowed as an optimization; it may be NULL.
86 */
87void dvmReleaseTrackedAlloc(Object* obj, Thread* self);
88
89/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080090 * Returns true iff <obj> points to a valid allocated object.
91 */
92bool dvmIsValidObject(const Object* obj);
93
94/*
Barry Hayes364f9d92010-06-11 16:12:47 -070095 * Returns true iff <ptr> points within allocation-managed address space.
96 */
97bool dvmIsValidObjectAddress(const void *ptr);
98
99/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800100 * Create a copy of an object.
101 *
102 * The new object will be added to the "tracked alloc" table.
103 */
104Object* dvmCloneObject(Object* obj);
105
106/*
107 * Validate the object pointer. Returns "false" and throws an exception if
108 * "obj" is null or invalid.
109 *
110 * This may be used in performance critical areas as a null-pointer check;
111 * anything else here should be for debug builds only. In particular, for
112 * "release" builds we want to skip the call to dvmIsValidObject() -- the
113 * classfile validation will screen out code that puts invalid data into
114 * object reference registers.
115 */
116INLINE int dvmValidateObject(Object* obj)
117{
118 if (obj == NULL) {
119 dvmThrowException("Ljava/lang/NullPointerException;", NULL);
120 return false;
121 }
122#ifdef WITH_EXTRA_OBJECT_VALIDATION
123 if (!dvmIsValidObject(obj)) {
Andy McFadden0423f0e2009-08-26 07:21:53 -0700124 dvmAbort();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800125 dvmThrowException("Ljava/lang/InternalError;",
126 "VM detected invalid object ptr");
127 return false;
128 }
129#endif
130#ifndef NDEBUG
131 /* check for heap corruption */
132 if (obj->clazz == NULL || ((u4) obj->clazz) <= 65536) {
The Android Open Source Project99409882009-03-18 22:20:24 -0700133 dvmAbort();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800134 dvmThrowException("Ljava/lang/InternalError;",
135 "VM detected invalid object class ptr");
136 return false;
137 }
138#endif
139 return true;
140}
141
142/*
143 * Determine the exact number of GC heap bytes used by an object. (Internal
144 * to heap code except for debugging.)
145 */
146size_t dvmObjectSizeInHeap(const Object* obj);
147
148/*
149 * Gets the current ideal heap utilization, represented as a number
150 * between zero and one.
151 */
152float dvmGetTargetHeapUtilization(void);
153
154/*
155 * Sets the new ideal heap utilization, represented as a number
156 * between zero and one.
157 */
158void dvmSetTargetHeapUtilization(float newTarget);
159
160/*
161 * If set is true, sets the new minimum heap size to size; always
162 * returns the current (or previous) size. If size is zero,
163 * removes the current minimum constraint (if present).
164 */
165size_t dvmMinimumHeapSize(size_t size, bool set);
166
167/*
168 * Updates the internal count of externally-allocated memory. If there's
169 * enough room for that memory, returns true. If not, returns false and
170 * does not update the count.
171 *
172 * May cause a GC as a side-effect.
173 */
174bool dvmTrackExternalAllocation(size_t n);
175
176/*
177 * Reduces the internal count of externally-allocated memory.
178 */
179void dvmTrackExternalFree(size_t n);
180
181/*
182 * Returns the number of externally-allocated bytes being tracked by
183 * dvmTrackExternalAllocation/Free().
184 */
185size_t dvmGetExternalBytesAllocated(void);
186
187#endif /*_DALVIK_ALLOC_ALLOC*/