| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | |
| Carl Shapiro | ad9400f | 2010-05-01 20:08:57 -0700 | [diff] [blame] | 22 | #include <stddef.h> |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * Initialization. |
| 26 | */ |
| 27 | bool dvmGcStartup(void); |
| Andy McFadden | 7fc3ce8 | 2009-07-14 15:57:23 -0700 | [diff] [blame] | 28 | bool dvmCreateStockExceptions(void); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 29 | bool dvmGcStartupAfterZygote(void); |
| 30 | void dvmGcShutdown(void); |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 31 | void dvmGcThreadShutdown(void); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 |
| Barry Hayes | d4f78d3 | 2010-06-08 09:34:42 -0700 | [diff] [blame] | 42 | * flags is ALLOC_DONT_TRACK. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 |
| Barry Hayes | d4f78d3 | 2010-06-08 09:34:42 -0700 | [diff] [blame] | 52 | * flags is ALLOC_DONT_TRACK. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 53 | * |
| 54 | * Returns NULL and throws an exception on failure. |
| 55 | */ |
| 56 | Object* dvmAllocObject(ClassObject* clazz, int flags); |
| 57 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 58 | /* flags for dvmMalloc */ |
| 59 | enum { |
| 60 | ALLOC_DEFAULT = 0x00, |
| Barry Hayes | d4f78d3 | 2010-06-08 09:34:42 -0700 | [diff] [blame] | 61 | ALLOC_DONT_TRACK = 0x01, /* don't add to internal tracking list */ |
| 62 | ALLOC_FINALIZABLE = 0x02, /* call finalize() before freeing */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | */ |
| 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)) { |
| Andy McFadden | 0423f0e | 2009-08-26 07:21:53 -0700 | [diff] [blame] | 119 | dvmAbort(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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) { |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 128 | dvmAbort(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | /* |
| Carl Shapiro | 0445e48 | 2010-12-02 17:27:01 -0800 | [diff] [blame] | 156 | * Initiate garbage collection. |
| 157 | * |
| Carl Shapiro | a371fad | 2011-01-23 15:58:31 -0800 | [diff] [blame^] | 158 | * This usually happens automatically, but can also be caused by |
| 159 | * Runtime.gc(). If clearSoftReferences is true, the garbage |
| 160 | * collector will not attempt to preserve any softly-reachable |
| 161 | * SoftReference referents. |
| Carl Shapiro | 0445e48 | 2010-12-02 17:27:01 -0800 | [diff] [blame] | 162 | */ |
| Carl Shapiro | a371fad | 2011-01-23 15:58:31 -0800 | [diff] [blame^] | 163 | void dvmCollectGarbage(bool clearSoftReferences); |
| Carl Shapiro | 0445e48 | 2010-12-02 17:27:01 -0800 | [diff] [blame] | 164 | |
| 165 | /* |
| Carl Shapiro | f331a60 | 2010-11-04 15:12:09 -0700 | [diff] [blame] | 166 | * Returns a count of the direct instances of a class. |
| Carl Shapiro | 41eb6e9 | 2010-08-17 13:30:48 -0700 | [diff] [blame] | 167 | */ |
| 168 | size_t dvmCountInstancesOfClass(const ClassObject *clazz); |
| 169 | |
| Carl Shapiro | f331a60 | 2010-11-04 15:12:09 -0700 | [diff] [blame] | 170 | /* |
| 171 | * Returns a count of the instances of a class and its subclasses. |
| 172 | */ |
| 173 | size_t dvmCountAssignableInstancesOfClass(const ClassObject *clazz); |
| 174 | |
| Carl Shapiro | df9f08b | 2011-01-18 17:59:30 -0800 | [diff] [blame] | 175 | /* |
| 176 | * Removes any growth limits from the heap. |
| 177 | */ |
| 178 | void dvmClearGrowthLimit(void); |
| 179 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 180 | #endif /*_DALVIK_ALLOC_ALLOC*/ |