* configure.in: Add support for --enable-cgen-maint.
[deliverable/binutils-gdb.git] / mmalloc / mmalloc.texi
CommitLineData
d203e172
RP
1\input texinfo @c -*- Texinfo -*-
2@setfilename mmalloc.info
3
4@ifinfo
5@format
6START-INFO-DIR-ENTRY
7* Mmalloc: (mmalloc). The GNU mapped-malloc package.
8END-INFO-DIR-ENTRY
9@end format
10
11This file documents the GNU mmalloc (mapped-malloc) package, written by
12fnf@@cygnus.com.
13
14Copyright (C) 1992 Free Software Foundation, Inc.
15
16Permission is granted to make and distribute verbatim copies of
17this manual provided the copyright notice and this permission notice
18are preserved on all copies.
19
20@ignore
21Permission is granted to process this file through Tex and print the
22results, provided the printed document carries copying permission
23notice identical to this one except for the removal of this paragraph
24(this paragraph not being relevant to the printed manual).
25
26@end ignore
27Permission is granted to copy and distribute modified versions of this
28manual under the conditions for verbatim copying, provided also that the
29entire resulting derived work is distributed under the terms of a
30permission notice identical to this one.
31
32Permission is granted to copy and distribute translations of this manual
33into another language, under the above conditions for modified versions.
34@end ifinfo
35@iftex
36@c @finalout
37@setchapternewpage odd
38@settitle MMALLOC, the GNU memory-mapped malloc package
39@titlepage
40@title mmalloc
41@subtitle The GNU memory-mapped malloc package
42@author Fred Fish
43@author Cygnus Support
44@page
45
46@tex
47\def\$#1${{#1}} % Kluge: collect RCS revision info without $...$
48\xdef\manvers{\$Revision$} % For use in headers, footers too
49{\parskip=0pt
50\hfill Cygnus Support\par
51\hfill fnf\@cygnus.com\par
52\hfill {\it MMALLOC, the GNU memory-mapped malloc package}, \manvers\par
53\hfill \TeX{}info \texinfoversion\par
54}
55@end tex
56
57@vskip 0pt plus 1filll
58Copyright @copyright{} 1992 Free Software Foundation, Inc.
59
60Permission is granted to make and distribute verbatim copies of
61this manual provided the copyright notice and this permission notice
62are preserved on all copies.
63
64Permission is granted to copy and distribute modified versions of this
65manual under the conditions for verbatim copying, provided also that
66the entire resulting derived work is distributed under the terms of a
67permission notice identical to this one.
68
69Permission is granted to copy and distribute translations of this manual
70into another language, under the above conditions for modified versions.
71@end titlepage
72@end iftex
73
74@ifinfo
75@node Top, Overview, (dir), (dir)
76@top mmalloc
77This file documents the GNU memory-mapped malloc package mmalloc.
78
79@menu
80* Overview:: Overall Description
81* Implementation:: Implementation
82
83 --- The Detailed Node Listing ---
84
85Implementation
86
87* Compatibility:: Backwards Compatibility
88* Functions:: Function Descriptions
89@end menu
90
91@end ifinfo
92
93@node Overview, Implementation, Top, Top
94@chapter Overall Description
95
96This is a heavily modified version of GNU @code{malloc}. It uses
97@code{mmap} as the basic mechanism for for obtaining memory from the
98system, rather than @code{sbrk}. This gives it several advantages over the
99more traditional malloc:
100
101@itemize @bullet
102@item
103Providing suitable precautions are taken to avoid memory region
104collisions, @code{sbrk} is now available for use by applications that
105use this package and still need to use some memory management
106package that includes functions like @code{malloc}, @code{realloc}, and
107@code{free}.
108
109@item
110Several different memory pools can be used, each of them growing
111or shinking under control of @code{mmap}, with the @code{mmalloc} functions
112using a specific pool on a call by call basis.
113
114@item
115By using @code{mmap}, it is easy to create data pools which are intended to
116be persistent and exist as a filesystem object after the creating
117process has gone away.
118
119@item
120Because multiple memory pools can be managed, data used for a
121specific purpose can be allocated into its own memory pool, making
122it easier to allow applications to ``dump'' and ``restore'' initialized
123malloc-managed memory regions. For example, the ``unexec'' hack popularized
124by GNU Emacs could potentially go away.
125@end itemize
126
127@node Implementation, , Overview, Top
128@chapter Implementation
129
130The @code{mmalloc} functions contain no internal static state. All
131@code{mmalloc} internal data is allocated in the mapped in region, along
132with the user data that it manages. This allows it to manage multiple
133such regions and to ``pick up where it left off'' when such regions are
134later dynamically mapped back in.
135
136In some sense, malloc has been ``purified'' to contain no internal state
137information and generalized to use multiple memory regions rather than a
138single region managed by @code{sbrk}. However the new routines now need an
139extra parameter which informs @code{mmalloc} which memory region it is dealing
140with (along with other information). This parameter is called the
141@dfn{malloc descriptor}.
142
143For ease of initial implementation, and to avoid exporting or importing
144any more global variables or routines than necessary, this package is
145implemented with all functions contained within a single source file.
146At some future point, once everything has stabilized, it may be desirable
147to split it up into separate files.
148
149The functions initially provided by @code{mmalloc} are:
150
151@example
152void *mmalloc_attach (int fd, void *baseaddr);
153void *mmalloc_detach (void *md);
154int mmalloc_errno (void *md);
155int mmalloc_setkey (void *md, int keynum, void *key);
156void *mmalloc_getkey (void *md, int keynum);
157
158void *mmalloc (void *md, size_t size);
159void *mrealloc (void *md, void *ptr, size_t size);
160void *mvalloc (void *md, size_t size);
161void mfree (void *md, void *ptr);
162@end example
163
164@menu
165* Compatibility:: Backwards Compatibility
166* Functions:: Function Descriptions
167@end menu
168
169@node Compatibility, Functions, Implementation, Implementation
170@section Backwards Compatibility
171
172To allow a single malloc package to be used in a given application,
173provision is made for the traditional @code{malloc}, @code{realloc}, and
174@code{free} functions to be implemented as special cases of the
175@code{mmalloc} functions. In particular, if any of the functions that
176expect malloc descriptors are called with a @code{NULL} pointer rather than a
177valid malloc descriptor, then they default to using a memory-mapped region
178starting at the current @code{sbrk} value and mapped to @file{/dev/zero}.
179Applications can simply include the following defines to use the
180@code{mmalloc} versions:
181
182@example
183#define malloc(size) mmalloc ((void *)0, (size))
184#define realloc(ptr,size) mrealloc ((void *)0, (ptr), (size));
185#define free(ptr) mfree ((void *)0, (ptr))
186@end example
187
188@noindent
189or replace the existing @code{malloc}, @code{realloc}, and @code{free}
190calls with the above patterns if using @code{#define} causes problems.
191
192Note that this does not prevent calls to @code{malloc}, @code{realloc},
193or @code{free} within libraries from continuing to use the library
194version of malloc, so if this is a problem, the compatibility issue
195needs to be dealt with in another way.
196
197
198@node Functions, , Compatibility, Implementation
199@section Function Descriptions
200
201These are the details on the functions that make up the @code{mmalloc}
202package.
203
204@table @code
205@item void *mmalloc_attach (int @var{fd}, void *@var{baseaddr});
206Initialize access to a @code{mmalloc} managed region.
207
208If @var{fd} is a valid file descriptor for an open file, then data for the
209@code{mmalloc} managed region is mapped to that file. Otherwise
210@file{/dev/zero} is used and the data will not exist in any filesystem object.
211
212If the open file corresponding to @var{fd} is from a previous use of
213@code{mmalloc} and passes some basic sanity checks to ensure that it is
214compatible with the current @code{mmalloc} package, then its data is
215mapped in and is immediately accessible at the same addresses in
216the current process as the process that created the file.
217
218If @var{baseaddr} is not @code{NULL}, the mapping is established
219starting at the specified address in the process address space. If
220@var{baseaddr} is @code{NULL}, the @code{mmalloc} package chooses a
221suitable address at which to start the mapped region, which will be the
222value of the previous mapping if opening an existing file which was
223previously built by @code{mmalloc}, or for new files will be a value
224chosen by @code{mmap}.
225
226Specifying @var{baseaddr} provides more control over where the regions
227start and how big they can be before bumping into existing mapped
228regions or future mapped regions.
229
230On success, returns a malloc descriptor which is used in subsequent
231calls to other @code{mmalloc} package functions. It is explicitly
232@samp{void *} (@samp{char *} for systems that don't fully support
233@code{void}) so that users of the package don't have to worry about the
234actual implementation details.
235
236On failure returns @code{NULL}.
237
238@item void *mmalloc_detach (void *@var{md});
239Terminate access to a @code{mmalloc} managed region identified by the
240descriptor @var{md}, by closing the base file and unmapping all memory
241pages associated with the region.
242
243Returns @code{NULL} on success.
244
245Returns the malloc descriptor on failure, which can subsequently
246be used for further action (such as obtaining more information about
247the nature of the failure).
248
249@item void *mmalloc (void *@var{md}, size_t @var{size});
250Given an @code{mmalloc} descriptor @var{md}, allocate additional memory of
251@var{size} bytes in the associated mapped region.
252
253@item *mrealloc (void *@var{md}, void *@var{ptr}, size_t @var{size});
254Given an @code{mmalloc} descriptor @var{md} and a pointer to memory
255previously allocated by @code{mmalloc} in @var{ptr}, reallocate the
256memory to be @var{size} bytes long, possibly moving the existing
257contents of memory if necessary.
258
259@item void *mvalloc (void *@var{md}, size_t @var{size});
260Like @code{mmalloc} but the resulting memory is aligned on a page boundary.
261
262@item void mfree (void *@var{md}, void *@var{ptr});
263Given an @code{mmalloc} descriptor @var{md} and a pointer to memory previously
264allocated by @code{mmalloc} in @var{ptr}, free the previously allocated memory.
265
266@item int mmalloc_errno (void *@var{md});
267Given a @code{mmalloc} descriptor, if the last @code{mmalloc} operation
268failed for some reason due to a system call failure, then
269returns the associated @code{errno}. Returns 0 otherwise.
270@end table
271
272@bye
This page took 0.2202 seconds and 4 git commands to generate.