blob: 772d38d5b91a05cacc58cb252771fbfbc17be9a1 [file] [log] [blame]
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -07001/*
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#ifndef _DALVIK_HEAP_SOURCE
17#define _DALVIK_HEAP_SOURCE
18
Carl Shapirod25566d2010-03-11 20:39:47 -080019#include "alloc/Heap.h"
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070020#include "alloc/HeapInternal.h" // for GcHeap
21
22/* dlmalloc uses one size_t per allocated chunk.
23 */
24#define HEAP_SOURCE_CHUNK_OVERHEAD (1 * sizeof (size_t))
25#define HEAP_SOURCE_WORST_CHUNK_OVERHEAD (32 * sizeof (size_t))
26
27/* The largest number of separate heaps we can handle.
28 */
Carl Shapiroc8e06c82010-02-04 19:12:55 -080029#define HEAP_SOURCE_MAX_HEAP_COUNT 2
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070030
31/*
32 * Initializes the heap source; must be called before any other
33 * dvmHeapSource*() functions.
34 */
35GcHeap *dvmHeapSourceStartup(size_t startSize, size_t absoluteMaxSize);
36
37/*
38 * If the HeapSource was created while in zygote mode, this
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070039 * will create an additional zygote heap before the first fork().
40 * Having a separate heap should reduce the number of shared
41 * pages subsequently touched by the zygote process.
42 */
43bool dvmHeapSourceStartupBeforeFork(void);
44
45/*
46 * Tears down the heap source and frees any resources associated with it.
47 */
Carl Shapiroa199eb72010-02-09 16:26:30 -080048void dvmHeapSourceShutdown(GcHeap **gcHeap);
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070049
50/*
Carl Shapirof373efd2010-02-19 00:46:33 -080051 * Initializes a vector of object and mark bits to the object and mark
52 * bits of to each heap.
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070053 */
Carl Shapirof373efd2010-02-19 00:46:33 -080054void dvmHeapSourceGetObjectBitmaps(HeapBitmap objBits[], HeapBitmap markBits[],
55 size_t numHeaps);
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070056
57/*
Barry Hayes962adba2010-03-17 12:12:39 -070058 * Get the bitmap representing all live objects.
59 */
60HeapBitmap *dvmHeapSourceGetLiveBits();
61
62/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070063 * Returns the requested value. If the per-heap stats are requested, fill
64 * them as well.
65 */
66enum HeapSourceValueSpec {
67 HS_FOOTPRINT,
68 HS_ALLOWED_FOOTPRINT,
69 HS_BYTES_ALLOCATED,
70 HS_OBJECTS_ALLOCATED,
71 HS_EXTERNAL_BYTES_ALLOCATED,
72 HS_EXTERNAL_LIMIT
73};
74size_t dvmHeapSourceGetValue(enum HeapSourceValueSpec spec,
75 size_t perHeapStats[], size_t arrayLen);
76
77/*
78 * Allocates <n> bytes of zeroed data.
79 */
80void *dvmHeapSourceAlloc(size_t n);
81
82/*
83 * Allocates <n> bytes of zeroed data, growing up to absoluteMaxSize
84 * if necessary.
85 */
86void *dvmHeapSourceAllocAndGrow(size_t n);
87
88/*
89 * Frees the memory pointed to by <ptr>, which may be NULL.
90 */
91void dvmHeapSourceFree(void *ptr);
92
93/*
Barry Hayesdde8ab02009-05-20 12:10:36 -070094 * Frees the first numPtrs objects in the ptrs list. The list must
95 * contain addresses all in the same mspace, and must be in increasing
96 * order. This implies that there are no duplicates, and no entries
97 * are NULL.
98 */
99void dvmHeapSourceFreeList(size_t numPtrs, void **ptrs);
100
101/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700102 * Returns true iff <ptr> was allocated from the heap source.
103 */
104bool dvmHeapSourceContains(const void *ptr);
105
106/*
107 * Returns the value of the requested flag.
108 */
109enum HeapSourcePtrFlag {
110 HS_CONTAINS, // identical to dvmHeapSourceContains()
111 HS_ALLOCATED_IN_ZYGOTE
112};
113bool dvmHeapSourceGetPtrFlag(const void *ptr, enum HeapSourcePtrFlag flag);
114
115/*
116 * Returns the number of usable bytes in an allocated chunk; the size
117 * may be larger than the size passed to dvmHeapSourceAlloc().
118 */
119size_t dvmHeapSourceChunkSize(const void *ptr);
120
121/*
122 * Returns the number of bytes that the heap source has allocated
123 * from the system using sbrk/mmap, etc.
124 */
125size_t dvmHeapSourceFootprint(void);
126
127/*
128 * Gets the maximum number of bytes that the heap source is allowed
129 * to allocate from the system.
130 */
131size_t dvmHeapSourceGetIdealFootprint(void);
132
133/*
134 * Given the current contents of the heap, increase the allowed
135 * heap footprint to match the target utilization ratio. This
136 * should only be called immediately after a full mark/sweep.
137 */
138void dvmHeapSourceGrowForUtilization(void);
139
140/*
141 * Return unused memory to the system if possible. If <bytesTrimmed>
142 * is non-NULL, the number of bytes returned to the system is written to it.
143 */
144void dvmHeapSourceTrim(size_t bytesTrimmed[], size_t arrayLen);
145
146/*
147 * Walks over the heap source and passes every allocated and
148 * free chunk to the callback.
149 */
150void dvmHeapSourceWalk(void(*callback)(const void *chunkptr, size_t chunklen,
151 const void *userptr, size_t userlen,
152 void *arg),
153 void *arg);
154/*
155 * Gets the number of heaps available in the heap source.
156 */
157size_t dvmHeapSourceGetNumHeaps(void);
158
Carl Shapirof373efd2010-02-19 00:46:33 -0800159/*
160 * Exchanges the mark and object bitmaps and zeros the mark bitmap.
161 */
162void dvmHeapSourceSwapBitmaps(void);
163
Carl Shapirod25566d2010-03-11 20:39:47 -0800164/*
Barry Hayes425848f2010-05-04 13:32:12 -0700165 * Marks all objects inside the immune region of the heap. Addresses
166 * at or above this pointer are threatened, addresses below this
167 * pointer are immune.
Carl Shapirod25566d2010-03-11 20:39:47 -0800168 */
Barry Hayes425848f2010-05-04 13:32:12 -0700169void dvmMarkImmuneObjects(const char *immuneLimit);
Carl Shapirod25566d2010-03-11 20:39:47 -0800170
171/*
172 * Returns a pointer that demarcates the threatened region of the
173 * heap. Addresses at or above this pointer are threatened, addresses
Barry Hayes425848f2010-05-04 13:32:12 -0700174 * below this pointer are immune.
Carl Shapirod25566d2010-03-11 20:39:47 -0800175 */
176void *dvmHeapSourceGetImmuneLimit(GcMode mode);
177
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700178#endif // _DALVIK_HEAP_SOURCE