mspace.h revision 4f6e8d7a00cbeda1e70cc15be9c4af1018bdad53
1/*
2 * Copyright (C) 2006 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/* A wrapper file for dlmalloc.h that defines prototypes for the
18 * mspace_*() functions, which provide an interface for creating
19 * multiple heaps.
20 */
21
22#ifndef MSPACE_H_
23#define MSPACE_H_
24
25/* It's a pain getting the mallinfo stuff to work
26 * with Linux, OSX, and klibc, so just turn it off
27 * for now.
28 * TODO: make mallinfo work
29 */
30#define NO_MALLINFO 1
31
32/* Allow setting the maximum heap footprint.
33 */
34#define USE_MAX_ALLOWED_FOOTPRINT 1
35
36#define USE_CONTIGUOUS_MSPACES 1
37#if USE_CONTIGUOUS_MSPACES
38#define HAVE_MMAP 0
39#define HAVE_MORECORE 1
40#define MORECORE_CONTIGUOUS 0
41#endif
42
43#define MSPACES 1
44#define ONLY_MSPACES 1
45#include "../../../../bionic/libc/bionic/dlmalloc.h"
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51/*
52  mspace_usable_size(void* p);
53
54  Returns the number of bytes you can actually use in
55  an allocated chunk, which may be more than you requested (although
56  often not) due to alignment and minimum size constraints.
57  You can use this many bytes without worrying about
58  overwriting other allocated objects. This is not a particularly great
59  programming practice. mspace_usable_size can be more useful in
60  debugging and assertions, for example:
61
62  p = mspace_malloc(msp, n);
63  assert(mspace_usable_size(msp, p) >= 256);
64*/
65size_t mspace_usable_size(mspace, const void*);
66
67#if USE_CONTIGUOUS_MSPACES
68/*
69  Similar to create_mspace(), but the underlying memory is
70  guaranteed to be contiguous.  No more than max_capacity
71  bytes is ever allocated to the mspace.
72 */
73mspace create_contiguous_mspace(size_t starting_capacity, size_t max_capacity,
74    int locked);
75
76/*
77   Identical to create_contiguous_mspace, but labels the mapping 'mspace/name'
78   instead of 'mspace'
79*/
80mspace create_contiguous_mspace_with_name(size_t starting_capacity,
81    size_t max_capacity, int locked, const char *name);
82
83size_t destroy_contiguous_mspace(mspace msp);
84#endif
85
86/*
87  Call the handler for each block in the specified mspace.
88  chunkptr and chunklen refer to the heap-level chunk including
89  the chunk overhead, and userptr and userlen refer to the
90  user-usable part of the chunk.  If the chunk is free, userptr
91  will be NULL and userlen will be 0.  userlen is not guaranteed
92  to be the same value passed into malloc() for a given chunk;
93  it is >= the requested size.
94 */
95void mspace_walk_heap(mspace msp,
96    void(*handler)(const void *chunkptr, size_t chunklen,
97        const void *userptr, size_t userlen, void *arg), void *harg);
98
99/*
100  mspace_walk_free_pages(handler, harg)
101
102  Calls the provided handler on each free region in the specified
103  mspace.  The memory between start and end are guaranteed not to
104  contain any important data, so the handler is free to alter the
105  contents in any way.  This can be used to advise the OS that large
106  free regions may be swapped out.
107
108  The value in harg will be passed to each call of the handler.
109 */
110void mspace_walk_free_pages(mspace msp,
111    void(*handler)(void *start, void *end, void *arg), void *harg);
112
113#ifdef __cplusplus
114};  /* end of extern "C" */
115#endif
116
117#endif /* MSPACE_H_ */
118