2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef _DALVIK_HEAP_SOURCE
17 #define _DALVIK_HEAP_SOURCE
19 #include "alloc/Heap.h"
20 #include "alloc/HeapInternal.h" // for GcHeap
22 /* dlmalloc uses one size_t per allocated chunk.
24 #define HEAP_SOURCE_CHUNK_OVERHEAD (1 * sizeof (size_t))
25 #define HEAP_SOURCE_WORST_CHUNK_OVERHEAD (32 * sizeof (size_t))
27 /* The largest number of separate heaps we can handle.
29 #define HEAP_SOURCE_MAX_HEAP_COUNT 2
32 * Initializes the heap source; must be called before any other
33 * dvmHeapSource*() functions.
35 GcHeap *dvmHeapSourceStartup(size_t startSize, size_t absoluteMaxSize);
38 * If the HeapSource was created while in zygote mode, this
39 * will create a new heap for post-zygote allocations.
40 * Having a separate heap should maximize the number of pages
41 * that a given app_process shares with the zygote process.
43 bool dvmHeapSourceStartupAfterZygote(void);
46 * If the HeapSource was created while in zygote mode, this
47 * will create an additional zygote heap before the first fork().
48 * Having a separate heap should reduce the number of shared
49 * pages subsequently touched by the zygote process.
51 bool dvmHeapSourceStartupBeforeFork(void);
54 * Shutdown any threads internal to the heap source. This should be
55 * called before the heap source itself is shutdown.
57 void dvmHeapSourceThreadShutdown(void);
60 * Tears down the heap source and frees any resources associated with it.
62 void dvmHeapSourceShutdown(GcHeap **gcHeap);
65 * Returns the base and inclusive max addresses of the heap source
66 * heaps. The base and max values are suitable for passing directly
67 * to the bitmap sweeping routine.
69 void dvmHeapSourceGetRegions(uintptr_t *base, uintptr_t *max, size_t numHeaps);
72 * Get the bitmap representing all live objects.
74 HeapBitmap *dvmHeapSourceGetLiveBits(void);
77 * Get the bitmap representing all marked objects.
79 HeapBitmap *dvmHeapSourceGetMarkBits(void);
82 * Gets the begining of the allocation for the HeapSource.
84 void *dvmHeapSourceGetBase(void);
87 * Returns the requested value. If the per-heap stats are requested, fill
90 enum HeapSourceValueSpec {
95 HS_EXTERNAL_BYTES_ALLOCATED,
98 size_t dvmHeapSourceGetValue(enum HeapSourceValueSpec spec,
99 size_t perHeapStats[], size_t arrayLen);
102 * Allocates <n> bytes of zeroed data.
104 void *dvmHeapSourceAlloc(size_t n);
107 * Allocates <n> bytes of zeroed data, growing up to absoluteMaxSize
110 void *dvmHeapSourceAllocAndGrow(size_t n);
113 * Frees the first numPtrs objects in the ptrs list and returns the
114 * amount of reclaimed storage. The list must contain addresses all
115 * in the same mspace, and must be in increasing order. This implies
116 * that there are no duplicates, and no entries are NULL.
118 size_t dvmHeapSourceFreeList(size_t numPtrs, void **ptrs);
121 * Returns true iff <ptr> was allocated from the heap source.
123 bool dvmHeapSourceContains(const void *ptr);
126 * Returns true iff <ptr> is within the address space managed by heap source.
128 bool dvmHeapSourceContainsAddress(const void *ptr);
131 * Returns the value of the requested flag.
133 enum HeapSourcePtrFlag {
134 HS_CONTAINS, // identical to dvmHeapSourceContains()
135 HS_ALLOCATED_IN_ZYGOTE
137 bool dvmHeapSourceGetPtrFlag(const void *ptr, enum HeapSourcePtrFlag flag);
140 * Returns the number of usable bytes in an allocated chunk; the size
141 * may be larger than the size passed to dvmHeapSourceAlloc().
143 size_t dvmHeapSourceChunkSize(const void *ptr);
146 * Returns the number of bytes that the heap source has allocated
147 * from the system using sbrk/mmap, etc.
149 size_t dvmHeapSourceFootprint(void);
152 * Gets the maximum number of bytes that the heap source is allowed
153 * to allocate from the system.
155 size_t dvmHeapSourceGetIdealFootprint(void);
158 * Given the current contents of the heap, increase the allowed
159 * heap footprint to match the target utilization ratio. This
160 * should only be called immediately after a full mark/sweep.
162 void dvmHeapSourceGrowForUtilization(void);
165 * Return unused memory to the system if possible. If <bytesTrimmed>
166 * is non-NULL, the number of bytes returned to the system is written to it.
168 void dvmHeapSourceTrim(size_t bytesTrimmed[], size_t arrayLen);
171 * Walks over the heap source and passes every allocated and
172 * free chunk to the callback.
174 void dvmHeapSourceWalk(void(*callback)(const void *chunkptr, size_t chunklen,
175 const void *userptr, size_t userlen,
179 * Gets the number of heaps available in the heap source.
181 size_t dvmHeapSourceGetNumHeaps(void);
184 * Exchanges the mark and object bitmaps.
186 void dvmHeapSourceSwapBitmaps(void);
189 * Zeroes the mark bitmap.
191 void dvmHeapSourceZeroMarkBitmap(void);
194 * Marks all objects inside the immune region of the heap. Addresses
195 * at or above this pointer are threatened, addresses below this
196 * pointer are immune.
198 void dvmMarkImmuneObjects(const char *immuneLimit);
201 * Returns a pointer that demarcates the threatened region of the
202 * heap. Addresses at or above this pointer are threatened, addresses
203 * below this pointer are immune.
205 void *dvmHeapSourceGetImmuneLimit(GcMode mode);
207 #endif // _DALVIK_HEAP_SOURCE