• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
27 bool dvmGcStartup(void);
28 bool dvmCreateStockExceptions(void);
29 bool dvmGcStartupAfterZygote(void);
30 void dvmGcShutdown(void);
31 void dvmGcThreadShutdown(void);
32 
33 /*
34  * Do any last-minute preparation before we call fork() for the first time.
35  */
36 bool 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  */
46 void* 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  */
56 Object* dvmAllocObject(ClassObject* clazz, int flags);
57 
58 /* flags for dvmMalloc */
59 enum {
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  */
69 void 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  */
78 void 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  */
87 void dvmReleaseTrackedAlloc(Object* obj, Thread* self);
88 
89 /*
90  * Returns true iff <obj> points to a valid allocated object.
91  */
92 bool 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  */
99 Object* 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  */
dvmValidateObject(Object * obj)111 INLINE 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  */
141 size_t dvmObjectSizeInHeap(const Object* obj);
142 
143 /*
144  * Gets the current ideal heap utilization, represented as a number
145  * between zero and one.
146  */
147 float dvmGetTargetHeapUtilization(void);
148 
149 /*
150  * Sets the new ideal heap utilization, represented as a number
151  * between zero and one.
152  */
153 void dvmSetTargetHeapUtilization(float newTarget);
154 
155 /*
156  * If set is true, sets the new minimum heap size to size; always
157  * returns the current (or previous) size.  If size is zero,
158  * removes the current minimum constraint (if present).
159  */
160 size_t dvmMinimumHeapSize(size_t size, bool set);
161 
162 /*
163  * Updates the internal count of externally-allocated memory.  If there's
164  * enough room for that memory, returns true.  If not, returns false and
165  * does not update the count.
166  *
167  * May cause a GC as a side-effect.
168  */
169 bool dvmTrackExternalAllocation(size_t n);
170 
171 /*
172  * Reduces the internal count of externally-allocated memory.
173  */
174 void dvmTrackExternalFree(size_t n);
175 
176 /*
177  * Returns the number of externally-allocated bytes being tracked by
178  * dvmTrackExternalAllocation/Free().
179  */
180 size_t dvmGetExternalBytesAllocated(void);
181 
182 /*
183  * Returns a count of the direct instances of a class.
184  */
185 size_t dvmCountInstancesOfClass(const ClassObject *clazz);
186 
187 /*
188  * Returns a count of the instances of a class and its subclasses.
189  */
190 size_t dvmCountAssignableInstancesOfClass(const ClassObject *clazz);
191 
192 #endif /*_DALVIK_ALLOC_ALLOC*/
193