Alloc.h revision a05f6504440ccf460477e9883c87cd70aca77b24
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 * Garbage-collecting allocator.
18 */
19#ifndef _DALVIK_ALLOC_ALLOC
20#define _DALVIK_ALLOC_ALLOC
21
22#include <stddef.h>
23
24/*
25 * Initialization.
26 */
27bool dvmGcStartup(void);
28bool dvmCreateStockExceptions(void);
29bool dvmGcStartupAfterZygote(void);
30void dvmGcShutdown(void);
31void dvmGcThreadShutdown(void);
32
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
42 * flags is ALLOC_DONT_TRACK.
43 *
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
52 * flags is ALLOC_DONT_TRACK.
53 *
54 * Returns NULL and throws an exception on failure.
55 */
56Object* dvmAllocObject(ClassObject* clazz, int flags);
57
58/* flags for dvmMalloc */
59enum {
60    ALLOC_DEFAULT       = 0x00,
61    ALLOC_DONT_TRACK    = 0x01,     /* don't add to internal tracking list */
62    ALLOC_FINALIZABLE   = 0x02,     /* call finalize() before freeing */
63};
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/*
90 * Returns true iff <obj> points to a valid allocated object.
91 */
92bool dvmIsValidObject(const Object* obj);
93
94/*
95 * Create a copy of an object.
96 *
97 * The new object will be added to the "tracked alloc" table.
98 */
99Object* dvmCloneObject(Object* obj);
100
101/*
102 * Validate the object pointer.  Returns "false" and throws an exception if
103 * "obj" is null or invalid.
104 *
105 * This may be used in performance critical areas as a null-pointer check;
106 * anything else here should be for debug builds only.  In particular, for
107 * "release" builds we want to skip the call to dvmIsValidObject() -- the
108 * classfile validation will screen out code that puts invalid data into
109 * object reference registers.
110 */
111INLINE int dvmValidateObject(Object* obj)
112{
113    if (obj == NULL) {
114        dvmThrowException("Ljava/lang/NullPointerException;", NULL);
115        return false;
116    }
117#ifdef WITH_EXTRA_OBJECT_VALIDATION
118    if (!dvmIsValidObject(obj)) {
119        dvmAbort();
120        dvmThrowException("Ljava/lang/InternalError;",
121            "VM detected invalid object ptr");
122        return false;
123    }
124#endif
125#ifndef NDEBUG
126    /* check for heap corruption */
127    if (obj->clazz == NULL || ((u4) obj->clazz) <= 65536) {
128        dvmAbort();
129        dvmThrowException("Ljava/lang/InternalError;",
130            "VM detected invalid object class ptr");
131        return false;
132    }
133#endif
134    return true;
135}
136
137/*
138 * Determine the exact number of GC heap bytes used by an object.  (Internal
139 * to heap code except for debugging.)
140 */
141size_t dvmObjectSizeInHeap(const Object* obj);
142
143/*
144 * Gets the current ideal heap utilization, represented as a number
145 * between zero and one.
146 */
147float dvmGetTargetHeapUtilization(void);
148
149/*
150 * Sets the new ideal heap utilization, represented as a number
151 * between zero and one.
152 */
153void dvmSetTargetHeapUtilization(float newTarget);
154
155/*
156 * Initiate garbage collection.
157 *
158 * This usually happens automatically, but can also be caused by Runtime.gc().
159 */
160void dvmCollectGarbage(bool collectSoftRefs);
161
162/*
163 * Returns a count of the direct instances of a class.
164 */
165size_t dvmCountInstancesOfClass(const ClassObject *clazz);
166
167/*
168 * Returns a count of the instances of a class and its subclasses.
169 */
170size_t dvmCountAssignableInstancesOfClass(const ClassObject *clazz);
171
172#endif /*_DALVIK_ALLOC_ALLOC*/
173