• 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 <stdlib.h>
23 
24 /*
25  * Initialization.
26  */
27 bool dvmGcStartup(void);
28 bool dvmCreateStockExceptions(void);
29 bool dvmGcStartupAfterZygote(void);
30 void dvmGcShutdown(void);
31 
32 /*
33  * Do any last-minute preparation before we call fork() for the first time.
34  */
35 bool dvmGcPreZygoteFork(void);
36 
37 /*
38  * Basic allocation function.
39  *
40  * The new object will be added to the "tracked alloc" table unless
41  * flags is ALLOC_DONT_TRACK or ALLOC_NO_GC.
42  *
43  * Returns NULL and throws an exception on failure.
44  */
45 void* dvmMalloc(size_t size, int flags);
46 
47 /*
48  * Allocate a new object.
49  *
50  * The new object will be added to the "tracked alloc" table unless
51  * flags is ALLOC_DONT_TRACK or ALLOC_NO_GC.
52  *
53  * Returns NULL and throws an exception on failure.
54  */
55 Object* dvmAllocObject(ClassObject* clazz, int flags);
56 
57 /*
58  * Clear flags set by dvmMalloc.  Pass in a bit mask of the flags that
59  * should be cleared.
60  */
61 void dvmClearAllocFlags(Object* obj, int mask);
62 
63 /* flags for dvmMalloc */
64 enum {
65     ALLOC_DEFAULT       = 0x00,
66     ALLOC_NO_GC         = 0x01,     /* do not garbage collect this object */
67     ALLOC_DONT_TRACK    = 0x02,     /* don't add to internal tracking list */
68     ALLOC_FINALIZABLE   = 0x04,     /* call finalize() before freeing */
69     // ALLOC_NO_MOVE?
70 };
71 
72 /*
73  * Call when a request is so far off that we can't call dvmMalloc().  Throws
74  * an exception with the specified message.
75  */
76 void dvmThrowBadAllocException(const char* msg);
77 
78 /*
79  * Track an object reference that is currently only visible internally.
80  * This is called automatically by dvmMalloc() unless ALLOC_DONT_TRACK
81  * is set.
82  *
83  * The "self" argument is allowed as an optimization; it may be NULL.
84  */
85 void dvmAddTrackedAlloc(Object* obj, Thread* self);
86 
87 /*
88  * Remove an object from the internal tracking list.
89  *
90  * Does nothing if "obj" is NULL.
91  *
92  * The "self" argument is allowed as an optimization; it may be NULL.
93  */
94 void dvmReleaseTrackedAlloc(Object* obj, Thread* self);
95 
96 /*
97  * Like dvmReleaseTrackedAlloc, but only does the release if "allocFlags"
98  * indicates that it's necessary to do so.
99  */
dvmReleaseTrackedAllocIFN(Object * obj,Thread * self,int allocFlags)100 INLINE void dvmReleaseTrackedAllocIFN(Object* obj, Thread* self, int allocFlags)
101 {
102     if ((allocFlags & (ALLOC_NO_GC|ALLOC_DONT_TRACK)) == 0)
103         dvmReleaseTrackedAlloc(obj, self);
104 }
105 
106 /*
107  * Returns true iff <obj> points to a valid allocated object.
108  */
109 bool dvmIsValidObject(const Object* obj);
110 
111 /*
112  * Create a copy of an object.
113  *
114  * The new object will be added to the "tracked alloc" table.
115  */
116 Object* dvmCloneObject(Object* obj);
117 
118 /*
119  * Validate the object pointer.  Returns "false" and throws an exception if
120  * "obj" is null or invalid.
121  *
122  * This may be used in performance critical areas as a null-pointer check;
123  * anything else here should be for debug builds only.  In particular, for
124  * "release" builds we want to skip the call to dvmIsValidObject() -- the
125  * classfile validation will screen out code that puts invalid data into
126  * object reference registers.
127  */
dvmValidateObject(Object * obj)128 INLINE int dvmValidateObject(Object* obj)
129 {
130     if (obj == NULL) {
131         dvmThrowException("Ljava/lang/NullPointerException;", NULL);
132         return false;
133     }
134 #ifdef WITH_EXTRA_OBJECT_VALIDATION
135     if (!dvmIsValidObject(obj)) {
136         dvmAbort();
137         dvmThrowException("Ljava/lang/InternalError;",
138             "VM detected invalid object ptr");
139         return false;
140     }
141 #endif
142 #ifndef NDEBUG
143     /* check for heap corruption */
144     if (obj->clazz == NULL || ((u4) obj->clazz) <= 65536) {
145         dvmAbort();
146         dvmThrowException("Ljava/lang/InternalError;",
147             "VM detected invalid object class ptr");
148         return false;
149     }
150 #endif
151     return true;
152 }
153 
154 /*
155  * Determine the exact number of GC heap bytes used by an object.  (Internal
156  * to heap code except for debugging.)
157  */
158 size_t dvmObjectSizeInHeap(const Object* obj);
159 
160 /*
161  * Gets the current ideal heap utilization, represented as a number
162  * between zero and one.
163  */
164 float dvmGetTargetHeapUtilization(void);
165 
166 /*
167  * Sets the new ideal heap utilization, represented as a number
168  * between zero and one.
169  */
170 void dvmSetTargetHeapUtilization(float newTarget);
171 
172 /*
173  * If set is true, sets the new minimum heap size to size; always
174  * returns the current (or previous) size.  If size is zero,
175  * removes the current minimum constraint (if present).
176  */
177 size_t dvmMinimumHeapSize(size_t size, bool set);
178 
179 /*
180  * Updates the internal count of externally-allocated memory.  If there's
181  * enough room for that memory, returns true.  If not, returns false and
182  * does not update the count.
183  *
184  * May cause a GC as a side-effect.
185  */
186 bool dvmTrackExternalAllocation(size_t n);
187 
188 /*
189  * Reduces the internal count of externally-allocated memory.
190  */
191 void dvmTrackExternalFree(size_t n);
192 
193 /*
194  * Returns the number of externally-allocated bytes being tracked by
195  * dvmTrackExternalAllocation/Free().
196  */
197 size_t dvmGetExternalBytesAllocated(void);
198 
199 #endif /*_DALVIK_ALLOC_ALLOC*/
200