blob: b22d9a4d8e2d9c16da61d8f838b02bd8f25c31a1 [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001/*
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 "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
91/*
92 Returns the position of the "break" within the given mspace.
93*/
94void *contiguous_mspace_sbrk0(mspace msp);
95#endif
96
97/*
98 Call the handler for each block in the specified mspace.
99 chunkptr and chunklen refer to the heap-level chunk including
100 the chunk overhead, and userptr and userlen refer to the
101 user-usable part of the chunk. If the chunk is free, userptr
102 will be NULL and userlen will be 0. userlen is not guaranteed
103 to be the same value passed into malloc() for a given chunk;
104 it is >= the requested size.
105 */
106void mspace_walk_heap(mspace msp,
107 void(*handler)(const void *chunkptr, size_t chunklen,
108 const void *userptr, size_t userlen, void *arg), void *harg);
109
110/*
111 mspace_walk_free_pages(handler, harg)
112
113 Calls the provided handler on each free region in the specified
114 mspace. The memory between start and end are guaranteed not to
115 contain any important data, so the handler is free to alter the
116 contents in any way. This can be used to advise the OS that large
117 free regions may be swapped out.
118
119 The value in harg will be passed to each call of the handler.
120 */
121void mspace_walk_free_pages(mspace msp,
122 void(*handler)(void *start, void *end, void *arg), void *harg);
123
124#ifdef __cplusplus
125}; /* end of extern "C" */
126#endif
127
128#endif /* MSPACE_H_ */