mspace.h revision 17128f6e433afae326a57a7961499f529f532c61
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
83/*
84   Identical to create_contiguous_mspace, but uses previously mapped memory.
85*/
86mspace create_contiguous_mspace_with_base(size_t starting_capacity,
87    size_t max_capacity, int locked, void *base);
88
89size_t destroy_contiguous_mspace(mspace msp);
90#endif
91
92/*
93  Call the handler for each block in the specified mspace.
94  chunkptr and chunklen refer to the heap-level chunk including
95  the chunk overhead, and userptr and userlen refer to the
96  user-usable part of the chunk.  If the chunk is free, userptr
97  will be NULL and userlen will be 0.  userlen is not guaranteed
98  to be the same value passed into malloc() for a given chunk;
99  it is >= the requested size.
100 */
101void mspace_walk_heap(mspace msp,
102    void(*handler)(const void *chunkptr, size_t chunklen,
103        const void *userptr, size_t userlen, void *arg), void *harg);
104
105/*
106  mspace_walk_free_pages(handler, harg)
107
108  Calls the provided handler on each free region in the specified
109  mspace.  The memory between start and end are guaranteed not to
110  contain any important data, so the handler is free to alter the
111  contents in any way.  This can be used to advise the OS that large
112  free regions may be swapped out.
113
114  The value in harg will be passed to each call of the handler.
115 */
116void mspace_walk_free_pages(mspace msp,
117    void(*handler)(void *start, void *end, void *arg), void *harg);
118
119#ifdef __cplusplus
120};  /* end of extern "C" */
121#endif
122
123#endif /* MSPACE_H_ */
124