]> sourceware.org Git - glibc.git/blame - manual/memory.texi
2013-09-23 Steve Ellcey <sellcey@mips.com>
[glibc.git] / manual / memory.texi
CommitLineData
99a20616
UD
1@node Memory, Character Handling, Error Reporting, Top
2@chapter Virtual Memory Allocation And Paging
3@c %MENU% Allocating virtual memory and controlling paging
28f540f4
RM
4@cindex memory allocation
5@cindex storage allocation
6
99a20616 7This chapter describes how processes manage and use memory in a system
1f77f049 8that uses @theglibc{}.
99a20616 9
1f77f049 10@Theglibc{} has several functions for dynamically allocating
99a20616
UD
11virtual memory in various ways. They vary in generality and in
12efficiency. The library also provides functions for controlling paging
13and allocation of real memory.
14
15
16@menu
17* Memory Concepts:: An introduction to concepts and terminology.
18* Memory Allocation:: Allocating storage for your program data
99a20616 19* Resizing the Data Segment:: @code{brk}, @code{sbrk}
4c23fed5 20* Locking Pages:: Preventing page faults
99a20616
UD
21@end menu
22
23Memory mapped I/O is not discussed in this chapter. @xref{Memory-mapped I/O}.
24
25
26
27@node Memory Concepts
28@section Process Memory Concepts
29
30One of the most basic resources a process has available to it is memory.
31There are a lot of different ways systems organize memory, but in a
32typical one, each process has one linear virtual address space, with
33addresses running from zero to some huge maximum. It need not be
11bf311e 34contiguous; i.e., not all of these addresses actually can be used to
99a20616
UD
35store data.
36
37The virtual memory is divided into pages (4 kilobytes is typical).
38Backing each page of virtual memory is a page of real memory (called a
39@dfn{frame}) or some secondary storage, usually disk space. The disk
40space might be swap space or just some ordinary disk file. Actually, a
41page of all zeroes sometimes has nothing at all backing it -- there's
42just a flag saying it is all zeroes.
43@cindex page frame
44@cindex frame, real memory
45@cindex swap space
46@cindex page, virtual memory
47
48The same frame of real memory or backing store can back multiple virtual
49pages belonging to multiple processes. This is normally the case, for
1f77f049 50example, with virtual memory occupied by @glibcadj{} code. The same
99a20616
UD
51real memory frame containing the @code{printf} function backs a virtual
52memory page in each of the existing processes that has a @code{printf}
53call in its program.
54
55In order for a program to access any part of a virtual page, the page
56must at that moment be backed by (``connected to'') a real frame. But
57because there is usually a lot more virtual memory than real memory, the
58pages must move back and forth between real memory and backing store
59regularly, coming into real memory when a process needs to access them
60and then retreating to backing store when not needed anymore. This
61movement is called @dfn{paging}.
62
63When a program attempts to access a page which is not at that moment
64backed by real memory, this is known as a @dfn{page fault}. When a page
65fault occurs, the kernel suspends the process, places the page into a
66real page frame (this is called ``paging in'' or ``faulting in''), then
67resumes the process so that from the process' point of view, the page
68was in real memory all along. In fact, to the process, all pages always
69seem to be in real memory. Except for one thing: the elapsed execution
70time of an instruction that would normally be a few nanoseconds is
71suddenly much, much, longer (because the kernel normally has to do I/O
72to complete the page-in). For programs sensitive to that, the functions
73described in @ref{Locking Pages} can control it.
74@cindex page fault
75@cindex paging
76
77Within each virtual address space, a process has to keep track of what
78is at which addresses, and that process is called memory allocation.
79Allocation usually brings to mind meting out scarce resources, but in
80the case of virtual memory, that's not a major goal, because there is
81generally much more of it than anyone needs. Memory allocation within a
68979757 82process is mainly just a matter of making sure that the same byte of
99a20616
UD
83memory isn't used to store two different things.
84
85Processes allocate memory in two major ways: by exec and
86programmatically. Actually, forking is a third way, but it's not very
87interesting. @xref{Creating a Process}.
88
89Exec is the operation of creating a virtual address space for a process,
90loading its basic program into it, and executing the program. It is
91done by the ``exec'' family of functions (e.g. @code{execl}). The
92operation takes a program file (an executable), it allocates space to
93load all the data in the executable, loads it, and transfers control to
94it. That data is most notably the instructions of the program (the
95@dfn{text}), but also literals and constants in the program and even
96some variables: C variables with the static storage class (@pxref{Memory
97Allocation and C}).
98@cindex executable
99@cindex literals
100@cindex constants
101
102Once that program begins to execute, it uses programmatic allocation to
1f77f049 103gain additional memory. In a C program with @theglibc{}, there
99a20616
UD
104are two kinds of programmatic allocation: automatic and dynamic.
105@xref{Memory Allocation and C}.
106
107Memory-mapped I/O is another form of dynamic virtual memory allocation.
108Mapping memory to a file means declaring that the contents of certain
109range of a process' addresses shall be identical to the contents of a
110specified regular file. The system makes the virtual memory initially
111contain the contents of the file, and if you modify the memory, the
112system writes the same modification to the file. Note that due to the
113magic of virtual memory and page faults, there is no reason for the
114system to do I/O to read the file, or allocate real memory for its
115contents, until the program accesses the virtual memory.
116@xref{Memory-mapped I/O}.
117@cindex memory mapped I/O
118@cindex memory mapped file
119@cindex files, accessing
120
121Just as it programmatically allocates memory, the program can
122programmatically deallocate (@dfn{free}) it. You can't free the memory
123that was allocated by exec. When the program exits or execs, you might
124say that all its memory gets freed, but since in both cases the address
125space ceases to exist, the point is really moot. @xref{Program
126Termination}.
127@cindex execing a program
128@cindex freeing memory
129@cindex exiting a program
130
131A process' virtual address space is divided into segments. A segment is
132a contiguous range of virtual addresses. Three important segments are:
28f540f4 133
28f540f4 134@itemize @bullet
28f540f4 135
68979757 136@item
99a20616
UD
137
138The @dfn{text segment} contains a program's instructions and literals and
139static constants. It is allocated by exec and stays the same size for
68979757 140the life of the virtual address space.
28f540f4
RM
141
142@item
99a20616
UD
143The @dfn{data segment} is working storage for the program. It can be
144preallocated and preloaded by exec and the process can extend or shrink
145it by calling functions as described in @xref{Resizing the Data
146Segment}. Its lower end is fixed.
147
68979757 148@item
99a20616
UD
149The @dfn{stack segment} contains a program stack. It grows as the stack
150grows, but doesn't shrink when the stack shrinks.
151
28f540f4 152@end itemize
99a20616
UD
153
154
155
156@node Memory Allocation
68979757 157@section Allocating Storage For Program Data
99a20616
UD
158
159This section covers how ordinary programs manage storage for their data,
160including the famous @code{malloc} function and some fancier facilities
1f77f049 161special @theglibc{} and GNU Compiler.
28f540f4
RM
162
163@menu
99a20616 164* Memory Allocation and C:: How to get different kinds of allocation in C.
28f540f4
RM
165* Unconstrained Allocation:: The @code{malloc} facility allows fully general
166 dynamic allocation.
bd355af0 167* Allocation Debugging:: Finding memory leaks and not freed memory.
28f540f4
RM
168* Obstacks:: Obstacks are less general than malloc
169 but more efficient and convenient.
170* Variable Size Automatic:: Allocation of variable-sized blocks
171 of automatic storage that are freed when the
172 calling function returns.
28f540f4
RM
173@end menu
174
28f540f4 175
99a20616
UD
176@node Memory Allocation and C
177@subsection Memory Allocation in C Programs
28f540f4 178
99a20616
UD
179The C language supports two kinds of memory allocation through the
180variables in C programs:
28f540f4
RM
181
182@itemize @bullet
183@item
184@dfn{Static allocation} is what happens when you declare a static or
185global variable. Each static or global variable defines one block of
186space, of a fixed size. The space is allocated once, when your program
99a20616
UD
187is started (part of the exec operation), and is never freed.
188@cindex static memory allocation
189@cindex static storage class
28f540f4
RM
190
191@item
192@dfn{Automatic allocation} happens when you declare an automatic
193variable, such as a function argument or a local variable. The space
194for an automatic variable is allocated when the compound statement
195containing the declaration is entered, and is freed when that
196compound statement is exited.
99a20616
UD
197@cindex automatic memory allocation
198@cindex automatic storage class
28f540f4 199
99a20616 200In GNU C, the size of the automatic storage can be an expression
28f540f4
RM
201that varies. In other C implementations, it must be a constant.
202@end itemize
203
99a20616 204A third important kind of memory allocation, @dfn{dynamic allocation},
1f77f049 205is not supported by C variables but is available via @glibcadj{}
99a20616
UD
206functions.
207@cindex dynamic memory allocation
208
209@subsubsection Dynamic Memory Allocation
210@cindex dynamic memory allocation
211
212@dfn{Dynamic memory allocation} is a technique in which programs
213determine as they are running where to store some information. You need
214dynamic allocation when the amount of memory you need, or how long you
215continue to need it, depends on factors that are not known before the
216program runs.
217
218For example, you may need a block to store a line read from an input
219file; since there is no limit to how long a line can be, you must
220allocate the memory dynamically and make it dynamically larger as you
221read more of the line.
222
223Or, you may need a block for each record or each definition in the input
224data; since you can't know in advance how many there will be, you must
225allocate a new block for each record or definition as you read it.
226
227When you use dynamic allocation, the allocation of a block of memory is
228an action that the program requests explicitly. You call a function or
229macro when you want to allocate space, and specify the size with an
230argument. If you want to free the space, you do so by calling another
231function or macro. You can do these things whenever you want, as often
232as you want.
233
28f540f4
RM
234Dynamic allocation is not supported by C variables; there is no storage
235class ``dynamic'', and there can never be a C variable whose value is
99a20616 236stored in dynamically allocated space. The only way to get dynamically
1f77f049
JM
237allocated memory is via a system call (which is generally via a @glibcadj{}
238function call), and the only way to refer to dynamically
99a20616
UD
239allocated space is through a pointer. Because it is less convenient,
240and because the actual process of dynamic allocation requires more
241computation time, programmers generally use dynamic allocation only when
242neither static nor automatic allocation will serve.
28f540f4
RM
243
244For example, if you want to allocate dynamically some space to hold a
245@code{struct foobar}, you cannot declare a variable of type @code{struct
246foobar} whose contents are the dynamically allocated space. But you can
247declare a variable of pointer type @code{struct foobar *} and assign it the
248address of the space. Then you can use the operators @samp{*} and
249@samp{->} on this pointer variable to refer to the contents of the space:
250
251@smallexample
252@{
253 struct foobar *ptr
254 = (struct foobar *) malloc (sizeof (struct foobar));
255 ptr->name = x;
256 ptr->next = current_foobar;
257 current_foobar = ptr;
258@}
259@end smallexample
260
261@node Unconstrained Allocation
99a20616
UD
262@subsection Unconstrained Allocation
263@cindex unconstrained memory allocation
28f540f4
RM
264@cindex @code{malloc} function
265@cindex heap, dynamic allocation from
266
267The most general dynamic allocation facility is @code{malloc}. It
268allows you to allocate blocks of memory of any size at any time, make
269them bigger or smaller at any time, and free the blocks individually at
270any time (or never).
271
272@menu
273* Basic Allocation:: Simple use of @code{malloc}.
274* Malloc Examples:: Examples of @code{malloc}. @code{xmalloc}.
275* Freeing after Malloc:: Use @code{free} to free a block you
276 got with @code{malloc}.
277* Changing Block Size:: Use @code{realloc} to make a block
278 bigger or smaller.
279* Allocating Cleared Space:: Use @code{calloc} to allocate a
280 block and clear it.
281* Efficiency and Malloc:: Efficiency considerations in use of
282 these functions.
68979757 283* Aligned Memory Blocks:: Allocating specially aligned memory.
c131718c
UD
284* Malloc Tunable Parameters:: Use @code{mallopt} to adjust allocation
285 parameters.
28f540f4
RM
286* Heap Consistency Checking:: Automatic checking for errors.
287* Hooks for Malloc:: You can use these hooks for debugging
288 programs that use @code{malloc}.
289* Statistics of Malloc:: Getting information about how much
290 memory your program is using.
291* Summary of Malloc:: Summary of @code{malloc} and related functions.
292@end menu
293
294@node Basic Allocation
99a20616 295@subsubsection Basic Memory Allocation
28f540f4
RM
296@cindex allocation of memory with @code{malloc}
297
298To allocate a block of memory, call @code{malloc}. The prototype for
299this function is in @file{stdlib.h}.
300@pindex stdlib.h
301
302@comment malloc.h stdlib.h
f65fd747 303@comment ISO
28f540f4
RM
304@deftypefun {void *} malloc (size_t @var{size})
305This function returns a pointer to a newly allocated block @var{size}
306bytes long, or a null pointer if the block could not be allocated.
307@end deftypefun
308
309The contents of the block are undefined; you must initialize it yourself
310(or use @code{calloc} instead; @pxref{Allocating Cleared Space}).
311Normally you would cast the value as a pointer to the kind of object
312that you want to store in the block. Here we show an example of doing
313so, and of initializing the space with zeros using the library function
314@code{memset} (@pxref{Copying and Concatenation}):
315
316@smallexample
317struct foo *ptr;
318@dots{}
319ptr = (struct foo *) malloc (sizeof (struct foo));
320if (ptr == 0) abort ();
321memset (ptr, 0, sizeof (struct foo));
322@end smallexample
323
324You can store the result of @code{malloc} into any pointer variable
f65fd747 325without a cast, because @w{ISO C} automatically converts the type
28f540f4
RM
326@code{void *} to another type of pointer when necessary. But the cast
327is necessary in contexts other than assignment operators or if you might
328want your code to run in traditional C.
329
330Remember that when allocating space for a string, the argument to
331@code{malloc} must be one plus the length of the string. This is
332because a string is terminated with a null character that doesn't count
333in the ``length'' of the string but does need space. For example:
334
335@smallexample
336char *ptr;
337@dots{}
338ptr = (char *) malloc (length + 1);
339@end smallexample
340
341@noindent
342@xref{Representation of Strings}, for more information about this.
343
344@node Malloc Examples
99a20616 345@subsubsection Examples of @code{malloc}
28f540f4
RM
346
347If no more space is available, @code{malloc} returns a null pointer.
348You should check the value of @emph{every} call to @code{malloc}. It is
349useful to write a subroutine that calls @code{malloc} and reports an
350error if the value is a null pointer, returning only if the value is
351nonzero. This function is conventionally called @code{xmalloc}. Here
352it is:
353
354@smallexample
355void *
356xmalloc (size_t size)
357@{
358 register void *value = malloc (size);
359 if (value == 0)
360 fatal ("virtual memory exhausted");
361 return value;
362@}
363@end smallexample
364
365Here is a real example of using @code{malloc} (by way of @code{xmalloc}).
366The function @code{savestring} will copy a sequence of characters into
367a newly allocated null-terminated string:
368
369@smallexample
370@group
371char *
372savestring (const char *ptr, size_t len)
373@{
374 register char *value = (char *) xmalloc (len + 1);
28f540f4 375 value[len] = '\0';
390955cb 376 return (char *) memcpy (value, ptr, len);
28f540f4
RM
377@}
378@end group
379@end smallexample
380
381The block that @code{malloc} gives you is guaranteed to be aligned so
a7a93d50 382that it can hold any type of data. On @gnusystems{}, the address is
c131718c
UD
383always a multiple of eight on most systems, and a multiple of 16 on
38464-bit systems. Only rarely is any higher boundary (such as a page
68979757
UD
385boundary) necessary; for those cases, use @code{memalign},
386@code{posix_memalign} or @code{valloc} (@pxref{Aligned Memory Blocks}).
28f540f4
RM
387
388Note that the memory located after the end of the block is likely to be
389in use for something else; perhaps a block already allocated by another
390call to @code{malloc}. If you attempt to treat the block as longer than
391you asked for it to be, you are liable to destroy the data that
392@code{malloc} uses to keep track of its blocks, or you may destroy the
393contents of another block. If you have already allocated a block and
394discover you want it to be bigger, use @code{realloc} (@pxref{Changing
395Block Size}).
396
397@node Freeing after Malloc
99a20616 398@subsubsection Freeing Memory Allocated with @code{malloc}
28f540f4
RM
399@cindex freeing memory allocated with @code{malloc}
400@cindex heap, freeing memory from
401
402When you no longer need a block that you got with @code{malloc}, use the
403function @code{free} to make the block available to be allocated again.
404The prototype for this function is in @file{stdlib.h}.
405@pindex stdlib.h
406
407@comment malloc.h stdlib.h
f65fd747 408@comment ISO
28f540f4 409@deftypefun void free (void *@var{ptr})
99a20616 410The @code{free} function deallocates the block of memory pointed at
28f540f4
RM
411by @var{ptr}.
412@end deftypefun
413
414@comment stdlib.h
415@comment Sun
416@deftypefun void cfree (void *@var{ptr})
417This function does the same thing as @code{free}. It's provided for
418backward compatibility with SunOS; you should use @code{free} instead.
419@end deftypefun
420
421Freeing a block alters the contents of the block. @strong{Do not expect to
422find any data (such as a pointer to the next block in a chain of blocks) in
423the block after freeing it.} Copy whatever you need out of the block before
424freeing it! Here is an example of the proper way to free all the blocks in
425a chain, and the strings that they point to:
426
427@smallexample
428struct chain
429 @{
430 struct chain *next;
431 char *name;
432 @}
433
434void
435free_chain (struct chain *chain)
436@{
437 while (chain != 0)
438 @{
439 struct chain *next = chain->next;
440 free (chain->name);
441 free (chain);
442 chain = next;
443 @}
444@}
445@end smallexample
446
447Occasionally, @code{free} can actually return memory to the operating
448system and make the process smaller. Usually, all it can do is allow a
449later call to @code{malloc} to reuse the space. In the meantime, the
450space remains in your program as part of a free-list used internally by
451@code{malloc}.
452
453There is no point in freeing blocks at the end of a program, because all
454of the program's space is given back to the system when the process
455terminates.
456
457@node Changing Block Size
99a20616 458@subsubsection Changing the Size of a Block
28f540f4
RM
459@cindex changing the size of a block (@code{malloc})
460
461Often you do not know for certain how big a block you will ultimately need
462at the time you must begin to use the block. For example, the block might
463be a buffer that you use to hold a line being read from a file; no matter
464how long you make the buffer initially, you may encounter a line that is
465longer.
466
467You can make the block longer by calling @code{realloc}. This function
468is declared in @file{stdlib.h}.
469@pindex stdlib.h
470
471@comment malloc.h stdlib.h
f65fd747 472@comment ISO
28f540f4
RM
473@deftypefun {void *} realloc (void *@var{ptr}, size_t @var{newsize})
474The @code{realloc} function changes the size of the block whose address is
475@var{ptr} to be @var{newsize}.
476
477Since the space after the end of the block may be in use, @code{realloc}
478may find it necessary to copy the block to a new address where more free
479space is available. The value of @code{realloc} is the new address of the
480block. If the block needs to be moved, @code{realloc} copies the old
481contents.
482
483If you pass a null pointer for @var{ptr}, @code{realloc} behaves just
484like @samp{malloc (@var{newsize})}. This can be convenient, but beware
f65fd747 485that older implementations (before @w{ISO C}) may not support this
28f540f4
RM
486behavior, and will probably crash when @code{realloc} is passed a null
487pointer.
488@end deftypefun
489
490Like @code{malloc}, @code{realloc} may return a null pointer if no
491memory space is available to make the block bigger. When this happens,
492the original block is untouched; it has not been modified or relocated.
493
494In most cases it makes no difference what happens to the original block
495when @code{realloc} fails, because the application program cannot continue
496when it is out of memory, and the only thing to do is to give a fatal error
497message. Often it is convenient to write and use a subroutine,
498conventionally called @code{xrealloc}, that takes care of the error message
499as @code{xmalloc} does for @code{malloc}:
500
501@smallexample
502void *
503xrealloc (void *ptr, size_t size)
504@{
505 register void *value = realloc (ptr, size);
506 if (value == 0)
507 fatal ("Virtual memory exhausted");
508 return value;
509@}
510@end smallexample
511
512You can also use @code{realloc} to make a block smaller. The reason you
ed277b4e 513would do this is to avoid tying up a lot of memory space when only a little
c131718c
UD
514is needed.
515@comment The following is no longer true with the new malloc.
516@comment But it seems wise to keep the warning for other implementations.
517In several allocation implementations, making a block smaller sometimes
518necessitates copying it, so it can fail if no other space is available.
28f540f4
RM
519
520If the new size you specify is the same as the old size, @code{realloc}
521is guaranteed to change nothing and return the same address that you gave.
522
523@node Allocating Cleared Space
99a20616 524@subsubsection Allocating Cleared Space
28f540f4
RM
525
526The function @code{calloc} allocates memory and clears it to zero. It
527is declared in @file{stdlib.h}.
528@pindex stdlib.h
529
530@comment malloc.h stdlib.h
f65fd747 531@comment ISO
28f540f4
RM
532@deftypefun {void *} calloc (size_t @var{count}, size_t @var{eltsize})
533This function allocates a block long enough to contain a vector of
534@var{count} elements, each of size @var{eltsize}. Its contents are
535cleared to zero before @code{calloc} returns.
536@end deftypefun
537
538You could define @code{calloc} as follows:
539
540@smallexample
541void *
542calloc (size_t count, size_t eltsize)
543@{
544 size_t size = count * eltsize;
545 void *value = malloc (size);
546 if (value != 0)
547 memset (value, 0, size);
548 return value;
549@}
550@end smallexample
551
c131718c
UD
552But in general, it is not guaranteed that @code{calloc} calls
553@code{malloc} internally. Therefore, if an application provides its own
554@code{malloc}/@code{realloc}/@code{free} outside the C library, it
555should always define @code{calloc}, too.
556
28f540f4 557@node Efficiency and Malloc
99a20616 558@subsubsection Efficiency Considerations for @code{malloc}
28f540f4
RM
559@cindex efficiency and @code{malloc}
560
99a20616
UD
561
562
563
c131718c
UD
564@ignore
565
566@c No longer true, see below instead.
28f540f4
RM
567To make the best use of @code{malloc}, it helps to know that the GNU
568version of @code{malloc} always dispenses small amounts of memory in
569blocks whose sizes are powers of two. It keeps separate pools for each
570power of two. This holds for sizes up to a page size. Therefore, if
571you are free to choose the size of a small block in order to make
572@code{malloc} more efficient, make it a power of two.
573@c !!! xref getpagesize
574
575Once a page is split up for a particular block size, it can't be reused
576for another size unless all the blocks in it are freed. In many
577programs, this is unlikely to happen. Thus, you can sometimes make a
578program use memory more efficiently by using blocks of the same size for
579many different purposes.
580
581When you ask for memory blocks of a page or larger, @code{malloc} uses a
582different strategy; it rounds the size up to a multiple of a page, and
583it can coalesce and split blocks as needed.
584
585The reason for the two strategies is that it is important to allocate
586and free small blocks as fast as possible, but speed is less important
587for a large block since the program normally spends a fair amount of
588time using it. Also, large blocks are normally fewer in number.
589Therefore, for large blocks, it makes sense to use a method which takes
590more time to minimize the wasted space.
591
c131718c
UD
592@end ignore
593
1f77f049 594As opposed to other versions, the @code{malloc} in @theglibc{}
99a20616
UD
595does not round up block sizes to powers of two, neither for large nor
596for small sizes. Neighboring chunks can be coalesced on a @code{free}
597no matter what their size is. This makes the implementation suitable
598for all kinds of allocation patterns without generally incurring high
599memory waste through fragmentation.
c131718c
UD
600
601Very large blocks (much larger than a page) are allocated with
602@code{mmap} (anonymous or via @code{/dev/zero}) by this implementation.
603This has the great advantage that these chunks are returned to the
604system immediately when they are freed. Therefore, it cannot happen
605that a large chunk becomes ``locked'' in between smaller ones and even
606after calling @code{free} wastes memory. The size threshold for
607@code{mmap} to be used can be adjusted with @code{mallopt}. The use of
608@code{mmap} can also be disabled completely.
609
28f540f4 610@node Aligned Memory Blocks
99a20616 611@subsubsection Allocating Aligned Memory Blocks
28f540f4
RM
612
613@cindex page boundary
614@cindex alignment (with @code{malloc})
615@pindex stdlib.h
616The address of a block returned by @code{malloc} or @code{realloc} in
a7a93d50 617@gnusystems{} is always a multiple of eight (or sixteen on 64-bit
c131718c 618systems). If you need a block whose address is a multiple of a higher
68979757 619power of two than that, use @code{memalign}, @code{posix_memalign}, or
eab0f04c 620@code{valloc}. @code{memalign} is declared in @file{malloc.h} and
67197215 621@code{posix_memalign} is declared in @file{stdlib.h}.
28f540f4 622
1f77f049 623With @theglibc{}, you can use @code{free} to free the blocks that
68979757
UD
624@code{memalign}, @code{posix_memalign}, and @code{valloc} return. That
625does not work in BSD, however---BSD does not provide any way to free
626such blocks.
28f540f4 627
eab0f04c 628@comment malloc.h
28f540f4 629@comment BSD
22a1292a 630@deftypefun {void *} memalign (size_t @var{boundary}, size_t @var{size})
28f540f4
RM
631The @code{memalign} function allocates a block of @var{size} bytes whose
632address is a multiple of @var{boundary}. The @var{boundary} must be a
c131718c
UD
633power of two! The function @code{memalign} works by allocating a
634somewhat larger block, and then returning an address within the block
635that is on the specified boundary.
28f540f4
RM
636@end deftypefun
637
68979757
UD
638@comment stdlib.h
639@comment POSIX
640@deftypefun int posix_memalign (void **@var{memptr}, size_t @var{alignment}, size_t @var{size})
641The @code{posix_memalign} function is similar to the @code{memalign}
642function in that it returns a buffer of @var{size} bytes aligned to a
643multiple of @var{alignment}. But it adds one requirement to the
644parameter @var{alignment}: the value must be a power of two multiple of
645@code{sizeof (void *)}.
646
647If the function succeeds in allocation memory a pointer to the allocated
648memory is returned in @code{*@var{memptr}} and the return value is zero.
649Otherwise the function returns an error value indicating the problem.
650
651This function was introduced in POSIX 1003.1d.
652@end deftypefun
653
28f540f4
RM
654@comment malloc.h stdlib.h
655@comment BSD
656@deftypefun {void *} valloc (size_t @var{size})
657Using @code{valloc} is like using @code{memalign} and passing the page size
658as the value of the second argument. It is implemented like this:
659
660@smallexample
661void *
662valloc (size_t size)
663@{
22a1292a 664 return memalign (getpagesize (), size);
28f540f4
RM
665@}
666@end smallexample
b642f101
UD
667
668@ref{Query Memory Parameters} for more information about the memory
669subsystem.
28f540f4
RM
670@end deftypefun
671
c131718c 672@node Malloc Tunable Parameters
99a20616 673@subsubsection Malloc Tunable Parameters
c131718c
UD
674
675You can adjust some parameters for dynamic memory allocation with the
676@code{mallopt} function. This function is the general SVID/XPG
677interface, defined in @file{malloc.h}.
678@pindex malloc.h
679
680@deftypefun int mallopt (int @var{param}, int @var{value})
681When calling @code{mallopt}, the @var{param} argument specifies the
682parameter to be set, and @var{value} the new value to be set. Possible
683choices for @var{param}, as defined in @file{malloc.h}, are:
684
685@table @code
ec4ff04d
CD
686@comment TODO: @item M_ARENA_MAX
687@comment - Document ARENA_MAX env var.
688@comment TODO: @item M_ARENA_TEST
689@comment - Document ARENA_TEST env var.
690@comment TODO: @item M_CHECK_ACTION
691@item M_MMAP_MAX
692The maximum number of chunks to allocate with @code{mmap}. Setting this
693to zero disables all use of @code{mmap}.
c131718c
UD
694@item M_MMAP_THRESHOLD
695All chunks larger than this value are allocated outside the normal
696heap, using the @code{mmap} system call. This way it is guaranteed
697that the memory for these chunks can be returned to the system on
13c0f771
AJ
698@code{free}. Note that requests smaller than this threshold might still
699be allocated via @code{mmap}.
ec4ff04d 700@comment TODO: @item M_MXFAST
deb9cabb
AS
701@item M_PERTURB
702If non-zero, memory blocks are filled with values depending on some
703low order bits of this parameter when they are allocated (except when
704allocated by @code{calloc}) and freed. This can be used to debug the
b741de23
SP
705use of uninitialized or freed heap memory. Note that this option does not
706guarantee that the freed block will have any specific values. It only
707guarantees that the content the block had before it was freed will be
708overwritten.
ec4ff04d
CD
709@item M_TOP_PAD
710This parameter determines the amount of extra memory to obtain from the
711system when a call to @code{sbrk} is required. It also specifies the
712number of bytes to retain when shrinking the heap by calling @code{sbrk}
713with a negative argument. This provides the necessary hysteresis in
714heap size such that excessive amounts of system calls can be avoided.
715@item M_TRIM_THRESHOLD
716This is the minimum size (in bytes) of the top-most, releasable chunk
717that will cause @code{sbrk} to be called with a negative argument in
718order to return memory to the system.
c131718c
UD
719@end table
720
721@end deftypefun
722
28f540f4 723@node Heap Consistency Checking
99a20616 724@subsubsection Heap Consistency Checking
28f540f4
RM
725
726@cindex heap consistency checking
727@cindex consistency checking, of heap
728
99a20616 729You can ask @code{malloc} to check the consistency of dynamic memory by
28f540f4 730using the @code{mcheck} function. This function is a GNU extension,
4775243a
UD
731declared in @file{mcheck.h}.
732@pindex mcheck.h
28f540f4 733
4775243a 734@comment mcheck.h
28f540f4
RM
735@comment GNU
736@deftypefun int mcheck (void (*@var{abortfn}) (enum mcheck_status @var{status}))
737Calling @code{mcheck} tells @code{malloc} to perform occasional
738consistency checks. These will catch things such as writing
739past the end of a block that was allocated with @code{malloc}.
740
741The @var{abortfn} argument is the function to call when an inconsistency
742is found. If you supply a null pointer, then @code{mcheck} uses a
743default function which prints a message and calls @code{abort}
744(@pxref{Aborting a Program}). The function you supply is called with
745one argument, which says what sort of inconsistency was detected; its
746type is described below.
747
748It is too late to begin allocation checking once you have allocated
749anything with @code{malloc}. So @code{mcheck} does nothing in that
750case. The function returns @code{-1} if you call it too late, and
751@code{0} otherwise (when it is successful).
752
753The easiest way to arrange to call @code{mcheck} early enough is to use
754the option @samp{-lmcheck} when you link your program; then you don't
bc938d3d 755need to modify your program source at all. Alternatively you might use
3cb07217
UD
756a debugger to insert a call to @code{mcheck} whenever the program is
757started, for example these gdb commands will automatically call @code{mcheck}
758whenever the program starts:
759
760@smallexample
761(gdb) break main
762Breakpoint 1, main (argc=2, argv=0xbffff964) at whatever.c:10
763(gdb) command 1
764Type commands for when breakpoint 1 is hit, one per line.
765End with a line saying just "end".
766>call mcheck(0)
767>continue
768>end
95fdc6a0 769(gdb) @dots{}
3cb07217
UD
770@end smallexample
771
772This will however only work if no initialization function of any object
773involved calls any of the @code{malloc} functions since @code{mcheck}
774must be called before the first such function.
775
28f540f4
RM
776@end deftypefun
777
778@deftypefun {enum mcheck_status} mprobe (void *@var{pointer})
779The @code{mprobe} function lets you explicitly check for inconsistencies
780in a particular allocated block. You must have already called
781@code{mcheck} at the beginning of the program, to do its occasional
782checks; calling @code{mprobe} requests an additional consistency check
783to be done at the time of the call.
784
785The argument @var{pointer} must be a pointer returned by @code{malloc}
786or @code{realloc}. @code{mprobe} returns a value that says what
787inconsistency, if any, was found. The values are described below.
788@end deftypefun
789
790@deftp {Data Type} {enum mcheck_status}
791This enumerated type describes what kind of inconsistency was detected
792in an allocated block, if any. Here are the possible values:
793
794@table @code
795@item MCHECK_DISABLED
796@code{mcheck} was not called before the first allocation.
797No consistency checking can be done.
798@item MCHECK_OK
799No inconsistency detected.
800@item MCHECK_HEAD
801The data immediately before the block was modified.
802This commonly happens when an array index or pointer
803is decremented too far.
804@item MCHECK_TAIL
805The data immediately after the block was modified.
806This commonly happens when an array index or pointer
807is incremented too far.
808@item MCHECK_FREE
809The block was already freed.
810@end table
811@end deftp
812
7551a1e5
UD
813Another possibility to check for and guard against bugs in the use of
814@code{malloc}, @code{realloc} and @code{free} is to set the environment
815variable @code{MALLOC_CHECK_}. When @code{MALLOC_CHECK_} is set, a
816special (less efficient) implementation is used which is designed to be
817tolerant against simple errors, such as double calls of @code{free} with
818the same argument, or overruns of a single byte (off-by-one bugs). Not
bc938d3d 819all such errors can be protected against, however, and memory leaks can
7551a1e5
UD
820result. If @code{MALLOC_CHECK_} is set to @code{0}, any detected heap
821corruption is silently ignored; if set to @code{1}, a diagnostic is
822printed on @code{stderr}; if set to @code{2}, @code{abort} is called
823immediately. This can be useful because otherwise a crash may happen
824much later, and the true cause for the problem is then very hard to
825track down.
826
68979757
UD
827There is one problem with @code{MALLOC_CHECK_}: in SUID or SGID binaries
828it could possibly be exploited since diverging from the normal programs
0bc93a2f 829behavior it now writes something to the standard error descriptor.
68979757
UD
830Therefore the use of @code{MALLOC_CHECK_} is disabled by default for
831SUID and SGID binaries. It can be enabled again by the system
832administrator by adding a file @file{/etc/suid-debug} (the content is
833not important it could be empty).
834
789b13c4 835So, what's the difference between using @code{MALLOC_CHECK_} and linking
bc938d3d 836with @samp{-lmcheck}? @code{MALLOC_CHECK_} is orthogonal with respect to
789b13c4
UD
837@samp{-lmcheck}. @samp{-lmcheck} has been added for backward
838compatibility. Both @code{MALLOC_CHECK_} and @samp{-lmcheck} should
839uncover the same bugs - but using @code{MALLOC_CHECK_} you don't need to
840recompile your application.
841
28f540f4 842@node Hooks for Malloc
99a20616 843@subsubsection Memory Allocation Hooks
28f540f4
RM
844@cindex allocation hooks, for @code{malloc}
845
1f77f049 846@Theglibc{} lets you modify the behavior of @code{malloc},
28f540f4
RM
847@code{realloc}, and @code{free} by specifying appropriate hook
848functions. You can use these hooks to help you debug programs that use
99a20616 849dynamic memory allocation, for example.
28f540f4
RM
850
851The hook variables are declared in @file{malloc.h}.
852@pindex malloc.h
853
854@comment malloc.h
855@comment GNU
856@defvar __malloc_hook
bc938d3d
UD
857The value of this variable is a pointer to the function that
858@code{malloc} uses whenever it is called. You should define this
859function to look like @code{malloc}; that is, like:
28f540f4
RM
860
861@smallexample
18a3a9a3 862void *@var{function} (size_t @var{size}, const void *@var{caller})
28f540f4 863@end smallexample
bd355af0
UD
864
865The value of @var{caller} is the return address found on the stack when
bc938d3d
UD
866the @code{malloc} function was called. This value allows you to trace
867the memory consumption of the program.
28f540f4
RM
868@end defvar
869
870@comment malloc.h
871@comment GNU
872@defvar __realloc_hook
873The value of this variable is a pointer to function that @code{realloc}
874uses whenever it is called. You should define this function to look
875like @code{realloc}; that is, like:
876
877@smallexample
18a3a9a3 878void *@var{function} (void *@var{ptr}, size_t @var{size}, const void *@var{caller})
28f540f4 879@end smallexample
bd355af0
UD
880
881The value of @var{caller} is the return address found on the stack when
e8b1163e 882the @code{realloc} function was called. This value allows you to trace the
bd355af0 883memory consumption of the program.
28f540f4
RM
884@end defvar
885
886@comment malloc.h
887@comment GNU
888@defvar __free_hook
889The value of this variable is a pointer to function that @code{free}
890uses whenever it is called. You should define this function to look
891like @code{free}; that is, like:
892
893@smallexample
18a3a9a3 894void @var{function} (void *@var{ptr}, const void *@var{caller})
28f540f4 895@end smallexample
bd355af0
UD
896
897The value of @var{caller} is the return address found on the stack when
e8b1163e 898the @code{free} function was called. This value allows you to trace the
bd355af0 899memory consumption of the program.
28f540f4
RM
900@end defvar
901
3cb07217
UD
902@comment malloc.h
903@comment GNU
904@defvar __memalign_hook
905The value of this variable is a pointer to function that @code{memalign}
906uses whenever it is called. You should define this function to look
907like @code{memalign}; that is, like:
908
909@smallexample
46ca7a1c 910void *@var{function} (size_t @var{alignment}, size_t @var{size}, const void *@var{caller})
3cb07217 911@end smallexample
18a3a9a3
UD
912
913The value of @var{caller} is the return address found on the stack when
914the @code{memalign} function was called. This value allows you to trace the
915memory consumption of the program.
3cb07217
UD
916@end defvar
917
28f540f4
RM
918You must make sure that the function you install as a hook for one of
919these functions does not call that function recursively without restoring
920the old value of the hook first! Otherwise, your program will get stuck
3cb07217
UD
921in an infinite recursion. Before calling the function recursively, one
922should make sure to restore all the hooks to their previous value. When
923coming back from the recursive call, all the hooks should be resaved
924since a hook might modify itself.
28f540f4 925
b2f46c3c
UD
926@comment malloc.h
927@comment GNU
928@defvar __malloc_initialize_hook
929The value of this variable is a pointer to a function that is called
930once when the malloc implementation is initialized. This is a weak
931variable, so it can be overridden in the application with a definition
932like the following:
933
934@smallexample
935void (*@var{__malloc_initialize_hook}) (void) = my_init_hook;
936@end smallexample
937@end defvar
938
939An issue to look out for is the time at which the malloc hook functions
940can be safely installed. If the hook functions call the malloc-related
941functions recursively, it is necessary that malloc has already properly
942initialized itself at the time when @code{__malloc_hook} etc. is
943assigned to. On the other hand, if the hook functions provide a
944complete malloc implementation of their own, it is vital that the hooks
945are assigned to @emph{before} the very first @code{malloc} call has
946completed, because otherwise a chunk obtained from the ordinary,
947un-hooked malloc may later be handed to @code{__free_hook}, for example.
948
949In both cases, the problem can be solved by setting up the hooks from
950within a user-defined function pointed to by
951@code{__malloc_initialize_hook}---then the hooks will be set up safely
952at the right time.
953
3cb07217
UD
954Here is an example showing how to use @code{__malloc_hook} and
955@code{__free_hook} properly. It installs a function that prints out
956information every time @code{malloc} or @code{free} is called. We just
957assume here that @code{realloc} and @code{memalign} are not used in our
958program.
28f540f4
RM
959
960@smallexample
18a3a9a3
UD
961/* Prototypes for __malloc_hook, __free_hook */
962#include <malloc.h>
3cb07217
UD
963
964/* Prototypes for our hooks. */
2ac057a0 965static void my_init_hook (void);
18a3a9a3
UD
966static void *my_malloc_hook (size_t, const void *);
967static void my_free_hook (void*, const void *);
b2f46c3c
UD
968
969/* Override initializing hook from the C library. */
970void (*__malloc_initialize_hook) (void) = my_init_hook;
971
972static void
973my_init_hook (void)
974@{
975 old_malloc_hook = __malloc_hook;
976 old_free_hook = __free_hook;
977 __malloc_hook = my_malloc_hook;
978 __free_hook = my_free_hook;
979@}
3cb07217 980
28f540f4 981static void *
18a3a9a3 982my_malloc_hook (size_t size, const void *caller)
28f540f4
RM
983@{
984 void *result;
3cb07217 985 /* Restore all old hooks */
28f540f4 986 __malloc_hook = old_malloc_hook;
3cb07217
UD
987 __free_hook = old_free_hook;
988 /* Call recursively */
28f540f4 989 result = malloc (size);
0bc93a2f 990 /* Save underlying hooks */
3cb07217
UD
991 old_malloc_hook = __malloc_hook;
992 old_free_hook = __free_hook;
28f540f4
RM
993 /* @r{@code{printf} might call @code{malloc}, so protect it too.} */
994 printf ("malloc (%u) returns %p\n", (unsigned int) size, result);
3cb07217 995 /* Restore our own hooks */
28f540f4 996 __malloc_hook = my_malloc_hook;
3cb07217 997 __free_hook = my_free_hook;
28f540f4
RM
998 return result;
999@}
1000
2ac057a0 1001static void
18a3a9a3 1002my_free_hook (void *ptr, const void *caller)
3cb07217
UD
1003@{
1004 /* Restore all old hooks */
1005 __malloc_hook = old_malloc_hook;
1006 __free_hook = old_free_hook;
1007 /* Call recursively */
1008 free (ptr);
0bc93a2f 1009 /* Save underlying hooks */
3cb07217
UD
1010 old_malloc_hook = __malloc_hook;
1011 old_free_hook = __free_hook;
1012 /* @r{@code{printf} might call @code{free}, so protect it too.} */
1013 printf ("freed pointer %p\n", ptr);
1014 /* Restore our own hooks */
1015 __malloc_hook = my_malloc_hook;
1016 __free_hook = my_free_hook;
1017@}
1018
28f540f4
RM
1019main ()
1020@{
95fdc6a0 1021 @dots{}
28f540f4
RM
1022@}
1023@end smallexample
1024
1025The @code{mcheck} function (@pxref{Heap Consistency Checking}) works by
1026installing such hooks.
1027
1028@c __morecore, __after_morecore_hook are undocumented
1029@c It's not clear whether to document them.
1030
1031@node Statistics of Malloc
99a20616 1032@subsubsection Statistics for Memory Allocation with @code{malloc}
28f540f4
RM
1033
1034@cindex allocation statistics
99a20616 1035You can get information about dynamic memory allocation by calling the
c131718c
UD
1036@code{mallinfo} function. This function and its associated data type
1037are declared in @file{malloc.h}; they are an extension of the standard
1038SVID/XPG version.
28f540f4
RM
1039@pindex malloc.h
1040
1041@comment malloc.h
1042@comment GNU
c131718c 1043@deftp {Data Type} {struct mallinfo}
28f540f4 1044This structure type is used to return information about the dynamic
99a20616 1045memory allocator. It contains the following members:
28f540f4
RM
1046
1047@table @code
c131718c
UD
1048@item int arena
1049This is the total size of memory allocated with @code{sbrk} by
1050@code{malloc}, in bytes.
1051
1052@item int ordblks
99a20616 1053This is the number of chunks not in use. (The memory allocator
c131718c
UD
1054internally gets chunks of memory from the operating system, and then
1055carves them up to satisfy individual @code{malloc} requests; see
1056@ref{Efficiency and Malloc}.)
1057
1058@item int smblks
1059This field is unused.
1060
1061@item int hblks
1062This is the total number of chunks allocated with @code{mmap}.
1063
1064@item int hblkhd
1065This is the total size of memory allocated with @code{mmap}, in bytes.
1066
1067@item int usmblks
1068This field is unused.
28f540f4 1069
c131718c
UD
1070@item int fsmblks
1071This field is unused.
28f540f4 1072
c131718c
UD
1073@item int uordblks
1074This is the total size of memory occupied by chunks handed out by
1075@code{malloc}.
1076
1077@item int fordblks
1078This is the total size of memory occupied by free (not in use) chunks.
28f540f4 1079
c131718c 1080@item int keepcost
e8b1163e 1081This is the size of the top-most releasable chunk that normally
11bf311e 1082borders the end of the heap (i.e., the high end of the virtual address
99a20616 1083space's data segment).
28f540f4 1084
28f540f4
RM
1085@end table
1086@end deftp
1087
1088@comment malloc.h
c131718c
UD
1089@comment SVID
1090@deftypefun {struct mallinfo} mallinfo (void)
28f540f4 1091This function returns information about the current dynamic memory usage
c131718c 1092in a structure of type @code{struct mallinfo}.
28f540f4
RM
1093@end deftypefun
1094
1095@node Summary of Malloc
99a20616 1096@subsubsection Summary of @code{malloc}-Related Functions
28f540f4
RM
1097
1098Here is a summary of the functions that work with @code{malloc}:
1099
1100@table @code
1101@item void *malloc (size_t @var{size})
1102Allocate a block of @var{size} bytes. @xref{Basic Allocation}.
1103
1104@item void free (void *@var{addr})
1105Free a block previously allocated by @code{malloc}. @xref{Freeing after
1106Malloc}.
1107
1108@item void *realloc (void *@var{addr}, size_t @var{size})
1109Make a block previously allocated by @code{malloc} larger or smaller,
1110possibly by copying it to a new location. @xref{Changing Block Size}.
1111
1112@item void *calloc (size_t @var{count}, size_t @var{eltsize})
1113Allocate a block of @var{count} * @var{eltsize} bytes using
1114@code{malloc}, and set its contents to zero. @xref{Allocating Cleared
1115Space}.
1116
1117@item void *valloc (size_t @var{size})
1118Allocate a block of @var{size} bytes, starting on a page boundary.
1119@xref{Aligned Memory Blocks}.
1120
1121@item void *memalign (size_t @var{size}, size_t @var{boundary})
1122Allocate a block of @var{size} bytes, starting on an address that is a
1123multiple of @var{boundary}. @xref{Aligned Memory Blocks}.
1124
c131718c 1125@item int mallopt (int @var{param}, int @var{value})
8b7fb588 1126Adjust a tunable parameter. @xref{Malloc Tunable Parameters}.
c131718c 1127
28f540f4
RM
1128@item int mcheck (void (*@var{abortfn}) (void))
1129Tell @code{malloc} to perform occasional consistency checks on
1130dynamically allocated memory, and to call @var{abortfn} when an
1131inconsistency is found. @xref{Heap Consistency Checking}.
1132
18a3a9a3 1133@item void *(*__malloc_hook) (size_t @var{size}, const void *@var{caller})
28f540f4
RM
1134A pointer to a function that @code{malloc} uses whenever it is called.
1135
18a3a9a3 1136@item void *(*__realloc_hook) (void *@var{ptr}, size_t @var{size}, const void *@var{caller})
28f540f4
RM
1137A pointer to a function that @code{realloc} uses whenever it is called.
1138
18a3a9a3 1139@item void (*__free_hook) (void *@var{ptr}, const void *@var{caller})
28f540f4
RM
1140A pointer to a function that @code{free} uses whenever it is called.
1141
18a3a9a3 1142@item void (*__memalign_hook) (size_t @var{size}, size_t @var{alignment}, const void *@var{caller})
3cb07217
UD
1143A pointer to a function that @code{memalign} uses whenever it is called.
1144
c131718c 1145@item struct mallinfo mallinfo (void)
28f540f4
RM
1146Return information about the current dynamic memory usage.
1147@xref{Statistics of Malloc}.
1148@end table
1149
bd355af0 1150@node Allocation Debugging
99a20616 1151@subsection Allocation Debugging
bd355af0
UD
1152@cindex allocation debugging
1153@cindex malloc debugger
1154
bc938d3d 1155A complicated task when programming with languages which do not use
bd355af0
UD
1156garbage collected dynamic memory allocation is to find memory leaks.
1157Long running programs must assure that dynamically allocated objects are
1158freed at the end of their lifetime. If this does not happen the system
1159runs out of memory, sooner or later.
1160
1f77f049 1161The @code{malloc} implementation in @theglibc{} provides some
bc938d3d 1162simple means to detect such leaks and obtain some information to find
bd355af0
UD
1163the location. To do this the application must be started in a special
1164mode which is enabled by an environment variable. There are no speed
bc938d3d 1165penalties for the program if the debugging mode is not enabled.
bd355af0
UD
1166
1167@menu
1168* Tracing malloc:: How to install the tracing functionality.
1169* Using the Memory Debugger:: Example programs excerpts.
1170* Tips for the Memory Debugger:: Some more or less clever ideas.
1171* Interpreting the traces:: What do all these lines mean?
1172@end menu
1173
1174@node Tracing malloc
99a20616 1175@subsubsection How to install the tracing functionality
bd355af0
UD
1176
1177@comment mcheck.h
1178@comment GNU
1179@deftypefun void mtrace (void)
1180When the @code{mtrace} function is called it looks for an environment
1181variable named @code{MALLOC_TRACE}. This variable is supposed to
1182contain a valid file name. The user must have write access. If the
1183file already exists it is truncated. If the environment variable is not
1184set or it does not name a valid file which can be opened for writing
0bc93a2f 1185nothing is done. The behavior of @code{malloc} etc. is not changed.
bc938d3d
UD
1186For obvious reasons this also happens if the application is installed
1187with the SUID or SGID bit set.
bd355af0 1188
e8b1163e 1189If the named file is successfully opened, @code{mtrace} installs special
bd355af0 1190handlers for the functions @code{malloc}, @code{realloc}, and
e8b1163e 1191@code{free} (@pxref{Hooks for Malloc}). From then on, all uses of these
bd355af0 1192functions are traced and protocolled into the file. There is now of
bc938d3d 1193course a speed penalty for all calls to the traced functions so tracing
e8b1163e 1194should not be enabled during normal use.
bd355af0
UD
1195
1196This function is a GNU extension and generally not available on other
1197systems. The prototype can be found in @file{mcheck.h}.
1198@end deftypefun
1199
1200@comment mcheck.h
1201@comment GNU
1202@deftypefun void muntrace (void)
1203The @code{muntrace} function can be called after @code{mtrace} was used
0bc93a2f 1204to enable tracing the @code{malloc} calls. If no (successful) call of
bd355af0
UD
1205@code{mtrace} was made @code{muntrace} does nothing.
1206
1207Otherwise it deinstalls the handlers for @code{malloc}, @code{realloc},
1208and @code{free} and then closes the protocol file. No calls are
bc938d3d 1209protocolled anymore and the program runs again at full speed.
bd355af0
UD
1210
1211This function is a GNU extension and generally not available on other
1212systems. The prototype can be found in @file{mcheck.h}.
1213@end deftypefun
1214
1215@node Using the Memory Debugger
99a20616 1216@subsubsection Example program excerpts
bd355af0
UD
1217
1218Even though the tracing functionality does not influence the runtime
0bc93a2f 1219behavior of the program it is not a good idea to call @code{mtrace} in
bc938d3d
UD
1220all programs. Just imagine that you debug a program using @code{mtrace}
1221and all other programs used in the debugging session also trace their
1222@code{malloc} calls. The output file would be the same for all programs
1223and thus is unusable. Therefore one should call @code{mtrace} only if
1224compiled for debugging. A program could therefore start like this:
bd355af0
UD
1225
1226@example
1227#include <mcheck.h>
1228
1229int
1230main (int argc, char *argv[])
1231@{
1232#ifdef DEBUGGING
1233 mtrace ();
1234#endif
1235 @dots{}
1236@}
1237@end example
1238
1239This is all what is needed if you want to trace the calls during the
1240whole runtime of the program. Alternatively you can stop the tracing at
1241any time with a call to @code{muntrace}. It is even possible to restart
bc938d3d
UD
1242the tracing again with a new call to @code{mtrace}. But this can cause
1243unreliable results since there may be calls of the functions which are
1244not called. Please note that not only the application uses the traced
1245functions, also libraries (including the C library itself) use these
1246functions.
bd355af0
UD
1247
1248This last point is also why it is no good idea to call @code{muntrace}
1249before the program terminated. The libraries are informed about the
1250termination of the program only after the program returns from
1251@code{main} or calls @code{exit} and so cannot free the memory they use
1252before this time.
1253
1254So the best thing one can do is to call @code{mtrace} as the very first
1255function in the program and never call @code{muntrace}. So the program
1256traces almost all uses of the @code{malloc} functions (except those
1257calls which are executed by constructors of the program or used
1258libraries).
1259
1260@node Tips for the Memory Debugger
99a20616 1261@subsubsection Some more or less clever ideas
bd355af0
UD
1262
1263You know the situation. The program is prepared for debugging and in
1264all debugging sessions it runs well. But once it is started without
bc938d3d
UD
1265debugging the error shows up. A typical example is a memory leak that
1266becomes visible only when we turn off the debugging. If you foresee
1267such situations you can still win. Simply use something equivalent to
1268the following little program:
bd355af0
UD
1269
1270@example
1271#include <mcheck.h>
1272#include <signal.h>
1273
1274static void
1275enable (int sig)
1276@{
1277 mtrace ();
1278 signal (SIGUSR1, enable);
1279@}
1280
1281static void
1282disable (int sig)
1283@{
1284 muntrace ();
1285 signal (SIGUSR2, disable);
1286@}
1287
1288int
1289main (int argc, char *argv[])
1290@{
1291 @dots{}
1292
1293 signal (SIGUSR1, enable);
1294 signal (SIGUSR2, disable);
1295
1296 @dots{}
1297@}
1298@end example
1299
9756dfe1 1300I.e., the user can start the memory debugger any time s/he wants if the
bd355af0
UD
1301program was started with @code{MALLOC_TRACE} set in the environment.
1302The output will of course not show the allocations which happened before
1303the first signal but if there is a memory leak this will show up
1304nevertheless.
1305
1306@node Interpreting the traces
99a20616 1307@subsubsection Interpreting the traces
bd355af0
UD
1308
1309If you take a look at the output it will look similar to this:
1310
1311@example
1312= Start
1313@ [0x8048209] - 0x8064cc8
1314@ [0x8048209] - 0x8064ce0
1315@ [0x8048209] - 0x8064cf8
1316@ [0x80481eb] + 0x8064c48 0x14
1317@ [0x80481eb] + 0x8064c60 0x14
1318@ [0x80481eb] + 0x8064c78 0x14
1319@ [0x80481eb] + 0x8064c90 0x14
1320= End
1321@end example
1322
1323What this all means is not really important since the trace file is not
bc938d3d 1324meant to be read by a human. Therefore no attention is given to
1f77f049
JM
1325readability. Instead there is a program which comes with @theglibc{}
1326which interprets the traces and outputs a summary in an
bd355af0
UD
1327user-friendly way. The program is called @code{mtrace} (it is in fact a
1328Perl script) and it takes one or two arguments. In any case the name of
bc938d3d
UD
1329the file with the trace output must be specified. If an optional
1330argument precedes the name of the trace file this must be the name of
1331the program which generated the trace.
bd355af0
UD
1332
1333@example
1334drepper$ mtrace tst-mtrace log
1335No memory leaks.
1336@end example
1337
1338In this case the program @code{tst-mtrace} was run and it produced a
1339trace file @file{log}. The message printed by @code{mtrace} shows there
1340are no problems with the code, all allocated memory was freed
1341afterwards.
1342
1343If we call @code{mtrace} on the example trace given above we would get a
1344different outout:
1345
1346@example
1347drepper$ mtrace errlog
1348- 0x08064cc8 Free 2 was never alloc'd 0x8048209
1349- 0x08064ce0 Free 3 was never alloc'd 0x8048209
1350- 0x08064cf8 Free 4 was never alloc'd 0x8048209
1351
1352Memory not freed:
1353-----------------
1354 Address Size Caller
13550x08064c48 0x14 at 0x80481eb
13560x08064c60 0x14 at 0x80481eb
13570x08064c78 0x14 at 0x80481eb
13580x08064c90 0x14 at 0x80481eb
1359@end example
1360
1361We have called @code{mtrace} with only one argument and so the script
1362has no chance to find out what is meant with the addresses given in the
1363trace. We can do better:
1364
1365@example
bc938d3d
UD
1366drepper$ mtrace tst errlog
1367- 0x08064cc8 Free 2 was never alloc'd /home/drepper/tst.c:39
1368- 0x08064ce0 Free 3 was never alloc'd /home/drepper/tst.c:39
1369- 0x08064cf8 Free 4 was never alloc'd /home/drepper/tst.c:39
bd355af0
UD
1370
1371Memory not freed:
1372-----------------
1373 Address Size Caller
bc938d3d
UD
13740x08064c48 0x14 at /home/drepper/tst.c:33
13750x08064c60 0x14 at /home/drepper/tst.c:33
13760x08064c78 0x14 at /home/drepper/tst.c:33
13770x08064c90 0x14 at /home/drepper/tst.c:33
bd355af0
UD
1378@end example
1379
1380Suddenly the output makes much more sense and the user can see
1381immediately where the function calls causing the trouble can be found.
1382
9756dfe1
UD
1383Interpreting this output is not complicated. There are at most two
1384different situations being detected. First, @code{free} was called for
1385pointers which were never returned by one of the allocation functions.
bc938d3d 1386This is usually a very bad problem and what this looks like is shown in
9756dfe1
UD
1387the first three lines of the output. Situations like this are quite
1388rare and if they appear they show up very drastically: the program
1389normally crashes.
1390
1391The other situation which is much harder to detect are memory leaks. As
1392you can see in the output the @code{mtrace} function collects all this
1393information and so can say that the program calls an allocation function
1394from line 33 in the source file @file{/home/drepper/tst-mtrace.c} four
1395times without freeing this memory before the program terminates.
bc938d3d 1396Whether this is a real problem remains to be investigated.
9756dfe1 1397
28f540f4 1398@node Obstacks
99a20616 1399@subsection Obstacks
28f540f4
RM
1400@cindex obstacks
1401
1402An @dfn{obstack} is a pool of memory containing a stack of objects. You
1403can create any number of separate obstacks, and then allocate objects in
1404specified obstacks. Within each obstack, the last object allocated must
1405always be the first one freed, but distinct obstacks are independent of
1406each other.
1407
1408Aside from this one constraint of order of freeing, obstacks are totally
1409general: an obstack can contain any number of objects of any size. They
1410are implemented with macros, so allocation is usually very fast as long as
1411the objects are usually small. And the only space overhead per object is
1412the padding needed to start each object on a suitable boundary.
1413
1414@menu
1415* Creating Obstacks:: How to declare an obstack in your program.
1416* Preparing for Obstacks:: Preparations needed before you can
1417 use obstacks.
1418* Allocation in an Obstack:: Allocating objects in an obstack.
1419* Freeing Obstack Objects:: Freeing objects in an obstack.
1420* Obstack Functions:: The obstack functions are both
1421 functions and macros.
1422* Growing Objects:: Making an object bigger by stages.
1423* Extra Fast Growing:: Extra-high-efficiency (though more
1424 complicated) growing objects.
1425* Status of an Obstack:: Inquiries about the status of an obstack.
1426* Obstacks Data Alignment:: Controlling alignment of objects in obstacks.
1427* Obstack Chunks:: How obstacks obtain and release chunks;
1428 efficiency considerations.
a5113b14 1429* Summary of Obstacks::
28f540f4
RM
1430@end menu
1431
1432@node Creating Obstacks
99a20616 1433@subsubsection Creating Obstacks
28f540f4
RM
1434
1435The utilities for manipulating obstacks are declared in the header
1436file @file{obstack.h}.
1437@pindex obstack.h
1438
1439@comment obstack.h
1440@comment GNU
1441@deftp {Data Type} {struct obstack}
1442An obstack is represented by a data structure of type @code{struct
1443obstack}. This structure has a small fixed size; it records the status
1444of the obstack and how to find the space in which objects are allocated.
1445It does not contain any of the objects themselves. You should not try
1446to access the contents of the structure directly; use only the functions
1447described in this chapter.
1448@end deftp
1449
1450You can declare variables of type @code{struct obstack} and use them as
1451obstacks, or you can allocate obstacks dynamically like any other kind
1452of object. Dynamic allocation of obstacks allows your program to have a
1453variable number of different stacks. (You can even allocate an
1454obstack structure in another obstack, but this is rarely useful.)
1455
1456All the functions that work with obstacks require you to specify which
1457obstack to use. You do this with a pointer of type @code{struct obstack
1458*}. In the following, we often say ``an obstack'' when strictly
1459speaking the object at hand is such a pointer.
1460
1461The objects in the obstack are packed into large blocks called
1462@dfn{chunks}. The @code{struct obstack} structure points to a chain of
1463the chunks currently in use.
1464
1465The obstack library obtains a new chunk whenever you allocate an object
1466that won't fit in the previous chunk. Since the obstack library manages
1467chunks automatically, you don't need to pay much attention to them, but
1468you do need to supply a function which the obstack library should use to
1469get a chunk. Usually you supply a function which uses @code{malloc}
1470directly or indirectly. You must also supply a function to free a chunk.
1471These matters are described in the following section.
1472
1473@node Preparing for Obstacks
99a20616 1474@subsubsection Preparing for Using Obstacks
28f540f4
RM
1475
1476Each source file in which you plan to use the obstack functions
1477must include the header file @file{obstack.h}, like this:
1478
1479@smallexample
1480#include <obstack.h>
1481@end smallexample
1482
1483@findex obstack_chunk_alloc
1484@findex obstack_chunk_free
1485Also, if the source file uses the macro @code{obstack_init}, it must
1486declare or define two functions or macros that will be called by the
1487obstack library. One, @code{obstack_chunk_alloc}, is used to allocate
1488the chunks of memory into which objects are packed. The other,
1489@code{obstack_chunk_free}, is used to return chunks when the objects in
1490them are freed. These macros should appear before any use of obstacks
1491in the source file.
1492
1493Usually these are defined to use @code{malloc} via the intermediary
1494@code{xmalloc} (@pxref{Unconstrained Allocation}). This is done with
1495the following pair of macro definitions:
1496
1497@smallexample
1498#define obstack_chunk_alloc xmalloc
1499#define obstack_chunk_free free
1500@end smallexample
1501
1502@noindent
99a20616 1503Though the memory you get using obstacks really comes from @code{malloc},
28f540f4
RM
1504using obstacks is faster because @code{malloc} is called less often, for
1505larger blocks of memory. @xref{Obstack Chunks}, for full details.
1506
1507At run time, before the program can use a @code{struct obstack} object
1508as an obstack, it must initialize the obstack by calling
1509@code{obstack_init}.
1510
1511@comment obstack.h
1512@comment GNU
1513@deftypefun int obstack_init (struct obstack *@var{obstack-ptr})
1514Initialize obstack @var{obstack-ptr} for allocation of objects. This
3cb07217
UD
1515function calls the obstack's @code{obstack_chunk_alloc} function. If
1516allocation of memory fails, the function pointed to by
1517@code{obstack_alloc_failed_handler} is called. The @code{obstack_init}
1518function always returns 1 (Compatibility notice: Former versions of
1519obstack returned 0 if allocation failed).
28f540f4
RM
1520@end deftypefun
1521
1522Here are two examples of how to allocate the space for an obstack and
1523initialize it. First, an obstack that is a static variable:
1524
1525@smallexample
1526static struct obstack myobstack;
1527@dots{}
1528obstack_init (&myobstack);
1529@end smallexample
1530
1531@noindent
1532Second, an obstack that is itself dynamically allocated:
1533
1534@smallexample
1535struct obstack *myobstack_ptr
1536 = (struct obstack *) xmalloc (sizeof (struct obstack));
1537
1538obstack_init (myobstack_ptr);
1539@end smallexample
1540
3cb07217
UD
1541@comment obstack.h
1542@comment GNU
1543@defvar obstack_alloc_failed_handler
1544The value of this variable is a pointer to a function that
1545@code{obstack} uses when @code{obstack_chunk_alloc} fails to allocate
1546memory. The default action is to print a message and abort.
1547You should supply a function that either calls @code{exit}
1548(@pxref{Program Termination}) or @code{longjmp} (@pxref{Non-Local
1549Exits}) and doesn't return.
1550
1551@smallexample
1552void my_obstack_alloc_failed (void)
1553@dots{}
1554obstack_alloc_failed_handler = &my_obstack_alloc_failed;
1555@end smallexample
1556
1557@end defvar
1558
28f540f4 1559@node Allocation in an Obstack
99a20616 1560@subsubsection Allocation in an Obstack
28f540f4
RM
1561@cindex allocation (obstacks)
1562
1563The most direct way to allocate an object in an obstack is with
1564@code{obstack_alloc}, which is invoked almost like @code{malloc}.
1565
1566@comment obstack.h
1567@comment GNU
1568@deftypefun {void *} obstack_alloc (struct obstack *@var{obstack-ptr}, int @var{size})
1569This allocates an uninitialized block of @var{size} bytes in an obstack
1570and returns its address. Here @var{obstack-ptr} specifies which obstack
1571to allocate the block in; it is the address of the @code{struct obstack}
1572object which represents the obstack. Each obstack function or macro
1573requires you to specify an @var{obstack-ptr} as the first argument.
1574
1575This function calls the obstack's @code{obstack_chunk_alloc} function if
3cb07217
UD
1576it needs to allocate a new chunk of memory; it calls
1577@code{obstack_alloc_failed_handler} if allocation of memory by
1578@code{obstack_chunk_alloc} failed.
28f540f4
RM
1579@end deftypefun
1580
1581For example, here is a function that allocates a copy of a string @var{str}
1582in a specific obstack, which is in the variable @code{string_obstack}:
1583
1584@smallexample
1585struct obstack string_obstack;
1586
1587char *
1588copystring (char *string)
1589@{
7cc27f44
UD
1590 size_t len = strlen (string) + 1;
1591 char *s = (char *) obstack_alloc (&string_obstack, len);
1592 memcpy (s, string, len);
28f540f4
RM
1593 return s;
1594@}
1595@end smallexample
1596
1597To allocate a block with specified contents, use the function
1598@code{obstack_copy}, declared like this:
1599
1600@comment obstack.h
1601@comment GNU
1602@deftypefun {void *} obstack_copy (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
1603This allocates a block and initializes it by copying @var{size}
3cb07217
UD
1604bytes of data starting at @var{address}. It calls
1605@code{obstack_alloc_failed_handler} if allocation of memory by
1606@code{obstack_chunk_alloc} failed.
28f540f4
RM
1607@end deftypefun
1608
1609@comment obstack.h
1610@comment GNU
1611@deftypefun {void *} obstack_copy0 (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
1612Like @code{obstack_copy}, but appends an extra byte containing a null
1613character. This extra byte is not counted in the argument @var{size}.
1614@end deftypefun
1615
1616The @code{obstack_copy0} function is convenient for copying a sequence
1617of characters into an obstack as a null-terminated string. Here is an
1618example of its use:
1619
1620@smallexample
1621char *
1622obstack_savestring (char *addr, int size)
1623@{
1624 return obstack_copy0 (&myobstack, addr, size);
1625@}
1626@end smallexample
1627
1628@noindent
1629Contrast this with the previous example of @code{savestring} using
1630@code{malloc} (@pxref{Basic Allocation}).
1631
1632@node Freeing Obstack Objects
99a20616 1633@subsubsection Freeing Objects in an Obstack
28f540f4
RM
1634@cindex freeing (obstacks)
1635
1636To free an object allocated in an obstack, use the function
1637@code{obstack_free}. Since the obstack is a stack of objects, freeing
1638one object automatically frees all other objects allocated more recently
1639in the same obstack.
1640
1641@comment obstack.h
1642@comment GNU
1643@deftypefun void obstack_free (struct obstack *@var{obstack-ptr}, void *@var{object})
1644If @var{object} is a null pointer, everything allocated in the obstack
1645is freed. Otherwise, @var{object} must be the address of an object
1646allocated in the obstack. Then @var{object} is freed, along with
1647everything allocated in @var{obstack} since @var{object}.
1648@end deftypefun
1649
1650Note that if @var{object} is a null pointer, the result is an
99a20616 1651uninitialized obstack. To free all memory in an obstack but leave it
28f540f4
RM
1652valid for further allocation, call @code{obstack_free} with the address
1653of the first object allocated on the obstack:
1654
1655@smallexample
1656obstack_free (obstack_ptr, first_object_allocated_ptr);
1657@end smallexample
1658
1659Recall that the objects in an obstack are grouped into chunks. When all
1660the objects in a chunk become free, the obstack library automatically
1661frees the chunk (@pxref{Preparing for Obstacks}). Then other
1662obstacks, or non-obstack allocation, can reuse the space of the chunk.
1663
1664@node Obstack Functions
99a20616 1665@subsubsection Obstack Functions and Macros
28f540f4
RM
1666@cindex macros
1667
1668The interfaces for using obstacks may be defined either as functions or
1669as macros, depending on the compiler. The obstack facility works with
f65fd747 1670all C compilers, including both @w{ISO C} and traditional C, but there are
28f540f4
RM
1671precautions you must take if you plan to use compilers other than GNU C.
1672
f65fd747 1673If you are using an old-fashioned @w{non-ISO C} compiler, all the obstack
28f540f4
RM
1674``functions'' are actually defined only as macros. You can call these
1675macros like functions, but you cannot use them in any other way (for
1676example, you cannot take their address).
1677
1678Calling the macros requires a special precaution: namely, the first
1679operand (the obstack pointer) may not contain any side effects, because
1680it may be computed more than once. For example, if you write this:
1681
1682@smallexample
1683obstack_alloc (get_obstack (), 4);
1684@end smallexample
1685
1686@noindent
1687you will find that @code{get_obstack} may be called several times.
1688If you use @code{*obstack_list_ptr++} as the obstack pointer argument,
1689you will get very strange results since the incrementation may occur
1690several times.
1691
f65fd747 1692In @w{ISO C}, each function has both a macro definition and a function
28f540f4
RM
1693definition. The function definition is used if you take the address of the
1694function without calling it. An ordinary call uses the macro definition by
1695default, but you can request the function definition instead by writing the
1696function name in parentheses, as shown here:
1697
1698@smallexample
1699char *x;
1700void *(*funcp) ();
1701/* @r{Use the macro}. */
1702x = (char *) obstack_alloc (obptr, size);
1703/* @r{Call the function}. */
1704x = (char *) (obstack_alloc) (obptr, size);
1705/* @r{Take the address of the function}. */
1706funcp = obstack_alloc;
1707@end smallexample
1708
1709@noindent
f65fd747 1710This is the same situation that exists in @w{ISO C} for the standard library
28f540f4
RM
1711functions. @xref{Macro Definitions}.
1712
1713@strong{Warning:} When you do use the macros, you must observe the
f65fd747 1714precaution of avoiding side effects in the first operand, even in @w{ISO C}.
28f540f4
RM
1715
1716If you use the GNU C compiler, this precaution is not necessary, because
1717various language extensions in GNU C permit defining the macros so as to
1718compute each argument only once.
1719
1720@node Growing Objects
99a20616 1721@subsubsection Growing Objects
28f540f4
RM
1722@cindex growing objects (in obstacks)
1723@cindex changing the size of a block (obstacks)
1724
99a20616 1725Because memory in obstack chunks is used sequentially, it is possible to
28f540f4
RM
1726build up an object step by step, adding one or more bytes at a time to the
1727end of the object. With this technique, you do not need to know how much
1728data you will put in the object until you come to the end of it. We call
1729this the technique of @dfn{growing objects}. The special functions
1730for adding data to the growing object are described in this section.
1731
1732You don't need to do anything special when you start to grow an object.
1733Using one of the functions to add data to the object automatically
1734starts it. However, it is necessary to say explicitly when the object is
1735finished. This is done with the function @code{obstack_finish}.
1736
1737The actual address of the object thus built up is not known until the
1738object is finished. Until then, it always remains possible that you will
1739add so much data that the object must be copied into a new chunk.
1740
1741While the obstack is in use for a growing object, you cannot use it for
1742ordinary allocation of another object. If you try to do so, the space
1743already added to the growing object will become part of the other object.
1744
1745@comment obstack.h
1746@comment GNU
1747@deftypefun void obstack_blank (struct obstack *@var{obstack-ptr}, int @var{size})
1748The most basic function for adding to a growing object is
1749@code{obstack_blank}, which adds space without initializing it.
1750@end deftypefun
1751
1752@comment obstack.h
1753@comment GNU
1754@deftypefun void obstack_grow (struct obstack *@var{obstack-ptr}, void *@var{data}, int @var{size})
1755To add a block of initialized space, use @code{obstack_grow}, which is
1756the growing-object analogue of @code{obstack_copy}. It adds @var{size}
1757bytes of data to the growing object, copying the contents from
1758@var{data}.
1759@end deftypefun
1760
1761@comment obstack.h
1762@comment GNU
1763@deftypefun void obstack_grow0 (struct obstack *@var{obstack-ptr}, void *@var{data}, int @var{size})
1764This is the growing-object analogue of @code{obstack_copy0}. It adds
1765@var{size} bytes copied from @var{data}, followed by an additional null
1766character.
1767@end deftypefun
1768
1769@comment obstack.h
1770@comment GNU
1771@deftypefun void obstack_1grow (struct obstack *@var{obstack-ptr}, char @var{c})
1772To add one character at a time, use the function @code{obstack_1grow}.
1773It adds a single byte containing @var{c} to the growing object.
1774@end deftypefun
1775
2c6fe0bd
UD
1776@comment obstack.h
1777@comment GNU
1778@deftypefun void obstack_ptr_grow (struct obstack *@var{obstack-ptr}, void *@var{data})
1779Adding the value of a pointer one can use the function
1780@code{obstack_ptr_grow}. It adds @code{sizeof (void *)} bytes
1781containing the value of @var{data}.
1782@end deftypefun
1783
1784@comment obstack.h
1785@comment GNU
1786@deftypefun void obstack_int_grow (struct obstack *@var{obstack-ptr}, int @var{data})
1787A single value of type @code{int} can be added by using the
1788@code{obstack_int_grow} function. It adds @code{sizeof (int)} bytes to
1789the growing object and initializes them with the value of @var{data}.
1790@end deftypefun
1791
28f540f4
RM
1792@comment obstack.h
1793@comment GNU
1794@deftypefun {void *} obstack_finish (struct obstack *@var{obstack-ptr})
1795When you are finished growing the object, use the function
1796@code{obstack_finish} to close it off and return its final address.
1797
1798Once you have finished the object, the obstack is available for ordinary
1799allocation or for growing another object.
1800
1801This function can return a null pointer under the same conditions as
1802@code{obstack_alloc} (@pxref{Allocation in an Obstack}).
1803@end deftypefun
1804
1805When you build an object by growing it, you will probably need to know
1806afterward how long it became. You need not keep track of this as you grow
1807the object, because you can find out the length from the obstack just
1808before finishing the object with the function @code{obstack_object_size},
1809declared as follows:
1810
1811@comment obstack.h
1812@comment GNU
1813@deftypefun int obstack_object_size (struct obstack *@var{obstack-ptr})
1814This function returns the current size of the growing object, in bytes.
1815Remember to call this function @emph{before} finishing the object.
1816After it is finished, @code{obstack_object_size} will return zero.
1817@end deftypefun
1818
1819If you have started growing an object and wish to cancel it, you should
1820finish it and then free it, like this:
1821
1822@smallexample
1823obstack_free (obstack_ptr, obstack_finish (obstack_ptr));
1824@end smallexample
1825
1826@noindent
1827This has no effect if no object was growing.
1828
1829@cindex shrinking objects
1830You can use @code{obstack_blank} with a negative size argument to make
1831the current object smaller. Just don't try to shrink it beyond zero
1832length---there's no telling what will happen if you do that.
1833
1834@node Extra Fast Growing
99a20616 1835@subsubsection Extra Fast Growing Objects
28f540f4
RM
1836@cindex efficiency and obstacks
1837
1838The usual functions for growing objects incur overhead for checking
1839whether there is room for the new growth in the current chunk. If you
1840are frequently constructing objects in small steps of growth, this
1841overhead can be significant.
1842
1843You can reduce the overhead by using special ``fast growth''
1844functions that grow the object without checking. In order to have a
1845robust program, you must do the checking yourself. If you do this checking
1846in the simplest way each time you are about to add data to the object, you
1847have not saved anything, because that is what the ordinary growth
1848functions do. But if you can arrange to check less often, or check
1849more efficiently, then you make the program faster.
1850
1851The function @code{obstack_room} returns the amount of room available
1852in the current chunk. It is declared as follows:
1853
1854@comment obstack.h
1855@comment GNU
1856@deftypefun int obstack_room (struct obstack *@var{obstack-ptr})
1857This returns the number of bytes that can be added safely to the current
1858growing object (or to an object about to be started) in obstack
1859@var{obstack} using the fast growth functions.
1860@end deftypefun
1861
1862While you know there is room, you can use these fast growth functions
1863for adding data to a growing object:
1864
1865@comment obstack.h
1866@comment GNU
1867@deftypefun void obstack_1grow_fast (struct obstack *@var{obstack-ptr}, char @var{c})
1868The function @code{obstack_1grow_fast} adds one byte containing the
1869character @var{c} to the growing object in obstack @var{obstack-ptr}.
1870@end deftypefun
1871
2c6fe0bd
UD
1872@comment obstack.h
1873@comment GNU
1874@deftypefun void obstack_ptr_grow_fast (struct obstack *@var{obstack-ptr}, void *@var{data})
1875The function @code{obstack_ptr_grow_fast} adds @code{sizeof (void *)}
1876bytes containing the value of @var{data} to the growing object in
1877obstack @var{obstack-ptr}.
1878@end deftypefun
1879
1880@comment obstack.h
1881@comment GNU
1882@deftypefun void obstack_int_grow_fast (struct obstack *@var{obstack-ptr}, int @var{data})
1883The function @code{obstack_int_grow_fast} adds @code{sizeof (int)} bytes
1884containing the value of @var{data} to the growing object in obstack
1885@var{obstack-ptr}.
1886@end deftypefun
1887
28f540f4
RM
1888@comment obstack.h
1889@comment GNU
1890@deftypefun void obstack_blank_fast (struct obstack *@var{obstack-ptr}, int @var{size})
1891The function @code{obstack_blank_fast} adds @var{size} bytes to the
1892growing object in obstack @var{obstack-ptr} without initializing them.
1893@end deftypefun
1894
1895When you check for space using @code{obstack_room} and there is not
1896enough room for what you want to add, the fast growth functions
1897are not safe. In this case, simply use the corresponding ordinary
1898growth function instead. Very soon this will copy the object to a
a5113b14 1899new chunk; then there will be lots of room available again.
28f540f4
RM
1900
1901So, each time you use an ordinary growth function, check afterward for
1902sufficient space using @code{obstack_room}. Once the object is copied
1903to a new chunk, there will be plenty of space again, so the program will
1904start using the fast growth functions again.
1905
1906Here is an example:
1907
1908@smallexample
1909@group
1910void
1911add_string (struct obstack *obstack, const char *ptr, int len)
1912@{
1913 while (len > 0)
1914 @{
1915 int room = obstack_room (obstack);
1916 if (room == 0)
1917 @{
1918 /* @r{Not enough room. Add one character slowly,}
1919 @r{which may copy to a new chunk and make room.} */
1920 obstack_1grow (obstack, *ptr++);
1921 len--;
1922 @}
a5113b14 1923 else
28f540f4
RM
1924 @{
1925 if (room > len)
1926 room = len;
1927 /* @r{Add fast as much as we have room for.} */
1928 len -= room;
1929 while (room-- > 0)
1930 obstack_1grow_fast (obstack, *ptr++);
1931 @}
1932 @}
1933@}
1934@end group
1935@end smallexample
1936
1937@node Status of an Obstack
99a20616 1938@subsubsection Status of an Obstack
28f540f4
RM
1939@cindex obstack status
1940@cindex status of obstack
1941
1942Here are functions that provide information on the current status of
1943allocation in an obstack. You can use them to learn about an object while
1944still growing it.
1945
1946@comment obstack.h
1947@comment GNU
1948@deftypefun {void *} obstack_base (struct obstack *@var{obstack-ptr})
1949This function returns the tentative address of the beginning of the
1950currently growing object in @var{obstack-ptr}. If you finish the object
1951immediately, it will have that address. If you make it larger first, it
1952may outgrow the current chunk---then its address will change!
1953
1954If no object is growing, this value says where the next object you
1955allocate will start (once again assuming it fits in the current
1956chunk).
1957@end deftypefun
1958
1959@comment obstack.h
1960@comment GNU
1961@deftypefun {void *} obstack_next_free (struct obstack *@var{obstack-ptr})
1962This function returns the address of the first free byte in the current
1963chunk of obstack @var{obstack-ptr}. This is the end of the currently
1964growing object. If no object is growing, @code{obstack_next_free}
1965returns the same value as @code{obstack_base}.
1966@end deftypefun
1967
1968@comment obstack.h
1969@comment GNU
1970@deftypefun int obstack_object_size (struct obstack *@var{obstack-ptr})
1971This function returns the size in bytes of the currently growing object.
1972This is equivalent to
1973
1974@smallexample
1975obstack_next_free (@var{obstack-ptr}) - obstack_base (@var{obstack-ptr})
1976@end smallexample
1977@end deftypefun
1978
1979@node Obstacks Data Alignment
99a20616 1980@subsubsection Alignment of Data in Obstacks
28f540f4
RM
1981@cindex alignment (in obstacks)
1982
1983Each obstack has an @dfn{alignment boundary}; each object allocated in
1984the obstack automatically starts on an address that is a multiple of the
11883883
RM
1985specified boundary. By default, this boundary is aligned so that
1986the object can hold any type of data.
28f540f4
RM
1987
1988To access an obstack's alignment boundary, use the macro
1989@code{obstack_alignment_mask}, whose function prototype looks like
1990this:
1991
1992@comment obstack.h
1993@comment GNU
1994@deftypefn Macro int obstack_alignment_mask (struct obstack *@var{obstack-ptr})
1995The value is a bit mask; a bit that is 1 indicates that the corresponding
1996bit in the address of an object should be 0. The mask value should be one
1997less than a power of 2; the effect is that all object addresses are
11883883
RM
1998multiples of that power of 2. The default value of the mask is a value
1999that allows aligned objects to hold any type of data: for example, if
2000its value is 3, any type of data can be stored at locations whose
28f540f4
RM
2001addresses are multiples of 4. A mask value of 0 means an object can start
2002on any multiple of 1 (that is, no alignment is required).
2003
2004The expansion of the macro @code{obstack_alignment_mask} is an lvalue,
2005so you can alter the mask by assignment. For example, this statement:
2006
2007@smallexample
2008obstack_alignment_mask (obstack_ptr) = 0;
2009@end smallexample
2010
2011@noindent
2012has the effect of turning off alignment processing in the specified obstack.
2013@end deftypefn
2014
2015Note that a change in alignment mask does not take effect until
2016@emph{after} the next time an object is allocated or finished in the
2017obstack. If you are not growing an object, you can make the new
2018alignment mask take effect immediately by calling @code{obstack_finish}.
2019This will finish a zero-length object and then do proper alignment for
2020the next object.
2021
2022@node Obstack Chunks
99a20616 2023@subsubsection Obstack Chunks
28f540f4
RM
2024@cindex efficiency of chunks
2025@cindex chunks
2026
2027Obstacks work by allocating space for themselves in large chunks, and
2028then parceling out space in the chunks to satisfy your requests. Chunks
2029are normally 4096 bytes long unless you specify a different chunk size.
2030The chunk size includes 8 bytes of overhead that are not actually used
2031for storing objects. Regardless of the specified size, longer chunks
2032will be allocated when necessary for long objects.
2033
2034The obstack library allocates chunks by calling the function
2035@code{obstack_chunk_alloc}, which you must define. When a chunk is no
2036longer needed because you have freed all the objects in it, the obstack
2037library frees the chunk by calling @code{obstack_chunk_free}, which you
2038must also define.
2039
2040These two must be defined (as macros) or declared (as functions) in each
2041source file that uses @code{obstack_init} (@pxref{Creating Obstacks}).
2042Most often they are defined as macros like this:
2043
2044@smallexample
bd355af0 2045#define obstack_chunk_alloc malloc
28f540f4
RM
2046#define obstack_chunk_free free
2047@end smallexample
2048
2049Note that these are simple macros (no arguments). Macro definitions with
2050arguments will not work! It is necessary that @code{obstack_chunk_alloc}
2051or @code{obstack_chunk_free}, alone, expand into a function name if it is
2052not itself a function name.
2053
2054If you allocate chunks with @code{malloc}, the chunk size should be a
2055power of 2. The default chunk size, 4096, was chosen because it is long
2056enough to satisfy many typical requests on the obstack yet short enough
2057not to waste too much memory in the portion of the last chunk not yet used.
2058
2059@comment obstack.h
2060@comment GNU
2061@deftypefn Macro int obstack_chunk_size (struct obstack *@var{obstack-ptr})
2062This returns the chunk size of the given obstack.
2063@end deftypefn
2064
2065Since this macro expands to an lvalue, you can specify a new chunk size by
2066assigning it a new value. Doing so does not affect the chunks already
2067allocated, but will change the size of chunks allocated for that particular
2068obstack in the future. It is unlikely to be useful to make the chunk size
2069smaller, but making it larger might improve efficiency if you are
2070allocating many objects whose size is comparable to the chunk size. Here
2071is how to do so cleanly:
2072
2073@smallexample
2074if (obstack_chunk_size (obstack_ptr) < @var{new-chunk-size})
2075 obstack_chunk_size (obstack_ptr) = @var{new-chunk-size};
2076@end smallexample
2077
2078@node Summary of Obstacks
99a20616 2079@subsubsection Summary of Obstack Functions
28f540f4
RM
2080
2081Here is a summary of all the functions associated with obstacks. Each
2082takes the address of an obstack (@code{struct obstack *}) as its first
2083argument.
2084
2085@table @code
2086@item void obstack_init (struct obstack *@var{obstack-ptr})
2087Initialize use of an obstack. @xref{Creating Obstacks}.
2088
2089@item void *obstack_alloc (struct obstack *@var{obstack-ptr}, int @var{size})
2090Allocate an object of @var{size} uninitialized bytes.
2091@xref{Allocation in an Obstack}.
2092
2093@item void *obstack_copy (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
2094Allocate an object of @var{size} bytes, with contents copied from
2095@var{address}. @xref{Allocation in an Obstack}.
2096
2097@item void *obstack_copy0 (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
2098Allocate an object of @var{size}+1 bytes, with @var{size} of them copied
2099from @var{address}, followed by a null character at the end.
2100@xref{Allocation in an Obstack}.
2101
2102@item void obstack_free (struct obstack *@var{obstack-ptr}, void *@var{object})
2103Free @var{object} (and everything allocated in the specified obstack
2104more recently than @var{object}). @xref{Freeing Obstack Objects}.
2105
2106@item void obstack_blank (struct obstack *@var{obstack-ptr}, int @var{size})
2107Add @var{size} uninitialized bytes to a growing object.
2108@xref{Growing Objects}.
2109
2110@item void obstack_grow (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
2111Add @var{size} bytes, copied from @var{address}, to a growing object.
2112@xref{Growing Objects}.
2113
2114@item void obstack_grow0 (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
2115Add @var{size} bytes, copied from @var{address}, to a growing object,
2116and then add another byte containing a null character. @xref{Growing
2117Objects}.
2118
2119@item void obstack_1grow (struct obstack *@var{obstack-ptr}, char @var{data-char})
2120Add one byte containing @var{data-char} to a growing object.
2121@xref{Growing Objects}.
2122
2123@item void *obstack_finish (struct obstack *@var{obstack-ptr})
2124Finalize the object that is growing and return its permanent address.
2125@xref{Growing Objects}.
2126
2127@item int obstack_object_size (struct obstack *@var{obstack-ptr})
2128Get the current size of the currently growing object. @xref{Growing
2129Objects}.
2130
2131@item void obstack_blank_fast (struct obstack *@var{obstack-ptr}, int @var{size})
2132Add @var{size} uninitialized bytes to a growing object without checking
2133that there is enough room. @xref{Extra Fast Growing}.
2134
2135@item void obstack_1grow_fast (struct obstack *@var{obstack-ptr}, char @var{data-char})
2136Add one byte containing @var{data-char} to a growing object without
2137checking that there is enough room. @xref{Extra Fast Growing}.
2138
2139@item int obstack_room (struct obstack *@var{obstack-ptr})
2140Get the amount of room now available for growing the current object.
2141@xref{Extra Fast Growing}.
2142
2143@item int obstack_alignment_mask (struct obstack *@var{obstack-ptr})
2144The mask used for aligning the beginning of an object. This is an
2145lvalue. @xref{Obstacks Data Alignment}.
2146
2147@item int obstack_chunk_size (struct obstack *@var{obstack-ptr})
2148The size for allocating chunks. This is an lvalue. @xref{Obstack Chunks}.
2149
2150@item void *obstack_base (struct obstack *@var{obstack-ptr})
2151Tentative starting address of the currently growing object.
2152@xref{Status of an Obstack}.
2153
2154@item void *obstack_next_free (struct obstack *@var{obstack-ptr})
2155Address just after the end of the currently growing object.
2156@xref{Status of an Obstack}.
2157@end table
2158
2159@node Variable Size Automatic
99a20616 2160@subsection Automatic Storage with Variable Size
28f540f4
RM
2161@cindex automatic freeing
2162@cindex @code{alloca} function
2163@cindex automatic storage with variable size
2164
2165The function @code{alloca} supports a kind of half-dynamic allocation in
2166which blocks are allocated dynamically but freed automatically.
2167
2168Allocating a block with @code{alloca} is an explicit action; you can
2169allocate as many blocks as you wish, and compute the size at run time. But
2170all the blocks are freed when you exit the function that @code{alloca} was
2171called from, just as if they were automatic variables declared in that
2172function. There is no way to free the space explicitly.
2173
2174The prototype for @code{alloca} is in @file{stdlib.h}. This function is
2175a BSD extension.
2176@pindex stdlib.h
2177
2178@comment stdlib.h
2179@comment GNU, BSD
cc6e48bc 2180@deftypefun {void *} alloca (size_t @var{size})
28f540f4 2181The return value of @code{alloca} is the address of a block of @var{size}
99a20616 2182bytes of memory, allocated in the stack frame of the calling function.
28f540f4
RM
2183@end deftypefun
2184
2185Do not use @code{alloca} inside the arguments of a function call---you
2186will get unpredictable results, because the stack space for the
2187@code{alloca} would appear on the stack in the middle of the space for
2188the function arguments. An example of what to avoid is @code{foo (x,
2189alloca (4), y)}.
2190@c This might get fixed in future versions of GCC, but that won't make
2191@c it safe with compilers generally.
2192
2193@menu
2194* Alloca Example:: Example of using @code{alloca}.
2195* Advantages of Alloca:: Reasons to use @code{alloca}.
2196* Disadvantages of Alloca:: Reasons to avoid @code{alloca}.
2197* GNU C Variable-Size Arrays:: Only in GNU C, here is an alternative
2198 method of allocating dynamically and
2199 freeing automatically.
2200@end menu
2201
2202@node Alloca Example
99a20616 2203@subsubsection @code{alloca} Example
28f540f4 2204
bc938d3d
UD
2205As an example of the use of @code{alloca}, here is a function that opens
2206a file name made from concatenating two argument strings, and returns a
2207file descriptor or minus one signifying failure:
28f540f4
RM
2208
2209@smallexample
2210int
2211open2 (char *str1, char *str2, int flags, int mode)
2212@{
2213 char *name = (char *) alloca (strlen (str1) + strlen (str2) + 1);
a5113b14 2214 stpcpy (stpcpy (name, str1), str2);
28f540f4
RM
2215 return open (name, flags, mode);
2216@}
2217@end smallexample
2218
2219@noindent
2220Here is how you would get the same results with @code{malloc} and
2221@code{free}:
2222
2223@smallexample
2224int
2225open2 (char *str1, char *str2, int flags, int mode)
2226@{
2227 char *name = (char *) malloc (strlen (str1) + strlen (str2) + 1);
2228 int desc;
2229 if (name == 0)
2230 fatal ("virtual memory exceeded");
a5113b14 2231 stpcpy (stpcpy (name, str1), str2);
28f540f4
RM
2232 desc = open (name, flags, mode);
2233 free (name);
2234 return desc;
2235@}
2236@end smallexample
2237
2238As you can see, it is simpler with @code{alloca}. But @code{alloca} has
2239other, more important advantages, and some disadvantages.
2240
2241@node Advantages of Alloca
99a20616 2242@subsubsection Advantages of @code{alloca}
28f540f4
RM
2243
2244Here are the reasons why @code{alloca} may be preferable to @code{malloc}:
2245
2246@itemize @bullet
2247@item
2248Using @code{alloca} wastes very little space and is very fast. (It is
2249open-coded by the GNU C compiler.)
2250
2251@item
2252Since @code{alloca} does not have separate pools for different sizes of
2253block, space used for any size block can be reused for any other size.
99a20616 2254@code{alloca} does not cause memory fragmentation.
28f540f4
RM
2255
2256@item
2257@cindex longjmp
2258Nonlocal exits done with @code{longjmp} (@pxref{Non-Local Exits})
2259automatically free the space allocated with @code{alloca} when they exit
2260through the function that called @code{alloca}. This is the most
2261important reason to use @code{alloca}.
2262
2263To illustrate this, suppose you have a function
2264@code{open_or_report_error} which returns a descriptor, like
2265@code{open}, if it succeeds, but does not return to its caller if it
2266fails. If the file cannot be opened, it prints an error message and
2267jumps out to the command level of your program using @code{longjmp}.
2268Let's change @code{open2} (@pxref{Alloca Example}) to use this
2269subroutine:@refill
2270
2271@smallexample
2272int
2273open2 (char *str1, char *str2, int flags, int mode)
2274@{
2275 char *name = (char *) alloca (strlen (str1) + strlen (str2) + 1);
a5113b14 2276 stpcpy (stpcpy (name, str1), str2);
28f540f4
RM
2277 return open_or_report_error (name, flags, mode);
2278@}
2279@end smallexample
2280
2281@noindent
99a20616 2282Because of the way @code{alloca} works, the memory it allocates is
28f540f4
RM
2283freed even when an error occurs, with no special effort required.
2284
2285By contrast, the previous definition of @code{open2} (which uses
99a20616 2286@code{malloc} and @code{free}) would develop a memory leak if it were
28f540f4
RM
2287changed in this way. Even if you are willing to make more changes to
2288fix it, there is no easy way to do so.
2289@end itemize
2290
2291@node Disadvantages of Alloca
99a20616 2292@subsubsection Disadvantages of @code{alloca}
28f540f4
RM
2293
2294@cindex @code{alloca} disadvantages
2295@cindex disadvantages of @code{alloca}
2296These are the disadvantages of @code{alloca} in comparison with
2297@code{malloc}:
2298
2299@itemize @bullet
2300@item
99a20616 2301If you try to allocate more memory than the machine can provide, you
28f540f4
RM
2302don't get a clean error message. Instead you get a fatal signal like
2303the one you would get from an infinite recursion; probably a
2304segmentation violation (@pxref{Program Error Signals}).
2305
2306@item
a7a93d50 2307Some @nongnusystems{} fail to support @code{alloca}, so it is less
28f540f4
RM
2308portable. However, a slower emulation of @code{alloca} written in C
2309is available for use on systems with this deficiency.
2310@end itemize
2311
2312@node GNU C Variable-Size Arrays
99a20616 2313@subsubsection GNU C Variable-Size Arrays
28f540f4
RM
2314@cindex variable-sized arrays
2315
2316In GNU C, you can replace most uses of @code{alloca} with an array of
2317variable size. Here is how @code{open2} would look then:
2318
2319@smallexample
2320int open2 (char *str1, char *str2, int flags, int mode)
2321@{
2322 char name[strlen (str1) + strlen (str2) + 1];
a5113b14 2323 stpcpy (stpcpy (name, str1), str2);
28f540f4
RM
2324 return open (name, flags, mode);
2325@}
2326@end smallexample
2327
2328But @code{alloca} is not always equivalent to a variable-sized array, for
2329several reasons:
2330
2331@itemize @bullet
2332@item
2333A variable size array's space is freed at the end of the scope of the
2334name of the array. The space allocated with @code{alloca}
2335remains until the end of the function.
2336
2337@item
2338It is possible to use @code{alloca} within a loop, allocating an
2339additional block on each iteration. This is impossible with
2340variable-sized arrays.
2341@end itemize
2342
48b22986 2343@strong{NB:} If you mix use of @code{alloca} and variable-sized arrays
28f540f4
RM
2344within one function, exiting a scope in which a variable-sized array was
2345declared frees all blocks allocated with @code{alloca} during the
2346execution of that scope.
2347
99a20616
UD
2348
2349@node Resizing the Data Segment
2350@section Resizing the Data Segment
2351
2352The symbols in this section are declared in @file{unistd.h}.
2353
2354You will not normally use the functions in this section, because the
2355functions described in @ref{Memory Allocation} are easier to use. Those
1f77f049 2356are interfaces to a @glibcadj{} memory allocator that uses the
99a20616
UD
2357functions below itself. The functions below are simple interfaces to
2358system calls.
2359
2360@comment unistd.h
2361@comment BSD
2362@deftypefun int brk (void *@var{addr})
2363
2364@code{brk} sets the high end of the calling process' data segment to
2365@var{addr}.
2366
2367The address of the end of a segment is defined to be the address of the
2368last byte in the segment plus 1.
2369
2370The function has no effect if @var{addr} is lower than the low end of
2371the data segment. (This is considered success, by the way).
2372
2373The function fails if it would cause the data segment to overlap another
68979757 2374segment or exceed the process' data storage limit (@pxref{Limits on
99a20616
UD
2375Resources}).
2376
2377The function is named for a common historical case where data storage
2378and the stack are in the same segment. Data storage allocation grows
2379upward from the bottom of the segment while the stack grows downward
2380toward it from the top of the segment and the curtain between them is
2381called the @dfn{break}.
2382
2383The return value is zero on success. On failure, the return value is
68979757 2384@code{-1} and @code{errno} is set accordingly. The following @code{errno}
99a20616
UD
2385values are specific to this function:
2386
2387@table @code
2388@item ENOMEM
2389The request would cause the data segment to overlap another segment or
2390exceed the process' data storage limit.
2391@end table
2392
2393@c The Brk system call in Linux (as opposed to the GNU C Library function)
2394@c is considerably different. It always returns the new end of the data
2395@c segment, whether it succeeds or fails. The GNU C library Brk determines
bbf70ae9 2396@c it's a failure if and only if the system call returns an address less
99a20616
UD
2397@c than the address requested.
2398
2399@end deftypefun
2400
2401
2402@comment unistd.h
2403@comment BSD
d6868416 2404@deftypefun void *sbrk (ptrdiff_t @var{delta})
99a20616
UD
2405This function is the same as @code{brk} except that you specify the new
2406end of the data segment as an offset @var{delta} from the current end
2407and on success the return value is the address of the resulting end of
2408the data segment instead of zero.
2409
2410This means you can use @samp{sbrk(0)} to find out what the current end
2411of the data segment is.
2412
2413@end deftypefun
2414
2415
2416
2417@node Locking Pages
2418@section Locking Pages
2419@cindex locking pages
2420@cindex memory lock
2421@cindex paging
2422
2423You can tell the system to associate a particular virtual memory page
11bf311e 2424with a real page frame and keep it that way --- i.e., cause the page to
99a20616
UD
2425be paged in if it isn't already and mark it so it will never be paged
2426out and consequently will never cause a page fault. This is called
2427@dfn{locking} a page.
2428
2429The functions in this chapter lock and unlock the calling process'
2430pages.
2431
2432@menu
2433* Why Lock Pages:: Reasons to read this section.
2434* Locked Memory Details:: Everything you need to know locked
2435 memory
2436* Page Lock Functions:: Here's how to do it.
2437@end menu
2438
2439@node Why Lock Pages
2440@subsection Why Lock Pages
2441
2442Because page faults cause paged out pages to be paged in transparently,
68979757 2443a process rarely needs to be concerned about locking pages. However,
99a20616
UD
2444there are two reasons people sometimes are:
2445
2446@itemize @bullet
2447
2448@item
2449Speed. A page fault is transparent only insofar as the process is not
2450sensitive to how long it takes to do a simple memory access. Time-critical
2451processes, especially realtime processes, may not be able to wait or
2452may not be able to tolerate variance in execution speed.
2453@cindex realtime processing
2454@cindex speed of execution
2455
2456A process that needs to lock pages for this reason probably also needs
2457priority among other processes for use of the CPU. @xref{Priority}.
2458
2459In some cases, the programmer knows better than the system's demand
2460paging allocator which pages should remain in real memory to optimize
2461system performance. In this case, locking pages can help.
2462
2463@item
2464Privacy. If you keep secrets in virtual memory and that virtual memory
2465gets paged out, that increases the chance that the secrets will get out.
2466If a password gets written out to disk swap space, for example, it might
2467still be there long after virtual and real memory have been wiped clean.
2468
2469@end itemize
2470
2471Be aware that when you lock a page, that's one fewer page frame that can
2472be used to back other virtual memory (by the same or other processes),
2473which can mean more page faults, which means the system runs more
2474slowly. In fact, if you lock enough memory, some programs may not be
2475able to run at all for lack of real memory.
2476
2477@node Locked Memory Details
2478@subsection Locked Memory Details
2479
2480A memory lock is associated with a virtual page, not a real frame. The
2481paging rule is: If a frame backs at least one locked page, don't page it
2482out.
2483
11bf311e 2484Memory locks do not stack. I.e., you can't lock a particular page twice
99a20616
UD
2485so that it has to be unlocked twice before it is truly unlocked. It is
2486either locked or it isn't.
2487
2488A memory lock persists until the process that owns the memory explicitly
2489unlocks it. (But process termination and exec cause the virtual memory
2490to cease to exist, which you might say means it isn't locked any more).
2491
2492Memory locks are not inherited by child processes. (But note that on a
2493modern Unix system, immediately after a fork, the parent's and the
2494child's virtual address space are backed by the same real page frames,
2495so the child enjoys the parent's locks). @xref{Creating a Process}.
2496
2497Because of its ability to impact other processes, only the superuser can
2498lock a page. Any process can unlock its own page.
2499
2500The system sets limits on the amount of memory a process can have locked
2501and the amount of real memory it can have dedicated to it. @xref{Limits
2502on Resources}.
2503
2504In Linux, locked pages aren't as locked as you might think.
2505Two virtual pages that are not shared memory can nonetheless be backed
2506by the same real frame. The kernel does this in the name of efficiency
2507when it knows both virtual pages contain identical data, and does it
68979757 2508even if one or both of the virtual pages are locked.
99a20616
UD
2509
2510But when a process modifies one of those pages, the kernel must get it a
2511separate frame and fill it with the page's data. This is known as a
2512@dfn{copy-on-write page fault}. It takes a small amount of time and in
2513a pathological case, getting that frame may require I/O.
2514@cindex copy-on-write page fault
2515@cindex page fault, copy-on-write
2516
2517To make sure this doesn't happen to your program, don't just lock the
2518pages. Write to them as well, unless you know you won't write to them
2519ever. And to make sure you have pre-allocated frames for your stack,
2520enter a scope that declares a C automatic variable larger than the
2521maximum stack size you will need, set it to something, then return from
2522its scope.
2523
2524@node Page Lock Functions
2525@subsection Functions To Lock And Unlock Pages
2526
2527The symbols in this section are declared in @file{sys/mman.h}. These
2528functions are defined by POSIX.1b, but their availability depends on
2529your kernel. If your kernel doesn't allow these functions, they exist
2530but always fail. They @emph{are} available with a Linux kernel.
2531
2532@strong{Portability Note:} POSIX.1b requires that when the @code{mlock}
2533and @code{munlock} functions are available, the file @file{unistd.h}
2534define the macro @code{_POSIX_MEMLOCK_RANGE} and the file
2535@code{limits.h} define the macro @code{PAGESIZE} to be the size of a
2536memory page in bytes. It requires that when the @code{mlockall} and
2537@code{munlockall} functions are available, the @file{unistd.h} file
1f77f049 2538define the macro @code{_POSIX_MEMLOCK}. @Theglibc{} conforms to
99a20616
UD
2539this requirement.
2540
2541@comment sys/mman.h
2542@comment POSIX.1b
2543@deftypefun int mlock (const void *@var{addr}, size_t @var{len})
2544
2545@code{mlock} locks a range of the calling process' virtual pages.
2546
2547The range of memory starts at address @var{addr} and is @var{len} bytes
2548long. Actually, since you must lock whole pages, it is the range of
2549pages that include any part of the specified range.
2550
2551When the function returns successfully, each of those pages is backed by
2552(connected to) a real frame (is resident) and is marked to stay that
2553way. This means the function may cause page-ins and have to wait for
2554them.
2555
2556When the function fails, it does not affect the lock status of any
2557pages.
2558
2559The return value is zero if the function succeeds. Otherwise, it is
2560@code{-1} and @code{errno} is set accordingly. @code{errno} values
2561specific to this function are:
2562
2563@table @code
2564@item ENOMEM
2565@itemize @bullet
2566@item
2567At least some of the specified address range does not exist in the
2568calling process' virtual address space.
2569@item
2570The locking would cause the process to exceed its locked page limit.
2571@end itemize
2572
2573@item EPERM
2574The calling process is not superuser.
2575
2576@item EINVAL
2577@var{len} is not positive.
2578
2579@item ENOSYS
2580The kernel does not provide @code{mlock} capability.
2581
2582@end table
2583
2584You can lock @emph{all} a process' memory with @code{mlockall}. You
2585unlock memory with @code{munlock} or @code{munlockall}.
2586
2587To avoid all page faults in a C program, you have to use
2588@code{mlockall}, because some of the memory a program uses is hidden
2589from the C code, e.g. the stack and automatic variables, and you
2590wouldn't know what address to tell @code{mlock}.
2591
2592@end deftypefun
2593
2594@comment sys/mman.h
2595@comment POSIX.1b
2596@deftypefun int munlock (const void *@var{addr}, size_t @var{len})
2597
10e0498e 2598@code{munlock} unlocks a range of the calling process' virtual pages.
99a20616
UD
2599
2600@code{munlock} is the inverse of @code{mlock} and functions completely
2601analogously to @code{mlock}, except that there is no @code{EPERM}
2602failure.
2603
2604@end deftypefun
2605
2606@comment sys/mman.h
2607@comment POSIX.1b
2608@deftypefun int mlockall (int @var{flags})
2609
2610@code{mlockall} locks all the pages in a process' virtual memory address
2611space, and/or any that are added to it in the future. This includes the
2612pages of the code, data and stack segment, as well as shared libraries,
2613user space kernel data, shared memory, and memory mapped files.
2614
2615@var{flags} is a string of single bit flags represented by the following
2616macros. They tell @code{mlockall} which of its functions you want. All
2617other bits must be zero.
2618
2619@table @code
2620
2621@item MCL_CURRENT
2622Lock all pages which currently exist in the calling process' virtual
2623address space.
2624
2625@item MCL_FUTURE
2626Set a mode such that any pages added to the process' virtual address
2627space in the future will be locked from birth. This mode does not
2628affect future address spaces owned by the same process so exec, which
2629replaces a process' address space, wipes out @code{MCL_FUTURE}.
2630@xref{Executing a File}.
2631
2632@end table
2633
2634When the function returns successfully, and you specified
2635@code{MCL_CURRENT}, all of the process' pages are backed by (connected
2636to) real frames (they are resident) and are marked to stay that way.
2637This means the function may cause page-ins and have to wait for them.
2638
2639When the process is in @code{MCL_FUTURE} mode because it successfully
2640executed this function and specified @code{MCL_CURRENT}, any system call
2641by the process that requires space be added to its virtual address space
2642fails with @code{errno} = @code{ENOMEM} if locking the additional space
2643would cause the process to exceed its locked page limit. In the case
0bc93a2f 2644that the address space addition that can't be accommodated is stack
99a20616
UD
2645expansion, the stack expansion fails and the kernel sends a
2646@code{SIGSEGV} signal to the process.
2647
2648When the function fails, it does not affect the lock status of any pages
2649or the future locking mode.
2650
2651The return value is zero if the function succeeds. Otherwise, it is
2652@code{-1} and @code{errno} is set accordingly. @code{errno} values
2653specific to this function are:
2654
2655@table @code
2656@item ENOMEM
2657@itemize @bullet
2658@item
2659At least some of the specified address range does not exist in the
2660calling process' virtual address space.
2661@item
2662The locking would cause the process to exceed its locked page limit.
2663@end itemize
2664
2665@item EPERM
2666The calling process is not superuser.
2667
2668@item EINVAL
2669Undefined bits in @var{flags} are not zero.
2670
2671@item ENOSYS
2672The kernel does not provide @code{mlockall} capability.
2673
2674@end table
2675
2676You can lock just specific pages with @code{mlock}. You unlock pages
2677with @code{munlockall} and @code{munlock}.
2678
2679@end deftypefun
2680
2681
2682@comment sys/mman.h
2683@comment POSIX.1b
2684@deftypefun int munlockall (void)
2685
2686@code{munlockall} unlocks every page in the calling process' virtual
2687address space and turn off @code{MCL_FUTURE} future locking mode.
2688
2689The return value is zero if the function succeeds. Otherwise, it is
68979757 2690@code{-1} and @code{errno} is set accordingly. The only way this
99a20616
UD
2691function can fail is for generic reasons that all functions and system
2692calls can fail, so there are no specific @code{errno} values.
2693
2694@end deftypefun
2695
2696
2697
2698
a9ddb793
UD
2699@ignore
2700@c This was never actually implemented. -zw
28f540f4
RM
2701@node Relocating Allocator
2702@section Relocating Allocator
2703
2704@cindex relocating memory allocator
2705Any system of dynamic memory allocation has overhead: the amount of
2706space it uses is more than the amount the program asks for. The
2707@dfn{relocating memory allocator} achieves very low overhead by moving
2708blocks in memory as necessary, on its own initiative.
2709
a9ddb793
UD
2710@c @menu
2711@c * Relocator Concepts:: How to understand relocating allocation.
2712@c * Using Relocator:: Functions for relocating allocation.
2713@c @end menu
28f540f4
RM
2714
2715@node Relocator Concepts
2716@subsection Concepts of Relocating Allocation
2717
2718@ifinfo
2719The @dfn{relocating memory allocator} achieves very low overhead by
2720moving blocks in memory as necessary, on its own initiative.
2721@end ifinfo
2722
2723When you allocate a block with @code{malloc}, the address of the block
2724never changes unless you use @code{realloc} to change its size. Thus,
2725you can safely store the address in various places, temporarily or
2726permanently, as you like. This is not safe when you use the relocating
2727memory allocator, because any and all relocatable blocks can move
2728whenever you allocate memory in any fashion. Even calling @code{malloc}
2729or @code{realloc} can move the relocatable blocks.
2730
2731@cindex handle
2732For each relocatable block, you must make a @dfn{handle}---a pointer
2733object in memory, designated to store the address of that block. The
2734relocating allocator knows where each block's handle is, and updates the
2735address stored there whenever it moves the block, so that the handle
2736always points to the block. Each time you access the contents of the
2737block, you should fetch its address anew from the handle.
2738
2739To call any of the relocating allocator functions from a signal handler
2740is almost certainly incorrect, because the signal could happen at any
2741time and relocate all the blocks. The only way to make this safe is to
2742block the signal around any access to the contents of any relocatable
2743block---not a convenient mode of operation. @xref{Nonreentrancy}.
2744
2745@node Using Relocator
2746@subsection Allocating and Freeing Relocatable Blocks
2747
2748@pindex malloc.h
2749In the descriptions below, @var{handleptr} designates the address of the
2750handle. All the functions are declared in @file{malloc.h}; all are GNU
2751extensions.
2752
2753@comment malloc.h
2754@comment GNU
a9ddb793 2755@c @deftypefun {void *} r_alloc (void **@var{handleptr}, size_t @var{size})
28f540f4
RM
2756This function allocates a relocatable block of size @var{size}. It
2757stores the block's address in @code{*@var{handleptr}} and returns
2758a non-null pointer to indicate success.
2759
2760If @code{r_alloc} can't get the space needed, it stores a null pointer
2761in @code{*@var{handleptr}}, and returns a null pointer.
2762@end deftypefun
2763
2764@comment malloc.h
2765@comment GNU
a9ddb793 2766@c @deftypefun void r_alloc_free (void **@var{handleptr})
28f540f4
RM
2767This function is the way to free a relocatable block. It frees the
2768block that @code{*@var{handleptr}} points to, and stores a null pointer
2769in @code{*@var{handleptr}} to show it doesn't point to an allocated
2770block any more.
2771@end deftypefun
2772
2773@comment malloc.h
2774@comment GNU
a9ddb793 2775@c @deftypefun {void *} r_re_alloc (void **@var{handleptr}, size_t @var{size})
28f540f4
RM
2776The function @code{r_re_alloc} adjusts the size of the block that
2777@code{*@var{handleptr}} points to, making it @var{size} bytes long. It
2778stores the address of the resized block in @code{*@var{handleptr}} and
2779returns a non-null pointer to indicate success.
2780
2781If enough memory is not available, this function returns a null pointer
2782and does not modify @code{*@var{handleptr}}.
2783@end deftypefun
a9ddb793 2784@end ignore
28f540f4 2785
99a20616
UD
2786
2787
2788
c131718c
UD
2789@ignore
2790@comment No longer available...
2791
2792@comment @node Memory Warnings
2793@comment @section Memory Usage Warnings
2794@comment @cindex memory usage warnings
2795@comment @cindex warnings of memory almost full
28f540f4
RM
2796
2797@pindex malloc.c
2798You can ask for warnings as the program approaches running out of memory
2799space, by calling @code{memory_warnings}. This tells @code{malloc} to
2800check memory usage every time it asks for more memory from the operating
2801system. This is a GNU extension declared in @file{malloc.h}.
2802
2803@comment malloc.h
2804@comment GNU
c131718c 2805@comment @deftypefun void memory_warnings (void *@var{start}, void (*@var{warn-func}) (const char *))
28f540f4
RM
2806Call this function to request warnings for nearing exhaustion of virtual
2807memory.
2808
2809The argument @var{start} says where data space begins, in memory. The
2810allocator compares this against the last address used and against the
2811limit of data space, to determine the fraction of available memory in
2812use. If you supply zero for @var{start}, then a default value is used
2813which is right in most circumstances.
2814
2815For @var{warn-func}, supply a function that @code{malloc} can call to
2816warn you. It is called with a string (a warning message) as argument.
2817Normally it ought to display the string for the user to read.
2818@end deftypefun
2819
2820The warnings come when memory becomes 75% full, when it becomes 85%
2821full, and when it becomes 95% full. Above 95% you get another warning
2822each time memory usage increases.
c131718c
UD
2823
2824@end ignore
This page took 0.707872 seconds and 5 git commands to generate.