| 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 | /* |
| Barry Hayes | 364f9d9 | 2010-06-11 16:12:47 -0700 | [diff] [blame] | 95 | * Returns true iff <ptr> points within allocation-managed address space. |
| 96 | */ |
| 97 | bool dvmIsValidObjectAddress(const void *ptr); |
| 98 | |
| 99 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 100 | * Create a copy of an object. |
| 101 | * |
| 102 | * The new object will be added to the "tracked alloc" table. |
| 103 | */ |
| 104 | Object* 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 | */ |
| 116 | INLINE 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 McFadden | 0423f0e | 2009-08-26 07:21:53 -0700 | [diff] [blame] | 124 | dvmAbort(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 125 | 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 Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 133 | dvmAbort(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 134 | 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 | */ |
| 146 | size_t dvmObjectSizeInHeap(const Object* obj); |
| 147 | |
| 148 | /* |
| 149 | * Gets the current ideal heap utilization, represented as a number |
| 150 | * between zero and one. |
| 151 | */ |
| 152 | float dvmGetTargetHeapUtilization(void); |
| 153 | |
| 154 | /* |
| 155 | * Sets the new ideal heap utilization, represented as a number |
| 156 | * between zero and one. |
| 157 | */ |
| 158 | void 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 | */ |
| 165 | size_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 | */ |
| 174 | bool dvmTrackExternalAllocation(size_t n); |
| 175 | |
| 176 | /* |
| 177 | * Reduces the internal count of externally-allocated memory. |
| 178 | */ |
| 179 | void dvmTrackExternalFree(size_t n); |
| 180 | |
| 181 | /* |
| 182 | * Returns the number of externally-allocated bytes being tracked by |
| 183 | * dvmTrackExternalAllocation/Free(). |
| 184 | */ |
| 185 | size_t dvmGetExternalBytesAllocated(void); |
| 186 | |
| 187 | #endif /*_DALVIK_ALLOC_ALLOC*/ |