Alloc.h revision 375fb116bcb817b37509ab579dbd55cdbb765cbf
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 */ 25enum { 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 */ 34bool dvmGcStartup(void); 35bool dvmCreateStockExceptions(void); 36bool dvmGcStartupAfterZygote(void); 37void dvmGcShutdown(void); 38void dvmGcThreadShutdown(void); 39bool dvmGcStartupClasses(void); 40 41/* 42 * Do any last-minute preparation before we call fork() for the first time. 43 */ 44bool 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 */ 54void* 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 */ 64extern "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 */ 73extern "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 */ 82extern "C" void dvmReleaseTrackedAlloc(Object* obj, Thread* self); 83 84/* 85 * Returns true iff <obj> points to a valid allocated object. 86 */ 87bool dvmIsValidObject(const Object* obj); 88 89/* 90 * Returns true iff <obj> points to a zygote allocated object. 91 */ 92bool dvmIsZygoteObject(const Object* obj); 93 94/* 95 * Create a copy of an object. 96 * 97 * Returns NULL and throws an exception on failure. 98 */ 99Object* dvmCloneObject(Object* obj, int flags); 100 101/* 102 * Make the object finalizable. 103 */ 104extern "C" void dvmSetFinalizable(Object* obj); 105 106/* 107 * Determine the exact number of GC heap bytes used by an object. (Internal 108 * to heap code except for debugging.) 109 */ 110size_t dvmObjectSizeInHeap(const Object* obj); 111 112/* 113 * Gets the current ideal heap utilization, represented as a number 114 * between zero and one. 115 */ 116float dvmGetTargetHeapUtilization(void); 117 118/* 119 * Sets the new ideal heap utilization, represented as a number 120 * between zero and one. 121 */ 122void dvmSetTargetHeapUtilization(float newTarget); 123 124/* 125 * Initiate garbage collection. 126 * 127 * This usually happens automatically, but can also be caused by 128 * Runtime.gc(). 129 */ 130void dvmCollectGarbage(void); 131 132/* 133 * Returns a count of the direct instances of a class. 134 */ 135size_t dvmCountInstancesOfClass(const ClassObject *clazz); 136 137/* 138 * Returns a count of the instances of a class and its subclasses. 139 */ 140size_t dvmCountAssignableInstancesOfClass(const ClassObject *clazz); 141 142/* 143 * Removes any growth limits from the heap. 144 */ 145void dvmClearGrowthLimit(void); 146 147/* 148 * Returns true if the address is within the bounds of the heap. 149 */ 150bool dvmIsHeapAddress(void *address); 151 152bool dvmIsNonMovingObject(const Object* object); 153 154#endif // DALVIK_ALLOC_ALLOC_H_ 155