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_H_ 20 #define DALVIK_ALLOC_ALLOC_H_ 21 22 #include <stddef.h> 23 24 /* flags for dvmMalloc */ 25 enum { 26 ALLOC_DEFAULT = 0x00, 27 ALLOC_DONT_TRACK = 0x01, /* don't add to internal tracking list */ 28 ALLOC_NON_MOVING = 0x02, 29 }; 30 31 /* 32 * Initialization. 33 */ 34 bool dvmGcStartup(void); 35 bool dvmCreateStockExceptions(void); 36 bool dvmGcStartupAfterZygote(void); 37 void dvmGcShutdown(void); 38 void dvmGcThreadShutdown(void); 39 bool dvmGcStartupClasses(void); 40 41 /* 42 * Do any last-minute preparation before we call fork() for the first time. 43 */ 44 bool dvmGcPreZygoteFork(void); 45 46 /* 47 * Basic allocation function. 48 * 49 * The new object will be added to the "tracked alloc" table unless 50 * flags is ALLOC_DONT_TRACK. 51 * 52 * Returns NULL and throws an exception on failure. 53 */ 54 void* dvmMalloc(size_t size, int flags); 55 56 /* 57 * Allocate a new object. 58 * 59 * The new object will be added to the "tracked alloc" table unless 60 * flags is ALLOC_DONT_TRACK. 61 * 62 * Returns NULL and throws an exception on failure. 63 */ 64 extern "C" Object* dvmAllocObject(ClassObject* clazz, int flags); 65 66 /* 67 * Track an object reference that is currently only visible internally. 68 * This is called automatically by dvmMalloc() unless ALLOC_DONT_TRACK 69 * is set. 70 * 71 * The "self" argument is allowed as an optimization; it may be NULL. 72 */ 73 extern "C" void dvmAddTrackedAlloc(Object* obj, Thread* self); 74 75 /* 76 * Remove an object from the internal tracking list. 77 * 78 * Does nothing if "obj" is NULL. 79 * 80 * The "self" argument is allowed as an optimization; it may be NULL. 81 */ 82 extern "C" void dvmReleaseTrackedAlloc(Object* obj, Thread* self); 83 84 /* 85 * Returns true iff <obj> points to a zygote allocated object. 86 */ 87 bool dvmIsZygoteObject(const Object* obj); 88 89 /* 90 * Create a copy of an object. 91 * 92 * Returns NULL and throws an exception on failure. 93 */ 94 Object* dvmCloneObject(Object* obj, int flags); 95 96 /* 97 * Make the object finalizable. 98 */ 99 extern "C" void dvmSetFinalizable(Object* obj); 100 101 /* 102 * Determine the exact number of GC heap bytes used by an object. (Internal 103 * to heap code except for debugging.) 104 */ 105 size_t dvmObjectSizeInHeap(const Object* obj); 106 107 /* 108 * Gets the current ideal heap utilization, represented as a number 109 * between zero and one. 110 */ 111 float dvmGetTargetHeapUtilization(void); 112 113 /* 114 * Sets the new ideal heap utilization, represented as a number 115 * between zero and one. 116 */ 117 void dvmSetTargetHeapUtilization(float newTarget); 118 119 /* 120 * Initiate garbage collection. 121 * 122 * This usually happens automatically, but can also be caused by 123 * Runtime.gc(). 124 */ 125 void dvmCollectGarbage(void); 126 127 /* 128 * Returns a count of the direct instances of a class. 129 */ 130 size_t dvmCountInstancesOfClass(const ClassObject *clazz); 131 132 /* 133 * Returns a count of the instances of a class and its subclasses. 134 */ 135 size_t dvmCountAssignableInstancesOfClass(const ClassObject *clazz); 136 137 /* 138 * Removes any growth limits from the heap. 139 */ 140 void dvmClearGrowthLimit(void); 141 142 /* 143 * Returns true if the address is aligned appropriately for a heap object. 144 * Does not require the caller to hold the heap lock, and does not take the 145 * heap lock internally. 146 */ 147 bool dvmIsHeapAddress(void *address); 148 149 bool dvmIsNonMovingObject(const Object* object); 150 151 #endif // DALVIK_ALLOC_ALLOC_H_ 152