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