blob: 0729e702db5086b2cfce62b69296beddd6c5c5b3 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001@node Memory, Character Handling, Error Reporting, Top
2@chapter Virtual Memory Allocation And Paging
3@c %MENU% Allocating virtual memory and controlling paging
4@cindex memory allocation
5@cindex storage allocation
6
7This chapter describes how processes manage and use memory in a system
8that uses @theglibc{}.
9
10@Theglibc{} has several functions for dynamically allocating
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
19* Resizing the Data Segment:: @code{brk}, @code{sbrk}
20* Locking Pages:: Preventing page faults
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
34contiguous; i.e., not all of these addresses actually can be used to
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
50example, with virtual memory occupied by @glibcadj{} code. The same
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
82process is mainly just a matter of making sure that the same byte of
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
103gain additional memory. In a C program with @theglibc{}, there
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:
133
134@itemize @bullet
135
136@item
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
140the life of the virtual address space.
141
142@item
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
148@item
149The @dfn{stack segment} contains a program stack. It grows as the stack
150grows, but doesn't shrink when the stack shrinks.
151
152@end itemize
153
154
155
156@node Memory Allocation
157@section Allocating Storage For Program Data
158
159This section covers how ordinary programs manage storage for their data,
160including the famous @code{malloc} function and some fancier facilities
161special @theglibc{} and GNU Compiler.
162
163@menu
164* Memory Allocation and C:: How to get different kinds of allocation in C.
165* Unconstrained Allocation:: The @code{malloc} facility allows fully general
166 dynamic allocation.
167* Allocation Debugging:: Finding memory leaks and not freed memory.
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.
173@end menu
174
175
176@node Memory Allocation and C
177@subsection Memory Allocation in C Programs
178
179The C language supports two kinds of memory allocation through the
180variables in C programs:
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
187is started (part of the exec operation), and is never freed.
188@cindex static memory allocation
189@cindex static storage class
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.
197@cindex automatic memory allocation
198@cindex automatic storage class
199
200In GNU C, the size of the automatic storage can be an expression
201that varies. In other C implementations, it must be a constant.
202@end itemize
203
204A third important kind of memory allocation, @dfn{dynamic allocation},
205is not supported by C variables but is available via @glibcadj{}
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
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
236stored in dynamically allocated space. The only way to get dynamically
237allocated memory is via a system call (which is generally via a @glibcadj{}
238function call), and the only way to refer to dynamically
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.
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
262@subsection Unconstrained Allocation
263@cindex unconstrained memory allocation
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.
283* Aligned Memory Blocks:: Allocating specially aligned memory.
284* Malloc Tunable Parameters:: Use @code{mallopt} to adjust allocation
285 parameters.
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
295@subsubsection Basic Memory Allocation
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
303@comment ISO
304@deftypefun {void *} malloc (size_t @var{size})
305@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
306@c Malloc hooks and __morecore pointers, as well as such parameters as
307@c max_n_mmaps and max_mmapped_mem, are accessed without guards, so they
308@c could pose a thread safety issue; in order to not declare malloc
309@c MT-unsafe, it's modifying the hooks and parameters while multiple
310@c threads are active that is regarded as unsafe. An arena's next field
311@c is initialized and never changed again, except for main_arena's,
312@c that's protected by list_lock; next_free is only modified while
313@c list_lock is held too. All other data members of an arena, as well
314@c as the metadata of the memory areas assigned to it, are only modified
315@c while holding the arena's mutex (fastbin pointers use catomic ops
316@c because they may be modified by free without taking the arena's
317@c lock). Some reassurance was needed for fastbins, for it wasn't clear
318@c how they were initialized. It turns out they are always
319@c zero-initialized: main_arena's, for being static data, and other
320@c arena's, for being just-mmapped memory.
321
322@c Leaking file descriptors and memory in case of cancellation is
323@c unavoidable without disabling cancellation, but the lock situation is
324@c a bit more complicated: we don't have fallback arenas for malloc to
325@c be safe to call from within signal handlers. Error-checking mutexes
326@c or trylock could enable us to try and use alternate arenas, even with
327@c -DPER_THREAD (enabled by default), but supporting interruption
328@c (cancellation or signal handling) while holding the arena list mutex
329@c would require more work; maybe blocking signals and disabling async
330@c cancellation while manipulating the arena lists?
331
332@c __libc_malloc @asulock @aculock @acsfd @acsmem
333@c force_reg ok
334@c *malloc_hook unguarded
335@c arena_lookup ok
336@c tsd_getspecific ok, TLS
337@c arena_lock @asulock @aculock @acsfd @acsmem
338@c mutex_lock @asulock @aculock
339@c arena_get2 @asulock @aculock @acsfd @acsmem
340@c get_free_list @asulock @aculock
341@c mutex_lock (list_lock) dup @asulock @aculock
342@c mutex_unlock (list_lock) dup @aculock
343@c mutex_lock (arena lock) dup @asulock @aculock [returns locked]
344@c tsd_setspecific ok, TLS
345@c __get_nprocs ext ok @acsfd
346@c NARENAS_FROM_NCORES ok
347@c catomic_compare_and_exchange_bool_acq ok
348@c _int_new_arena ok @asulock @aculock @acsmem
349@c new_heap ok @acsmem
350@c mmap ok @acsmem
351@c munmap ok @acsmem
352@c mprotect ok
353@c chunk2mem ok
354@c set_head ok
355@c tsd_setspecific dup ok
356@c mutex_init ok
357@c mutex_lock (just-created mutex) ok, returns locked
358@c mutex_lock (list_lock) dup @asulock @aculock
359@c atomic_write_barrier ok
360@c mutex_unlock (list_lock) @aculock
361@c catomic_decrement ok
362@c reused_arena @asulock @aculock
363@c reads&writes next_to_use and iterates over arena next without guards
364@c those are harmless as long as we don't drop arenas from the
365@c NEXT list, and we never do; when a thread terminates,
366@c arena_thread_freeres prepends the arena to the free_list
367@c NEXT_FREE list, but NEXT is never modified, so it's safe!
368@c mutex_trylock (arena lock) @asulock @aculock
369@c mutex_lock (arena lock) dup @asulock @aculock
370@c tsd_setspecific dup ok
371@c _int_malloc @acsfd @acsmem
372@c checked_request2size ok
373@c REQUEST_OUT_OF_RANGE ok
374@c request2size ok
375@c get_max_fast ok
376@c fastbin_index ok
377@c fastbin ok
378@c catomic_compare_and_exhange_val_acq ok
379@c malloc_printerr dup @mtsenv
380@c if we get to it, we're toast already, undefined behavior must have
381@c been invoked before
382@c libc_message @mtsenv [no leaks with cancellation disabled]
383@c FATAL_PREPARE ok
384@c pthread_setcancelstate disable ok
385@c libc_secure_getenv @mtsenv
386@c getenv @mtsenv
387@c open_not_cancel_2 dup @acsfd
388@c strchrnul ok
389@c WRITEV_FOR_FATAL ok
390@c writev ok
391@c mmap ok @acsmem
392@c munmap ok @acsmem
393@c BEFORE_ABORT @acsfd
394@c backtrace ok
395@c write_not_cancel dup ok
396@c backtrace_symbols_fd @aculock
397@c open_not_cancel_2 dup @acsfd
398@c read_not_cancel dup ok
399@c close_not_cancel_no_status dup @acsfd
400@c abort ok
401@c itoa_word ok
402@c abort ok
403@c check_remalloced_chunk ok/disabled
404@c chunk2mem dup ok
405@c alloc_perturb ok
406@c in_smallbin_range ok
407@c smallbin_index ok
408@c bin_at ok
409@c last ok
410@c malloc_consolidate ok
411@c get_max_fast dup ok
412@c clear_fastchunks ok
413@c unsorted_chunks dup ok
414@c fastbin dup ok
415@c atomic_exchange_acq ok
416@c check_inuse_chunk dup ok/disabled
417@c chunk_at_offset dup ok
418@c chunksize dup ok
419@c inuse_bit_at_offset dup ok
420@c unlink dup ok
421@c clear_inuse_bit_at_offset dup ok
422@c in_smallbin_range dup ok
423@c set_head dup ok
424@c malloc_init_state ok
425@c bin_at dup ok
426@c set_noncontiguous dup ok
427@c set_max_fast dup ok
428@c initial_top ok
429@c unsorted_chunks dup ok
430@c check_malloc_state ok/disabled
431@c set_inuse_bit_at_offset ok
432@c check_malloced_chunk ok/disabled
433@c largebin_index ok
434@c have_fastchunks ok
435@c unsorted_chunks ok
436@c bin_at ok
437@c chunksize ok
438@c chunk_at_offset ok
439@c set_head ok
440@c set_foot ok
441@c mark_bin ok
442@c idx2bit ok
443@c first ok
444@c unlink ok
445@c malloc_printerr dup ok
446@c in_smallbin_range dup ok
447@c idx2block ok
448@c idx2bit dup ok
449@c next_bin ok
450@c sysmalloc @acsfd @acsmem
451@c MMAP @acsmem
452@c set_head dup ok
453@c check_chunk ok/disabled
454@c chunk2mem dup ok
455@c chunksize dup ok
456@c chunk_at_offset dup ok
457@c heap_for_ptr ok
458@c grow_heap ok
459@c mprotect ok
460@c set_head dup ok
461@c new_heap @acsmem
462@c MMAP dup @acsmem
463@c munmap @acsmem
464@c top ok
465@c set_foot dup ok
466@c contiguous ok
467@c MORECORE ok
468@c *__morecore ok unguarded
469@c __default_morecore
470@c sbrk ok
471@c force_reg dup ok
472@c *__after_morecore_hook unguarded
473@c set_noncontiguous ok
474@c malloc_printerr dup ok
475@c _int_free (have_lock) @acsfd @acsmem [@asulock @aculock]
476@c chunksize dup ok
477@c mutex_unlock dup @aculock/!have_lock
478@c malloc_printerr dup ok
479@c check_inuse_chunk ok/disabled
480@c chunk_at_offset dup ok
481@c mutex_lock dup @asulock @aculock/@have_lock
482@c chunk2mem dup ok
483@c free_perturb ok
484@c set_fastchunks ok
485@c catomic_and ok
486@c fastbin_index dup ok
487@c fastbin dup ok
488@c catomic_compare_and_exchange_val_rel ok
489@c chunk_is_mmapped ok
490@c contiguous dup ok
491@c prev_inuse ok
492@c unlink dup ok
493@c inuse_bit_at_offset dup ok
494@c clear_inuse_bit_at_offset ok
495@c unsorted_chunks dup ok
496@c in_smallbin_range dup ok
497@c set_head dup ok
498@c set_foot dup ok
499@c check_free_chunk ok/disabled
500@c check_chunk dup ok/disabled
501@c have_fastchunks dup ok
502@c malloc_consolidate dup ok
503@c systrim ok
504@c MORECORE dup ok
505@c *__after_morecore_hook dup unguarded
506@c set_head dup ok
507@c check_malloc_state ok/disabled
508@c top dup ok
509@c heap_for_ptr dup ok
510@c heap_trim @acsfd @acsmem
511@c top dup ok
512@c chunk_at_offset dup ok
513@c prev_chunk ok
514@c chunksize dup ok
515@c prev_inuse dup ok
516@c delete_heap @acsmem
517@c munmap dup @acsmem
518@c unlink dup ok
519@c set_head dup ok
520@c shrink_heap @acsfd
521@c check_may_shrink_heap @acsfd
522@c open_not_cancel_2 @acsfd
523@c read_not_cancel ok
524@c close_not_cancel_no_status @acsfd
525@c MMAP dup ok
526@c madvise ok
527@c munmap_chunk @acsmem
528@c chunksize dup ok
529@c chunk_is_mmapped dup ok
530@c chunk2mem dup ok
531@c malloc_printerr dup ok
532@c munmap dup @acsmem
533@c check_malloc_state ok/disabled
534@c arena_get_retry @asulock @aculock @acsfd @acsmem
535@c mutex_unlock dup @aculock
536@c mutex_lock dup @asulock @aculock
537@c arena_get2 dup @asulock @aculock @acsfd @acsmem
538@c mutex_unlock @aculock
539@c mem2chunk ok
540@c chunk_is_mmapped ok
541@c arena_for_chunk ok
542@c chunk_non_main_arena ok
543@c heap_for_ptr ok
544This function returns a pointer to a newly allocated block @var{size}
545bytes long, or a null pointer if the block could not be allocated.
546@end deftypefun
547
548The contents of the block are undefined; you must initialize it yourself
549(or use @code{calloc} instead; @pxref{Allocating Cleared Space}).
550Normally you would cast the value as a pointer to the kind of object
551that you want to store in the block. Here we show an example of doing
552so, and of initializing the space with zeros using the library function
553@code{memset} (@pxref{Copying and Concatenation}):
554
555@smallexample
556struct foo *ptr;
557@dots{}
558ptr = (struct foo *) malloc (sizeof (struct foo));
559if (ptr == 0) abort ();
560memset (ptr, 0, sizeof (struct foo));
561@end smallexample
562
563You can store the result of @code{malloc} into any pointer variable
564without a cast, because @w{ISO C} automatically converts the type
565@code{void *} to another type of pointer when necessary. But the cast
566is necessary in contexts other than assignment operators or if you might
567want your code to run in traditional C.
568
569Remember that when allocating space for a string, the argument to
570@code{malloc} must be one plus the length of the string. This is
571because a string is terminated with a null character that doesn't count
572in the ``length'' of the string but does need space. For example:
573
574@smallexample
575char *ptr;
576@dots{}
577ptr = (char *) malloc (length + 1);
578@end smallexample
579
580@noindent
581@xref{Representation of Strings}, for more information about this.
582
583@node Malloc Examples
584@subsubsection Examples of @code{malloc}
585
586If no more space is available, @code{malloc} returns a null pointer.
587You should check the value of @emph{every} call to @code{malloc}. It is
588useful to write a subroutine that calls @code{malloc} and reports an
589error if the value is a null pointer, returning only if the value is
590nonzero. This function is conventionally called @code{xmalloc}. Here
591it is:
592
593@smallexample
594void *
595xmalloc (size_t size)
596@{
597 void *value = malloc (size);
598 if (value == 0)
599 fatal ("virtual memory exhausted");
600 return value;
601@}
602@end smallexample
603
604Here is a real example of using @code{malloc} (by way of @code{xmalloc}).
605The function @code{savestring} will copy a sequence of characters into
606a newly allocated null-terminated string:
607
608@smallexample
609@group
610char *
611savestring (const char *ptr, size_t len)
612@{
613 char *value = (char *) xmalloc (len + 1);
614 value[len] = '\0';
615 return (char *) memcpy (value, ptr, len);
616@}
617@end group
618@end smallexample
619
620The block that @code{malloc} gives you is guaranteed to be aligned so
621that it can hold any type of data. On @gnusystems{}, the address is
622always a multiple of eight on 32-bit systems, and a multiple of 16 on
62364-bit systems. Only rarely is any higher boundary (such as a page
624boundary) necessary; for those cases, use @code{aligned_alloc} or
625@code{posix_memalign} (@pxref{Aligned Memory Blocks}).
626
627Note that the memory located after the end of the block is likely to be
628in use for something else; perhaps a block already allocated by another
629call to @code{malloc}. If you attempt to treat the block as longer than
630you asked for it to be, you are liable to destroy the data that
631@code{malloc} uses to keep track of its blocks, or you may destroy the
632contents of another block. If you have already allocated a block and
633discover you want it to be bigger, use @code{realloc} (@pxref{Changing
634Block Size}).
635
636@node Freeing after Malloc
637@subsubsection Freeing Memory Allocated with @code{malloc}
638@cindex freeing memory allocated with @code{malloc}
639@cindex heap, freeing memory from
640
641When you no longer need a block that you got with @code{malloc}, use the
642function @code{free} to make the block available to be allocated again.
643The prototype for this function is in @file{stdlib.h}.
644@pindex stdlib.h
645
646@comment malloc.h stdlib.h
647@comment ISO
648@deftypefun void free (void *@var{ptr})
649@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
650@c __libc_free @asulock @aculock @acsfd @acsmem
651@c releasing memory into fastbins modifies the arena without taking
652@c its mutex, but catomic operations ensure safety. If two (or more)
653@c threads are running malloc and have their own arenas locked when
654@c each gets a signal whose handler free()s large (non-fastbin-able)
655@c blocks from each other's arena, we deadlock; this is a more general
656@c case of @asulock.
657@c *__free_hook unguarded
658@c mem2chunk ok
659@c chunk_is_mmapped ok, chunk bits not modified after allocation
660@c chunksize ok
661@c munmap_chunk dup @acsmem
662@c arena_for_chunk dup ok
663@c _int_free (!have_lock) dup @asulock @aculock @acsfd @acsmem
664The @code{free} function deallocates the block of memory pointed at
665by @var{ptr}.
666@end deftypefun
667
668@comment stdlib.h
669@comment Sun
670@deftypefun void cfree (void *@var{ptr})
671@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
672@c alias to free
673This function does the same thing as @code{free}. It's provided for
674backward compatibility with SunOS; you should use @code{free} instead.
675@end deftypefun
676
677Freeing a block alters the contents of the block. @strong{Do not expect to
678find any data (such as a pointer to the next block in a chain of blocks) in
679the block after freeing it.} Copy whatever you need out of the block before
680freeing it! Here is an example of the proper way to free all the blocks in
681a chain, and the strings that they point to:
682
683@smallexample
684struct chain
685 @{
686 struct chain *next;
687 char *name;
688 @}
689
690void
691free_chain (struct chain *chain)
692@{
693 while (chain != 0)
694 @{
695 struct chain *next = chain->next;
696 free (chain->name);
697 free (chain);
698 chain = next;
699 @}
700@}
701@end smallexample
702
703Occasionally, @code{free} can actually return memory to the operating
704system and make the process smaller. Usually, all it can do is allow a
705later call to @code{malloc} to reuse the space. In the meantime, the
706space remains in your program as part of a free-list used internally by
707@code{malloc}.
708
709There is no point in freeing blocks at the end of a program, because all
710of the program's space is given back to the system when the process
711terminates.
712
713@node Changing Block Size
714@subsubsection Changing the Size of a Block
715@cindex changing the size of a block (@code{malloc})
716
717Often you do not know for certain how big a block you will ultimately need
718at the time you must begin to use the block. For example, the block might
719be a buffer that you use to hold a line being read from a file; no matter
720how long you make the buffer initially, you may encounter a line that is
721longer.
722
723You can make the block longer by calling @code{realloc}. This function
724is declared in @file{stdlib.h}.
725@pindex stdlib.h
726
727@comment malloc.h stdlib.h
728@comment ISO
729@deftypefun {void *} realloc (void *@var{ptr}, size_t @var{newsize})
730@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
731@c It may call the implementations of malloc and free, so all of their
732@c issues arise, plus the realloc hook, also accessed without guards.
733
734@c __libc_realloc @asulock @aculock @acsfd @acsmem
735@c *__realloc_hook unguarded
736@c __libc_free dup @asulock @aculock @acsfd @acsmem
737@c __libc_malloc dup @asulock @aculock @acsfd @acsmem
738@c mem2chunk dup ok
739@c chunksize dup ok
740@c malloc_printerr dup ok
741@c checked_request2size dup ok
742@c chunk_is_mmapped dup ok
743@c mremap_chunk
744@c chunksize dup ok
745@c __mremap ok
746@c set_head dup ok
747@c MALLOC_COPY ok
748@c memcpy ok
749@c munmap_chunk dup @acsmem
750@c arena_for_chunk dup ok
751@c mutex_lock (arena mutex) dup @asulock @aculock
752@c _int_realloc @acsfd @acsmem
753@c malloc_printerr dup ok
754@c check_inuse_chunk dup ok/disabled
755@c chunk_at_offset dup ok
756@c chunksize dup ok
757@c set_head_size dup ok
758@c chunk_at_offset dup ok
759@c set_head dup ok
760@c chunk2mem dup ok
761@c inuse dup ok
762@c unlink dup ok
763@c _int_malloc dup @acsfd @acsmem
764@c mem2chunk dup ok
765@c MALLOC_COPY dup ok
766@c _int_free (have_lock) dup @acsfd @acsmem
767@c set_inuse_bit_at_offset dup ok
768@c set_head dup ok
769@c mutex_unlock (arena mutex) dup @aculock
770@c _int_free (!have_lock) dup @asulock @aculock @acsfd @acsmem
771
772The @code{realloc} function changes the size of the block whose address is
773@var{ptr} to be @var{newsize}.
774
775Since the space after the end of the block may be in use, @code{realloc}
776may find it necessary to copy the block to a new address where more free
777space is available. The value of @code{realloc} is the new address of the
778block. If the block needs to be moved, @code{realloc} copies the old
779contents.
780
781If you pass a null pointer for @var{ptr}, @code{realloc} behaves just
782like @samp{malloc (@var{newsize})}. This can be convenient, but beware
783that older implementations (before @w{ISO C}) may not support this
784behavior, and will probably crash when @code{realloc} is passed a null
785pointer.
786@end deftypefun
787
788Like @code{malloc}, @code{realloc} may return a null pointer if no
789memory space is available to make the block bigger. When this happens,
790the original block is untouched; it has not been modified or relocated.
791
792In most cases it makes no difference what happens to the original block
793when @code{realloc} fails, because the application program cannot continue
794when it is out of memory, and the only thing to do is to give a fatal error
795message. Often it is convenient to write and use a subroutine,
796conventionally called @code{xrealloc}, that takes care of the error message
797as @code{xmalloc} does for @code{malloc}:
798
799@smallexample
800void *
801xrealloc (void *ptr, size_t size)
802@{
803 void *value = realloc (ptr, size);
804 if (value == 0)
805 fatal ("Virtual memory exhausted");
806 return value;
807@}
808@end smallexample
809
810You can also use @code{realloc} to make a block smaller. The reason you
811would do this is to avoid tying up a lot of memory space when only a little
812is needed.
813@comment The following is no longer true with the new malloc.
814@comment But it seems wise to keep the warning for other implementations.
815In several allocation implementations, making a block smaller sometimes
816necessitates copying it, so it can fail if no other space is available.
817
818If the new size you specify is the same as the old size, @code{realloc}
819is guaranteed to change nothing and return the same address that you gave.
820
821@node Allocating Cleared Space
822@subsubsection Allocating Cleared Space
823
824The function @code{calloc} allocates memory and clears it to zero. It
825is declared in @file{stdlib.h}.
826@pindex stdlib.h
827
828@comment malloc.h stdlib.h
829@comment ISO
830@deftypefun {void *} calloc (size_t @var{count}, size_t @var{eltsize})
831@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
832@c Same caveats as malloc.
833
834@c __libc_calloc @asulock @aculock @acsfd @acsmem
835@c *__malloc_hook dup unguarded
836@c memset dup ok
837@c arena_get @asulock @aculock @acsfd @acsmem
838@c arena_lookup dup ok
839@c arena_lock dup @asulock @aculock @acsfd @acsmem
840@c top dup ok
841@c chunksize dup ok
842@c heap_for_ptr dup ok
843@c _int_malloc dup @acsfd @acsmem
844@c arena_get_retry dup @asulock @aculock @acsfd @acsmem
845@c mutex_unlock dup @aculock
846@c mem2chunk dup ok
847@c chunk_is_mmapped dup ok
848@c MALLOC_ZERO ok
849@c memset dup ok
850This function allocates a block long enough to contain a vector of
851@var{count} elements, each of size @var{eltsize}. Its contents are
852cleared to zero before @code{calloc} returns.
853@end deftypefun
854
855You could define @code{calloc} as follows:
856
857@smallexample
858void *
859calloc (size_t count, size_t eltsize)
860@{
861 size_t size = count * eltsize;
862 void *value = malloc (size);
863 if (value != 0)
864 memset (value, 0, size);
865 return value;
866@}
867@end smallexample
868
869But in general, it is not guaranteed that @code{calloc} calls
870@code{malloc} internally. Therefore, if an application provides its own
871@code{malloc}/@code{realloc}/@code{free} outside the C library, it
872should always define @code{calloc}, too.
873
874@node Efficiency and Malloc
875@subsubsection Efficiency Considerations for @code{malloc}
876@cindex efficiency and @code{malloc}
877
878
879
880
881@ignore
882
883@c No longer true, see below instead.
884To make the best use of @code{malloc}, it helps to know that the GNU
885version of @code{malloc} always dispenses small amounts of memory in
886blocks whose sizes are powers of two. It keeps separate pools for each
887power of two. This holds for sizes up to a page size. Therefore, if
888you are free to choose the size of a small block in order to make
889@code{malloc} more efficient, make it a power of two.
890@c !!! xref getpagesize
891
892Once a page is split up for a particular block size, it can't be reused
893for another size unless all the blocks in it are freed. In many
894programs, this is unlikely to happen. Thus, you can sometimes make a
895program use memory more efficiently by using blocks of the same size for
896many different purposes.
897
898When you ask for memory blocks of a page or larger, @code{malloc} uses a
899different strategy; it rounds the size up to a multiple of a page, and
900it can coalesce and split blocks as needed.
901
902The reason for the two strategies is that it is important to allocate
903and free small blocks as fast as possible, but speed is less important
904for a large block since the program normally spends a fair amount of
905time using it. Also, large blocks are normally fewer in number.
906Therefore, for large blocks, it makes sense to use a method which takes
907more time to minimize the wasted space.
908
909@end ignore
910
911As opposed to other versions, the @code{malloc} in @theglibc{}
912does not round up block sizes to powers of two, neither for large nor
913for small sizes. Neighboring chunks can be coalesced on a @code{free}
914no matter what their size is. This makes the implementation suitable
915for all kinds of allocation patterns without generally incurring high
916memory waste through fragmentation.
917
918Very large blocks (much larger than a page) are allocated with
919@code{mmap} (anonymous or via @code{/dev/zero}) by this implementation.
920This has the great advantage that these chunks are returned to the
921system immediately when they are freed. Therefore, it cannot happen
922that a large chunk becomes ``locked'' in between smaller ones and even
923after calling @code{free} wastes memory. The size threshold for
924@code{mmap} to be used can be adjusted with @code{mallopt}. The use of
925@code{mmap} can also be disabled completely.
926
927@node Aligned Memory Blocks
928@subsubsection Allocating Aligned Memory Blocks
929
930@cindex page boundary
931@cindex alignment (with @code{malloc})
932@pindex stdlib.h
933The address of a block returned by @code{malloc} or @code{realloc} in
934@gnusystems{} is always a multiple of eight (or sixteen on 64-bit
935systems). If you need a block whose address is a multiple of a higher
936power of two than that, use @code{aligned_alloc} or @code{posix_memalign}.
937@code{aligned_alloc} and @code{posix_memalign} are declared in
938@file{stdlib.h}.
939
940@comment stdlib.h
941@deftypefun {void *} aligned_alloc (size_t @var{alignment}, size_t @var{size})
942@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
943@c Alias to memalign.
944The @code{aligned_alloc} function allocates a block of @var{size} bytes whose
945address is a multiple of @var{alignment}. The @var{alignment} must be a
946power of two and @var{size} must be a multiple of @var{alignment}.
947
948The @code{aligned_alloc} function returns a null pointer on error and sets
949@code{errno} to one of the following values:
950
951@table @code
952@item ENOMEM
953There was insufficient memory available to satisfy the request.
954
955@item EINVAL
956@var{alignment} is not a power of two.
957
958This function was introduced in @w{ISO C11} and hence may have better
959portability to modern non-POSIX systems than @code{posix_memalign}.
960@end table
961
962@end deftypefun
963
964@comment malloc.h
965@comment BSD
966@deftypefun {void *} memalign (size_t @var{boundary}, size_t @var{size})
967@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
968@c Same issues as malloc. The padding bytes are safely freed in
969@c _int_memalign, with the arena still locked.
970
971@c __libc_memalign @asulock @aculock @acsfd @acsmem
972@c *__memalign_hook dup unguarded
973@c __libc_malloc dup @asulock @aculock @acsfd @acsmem
974@c arena_get dup @asulock @aculock @acsfd @acsmem
975@c _int_memalign @acsfd @acsmem
976@c _int_malloc dup @acsfd @acsmem
977@c checked_request2size dup ok
978@c mem2chunk dup ok
979@c chunksize dup ok
980@c chunk_is_mmapped dup ok
981@c set_head dup ok
982@c chunk2mem dup ok
983@c set_inuse_bit_at_offset dup ok
984@c set_head_size dup ok
985@c _int_free (have_lock) dup @acsfd @acsmem
986@c chunk_at_offset dup ok
987@c check_inuse_chunk dup ok
988@c arena_get_retry dup @asulock @aculock @acsfd @acsmem
989@c mutex_unlock dup @aculock
990The @code{memalign} function allocates a block of @var{size} bytes whose
991address is a multiple of @var{boundary}. The @var{boundary} must be a
992power of two! The function @code{memalign} works by allocating a
993somewhat larger block, and then returning an address within the block
994that is on the specified boundary.
995
996The @code{memalign} function returns a null pointer on error and sets
997@code{errno} to one of the following values:
998
999@table @code
1000@item ENOMEM
1001There was insufficient memory available to satisfy the request.
1002
1003@item EINVAL
1004@var{alignment} is not a power of two.
1005
1006@end table
1007
1008The @code{memalign} function is obsolete and @code{aligned_alloc} or
1009@code{posix_memalign} should be used instead.
1010@end deftypefun
1011
1012@comment stdlib.h
1013@comment POSIX
1014@deftypefun int posix_memalign (void **@var{memptr}, size_t @var{alignment}, size_t @var{size})
1015@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
1016@c Calls memalign unless the requirements are not met (powerof2 macro is
1017@c safe given an automatic variable as an argument) or there's a
1018@c memalign hook (accessed unguarded, but safely).
1019The @code{posix_memalign} function is similar to the @code{memalign}
1020function in that it returns a buffer of @var{size} bytes aligned to a
1021multiple of @var{alignment}. But it adds one requirement to the
1022parameter @var{alignment}: the value must be a power of two multiple of
1023@code{sizeof (void *)}.
1024
1025If the function succeeds in allocation memory a pointer to the allocated
1026memory is returned in @code{*@var{memptr}} and the return value is zero.
1027Otherwise the function returns an error value indicating the problem.
1028The possible error values returned are:
1029
1030@table @code
1031@item ENOMEM
1032There was insufficient memory available to satisfy the request.
1033
1034@item EINVAL
1035@var{alignment} is not a power of two multiple of @code{sizeof (void *)}.
1036
1037@end table
1038
1039This function was introduced in POSIX 1003.1d. Although this function is
1040superseded by @code{aligned_alloc}, it is more portable to older POSIX
1041systems that do not support @w{ISO C11}.
1042@end deftypefun
1043
1044@comment malloc.h stdlib.h
1045@comment BSD
1046@deftypefun {void *} valloc (size_t @var{size})
1047@safety{@prelim{}@mtunsafe{@mtuinit{}}@asunsafe{@asuinit{} @asulock{}}@acunsafe{@acuinit{} @aculock{} @acsfd{} @acsmem{}}}
1048@c __libc_valloc @mtuinit @asuinit @asulock @aculock @acsfd @acsmem
1049@c ptmalloc_init (once) @mtsenv @asulock @aculock @acsfd @acsmem
1050@c _dl_addr @asucorrupt? @aculock
1051@c __rtld_lock_lock_recursive (dl_load_lock) @asucorrupt? @aculock
1052@c _dl_find_dso_for_object ok, iterates over dl_ns and its _ns_loaded objs
1053@c the ok above assumes no partial updates on dl_ns and _ns_loaded
1054@c that could confuse a _dl_addr call in a signal handler
1055@c _dl_addr_inside_object ok
1056@c determine_info ok
1057@c __rtld_lock_unlock_recursive (dl_load_lock) @aculock
1058@c thread_atfork @asulock @aculock @acsfd @acsmem
1059@c __register_atfork @asulock @aculock @acsfd @acsmem
1060@c lll_lock (__fork_lock) @asulock @aculock
1061@c fork_handler_alloc @asulock @aculock @acsfd @acsmem
1062@c calloc dup @asulock @aculock @acsfd @acsmem
1063@c __linkin_atfork ok
1064@c catomic_compare_and_exchange_bool_acq ok
1065@c lll_unlock (__fork_lock) @aculock
1066@c *_environ @mtsenv
1067@c next_env_entry ok
1068@c strcspn dup ok
1069@c __libc_mallopt dup @mtasuconst:mallopt [setting mp_]
1070@c __malloc_check_init @mtasuconst:malloc_hooks [setting hooks]
1071@c *__malloc_initialize_hook unguarded, ok
1072@c *__memalign_hook dup ok, unguarded
1073@c arena_get dup @asulock @aculock @acsfd @acsmem
1074@c _int_valloc @acsfd @acsmem
1075@c malloc_consolidate dup ok
1076@c _int_memalign dup @acsfd @acsmem
1077@c arena_get_retry dup @asulock @aculock @acsfd @acsmem
1078@c _int_memalign dup @acsfd @acsmem
1079@c mutex_unlock dup @aculock
1080Using @code{valloc} is like using @code{memalign} and passing the page size
1081as the value of the second argument. It is implemented like this:
1082
1083@smallexample
1084void *
1085valloc (size_t size)
1086@{
1087 return memalign (getpagesize (), size);
1088@}
1089@end smallexample
1090
1091@ref{Query Memory Parameters} for more information about the memory
1092subsystem.
1093
1094The @code{valloc} function is obsolete and @code{aligned_alloc} or
1095@code{posix_memalign} should be used instead.
1096@end deftypefun
1097
1098@node Malloc Tunable Parameters
1099@subsubsection Malloc Tunable Parameters
1100
1101You can adjust some parameters for dynamic memory allocation with the
1102@code{mallopt} function. This function is the general SVID/XPG
1103interface, defined in @file{malloc.h}.
1104@pindex malloc.h
1105
1106@deftypefun int mallopt (int @var{param}, int @var{value})
1107@safety{@prelim{}@mtunsafe{@mtuinit{} @mtasuconst{:mallopt}}@asunsafe{@asuinit{} @asulock{}}@acunsafe{@acuinit{} @aculock{}}}
1108@c __libc_mallopt @mtuinit @mtasuconst:mallopt @asuinit @asulock @aculock
1109@c ptmalloc_init (once) dup @mtsenv @asulock @aculock @acsfd @acsmem
1110@c mutex_lock (main_arena->mutex) @asulock @aculock
1111@c malloc_consolidate dup ok
1112@c set_max_fast ok
1113@c mutex_unlock dup @aculock
1114
1115When calling @code{mallopt}, the @var{param} argument specifies the
1116parameter to be set, and @var{value} the new value to be set. Possible
1117choices for @var{param}, as defined in @file{malloc.h}, are:
1118
1119@table @code
1120@comment TODO: @item M_ARENA_MAX
1121@comment - Document ARENA_MAX env var.
1122@comment TODO: @item M_ARENA_TEST
1123@comment - Document ARENA_TEST env var.
1124@comment TODO: @item M_CHECK_ACTION
1125@item M_MMAP_MAX
1126The maximum number of chunks to allocate with @code{mmap}. Setting this
1127to zero disables all use of @code{mmap}.
1128@item M_MMAP_THRESHOLD
1129All chunks larger than this value are allocated outside the normal
1130heap, using the @code{mmap} system call. This way it is guaranteed
1131that the memory for these chunks can be returned to the system on
1132@code{free}. Note that requests smaller than this threshold might still
1133be allocated via @code{mmap}.
1134@comment TODO: @item M_MXFAST
1135@item M_PERTURB
1136If non-zero, memory blocks are filled with values depending on some
1137low order bits of this parameter when they are allocated (except when
1138allocated by @code{calloc}) and freed. This can be used to debug the
1139use of uninitialized or freed heap memory. Note that this option does not
1140guarantee that the freed block will have any specific values. It only
1141guarantees that the content the block had before it was freed will be
1142overwritten.
1143@item M_TOP_PAD
1144This parameter determines the amount of extra memory to obtain from the
1145system when a call to @code{sbrk} is required. It also specifies the
1146number of bytes to retain when shrinking the heap by calling @code{sbrk}
1147with a negative argument. This provides the necessary hysteresis in
1148heap size such that excessive amounts of system calls can be avoided.
1149@item M_TRIM_THRESHOLD
1150This is the minimum size (in bytes) of the top-most, releasable chunk
1151that will cause @code{sbrk} to be called with a negative argument in
1152order to return memory to the system.
1153@end table
1154
1155@end deftypefun
1156
1157@node Heap Consistency Checking
1158@subsubsection Heap Consistency Checking
1159
1160@cindex heap consistency checking
1161@cindex consistency checking, of heap
1162
1163You can ask @code{malloc} to check the consistency of dynamic memory by
1164using the @code{mcheck} function. This function is a GNU extension,
1165declared in @file{mcheck.h}.
1166@pindex mcheck.h
1167
1168@comment mcheck.h
1169@comment GNU
1170@deftypefun int mcheck (void (*@var{abortfn}) (enum mcheck_status @var{status}))
1171@safety{@prelim{}@mtunsafe{@mtasurace{:mcheck} @mtasuconst{:malloc_hooks}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
1172@c The hooks must be set up before malloc is first used, which sort of
1173@c implies @mtuinit/@asuinit but since the function is a no-op if malloc
1174@c was already used, that doesn't pose any safety issues. The actual
1175@c problem is with the hooks, designed for single-threaded
1176@c fully-synchronous operation: they manage an unguarded linked list of
1177@c allocated blocks, and get temporarily overwritten before calling the
1178@c allocation functions recursively while holding the old hooks. There
1179@c are no guards for thread safety, and inconsistent hooks may be found
1180@c within signal handlers or left behind in case of cancellation.
1181
1182Calling @code{mcheck} tells @code{malloc} to perform occasional
1183consistency checks. These will catch things such as writing
1184past the end of a block that was allocated with @code{malloc}.
1185
1186The @var{abortfn} argument is the function to call when an inconsistency
1187is found. If you supply a null pointer, then @code{mcheck} uses a
1188default function which prints a message and calls @code{abort}
1189(@pxref{Aborting a Program}). The function you supply is called with
1190one argument, which says what sort of inconsistency was detected; its
1191type is described below.
1192
1193It is too late to begin allocation checking once you have allocated
1194anything with @code{malloc}. So @code{mcheck} does nothing in that
1195case. The function returns @code{-1} if you call it too late, and
1196@code{0} otherwise (when it is successful).
1197
1198The easiest way to arrange to call @code{mcheck} early enough is to use
1199the option @samp{-lmcheck} when you link your program; then you don't
1200need to modify your program source at all. Alternatively you might use
1201a debugger to insert a call to @code{mcheck} whenever the program is
1202started, for example these gdb commands will automatically call @code{mcheck}
1203whenever the program starts:
1204
1205@smallexample
1206(gdb) break main
1207Breakpoint 1, main (argc=2, argv=0xbffff964) at whatever.c:10
1208(gdb) command 1
1209Type commands for when breakpoint 1 is hit, one per line.
1210End with a line saying just "end".
1211>call mcheck(0)
1212>continue
1213>end
1214(gdb) @dots{}
1215@end smallexample
1216
1217This will however only work if no initialization function of any object
1218involved calls any of the @code{malloc} functions since @code{mcheck}
1219must be called before the first such function.
1220
1221@end deftypefun
1222
1223@deftypefun {enum mcheck_status} mprobe (void *@var{pointer})
1224@safety{@prelim{}@mtunsafe{@mtasurace{:mcheck} @mtasuconst{:malloc_hooks}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
1225@c The linked list of headers may be modified concurrently by other
1226@c threads, and it may find a partial update if called from a signal
1227@c handler. It's mostly read only, so cancelling it might be safe, but
1228@c it will modify global state that, if cancellation hits at just the
1229@c right spot, may be left behind inconsistent. This path is only taken
1230@c if checkhdr finds an inconsistency. If the inconsistency could only
1231@c occur because of earlier undefined behavior, that wouldn't be an
1232@c additional safety issue problem, but because of the other concurrency
1233@c issues in the mcheck hooks, the apparent inconsistency could be the
1234@c result of mcheck's own internal data race. So, AC-Unsafe it is.
1235
1236The @code{mprobe} function lets you explicitly check for inconsistencies
1237in a particular allocated block. You must have already called
1238@code{mcheck} at the beginning of the program, to do its occasional
1239checks; calling @code{mprobe} requests an additional consistency check
1240to be done at the time of the call.
1241
1242The argument @var{pointer} must be a pointer returned by @code{malloc}
1243or @code{realloc}. @code{mprobe} returns a value that says what
1244inconsistency, if any, was found. The values are described below.
1245@end deftypefun
1246
1247@deftp {Data Type} {enum mcheck_status}
1248This enumerated type describes what kind of inconsistency was detected
1249in an allocated block, if any. Here are the possible values:
1250
1251@table @code
1252@item MCHECK_DISABLED
1253@code{mcheck} was not called before the first allocation.
1254No consistency checking can be done.
1255@item MCHECK_OK
1256No inconsistency detected.
1257@item MCHECK_HEAD
1258The data immediately before the block was modified.
1259This commonly happens when an array index or pointer
1260is decremented too far.
1261@item MCHECK_TAIL
1262The data immediately after the block was modified.
1263This commonly happens when an array index or pointer
1264is incremented too far.
1265@item MCHECK_FREE
1266The block was already freed.
1267@end table
1268@end deftp
1269
1270Another possibility to check for and guard against bugs in the use of
1271@code{malloc}, @code{realloc} and @code{free} is to set the environment
1272variable @code{MALLOC_CHECK_}. When @code{MALLOC_CHECK_} is set, a
1273special (less efficient) implementation is used which is designed to be
1274tolerant against simple errors, such as double calls of @code{free} with
1275the same argument, or overruns of a single byte (off-by-one bugs). Not
1276all such errors can be protected against, however, and memory leaks can
1277result. If @code{MALLOC_CHECK_} is set to @code{0}, any detected heap
1278corruption is silently ignored; if set to @code{1}, a diagnostic is
1279printed on @code{stderr}; if set to @code{2}, @code{abort} is called
1280immediately. This can be useful because otherwise a crash may happen
1281much later, and the true cause for the problem is then very hard to
1282track down.
1283
1284There is one problem with @code{MALLOC_CHECK_}: in SUID or SGID binaries
1285it could possibly be exploited since diverging from the normal programs
1286behavior it now writes something to the standard error descriptor.
1287Therefore the use of @code{MALLOC_CHECK_} is disabled by default for
1288SUID and SGID binaries. It can be enabled again by the system
1289administrator by adding a file @file{/etc/suid-debug} (the content is
1290not important it could be empty).
1291
1292So, what's the difference between using @code{MALLOC_CHECK_} and linking
1293with @samp{-lmcheck}? @code{MALLOC_CHECK_} is orthogonal with respect to
1294@samp{-lmcheck}. @samp{-lmcheck} has been added for backward
1295compatibility. Both @code{MALLOC_CHECK_} and @samp{-lmcheck} should
1296uncover the same bugs - but using @code{MALLOC_CHECK_} you don't need to
1297recompile your application.
1298
1299@node Hooks for Malloc
1300@subsubsection Memory Allocation Hooks
1301@cindex allocation hooks, for @code{malloc}
1302
1303@Theglibc{} lets you modify the behavior of @code{malloc},
1304@code{realloc}, and @code{free} by specifying appropriate hook
1305functions. You can use these hooks to help you debug programs that use
1306dynamic memory allocation, for example.
1307
1308The hook variables are declared in @file{malloc.h}.
1309@pindex malloc.h
1310
1311@comment malloc.h
1312@comment GNU
1313@defvar __malloc_hook
1314The value of this variable is a pointer to the function that
1315@code{malloc} uses whenever it is called. You should define this
1316function to look like @code{malloc}; that is, like:
1317
1318@smallexample
1319void *@var{function} (size_t @var{size}, const void *@var{caller})
1320@end smallexample
1321
1322The value of @var{caller} is the return address found on the stack when
1323the @code{malloc} function was called. This value allows you to trace
1324the memory consumption of the program.
1325@end defvar
1326
1327@comment malloc.h
1328@comment GNU
1329@defvar __realloc_hook
1330The value of this variable is a pointer to function that @code{realloc}
1331uses whenever it is called. You should define this function to look
1332like @code{realloc}; that is, like:
1333
1334@smallexample
1335void *@var{function} (void *@var{ptr}, size_t @var{size}, const void *@var{caller})
1336@end smallexample
1337
1338The value of @var{caller} is the return address found on the stack when
1339the @code{realloc} function was called. This value allows you to trace the
1340memory consumption of the program.
1341@end defvar
1342
1343@comment malloc.h
1344@comment GNU
1345@defvar __free_hook
1346The value of this variable is a pointer to function that @code{free}
1347uses whenever it is called. You should define this function to look
1348like @code{free}; that is, like:
1349
1350@smallexample
1351void @var{function} (void *@var{ptr}, const void *@var{caller})
1352@end smallexample
1353
1354The value of @var{caller} is the return address found on the stack when
1355the @code{free} function was called. This value allows you to trace the
1356memory consumption of the program.
1357@end defvar
1358
1359@comment malloc.h
1360@comment GNU
1361@defvar __memalign_hook
1362The value of this variable is a pointer to function that @code{aligned_alloc},
1363@code{memalign}, @code{posix_memalign} and @code{valloc} use whenever they
1364are called. You should define this function to look like @code{aligned_alloc};
1365that is, like:
1366
1367@smallexample
1368void *@var{function} (size_t @var{alignment}, size_t @var{size}, const void *@var{caller})
1369@end smallexample
1370
1371The value of @var{caller} is the return address found on the stack when
1372the @code{aligned_alloc}, @code{memalign}, @code{posix_memalign} or
1373@code{valloc} functions are called. This value allows you to trace the
1374memory consumption of the program.
1375@end defvar
1376
1377You must make sure that the function you install as a hook for one of
1378these functions does not call that function recursively without restoring
1379the old value of the hook first! Otherwise, your program will get stuck
1380in an infinite recursion. Before calling the function recursively, one
1381should make sure to restore all the hooks to their previous value. When
1382coming back from the recursive call, all the hooks should be resaved
1383since a hook might modify itself.
1384
1385@comment malloc.h
1386@comment GNU
1387@defvar __malloc_initialize_hook
1388The value of this variable is a pointer to a function that is called
1389once when the malloc implementation is initialized. This is a weak
1390variable, so it can be overridden in the application with a definition
1391like the following:
1392
1393@smallexample
1394void (*@var{__malloc_initialize_hook}) (void) = my_init_hook;
1395@end smallexample
1396@end defvar
1397
1398An issue to look out for is the time at which the malloc hook functions
1399can be safely installed. If the hook functions call the malloc-related
1400functions recursively, it is necessary that malloc has already properly
1401initialized itself at the time when @code{__malloc_hook} etc. is
1402assigned to. On the other hand, if the hook functions provide a
1403complete malloc implementation of their own, it is vital that the hooks
1404are assigned to @emph{before} the very first @code{malloc} call has
1405completed, because otherwise a chunk obtained from the ordinary,
1406un-hooked malloc may later be handed to @code{__free_hook}, for example.
1407
1408In both cases, the problem can be solved by setting up the hooks from
1409within a user-defined function pointed to by
1410@code{__malloc_initialize_hook}---then the hooks will be set up safely
1411at the right time.
1412
1413Here is an example showing how to use @code{__malloc_hook} and
1414@code{__free_hook} properly. It installs a function that prints out
1415information every time @code{malloc} or @code{free} is called. We just
1416assume here that @code{realloc} and @code{memalign} are not used in our
1417program.
1418
1419@smallexample
1420/* Prototypes for __malloc_hook, __free_hook */
1421#include <malloc.h>
1422
1423/* Prototypes for our hooks. */
1424static void my_init_hook (void);
1425static void *my_malloc_hook (size_t, const void *);
1426static void my_free_hook (void*, const void *);
1427
1428/* Override initializing hook from the C library. */
1429void (*__malloc_initialize_hook) (void) = my_init_hook;
1430
1431static void
1432my_init_hook (void)
1433@{
1434 old_malloc_hook = __malloc_hook;
1435 old_free_hook = __free_hook;
1436 __malloc_hook = my_malloc_hook;
1437 __free_hook = my_free_hook;
1438@}
1439
1440static void *
1441my_malloc_hook (size_t size, const void *caller)
1442@{
1443 void *result;
1444 /* Restore all old hooks */
1445 __malloc_hook = old_malloc_hook;
1446 __free_hook = old_free_hook;
1447 /* Call recursively */
1448 result = malloc (size);
1449 /* Save underlying hooks */
1450 old_malloc_hook = __malloc_hook;
1451 old_free_hook = __free_hook;
1452 /* @r{@code{printf} might call @code{malloc}, so protect it too.} */
1453 printf ("malloc (%u) returns %p\n", (unsigned int) size, result);
1454 /* Restore our own hooks */
1455 __malloc_hook = my_malloc_hook;
1456 __free_hook = my_free_hook;
1457 return result;
1458@}
1459
1460static void
1461my_free_hook (void *ptr, const void *caller)
1462@{
1463 /* Restore all old hooks */
1464 __malloc_hook = old_malloc_hook;
1465 __free_hook = old_free_hook;
1466 /* Call recursively */
1467 free (ptr);
1468 /* Save underlying hooks */
1469 old_malloc_hook = __malloc_hook;
1470 old_free_hook = __free_hook;
1471 /* @r{@code{printf} might call @code{free}, so protect it too.} */
1472 printf ("freed pointer %p\n", ptr);
1473 /* Restore our own hooks */
1474 __malloc_hook = my_malloc_hook;
1475 __free_hook = my_free_hook;
1476@}
1477
1478main ()
1479@{
1480 @dots{}
1481@}
1482@end smallexample
1483
1484The @code{mcheck} function (@pxref{Heap Consistency Checking}) works by
1485installing such hooks.
1486
1487@c __morecore, __after_morecore_hook are undocumented
1488@c It's not clear whether to document them.
1489
1490@node Statistics of Malloc
1491@subsubsection Statistics for Memory Allocation with @code{malloc}
1492
1493@cindex allocation statistics
1494You can get information about dynamic memory allocation by calling the
1495@code{mallinfo} function. This function and its associated data type
1496are declared in @file{malloc.h}; they are an extension of the standard
1497SVID/XPG version.
1498@pindex malloc.h
1499
1500@comment malloc.h
1501@comment GNU
1502@deftp {Data Type} {struct mallinfo}
1503This structure type is used to return information about the dynamic
1504memory allocator. It contains the following members:
1505
1506@table @code
1507@item int arena
1508This is the total size of memory allocated with @code{sbrk} by
1509@code{malloc}, in bytes.
1510
1511@item int ordblks
1512This is the number of chunks not in use. (The memory allocator
1513internally gets chunks of memory from the operating system, and then
1514carves them up to satisfy individual @code{malloc} requests; see
1515@ref{Efficiency and Malloc}.)
1516
1517@item int smblks
1518This field is unused.
1519
1520@item int hblks
1521This is the total number of chunks allocated with @code{mmap}.
1522
1523@item int hblkhd
1524This is the total size of memory allocated with @code{mmap}, in bytes.
1525
1526@item int usmblks
1527This field is unused.
1528
1529@item int fsmblks
1530This field is unused.
1531
1532@item int uordblks
1533This is the total size of memory occupied by chunks handed out by
1534@code{malloc}.
1535
1536@item int fordblks
1537This is the total size of memory occupied by free (not in use) chunks.
1538
1539@item int keepcost
1540This is the size of the top-most releasable chunk that normally
1541borders the end of the heap (i.e., the high end of the virtual address
1542space's data segment).
1543
1544@end table
1545@end deftp
1546
1547@comment malloc.h
1548@comment SVID
1549@deftypefun {struct mallinfo} mallinfo (void)
1550@safety{@prelim{}@mtunsafe{@mtuinit{} @mtasuconst{:mallopt}}@asunsafe{@asuinit{} @asulock{}}@acunsafe{@acuinit{} @aculock{}}}
1551@c Accessing mp_.n_mmaps and mp_.max_mmapped_mem, modified with atomics
1552@c but non-atomically elsewhere, may get us inconsistent results. We
1553@c mark the statistics as unsafe, rather than the fast-path functions
1554@c that collect the possibly inconsistent data.
1555
1556@c __libc_mallinfo @mtuinit @mtasuconst:mallopt @asuinit @asulock @aculock
1557@c ptmalloc_init (once) dup @mtsenv @asulock @aculock @acsfd @acsmem
1558@c mutex_lock dup @asulock @aculock
1559@c int_mallinfo @mtasuconst:mallopt [mp_ access on main_arena]
1560@c malloc_consolidate dup ok
1561@c check_malloc_state dup ok/disabled
1562@c chunksize dup ok
1563@c fastbin dupo ok
1564@c bin_at dup ok
1565@c last dup ok
1566@c mutex_unlock @aculock
1567
1568This function returns information about the current dynamic memory usage
1569in a structure of type @code{struct mallinfo}.
1570@end deftypefun
1571
1572@node Summary of Malloc
1573@subsubsection Summary of @code{malloc}-Related Functions
1574
1575Here is a summary of the functions that work with @code{malloc}:
1576
1577@table @code
1578@item void *malloc (size_t @var{size})
1579Allocate a block of @var{size} bytes. @xref{Basic Allocation}.
1580
1581@item void free (void *@var{addr})
1582Free a block previously allocated by @code{malloc}. @xref{Freeing after
1583Malloc}.
1584
1585@item void *realloc (void *@var{addr}, size_t @var{size})
1586Make a block previously allocated by @code{malloc} larger or smaller,
1587possibly by copying it to a new location. @xref{Changing Block Size}.
1588
1589@item void *calloc (size_t @var{count}, size_t @var{eltsize})
1590Allocate a block of @var{count} * @var{eltsize} bytes using
1591@code{malloc}, and set its contents to zero. @xref{Allocating Cleared
1592Space}.
1593
1594@item void *valloc (size_t @var{size})
1595Allocate a block of @var{size} bytes, starting on a page boundary.
1596@xref{Aligned Memory Blocks}.
1597
1598@item void *aligned_alloc (size_t @var{size}, size_t @var{alignment})
1599Allocate a block of @var{size} bytes, starting on an address that is a
1600multiple of @var{alignment}. @xref{Aligned Memory Blocks}.
1601
1602@item int posix_memalign (void **@var{memptr}, size_t @var{alignment}, size_t @var{size})
1603Allocate a block of @var{size} bytes, starting on an address that is a
1604multiple of @var{alignment}. @xref{Aligned Memory Blocks}.
1605
1606@item void *memalign (size_t @var{size}, size_t @var{boundary})
1607Allocate a block of @var{size} bytes, starting on an address that is a
1608multiple of @var{boundary}. @xref{Aligned Memory Blocks}.
1609
1610@item int mallopt (int @var{param}, int @var{value})
1611Adjust a tunable parameter. @xref{Malloc Tunable Parameters}.
1612
1613@item int mcheck (void (*@var{abortfn}) (void))
1614Tell @code{malloc} to perform occasional consistency checks on
1615dynamically allocated memory, and to call @var{abortfn} when an
1616inconsistency is found. @xref{Heap Consistency Checking}.
1617
1618@item void *(*__malloc_hook) (size_t @var{size}, const void *@var{caller})
1619A pointer to a function that @code{malloc} uses whenever it is called.
1620
1621@item void *(*__realloc_hook) (void *@var{ptr}, size_t @var{size}, const void *@var{caller})
1622A pointer to a function that @code{realloc} uses whenever it is called.
1623
1624@item void (*__free_hook) (void *@var{ptr}, const void *@var{caller})
1625A pointer to a function that @code{free} uses whenever it is called.
1626
1627@item void (*__memalign_hook) (size_t @var{size}, size_t @var{alignment}, const void *@var{caller})
1628A pointer to a function that @code{aligned_alloc}, @code{memalign},
1629@code{posix_memalign} and @code{valloc} use whenever they are called.
1630
1631@item struct mallinfo mallinfo (void)
1632Return information about the current dynamic memory usage.
1633@xref{Statistics of Malloc}.
1634@end table
1635
1636@node Allocation Debugging
1637@subsection Allocation Debugging
1638@cindex allocation debugging
1639@cindex malloc debugger
1640
1641A complicated task when programming with languages which do not use
1642garbage collected dynamic memory allocation is to find memory leaks.
1643Long running programs must assure that dynamically allocated objects are
1644freed at the end of their lifetime. If this does not happen the system
1645runs out of memory, sooner or later.
1646
1647The @code{malloc} implementation in @theglibc{} provides some
1648simple means to detect such leaks and obtain some information to find
1649the location. To do this the application must be started in a special
1650mode which is enabled by an environment variable. There are no speed
1651penalties for the program if the debugging mode is not enabled.
1652
1653@menu
1654* Tracing malloc:: How to install the tracing functionality.
1655* Using the Memory Debugger:: Example programs excerpts.
1656* Tips for the Memory Debugger:: Some more or less clever ideas.
1657* Interpreting the traces:: What do all these lines mean?
1658@end menu
1659
1660@node Tracing malloc
1661@subsubsection How to install the tracing functionality
1662
1663@comment mcheck.h
1664@comment GNU
1665@deftypefun void mtrace (void)
1666@safety{@prelim{}@mtunsafe{@mtsenv{} @mtasurace{:mtrace} @mtasuconst{:malloc_hooks} @mtuinit{}}@asunsafe{@asuinit{} @ascuheap{} @asucorrupt{} @asulock{}}@acunsafe{@acuinit{} @acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
1667@c Like the mcheck hooks, these are not designed with thread safety in
1668@c mind, because the hook pointers are temporarily modified without
1669@c regard to other threads, signals or cancellation.
1670
1671@c mtrace @mtuinit @mtasurace:mtrace @mtsenv @asuinit @ascuheap @asucorrupt @acuinit @acucorrupt @aculock @acsfd @acsmem
1672@c __libc_secure_getenv dup @mtsenv
1673@c malloc dup @ascuheap @acsmem
1674@c fopen dup @ascuheap @asulock @aculock @acsmem @acsfd
1675@c fcntl dup ok
1676@c setvbuf dup @aculock
1677@c fprintf dup (on newly-created stream) @aculock
1678@c __cxa_atexit (once) dup @asulock @aculock @acsmem
1679@c free dup @ascuheap @acsmem
1680When the @code{mtrace} function is called it looks for an environment
1681variable named @code{MALLOC_TRACE}. This variable is supposed to
1682contain a valid file name. The user must have write access. If the
1683file already exists it is truncated. If the environment variable is not
1684set or it does not name a valid file which can be opened for writing
1685nothing is done. The behavior of @code{malloc} etc. is not changed.
1686For obvious reasons this also happens if the application is installed
1687with the SUID or SGID bit set.
1688
1689If the named file is successfully opened, @code{mtrace} installs special
1690handlers for the functions @code{malloc}, @code{realloc}, and
1691@code{free} (@pxref{Hooks for Malloc}). From then on, all uses of these
1692functions are traced and protocolled into the file. There is now of
1693course a speed penalty for all calls to the traced functions so tracing
1694should not be enabled during normal use.
1695
1696This function is a GNU extension and generally not available on other
1697systems. The prototype can be found in @file{mcheck.h}.
1698@end deftypefun
1699
1700@comment mcheck.h
1701@comment GNU
1702@deftypefun void muntrace (void)
1703@safety{@prelim{}@mtunsafe{@mtasurace{:mtrace} @mtasuconst{:malloc_hooks} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{} @aculock{} @acsfd{}}}
1704
1705@c muntrace @mtasurace:mtrace @mtslocale @asucorrupt @ascuheap @acucorrupt @acsmem @aculock @acsfd
1706@c fprintf (fputs) dup @mtslocale @asucorrupt @ascuheap @acsmem @aculock @acucorrupt
1707@c fclose dup @ascuheap @asulock @aculock @acsmem @acsfd
1708The @code{muntrace} function can be called after @code{mtrace} was used
1709to enable tracing the @code{malloc} calls. If no (successful) call of
1710@code{mtrace} was made @code{muntrace} does nothing.
1711
1712Otherwise it deinstalls the handlers for @code{malloc}, @code{realloc},
1713and @code{free} and then closes the protocol file. No calls are
1714protocolled anymore and the program runs again at full speed.
1715
1716This function is a GNU extension and generally not available on other
1717systems. The prototype can be found in @file{mcheck.h}.
1718@end deftypefun
1719
1720@node Using the Memory Debugger
1721@subsubsection Example program excerpts
1722
1723Even though the tracing functionality does not influence the runtime
1724behavior of the program it is not a good idea to call @code{mtrace} in
1725all programs. Just imagine that you debug a program using @code{mtrace}
1726and all other programs used in the debugging session also trace their
1727@code{malloc} calls. The output file would be the same for all programs
1728and thus is unusable. Therefore one should call @code{mtrace} only if
1729compiled for debugging. A program could therefore start like this:
1730
1731@example
1732#include <mcheck.h>
1733
1734int
1735main (int argc, char *argv[])
1736@{
1737#ifdef DEBUGGING
1738 mtrace ();
1739#endif
1740 @dots{}
1741@}
1742@end example
1743
1744This is all what is needed if you want to trace the calls during the
1745whole runtime of the program. Alternatively you can stop the tracing at
1746any time with a call to @code{muntrace}. It is even possible to restart
1747the tracing again with a new call to @code{mtrace}. But this can cause
1748unreliable results since there may be calls of the functions which are
1749not called. Please note that not only the application uses the traced
1750functions, also libraries (including the C library itself) use these
1751functions.
1752
1753This last point is also why it is no good idea to call @code{muntrace}
1754before the program terminated. The libraries are informed about the
1755termination of the program only after the program returns from
1756@code{main} or calls @code{exit} and so cannot free the memory they use
1757before this time.
1758
1759So the best thing one can do is to call @code{mtrace} as the very first
1760function in the program and never call @code{muntrace}. So the program
1761traces almost all uses of the @code{malloc} functions (except those
1762calls which are executed by constructors of the program or used
1763libraries).
1764
1765@node Tips for the Memory Debugger
1766@subsubsection Some more or less clever ideas
1767
1768You know the situation. The program is prepared for debugging and in
1769all debugging sessions it runs well. But once it is started without
1770debugging the error shows up. A typical example is a memory leak that
1771becomes visible only when we turn off the debugging. If you foresee
1772such situations you can still win. Simply use something equivalent to
1773the following little program:
1774
1775@example
1776#include <mcheck.h>
1777#include <signal.h>
1778
1779static void
1780enable (int sig)
1781@{
1782 mtrace ();
1783 signal (SIGUSR1, enable);
1784@}
1785
1786static void
1787disable (int sig)
1788@{
1789 muntrace ();
1790 signal (SIGUSR2, disable);
1791@}
1792
1793int
1794main (int argc, char *argv[])
1795@{
1796 @dots{}
1797
1798 signal (SIGUSR1, enable);
1799 signal (SIGUSR2, disable);
1800
1801 @dots{}
1802@}
1803@end example
1804
1805I.e., the user can start the memory debugger any time s/he wants if the
1806program was started with @code{MALLOC_TRACE} set in the environment.
1807The output will of course not show the allocations which happened before
1808the first signal but if there is a memory leak this will show up
1809nevertheless.
1810
1811@node Interpreting the traces
1812@subsubsection Interpreting the traces
1813
1814If you take a look at the output it will look similar to this:
1815
1816@example
1817= Start
1818@ [0x8048209] - 0x8064cc8
1819@ [0x8048209] - 0x8064ce0
1820@ [0x8048209] - 0x8064cf8
1821@ [0x80481eb] + 0x8064c48 0x14
1822@ [0x80481eb] + 0x8064c60 0x14
1823@ [0x80481eb] + 0x8064c78 0x14
1824@ [0x80481eb] + 0x8064c90 0x14
1825= End
1826@end example
1827
1828What this all means is not really important since the trace file is not
1829meant to be read by a human. Therefore no attention is given to
1830readability. Instead there is a program which comes with @theglibc{}
1831which interprets the traces and outputs a summary in an
1832user-friendly way. The program is called @code{mtrace} (it is in fact a
1833Perl script) and it takes one or two arguments. In any case the name of
1834the file with the trace output must be specified. If an optional
1835argument precedes the name of the trace file this must be the name of
1836the program which generated the trace.
1837
1838@example
1839drepper$ mtrace tst-mtrace log
1840No memory leaks.
1841@end example
1842
1843In this case the program @code{tst-mtrace} was run and it produced a
1844trace file @file{log}. The message printed by @code{mtrace} shows there
1845are no problems with the code, all allocated memory was freed
1846afterwards.
1847
1848If we call @code{mtrace} on the example trace given above we would get a
1849different outout:
1850
1851@example
1852drepper$ mtrace errlog
1853- 0x08064cc8 Free 2 was never alloc'd 0x8048209
1854- 0x08064ce0 Free 3 was never alloc'd 0x8048209
1855- 0x08064cf8 Free 4 was never alloc'd 0x8048209
1856
1857Memory not freed:
1858-----------------
1859 Address Size Caller
18600x08064c48 0x14 at 0x80481eb
18610x08064c60 0x14 at 0x80481eb
18620x08064c78 0x14 at 0x80481eb
18630x08064c90 0x14 at 0x80481eb
1864@end example
1865
1866We have called @code{mtrace} with only one argument and so the script
1867has no chance to find out what is meant with the addresses given in the
1868trace. We can do better:
1869
1870@example
1871drepper$ mtrace tst errlog
1872- 0x08064cc8 Free 2 was never alloc'd /home/drepper/tst.c:39
1873- 0x08064ce0 Free 3 was never alloc'd /home/drepper/tst.c:39
1874- 0x08064cf8 Free 4 was never alloc'd /home/drepper/tst.c:39
1875
1876Memory not freed:
1877-----------------
1878 Address Size Caller
18790x08064c48 0x14 at /home/drepper/tst.c:33
18800x08064c60 0x14 at /home/drepper/tst.c:33
18810x08064c78 0x14 at /home/drepper/tst.c:33
18820x08064c90 0x14 at /home/drepper/tst.c:33
1883@end example
1884
1885Suddenly the output makes much more sense and the user can see
1886immediately where the function calls causing the trouble can be found.
1887
1888Interpreting this output is not complicated. There are at most two
1889different situations being detected. First, @code{free} was called for
1890pointers which were never returned by one of the allocation functions.
1891This is usually a very bad problem and what this looks like is shown in
1892the first three lines of the output. Situations like this are quite
1893rare and if they appear they show up very drastically: the program
1894normally crashes.
1895
1896The other situation which is much harder to detect are memory leaks. As
1897you can see in the output the @code{mtrace} function collects all this
1898information and so can say that the program calls an allocation function
1899from line 33 in the source file @file{/home/drepper/tst-mtrace.c} four
1900times without freeing this memory before the program terminates.
1901Whether this is a real problem remains to be investigated.
1902
1903@node Obstacks
1904@subsection Obstacks
1905@cindex obstacks
1906
1907An @dfn{obstack} is a pool of memory containing a stack of objects. You
1908can create any number of separate obstacks, and then allocate objects in
1909specified obstacks. Within each obstack, the last object allocated must
1910always be the first one freed, but distinct obstacks are independent of
1911each other.
1912
1913Aside from this one constraint of order of freeing, obstacks are totally
1914general: an obstack can contain any number of objects of any size. They
1915are implemented with macros, so allocation is usually very fast as long as
1916the objects are usually small. And the only space overhead per object is
1917the padding needed to start each object on a suitable boundary.
1918
1919@menu
1920* Creating Obstacks:: How to declare an obstack in your program.
1921* Preparing for Obstacks:: Preparations needed before you can
1922 use obstacks.
1923* Allocation in an Obstack:: Allocating objects in an obstack.
1924* Freeing Obstack Objects:: Freeing objects in an obstack.
1925* Obstack Functions:: The obstack functions are both
1926 functions and macros.
1927* Growing Objects:: Making an object bigger by stages.
1928* Extra Fast Growing:: Extra-high-efficiency (though more
1929 complicated) growing objects.
1930* Status of an Obstack:: Inquiries about the status of an obstack.
1931* Obstacks Data Alignment:: Controlling alignment of objects in obstacks.
1932* Obstack Chunks:: How obstacks obtain and release chunks;
1933 efficiency considerations.
1934* Summary of Obstacks::
1935@end menu
1936
1937@node Creating Obstacks
1938@subsubsection Creating Obstacks
1939
1940The utilities for manipulating obstacks are declared in the header
1941file @file{obstack.h}.
1942@pindex obstack.h
1943
1944@comment obstack.h
1945@comment GNU
1946@deftp {Data Type} {struct obstack}
1947An obstack is represented by a data structure of type @code{struct
1948obstack}. This structure has a small fixed size; it records the status
1949of the obstack and how to find the space in which objects are allocated.
1950It does not contain any of the objects themselves. You should not try
1951to access the contents of the structure directly; use only the functions
1952described in this chapter.
1953@end deftp
1954
1955You can declare variables of type @code{struct obstack} and use them as
1956obstacks, or you can allocate obstacks dynamically like any other kind
1957of object. Dynamic allocation of obstacks allows your program to have a
1958variable number of different stacks. (You can even allocate an
1959obstack structure in another obstack, but this is rarely useful.)
1960
1961All the functions that work with obstacks require you to specify which
1962obstack to use. You do this with a pointer of type @code{struct obstack
1963*}. In the following, we often say ``an obstack'' when strictly
1964speaking the object at hand is such a pointer.
1965
1966The objects in the obstack are packed into large blocks called
1967@dfn{chunks}. The @code{struct obstack} structure points to a chain of
1968the chunks currently in use.
1969
1970The obstack library obtains a new chunk whenever you allocate an object
1971that won't fit in the previous chunk. Since the obstack library manages
1972chunks automatically, you don't need to pay much attention to them, but
1973you do need to supply a function which the obstack library should use to
1974get a chunk. Usually you supply a function which uses @code{malloc}
1975directly or indirectly. You must also supply a function to free a chunk.
1976These matters are described in the following section.
1977
1978@node Preparing for Obstacks
1979@subsubsection Preparing for Using Obstacks
1980
1981Each source file in which you plan to use the obstack functions
1982must include the header file @file{obstack.h}, like this:
1983
1984@smallexample
1985#include <obstack.h>
1986@end smallexample
1987
1988@findex obstack_chunk_alloc
1989@findex obstack_chunk_free
1990Also, if the source file uses the macro @code{obstack_init}, it must
1991declare or define two functions or macros that will be called by the
1992obstack library. One, @code{obstack_chunk_alloc}, is used to allocate
1993the chunks of memory into which objects are packed. The other,
1994@code{obstack_chunk_free}, is used to return chunks when the objects in
1995them are freed. These macros should appear before any use of obstacks
1996in the source file.
1997
1998Usually these are defined to use @code{malloc} via the intermediary
1999@code{xmalloc} (@pxref{Unconstrained Allocation}). This is done with
2000the following pair of macro definitions:
2001
2002@smallexample
2003#define obstack_chunk_alloc xmalloc
2004#define obstack_chunk_free free
2005@end smallexample
2006
2007@noindent
2008Though the memory you get using obstacks really comes from @code{malloc},
2009using obstacks is faster because @code{malloc} is called less often, for
2010larger blocks of memory. @xref{Obstack Chunks}, for full details.
2011
2012At run time, before the program can use a @code{struct obstack} object
2013as an obstack, it must initialize the obstack by calling
2014@code{obstack_init}.
2015
2016@comment obstack.h
2017@comment GNU
2018@deftypefun int obstack_init (struct obstack *@var{obstack-ptr})
2019@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acsafe{@acsmem{}}}
2020@c obstack_init @mtsrace:obstack-ptr @acsmem
2021@c _obstack_begin @acsmem
2022@c chunkfun = obstack_chunk_alloc (suggested malloc)
2023@c freefun = obstack_chunk_free (suggested free)
2024@c *chunkfun @acsmem
2025@c obstack_chunk_alloc user-supplied
2026@c *obstack_alloc_failed_handler user-supplied
2027@c -> print_and_abort (default)
2028@c
2029@c print_and_abort
2030@c _ dup @ascuintl
2031@c fxprintf dup @asucorrupt @aculock @acucorrupt
2032@c exit @acucorrupt?
2033Initialize obstack @var{obstack-ptr} for allocation of objects. This
2034function calls the obstack's @code{obstack_chunk_alloc} function. If
2035allocation of memory fails, the function pointed to by
2036@code{obstack_alloc_failed_handler} is called. The @code{obstack_init}
2037function always returns 1 (Compatibility notice: Former versions of
2038obstack returned 0 if allocation failed).
2039@end deftypefun
2040
2041Here are two examples of how to allocate the space for an obstack and
2042initialize it. First, an obstack that is a static variable:
2043
2044@smallexample
2045static struct obstack myobstack;
2046@dots{}
2047obstack_init (&myobstack);
2048@end smallexample
2049
2050@noindent
2051Second, an obstack that is itself dynamically allocated:
2052
2053@smallexample
2054struct obstack *myobstack_ptr
2055 = (struct obstack *) xmalloc (sizeof (struct obstack));
2056
2057obstack_init (myobstack_ptr);
2058@end smallexample
2059
2060@comment obstack.h
2061@comment GNU
2062@defvar obstack_alloc_failed_handler
2063The value of this variable is a pointer to a function that
2064@code{obstack} uses when @code{obstack_chunk_alloc} fails to allocate
2065memory. The default action is to print a message and abort.
2066You should supply a function that either calls @code{exit}
2067(@pxref{Program Termination}) or @code{longjmp} (@pxref{Non-Local
2068Exits}) and doesn't return.
2069
2070@smallexample
2071void my_obstack_alloc_failed (void)
2072@dots{}
2073obstack_alloc_failed_handler = &my_obstack_alloc_failed;
2074@end smallexample
2075
2076@end defvar
2077
2078@node Allocation in an Obstack
2079@subsubsection Allocation in an Obstack
2080@cindex allocation (obstacks)
2081
2082The most direct way to allocate an object in an obstack is with
2083@code{obstack_alloc}, which is invoked almost like @code{malloc}.
2084
2085@comment obstack.h
2086@comment GNU
2087@deftypefun {void *} obstack_alloc (struct obstack *@var{obstack-ptr}, int @var{size})
2088@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{} @acsmem{}}}
2089@c obstack_alloc @mtsrace:obstack-ptr @acucorrupt @acsmem
2090@c obstack_blank dup @mtsrace:obstack-ptr @acucorrupt @acsmem
2091@c obstack_finish dup @mtsrace:obstack-ptr @acucorrupt
2092This allocates an uninitialized block of @var{size} bytes in an obstack
2093and returns its address. Here @var{obstack-ptr} specifies which obstack
2094to allocate the block in; it is the address of the @code{struct obstack}
2095object which represents the obstack. Each obstack function or macro
2096requires you to specify an @var{obstack-ptr} as the first argument.
2097
2098This function calls the obstack's @code{obstack_chunk_alloc} function if
2099it needs to allocate a new chunk of memory; it calls
2100@code{obstack_alloc_failed_handler} if allocation of memory by
2101@code{obstack_chunk_alloc} failed.
2102@end deftypefun
2103
2104For example, here is a function that allocates a copy of a string @var{str}
2105in a specific obstack, which is in the variable @code{string_obstack}:
2106
2107@smallexample
2108struct obstack string_obstack;
2109
2110char *
2111copystring (char *string)
2112@{
2113 size_t len = strlen (string) + 1;
2114 char *s = (char *) obstack_alloc (&string_obstack, len);
2115 memcpy (s, string, len);
2116 return s;
2117@}
2118@end smallexample
2119
2120To allocate a block with specified contents, use the function
2121@code{obstack_copy}, declared like this:
2122
2123@comment obstack.h
2124@comment GNU
2125@deftypefun {void *} obstack_copy (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
2126@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{} @acsmem{}}}
2127@c obstack_copy @mtsrace:obstack-ptr @acucorrupt @acsmem
2128@c obstack_grow dup @mtsrace:obstack-ptr @acucorrupt @acsmem
2129@c obstack_finish dup @mtsrace:obstack-ptr @acucorrupt
2130This allocates a block and initializes it by copying @var{size}
2131bytes of data starting at @var{address}. It calls
2132@code{obstack_alloc_failed_handler} if allocation of memory by
2133@code{obstack_chunk_alloc} failed.
2134@end deftypefun
2135
2136@comment obstack.h
2137@comment GNU
2138@deftypefun {void *} obstack_copy0 (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
2139@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{} @acsmem{}}}
2140@c obstack_copy0 @mtsrace:obstack-ptr @acucorrupt @acsmem
2141@c obstack_grow0 dup @mtsrace:obstack-ptr @acucorrupt @acsmem
2142@c obstack_finish dup @mtsrace:obstack-ptr @acucorrupt
2143Like @code{obstack_copy}, but appends an extra byte containing a null
2144character. This extra byte is not counted in the argument @var{size}.
2145@end deftypefun
2146
2147The @code{obstack_copy0} function is convenient for copying a sequence
2148of characters into an obstack as a null-terminated string. Here is an
2149example of its use:
2150
2151@smallexample
2152char *
2153obstack_savestring (char *addr, int size)
2154@{
2155 return obstack_copy0 (&myobstack, addr, size);
2156@}
2157@end smallexample
2158
2159@noindent
2160Contrast this with the previous example of @code{savestring} using
2161@code{malloc} (@pxref{Basic Allocation}).
2162
2163@node Freeing Obstack Objects
2164@subsubsection Freeing Objects in an Obstack
2165@cindex freeing (obstacks)
2166
2167To free an object allocated in an obstack, use the function
2168@code{obstack_free}. Since the obstack is a stack of objects, freeing
2169one object automatically frees all other objects allocated more recently
2170in the same obstack.
2171
2172@comment obstack.h
2173@comment GNU
2174@deftypefun void obstack_free (struct obstack *@var{obstack-ptr}, void *@var{object})
2175@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{}}}
2176@c obstack_free @mtsrace:obstack-ptr @acucorrupt
2177@c (obstack_free) @mtsrace:obstack-ptr @acucorrupt
2178@c *freefun dup user-supplied
2179If @var{object} is a null pointer, everything allocated in the obstack
2180is freed. Otherwise, @var{object} must be the address of an object
2181allocated in the obstack. Then @var{object} is freed, along with
2182everything allocated in @var{obstack} since @var{object}.
2183@end deftypefun
2184
2185Note that if @var{object} is a null pointer, the result is an
2186uninitialized obstack. To free all memory in an obstack but leave it
2187valid for further allocation, call @code{obstack_free} with the address
2188of the first object allocated on the obstack:
2189
2190@smallexample
2191obstack_free (obstack_ptr, first_object_allocated_ptr);
2192@end smallexample
2193
2194Recall that the objects in an obstack are grouped into chunks. When all
2195the objects in a chunk become free, the obstack library automatically
2196frees the chunk (@pxref{Preparing for Obstacks}). Then other
2197obstacks, or non-obstack allocation, can reuse the space of the chunk.
2198
2199@node Obstack Functions
2200@subsubsection Obstack Functions and Macros
2201@cindex macros
2202
2203The interfaces for using obstacks may be defined either as functions or
2204as macros, depending on the compiler. The obstack facility works with
2205all C compilers, including both @w{ISO C} and traditional C, but there are
2206precautions you must take if you plan to use compilers other than GNU C.
2207
2208If you are using an old-fashioned @w{non-ISO C} compiler, all the obstack
2209``functions'' are actually defined only as macros. You can call these
2210macros like functions, but you cannot use them in any other way (for
2211example, you cannot take their address).
2212
2213Calling the macros requires a special precaution: namely, the first
2214operand (the obstack pointer) may not contain any side effects, because
2215it may be computed more than once. For example, if you write this:
2216
2217@smallexample
2218obstack_alloc (get_obstack (), 4);
2219@end smallexample
2220
2221@noindent
2222you will find that @code{get_obstack} may be called several times.
2223If you use @code{*obstack_list_ptr++} as the obstack pointer argument,
2224you will get very strange results since the incrementation may occur
2225several times.
2226
2227In @w{ISO C}, each function has both a macro definition and a function
2228definition. The function definition is used if you take the address of the
2229function without calling it. An ordinary call uses the macro definition by
2230default, but you can request the function definition instead by writing the
2231function name in parentheses, as shown here:
2232
2233@smallexample
2234char *x;
2235void *(*funcp) ();
2236/* @r{Use the macro}. */
2237x = (char *) obstack_alloc (obptr, size);
2238/* @r{Call the function}. */
2239x = (char *) (obstack_alloc) (obptr, size);
2240/* @r{Take the address of the function}. */
2241funcp = obstack_alloc;
2242@end smallexample
2243
2244@noindent
2245This is the same situation that exists in @w{ISO C} for the standard library
2246functions. @xref{Macro Definitions}.
2247
2248@strong{Warning:} When you do use the macros, you must observe the
2249precaution of avoiding side effects in the first operand, even in @w{ISO C}.
2250
2251If you use the GNU C compiler, this precaution is not necessary, because
2252various language extensions in GNU C permit defining the macros so as to
2253compute each argument only once.
2254
2255@node Growing Objects
2256@subsubsection Growing Objects
2257@cindex growing objects (in obstacks)
2258@cindex changing the size of a block (obstacks)
2259
2260Because memory in obstack chunks is used sequentially, it is possible to
2261build up an object step by step, adding one or more bytes at a time to the
2262end of the object. With this technique, you do not need to know how much
2263data you will put in the object until you come to the end of it. We call
2264this the technique of @dfn{growing objects}. The special functions
2265for adding data to the growing object are described in this section.
2266
2267You don't need to do anything special when you start to grow an object.
2268Using one of the functions to add data to the object automatically
2269starts it. However, it is necessary to say explicitly when the object is
2270finished. This is done with the function @code{obstack_finish}.
2271
2272The actual address of the object thus built up is not known until the
2273object is finished. Until then, it always remains possible that you will
2274add so much data that the object must be copied into a new chunk.
2275
2276While the obstack is in use for a growing object, you cannot use it for
2277ordinary allocation of another object. If you try to do so, the space
2278already added to the growing object will become part of the other object.
2279
2280@comment obstack.h
2281@comment GNU
2282@deftypefun void obstack_blank (struct obstack *@var{obstack-ptr}, int @var{size})
2283@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{} @acsmem{}}}
2284@c obstack_blank @mtsrace:obstack-ptr @acucorrupt @acsmem
2285@c _obstack_newchunk @mtsrace:obstack-ptr @acucorrupt @acsmem
2286@c *chunkfun dup @acsmem
2287@c *obstack_alloc_failed_handler dup user-supplied
2288@c *freefun
2289@c obstack_blank_fast dup @mtsrace:obstack-ptr
2290The most basic function for adding to a growing object is
2291@code{obstack_blank}, which adds space without initializing it.
2292@end deftypefun
2293
2294@comment obstack.h
2295@comment GNU
2296@deftypefun void obstack_grow (struct obstack *@var{obstack-ptr}, void *@var{data}, int @var{size})
2297@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{} @acsmem{}}}
2298@c obstack_grow @mtsrace:obstack-ptr @acucorrupt @acsmem
2299@c _obstack_newchunk dup @mtsrace:obstack-ptr @acucorrupt @acsmem
2300@c memcpy ok
2301To add a block of initialized space, use @code{obstack_grow}, which is
2302the growing-object analogue of @code{obstack_copy}. It adds @var{size}
2303bytes of data to the growing object, copying the contents from
2304@var{data}.
2305@end deftypefun
2306
2307@comment obstack.h
2308@comment GNU
2309@deftypefun void obstack_grow0 (struct obstack *@var{obstack-ptr}, void *@var{data}, int @var{size})
2310@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{} @acsmem{}}}
2311@c obstack_grow0 @mtsrace:obstack-ptr @acucorrupt @acsmem
2312@c (no sequence point between storing NUL and incrementing next_free)
2313@c (multiple changes to next_free => @acucorrupt)
2314@c _obstack_newchunk dup @mtsrace:obstack-ptr @acucorrupt @acsmem
2315@c memcpy ok
2316This is the growing-object analogue of @code{obstack_copy0}. It adds
2317@var{size} bytes copied from @var{data}, followed by an additional null
2318character.
2319@end deftypefun
2320
2321@comment obstack.h
2322@comment GNU
2323@deftypefun void obstack_1grow (struct obstack *@var{obstack-ptr}, char @var{c})
2324@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{} @acsmem{}}}
2325@c obstack_1grow @mtsrace:obstack-ptr @acucorrupt @acsmem
2326@c _obstack_newchunk dup @mtsrace:obstack-ptr @acucorrupt @acsmem
2327@c obstack_1grow_fast dup @mtsrace:obstack-ptr @acucorrupt @acsmem
2328To add one character at a time, use the function @code{obstack_1grow}.
2329It adds a single byte containing @var{c} to the growing object.
2330@end deftypefun
2331
2332@comment obstack.h
2333@comment GNU
2334@deftypefun void obstack_ptr_grow (struct obstack *@var{obstack-ptr}, void *@var{data})
2335@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{} @acsmem{}}}
2336@c obstack_ptr_grow @mtsrace:obstack-ptr @acucorrupt @acsmem
2337@c _obstack_newchunk dup @mtsrace:obstack-ptr @acucorrupt @acsmem
2338@c obstack_ptr_grow_fast dup @mtsrace:obstack-ptr
2339Adding the value of a pointer one can use the function
2340@code{obstack_ptr_grow}. It adds @code{sizeof (void *)} bytes
2341containing the value of @var{data}.
2342@end deftypefun
2343
2344@comment obstack.h
2345@comment GNU
2346@deftypefun void obstack_int_grow (struct obstack *@var{obstack-ptr}, int @var{data})
2347@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{} @acsmem{}}}
2348@c obstack_int_grow @mtsrace:obstack-ptr @acucorrupt @acsmem
2349@c _obstack_newchunk dup @mtsrace:obstack-ptr @acucorrupt @acsmem
2350@c obstack_int_grow_fast dup @mtsrace:obstack-ptr
2351A single value of type @code{int} can be added by using the
2352@code{obstack_int_grow} function. It adds @code{sizeof (int)} bytes to
2353the growing object and initializes them with the value of @var{data}.
2354@end deftypefun
2355
2356@comment obstack.h
2357@comment GNU
2358@deftypefun {void *} obstack_finish (struct obstack *@var{obstack-ptr})
2359@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{}}}
2360@c obstack_finish @mtsrace:obstack-ptr @acucorrupt
2361When you are finished growing the object, use the function
2362@code{obstack_finish} to close it off and return its final address.
2363
2364Once you have finished the object, the obstack is available for ordinary
2365allocation or for growing another object.
2366
2367This function can return a null pointer under the same conditions as
2368@code{obstack_alloc} (@pxref{Allocation in an Obstack}).
2369@end deftypefun
2370
2371When you build an object by growing it, you will probably need to know
2372afterward how long it became. You need not keep track of this as you grow
2373the object, because you can find out the length from the obstack just
2374before finishing the object with the function @code{obstack_object_size},
2375declared as follows:
2376
2377@comment obstack.h
2378@comment GNU
2379@deftypefun int obstack_object_size (struct obstack *@var{obstack-ptr})
2380@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acsafe{}}
2381This function returns the current size of the growing object, in bytes.
2382Remember to call this function @emph{before} finishing the object.
2383After it is finished, @code{obstack_object_size} will return zero.
2384@end deftypefun
2385
2386If you have started growing an object and wish to cancel it, you should
2387finish it and then free it, like this:
2388
2389@smallexample
2390obstack_free (obstack_ptr, obstack_finish (obstack_ptr));
2391@end smallexample
2392
2393@noindent
2394This has no effect if no object was growing.
2395
2396@cindex shrinking objects
2397You can use @code{obstack_blank} with a negative size argument to make
2398the current object smaller. Just don't try to shrink it beyond zero
2399length---there's no telling what will happen if you do that.
2400
2401@node Extra Fast Growing
2402@subsubsection Extra Fast Growing Objects
2403@cindex efficiency and obstacks
2404
2405The usual functions for growing objects incur overhead for checking
2406whether there is room for the new growth in the current chunk. If you
2407are frequently constructing objects in small steps of growth, this
2408overhead can be significant.
2409
2410You can reduce the overhead by using special ``fast growth''
2411functions that grow the object without checking. In order to have a
2412robust program, you must do the checking yourself. If you do this checking
2413in the simplest way each time you are about to add data to the object, you
2414have not saved anything, because that is what the ordinary growth
2415functions do. But if you can arrange to check less often, or check
2416more efficiently, then you make the program faster.
2417
2418The function @code{obstack_room} returns the amount of room available
2419in the current chunk. It is declared as follows:
2420
2421@comment obstack.h
2422@comment GNU
2423@deftypefun int obstack_room (struct obstack *@var{obstack-ptr})
2424@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acsafe{}}
2425This returns the number of bytes that can be added safely to the current
2426growing object (or to an object about to be started) in obstack
2427@var{obstack} using the fast growth functions.
2428@end deftypefun
2429
2430While you know there is room, you can use these fast growth functions
2431for adding data to a growing object:
2432
2433@comment obstack.h
2434@comment GNU
2435@deftypefun void obstack_1grow_fast (struct obstack *@var{obstack-ptr}, char @var{c})
2436@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acunsafe{@acucorrupt{} @acsmem{}}}
2437@c obstack_1grow_fast @mtsrace:obstack-ptr @acucorrupt @acsmem
2438@c (no sequence point between copying c and incrementing next_free)
2439The function @code{obstack_1grow_fast} adds one byte containing the
2440character @var{c} to the growing object in obstack @var{obstack-ptr}.
2441@end deftypefun
2442
2443@comment obstack.h
2444@comment GNU
2445@deftypefun void obstack_ptr_grow_fast (struct obstack *@var{obstack-ptr}, void *@var{data})
2446@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acsafe{}}
2447@c obstack_ptr_grow_fast @mtsrace:obstack-ptr
2448The function @code{obstack_ptr_grow_fast} adds @code{sizeof (void *)}
2449bytes containing the value of @var{data} to the growing object in
2450obstack @var{obstack-ptr}.
2451@end deftypefun
2452
2453@comment obstack.h
2454@comment GNU
2455@deftypefun void obstack_int_grow_fast (struct obstack *@var{obstack-ptr}, int @var{data})
2456@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acsafe{}}
2457@c obstack_int_grow_fast @mtsrace:obstack-ptr
2458The function @code{obstack_int_grow_fast} adds @code{sizeof (int)} bytes
2459containing the value of @var{data} to the growing object in obstack
2460@var{obstack-ptr}.
2461@end deftypefun
2462
2463@comment obstack.h
2464@comment GNU
2465@deftypefun void obstack_blank_fast (struct obstack *@var{obstack-ptr}, int @var{size})
2466@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acsafe{}}
2467@c obstack_blank_fast @mtsrace:obstack-ptr
2468The function @code{obstack_blank_fast} adds @var{size} bytes to the
2469growing object in obstack @var{obstack-ptr} without initializing them.
2470@end deftypefun
2471
2472When you check for space using @code{obstack_room} and there is not
2473enough room for what you want to add, the fast growth functions
2474are not safe. In this case, simply use the corresponding ordinary
2475growth function instead. Very soon this will copy the object to a
2476new chunk; then there will be lots of room available again.
2477
2478So, each time you use an ordinary growth function, check afterward for
2479sufficient space using @code{obstack_room}. Once the object is copied
2480to a new chunk, there will be plenty of space again, so the program will
2481start using the fast growth functions again.
2482
2483Here is an example:
2484
2485@smallexample
2486@group
2487void
2488add_string (struct obstack *obstack, const char *ptr, int len)
2489@{
2490 while (len > 0)
2491 @{
2492 int room = obstack_room (obstack);
2493 if (room == 0)
2494 @{
2495 /* @r{Not enough room. Add one character slowly,}
2496 @r{which may copy to a new chunk and make room.} */
2497 obstack_1grow (obstack, *ptr++);
2498 len--;
2499 @}
2500 else
2501 @{
2502 if (room > len)
2503 room = len;
2504 /* @r{Add fast as much as we have room for.} */
2505 len -= room;
2506 while (room-- > 0)
2507 obstack_1grow_fast (obstack, *ptr++);
2508 @}
2509 @}
2510@}
2511@end group
2512@end smallexample
2513
2514@node Status of an Obstack
2515@subsubsection Status of an Obstack
2516@cindex obstack status
2517@cindex status of obstack
2518
2519Here are functions that provide information on the current status of
2520allocation in an obstack. You can use them to learn about an object while
2521still growing it.
2522
2523@comment obstack.h
2524@comment GNU
2525@deftypefun {void *} obstack_base (struct obstack *@var{obstack-ptr})
2526@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acsafe{}}
2527This function returns the tentative address of the beginning of the
2528currently growing object in @var{obstack-ptr}. If you finish the object
2529immediately, it will have that address. If you make it larger first, it
2530may outgrow the current chunk---then its address will change!
2531
2532If no object is growing, this value says where the next object you
2533allocate will start (once again assuming it fits in the current
2534chunk).
2535@end deftypefun
2536
2537@comment obstack.h
2538@comment GNU
2539@deftypefun {void *} obstack_next_free (struct obstack *@var{obstack-ptr})
2540@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acsafe{}}
2541This function returns the address of the first free byte in the current
2542chunk of obstack @var{obstack-ptr}. This is the end of the currently
2543growing object. If no object is growing, @code{obstack_next_free}
2544returns the same value as @code{obstack_base}.
2545@end deftypefun
2546
2547@comment obstack.h
2548@comment GNU
2549@deftypefun int obstack_object_size (struct obstack *@var{obstack-ptr})
2550@c dup
2551@safety{@prelim{}@mtsafe{@mtsrace{:obstack-ptr}}@assafe{}@acsafe{}}
2552This function returns the size in bytes of the currently growing object.
2553This is equivalent to
2554
2555@smallexample
2556obstack_next_free (@var{obstack-ptr}) - obstack_base (@var{obstack-ptr})
2557@end smallexample
2558@end deftypefun
2559
2560@node Obstacks Data Alignment
2561@subsubsection Alignment of Data in Obstacks
2562@cindex alignment (in obstacks)
2563
2564Each obstack has an @dfn{alignment boundary}; each object allocated in
2565the obstack automatically starts on an address that is a multiple of the
2566specified boundary. By default, this boundary is aligned so that
2567the object can hold any type of data.
2568
2569To access an obstack's alignment boundary, use the macro
2570@code{obstack_alignment_mask}, whose function prototype looks like
2571this:
2572
2573@comment obstack.h
2574@comment GNU
2575@deftypefn Macro int obstack_alignment_mask (struct obstack *@var{obstack-ptr})
2576@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2577The value is a bit mask; a bit that is 1 indicates that the corresponding
2578bit in the address of an object should be 0. The mask value should be one
2579less than a power of 2; the effect is that all object addresses are
2580multiples of that power of 2. The default value of the mask is a value
2581that allows aligned objects to hold any type of data: for example, if
2582its value is 3, any type of data can be stored at locations whose
2583addresses are multiples of 4. A mask value of 0 means an object can start
2584on any multiple of 1 (that is, no alignment is required).
2585
2586The expansion of the macro @code{obstack_alignment_mask} is an lvalue,
2587so you can alter the mask by assignment. For example, this statement:
2588
2589@smallexample
2590obstack_alignment_mask (obstack_ptr) = 0;
2591@end smallexample
2592
2593@noindent
2594has the effect of turning off alignment processing in the specified obstack.
2595@end deftypefn
2596
2597Note that a change in alignment mask does not take effect until
2598@emph{after} the next time an object is allocated or finished in the
2599obstack. If you are not growing an object, you can make the new
2600alignment mask take effect immediately by calling @code{obstack_finish}.
2601This will finish a zero-length object and then do proper alignment for
2602the next object.
2603
2604@node Obstack Chunks
2605@subsubsection Obstack Chunks
2606@cindex efficiency of chunks
2607@cindex chunks
2608
2609Obstacks work by allocating space for themselves in large chunks, and
2610then parceling out space in the chunks to satisfy your requests. Chunks
2611are normally 4096 bytes long unless you specify a different chunk size.
2612The chunk size includes 8 bytes of overhead that are not actually used
2613for storing objects. Regardless of the specified size, longer chunks
2614will be allocated when necessary for long objects.
2615
2616The obstack library allocates chunks by calling the function
2617@code{obstack_chunk_alloc}, which you must define. When a chunk is no
2618longer needed because you have freed all the objects in it, the obstack
2619library frees the chunk by calling @code{obstack_chunk_free}, which you
2620must also define.
2621
2622These two must be defined (as macros) or declared (as functions) in each
2623source file that uses @code{obstack_init} (@pxref{Creating Obstacks}).
2624Most often they are defined as macros like this:
2625
2626@smallexample
2627#define obstack_chunk_alloc malloc
2628#define obstack_chunk_free free
2629@end smallexample
2630
2631Note that these are simple macros (no arguments). Macro definitions with
2632arguments will not work! It is necessary that @code{obstack_chunk_alloc}
2633or @code{obstack_chunk_free}, alone, expand into a function name if it is
2634not itself a function name.
2635
2636If you allocate chunks with @code{malloc}, the chunk size should be a
2637power of 2. The default chunk size, 4096, was chosen because it is long
2638enough to satisfy many typical requests on the obstack yet short enough
2639not to waste too much memory in the portion of the last chunk not yet used.
2640
2641@comment obstack.h
2642@comment GNU
2643@deftypefn Macro int obstack_chunk_size (struct obstack *@var{obstack-ptr})
2644@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2645This returns the chunk size of the given obstack.
2646@end deftypefn
2647
2648Since this macro expands to an lvalue, you can specify a new chunk size by
2649assigning it a new value. Doing so does not affect the chunks already
2650allocated, but will change the size of chunks allocated for that particular
2651obstack in the future. It is unlikely to be useful to make the chunk size
2652smaller, but making it larger might improve efficiency if you are
2653allocating many objects whose size is comparable to the chunk size. Here
2654is how to do so cleanly:
2655
2656@smallexample
2657if (obstack_chunk_size (obstack_ptr) < @var{new-chunk-size})
2658 obstack_chunk_size (obstack_ptr) = @var{new-chunk-size};
2659@end smallexample
2660
2661@node Summary of Obstacks
2662@subsubsection Summary of Obstack Functions
2663
2664Here is a summary of all the functions associated with obstacks. Each
2665takes the address of an obstack (@code{struct obstack *}) as its first
2666argument.
2667
2668@table @code
2669@item void obstack_init (struct obstack *@var{obstack-ptr})
2670Initialize use of an obstack. @xref{Creating Obstacks}.
2671
2672@item void *obstack_alloc (struct obstack *@var{obstack-ptr}, int @var{size})
2673Allocate an object of @var{size} uninitialized bytes.
2674@xref{Allocation in an Obstack}.
2675
2676@item void *obstack_copy (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
2677Allocate an object of @var{size} bytes, with contents copied from
2678@var{address}. @xref{Allocation in an Obstack}.
2679
2680@item void *obstack_copy0 (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
2681Allocate an object of @var{size}+1 bytes, with @var{size} of them copied
2682from @var{address}, followed by a null character at the end.
2683@xref{Allocation in an Obstack}.
2684
2685@item void obstack_free (struct obstack *@var{obstack-ptr}, void *@var{object})
2686Free @var{object} (and everything allocated in the specified obstack
2687more recently than @var{object}). @xref{Freeing Obstack Objects}.
2688
2689@item void obstack_blank (struct obstack *@var{obstack-ptr}, int @var{size})
2690Add @var{size} uninitialized bytes to a growing object.
2691@xref{Growing Objects}.
2692
2693@item void obstack_grow (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
2694Add @var{size} bytes, copied from @var{address}, to a growing object.
2695@xref{Growing Objects}.
2696
2697@item void obstack_grow0 (struct obstack *@var{obstack-ptr}, void *@var{address}, int @var{size})
2698Add @var{size} bytes, copied from @var{address}, to a growing object,
2699and then add another byte containing a null character. @xref{Growing
2700Objects}.
2701
2702@item void obstack_1grow (struct obstack *@var{obstack-ptr}, char @var{data-char})
2703Add one byte containing @var{data-char} to a growing object.
2704@xref{Growing Objects}.
2705
2706@item void *obstack_finish (struct obstack *@var{obstack-ptr})
2707Finalize the object that is growing and return its permanent address.
2708@xref{Growing Objects}.
2709
2710@item int obstack_object_size (struct obstack *@var{obstack-ptr})
2711Get the current size of the currently growing object. @xref{Growing
2712Objects}.
2713
2714@item void obstack_blank_fast (struct obstack *@var{obstack-ptr}, int @var{size})
2715Add @var{size} uninitialized bytes to a growing object without checking
2716that there is enough room. @xref{Extra Fast Growing}.
2717
2718@item void obstack_1grow_fast (struct obstack *@var{obstack-ptr}, char @var{data-char})
2719Add one byte containing @var{data-char} to a growing object without
2720checking that there is enough room. @xref{Extra Fast Growing}.
2721
2722@item int obstack_room (struct obstack *@var{obstack-ptr})
2723Get the amount of room now available for growing the current object.
2724@xref{Extra Fast Growing}.
2725
2726@item int obstack_alignment_mask (struct obstack *@var{obstack-ptr})
2727The mask used for aligning the beginning of an object. This is an
2728lvalue. @xref{Obstacks Data Alignment}.
2729
2730@item int obstack_chunk_size (struct obstack *@var{obstack-ptr})
2731The size for allocating chunks. This is an lvalue. @xref{Obstack Chunks}.
2732
2733@item void *obstack_base (struct obstack *@var{obstack-ptr})
2734Tentative starting address of the currently growing object.
2735@xref{Status of an Obstack}.
2736
2737@item void *obstack_next_free (struct obstack *@var{obstack-ptr})
2738Address just after the end of the currently growing object.
2739@xref{Status of an Obstack}.
2740@end table
2741
2742@node Variable Size Automatic
2743@subsection Automatic Storage with Variable Size
2744@cindex automatic freeing
2745@cindex @code{alloca} function
2746@cindex automatic storage with variable size
2747
2748The function @code{alloca} supports a kind of half-dynamic allocation in
2749which blocks are allocated dynamically but freed automatically.
2750
2751Allocating a block with @code{alloca} is an explicit action; you can
2752allocate as many blocks as you wish, and compute the size at run time. But
2753all the blocks are freed when you exit the function that @code{alloca} was
2754called from, just as if they were automatic variables declared in that
2755function. There is no way to free the space explicitly.
2756
2757The prototype for @code{alloca} is in @file{stdlib.h}. This function is
2758a BSD extension.
2759@pindex stdlib.h
2760
2761@comment stdlib.h
2762@comment GNU, BSD
2763@deftypefun {void *} alloca (size_t @var{size})
2764@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2765The return value of @code{alloca} is the address of a block of @var{size}
2766bytes of memory, allocated in the stack frame of the calling function.
2767@end deftypefun
2768
2769Do not use @code{alloca} inside the arguments of a function call---you
2770will get unpredictable results, because the stack space for the
2771@code{alloca} would appear on the stack in the middle of the space for
2772the function arguments. An example of what to avoid is @code{foo (x,
2773alloca (4), y)}.
2774@c This might get fixed in future versions of GCC, but that won't make
2775@c it safe with compilers generally.
2776
2777@menu
2778* Alloca Example:: Example of using @code{alloca}.
2779* Advantages of Alloca:: Reasons to use @code{alloca}.
2780* Disadvantages of Alloca:: Reasons to avoid @code{alloca}.
2781* GNU C Variable-Size Arrays:: Only in GNU C, here is an alternative
2782 method of allocating dynamically and
2783 freeing automatically.
2784@end menu
2785
2786@node Alloca Example
2787@subsubsection @code{alloca} Example
2788
2789As an example of the use of @code{alloca}, here is a function that opens
2790a file name made from concatenating two argument strings, and returns a
2791file descriptor or minus one signifying failure:
2792
2793@smallexample
2794int
2795open2 (char *str1, char *str2, int flags, int mode)
2796@{
2797 char *name = (char *) alloca (strlen (str1) + strlen (str2) + 1);
2798 stpcpy (stpcpy (name, str1), str2);
2799 return open (name, flags, mode);
2800@}
2801@end smallexample
2802
2803@noindent
2804Here is how you would get the same results with @code{malloc} and
2805@code{free}:
2806
2807@smallexample
2808int
2809open2 (char *str1, char *str2, int flags, int mode)
2810@{
2811 char *name = (char *) malloc (strlen (str1) + strlen (str2) + 1);
2812 int desc;
2813 if (name == 0)
2814 fatal ("virtual memory exceeded");
2815 stpcpy (stpcpy (name, str1), str2);
2816 desc = open (name, flags, mode);
2817 free (name);
2818 return desc;
2819@}
2820@end smallexample
2821
2822As you can see, it is simpler with @code{alloca}. But @code{alloca} has
2823other, more important advantages, and some disadvantages.
2824
2825@node Advantages of Alloca
2826@subsubsection Advantages of @code{alloca}
2827
2828Here are the reasons why @code{alloca} may be preferable to @code{malloc}:
2829
2830@itemize @bullet
2831@item
2832Using @code{alloca} wastes very little space and is very fast. (It is
2833open-coded by the GNU C compiler.)
2834
2835@item
2836Since @code{alloca} does not have separate pools for different sizes of
2837block, space used for any size block can be reused for any other size.
2838@code{alloca} does not cause memory fragmentation.
2839
2840@item
2841@cindex longjmp
2842Nonlocal exits done with @code{longjmp} (@pxref{Non-Local Exits})
2843automatically free the space allocated with @code{alloca} when they exit
2844through the function that called @code{alloca}. This is the most
2845important reason to use @code{alloca}.
2846
2847To illustrate this, suppose you have a function
2848@code{open_or_report_error} which returns a descriptor, like
2849@code{open}, if it succeeds, but does not return to its caller if it
2850fails. If the file cannot be opened, it prints an error message and
2851jumps out to the command level of your program using @code{longjmp}.
2852Let's change @code{open2} (@pxref{Alloca Example}) to use this
2853subroutine:@refill
2854
2855@smallexample
2856int
2857open2 (char *str1, char *str2, int flags, int mode)
2858@{
2859 char *name = (char *) alloca (strlen (str1) + strlen (str2) + 1);
2860 stpcpy (stpcpy (name, str1), str2);
2861 return open_or_report_error (name, flags, mode);
2862@}
2863@end smallexample
2864
2865@noindent
2866Because of the way @code{alloca} works, the memory it allocates is
2867freed even when an error occurs, with no special effort required.
2868
2869By contrast, the previous definition of @code{open2} (which uses
2870@code{malloc} and @code{free}) would develop a memory leak if it were
2871changed in this way. Even if you are willing to make more changes to
2872fix it, there is no easy way to do so.
2873@end itemize
2874
2875@node Disadvantages of Alloca
2876@subsubsection Disadvantages of @code{alloca}
2877
2878@cindex @code{alloca} disadvantages
2879@cindex disadvantages of @code{alloca}
2880These are the disadvantages of @code{alloca} in comparison with
2881@code{malloc}:
2882
2883@itemize @bullet
2884@item
2885If you try to allocate more memory than the machine can provide, you
2886don't get a clean error message. Instead you get a fatal signal like
2887the one you would get from an infinite recursion; probably a
2888segmentation violation (@pxref{Program Error Signals}).
2889
2890@item
2891Some @nongnusystems{} fail to support @code{alloca}, so it is less
2892portable. However, a slower emulation of @code{alloca} written in C
2893is available for use on systems with this deficiency.
2894@end itemize
2895
2896@node GNU C Variable-Size Arrays
2897@subsubsection GNU C Variable-Size Arrays
2898@cindex variable-sized arrays
2899
2900In GNU C, you can replace most uses of @code{alloca} with an array of
2901variable size. Here is how @code{open2} would look then:
2902
2903@smallexample
2904int open2 (char *str1, char *str2, int flags, int mode)
2905@{
2906 char name[strlen (str1) + strlen (str2) + 1];
2907 stpcpy (stpcpy (name, str1), str2);
2908 return open (name, flags, mode);
2909@}
2910@end smallexample
2911
2912But @code{alloca} is not always equivalent to a variable-sized array, for
2913several reasons:
2914
2915@itemize @bullet
2916@item
2917A variable size array's space is freed at the end of the scope of the
2918name of the array. The space allocated with @code{alloca}
2919remains until the end of the function.
2920
2921@item
2922It is possible to use @code{alloca} within a loop, allocating an
2923additional block on each iteration. This is impossible with
2924variable-sized arrays.
2925@end itemize
2926
2927@strong{NB:} If you mix use of @code{alloca} and variable-sized arrays
2928within one function, exiting a scope in which a variable-sized array was
2929declared frees all blocks allocated with @code{alloca} during the
2930execution of that scope.
2931
2932
2933@node Resizing the Data Segment
2934@section Resizing the Data Segment
2935
2936The symbols in this section are declared in @file{unistd.h}.
2937
2938You will not normally use the functions in this section, because the
2939functions described in @ref{Memory Allocation} are easier to use. Those
2940are interfaces to a @glibcadj{} memory allocator that uses the
2941functions below itself. The functions below are simple interfaces to
2942system calls.
2943
2944@comment unistd.h
2945@comment BSD
2946@deftypefun int brk (void *@var{addr})
2947@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2948
2949@code{brk} sets the high end of the calling process' data segment to
2950@var{addr}.
2951
2952The address of the end of a segment is defined to be the address of the
2953last byte in the segment plus 1.
2954
2955The function has no effect if @var{addr} is lower than the low end of
2956the data segment. (This is considered success, by the way).
2957
2958The function fails if it would cause the data segment to overlap another
2959segment or exceed the process' data storage limit (@pxref{Limits on
2960Resources}).
2961
2962The function is named for a common historical case where data storage
2963and the stack are in the same segment. Data storage allocation grows
2964upward from the bottom of the segment while the stack grows downward
2965toward it from the top of the segment and the curtain between them is
2966called the @dfn{break}.
2967
2968The return value is zero on success. On failure, the return value is
2969@code{-1} and @code{errno} is set accordingly. The following @code{errno}
2970values are specific to this function:
2971
2972@table @code
2973@item ENOMEM
2974The request would cause the data segment to overlap another segment or
2975exceed the process' data storage limit.
2976@end table
2977
2978@c The Brk system call in Linux (as opposed to the GNU C Library function)
2979@c is considerably different. It always returns the new end of the data
2980@c segment, whether it succeeds or fails. The GNU C library Brk determines
2981@c it's a failure if and only if the system call returns an address less
2982@c than the address requested.
2983
2984@end deftypefun
2985
2986
2987@comment unistd.h
2988@comment BSD
2989@deftypefun void *sbrk (ptrdiff_t @var{delta})
2990@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2991
2992This function is the same as @code{brk} except that you specify the new
2993end of the data segment as an offset @var{delta} from the current end
2994and on success the return value is the address of the resulting end of
2995the data segment instead of zero.
2996
2997This means you can use @samp{sbrk(0)} to find out what the current end
2998of the data segment is.
2999
3000@end deftypefun
3001
3002
3003
3004@node Locking Pages
3005@section Locking Pages
3006@cindex locking pages
3007@cindex memory lock
3008@cindex paging
3009
3010You can tell the system to associate a particular virtual memory page
3011with a real page frame and keep it that way --- i.e., cause the page to
3012be paged in if it isn't already and mark it so it will never be paged
3013out and consequently will never cause a page fault. This is called
3014@dfn{locking} a page.
3015
3016The functions in this chapter lock and unlock the calling process'
3017pages.
3018
3019@menu
3020* Why Lock Pages:: Reasons to read this section.
3021* Locked Memory Details:: Everything you need to know locked
3022 memory
3023* Page Lock Functions:: Here's how to do it.
3024@end menu
3025
3026@node Why Lock Pages
3027@subsection Why Lock Pages
3028
3029Because page faults cause paged out pages to be paged in transparently,
3030a process rarely needs to be concerned about locking pages. However,
3031there are two reasons people sometimes are:
3032
3033@itemize @bullet
3034
3035@item
3036Speed. A page fault is transparent only insofar as the process is not
3037sensitive to how long it takes to do a simple memory access. Time-critical
3038processes, especially realtime processes, may not be able to wait or
3039may not be able to tolerate variance in execution speed.
3040@cindex realtime processing
3041@cindex speed of execution
3042
3043A process that needs to lock pages for this reason probably also needs
3044priority among other processes for use of the CPU. @xref{Priority}.
3045
3046In some cases, the programmer knows better than the system's demand
3047paging allocator which pages should remain in real memory to optimize
3048system performance. In this case, locking pages can help.
3049
3050@item
3051Privacy. If you keep secrets in virtual memory and that virtual memory
3052gets paged out, that increases the chance that the secrets will get out.
3053If a password gets written out to disk swap space, for example, it might
3054still be there long after virtual and real memory have been wiped clean.
3055
3056@end itemize
3057
3058Be aware that when you lock a page, that's one fewer page frame that can
3059be used to back other virtual memory (by the same or other processes),
3060which can mean more page faults, which means the system runs more
3061slowly. In fact, if you lock enough memory, some programs may not be
3062able to run at all for lack of real memory.
3063
3064@node Locked Memory Details
3065@subsection Locked Memory Details
3066
3067A memory lock is associated with a virtual page, not a real frame. The
3068paging rule is: If a frame backs at least one locked page, don't page it
3069out.
3070
3071Memory locks do not stack. I.e., you can't lock a particular page twice
3072so that it has to be unlocked twice before it is truly unlocked. It is
3073either locked or it isn't.
3074
3075A memory lock persists until the process that owns the memory explicitly
3076unlocks it. (But process termination and exec cause the virtual memory
3077to cease to exist, which you might say means it isn't locked any more).
3078
3079Memory locks are not inherited by child processes. (But note that on a
3080modern Unix system, immediately after a fork, the parent's and the
3081child's virtual address space are backed by the same real page frames,
3082so the child enjoys the parent's locks). @xref{Creating a Process}.
3083
3084Because of its ability to impact other processes, only the superuser can
3085lock a page. Any process can unlock its own page.
3086
3087The system sets limits on the amount of memory a process can have locked
3088and the amount of real memory it can have dedicated to it. @xref{Limits
3089on Resources}.
3090
3091In Linux, locked pages aren't as locked as you might think.
3092Two virtual pages that are not shared memory can nonetheless be backed
3093by the same real frame. The kernel does this in the name of efficiency
3094when it knows both virtual pages contain identical data, and does it
3095even if one or both of the virtual pages are locked.
3096
3097But when a process modifies one of those pages, the kernel must get it a
3098separate frame and fill it with the page's data. This is known as a
3099@dfn{copy-on-write page fault}. It takes a small amount of time and in
3100a pathological case, getting that frame may require I/O.
3101@cindex copy-on-write page fault
3102@cindex page fault, copy-on-write
3103
3104To make sure this doesn't happen to your program, don't just lock the
3105pages. Write to them as well, unless you know you won't write to them
3106ever. And to make sure you have pre-allocated frames for your stack,
3107enter a scope that declares a C automatic variable larger than the
3108maximum stack size you will need, set it to something, then return from
3109its scope.
3110
3111@node Page Lock Functions
3112@subsection Functions To Lock And Unlock Pages
3113
3114The symbols in this section are declared in @file{sys/mman.h}. These
3115functions are defined by POSIX.1b, but their availability depends on
3116your kernel. If your kernel doesn't allow these functions, they exist
3117but always fail. They @emph{are} available with a Linux kernel.
3118
3119@strong{Portability Note:} POSIX.1b requires that when the @code{mlock}
3120and @code{munlock} functions are available, the file @file{unistd.h}
3121define the macro @code{_POSIX_MEMLOCK_RANGE} and the file
3122@code{limits.h} define the macro @code{PAGESIZE} to be the size of a
3123memory page in bytes. It requires that when the @code{mlockall} and
3124@code{munlockall} functions are available, the @file{unistd.h} file
3125define the macro @code{_POSIX_MEMLOCK}. @Theglibc{} conforms to
3126this requirement.
3127
3128@comment sys/mman.h
3129@comment POSIX.1b
3130@deftypefun int mlock (const void *@var{addr}, size_t @var{len})
3131@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3132
3133@code{mlock} locks a range of the calling process' virtual pages.
3134
3135The range of memory starts at address @var{addr} and is @var{len} bytes
3136long. Actually, since you must lock whole pages, it is the range of
3137pages that include any part of the specified range.
3138
3139When the function returns successfully, each of those pages is backed by
3140(connected to) a real frame (is resident) and is marked to stay that
3141way. This means the function may cause page-ins and have to wait for
3142them.
3143
3144When the function fails, it does not affect the lock status of any
3145pages.
3146
3147The return value is zero if the function succeeds. Otherwise, it is
3148@code{-1} and @code{errno} is set accordingly. @code{errno} values
3149specific to this function are:
3150
3151@table @code
3152@item ENOMEM
3153@itemize @bullet
3154@item
3155At least some of the specified address range does not exist in the
3156calling process' virtual address space.
3157@item
3158The locking would cause the process to exceed its locked page limit.
3159@end itemize
3160
3161@item EPERM
3162The calling process is not superuser.
3163
3164@item EINVAL
3165@var{len} is not positive.
3166
3167@item ENOSYS
3168The kernel does not provide @code{mlock} capability.
3169
3170@end table
3171
3172You can lock @emph{all} a process' memory with @code{mlockall}. You
3173unlock memory with @code{munlock} or @code{munlockall}.
3174
3175To avoid all page faults in a C program, you have to use
3176@code{mlockall}, because some of the memory a program uses is hidden
3177from the C code, e.g. the stack and automatic variables, and you
3178wouldn't know what address to tell @code{mlock}.
3179
3180@end deftypefun
3181
3182@comment sys/mman.h
3183@comment POSIX.1b
3184@deftypefun int munlock (const void *@var{addr}, size_t @var{len})
3185@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3186
3187@code{munlock} unlocks a range of the calling process' virtual pages.
3188
3189@code{munlock} is the inverse of @code{mlock} and functions completely
3190analogously to @code{mlock}, except that there is no @code{EPERM}
3191failure.
3192
3193@end deftypefun
3194
3195@comment sys/mman.h
3196@comment POSIX.1b
3197@deftypefun int mlockall (int @var{flags})
3198@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3199
3200@code{mlockall} locks all the pages in a process' virtual memory address
3201space, and/or any that are added to it in the future. This includes the
3202pages of the code, data and stack segment, as well as shared libraries,
3203user space kernel data, shared memory, and memory mapped files.
3204
3205@var{flags} is a string of single bit flags represented by the following
3206macros. They tell @code{mlockall} which of its functions you want. All
3207other bits must be zero.
3208
3209@table @code
3210
3211@item MCL_CURRENT
3212Lock all pages which currently exist in the calling process' virtual
3213address space.
3214
3215@item MCL_FUTURE
3216Set a mode such that any pages added to the process' virtual address
3217space in the future will be locked from birth. This mode does not
3218affect future address spaces owned by the same process so exec, which
3219replaces a process' address space, wipes out @code{MCL_FUTURE}.
3220@xref{Executing a File}.
3221
3222@end table
3223
3224When the function returns successfully, and you specified
3225@code{MCL_CURRENT}, all of the process' pages are backed by (connected
3226to) real frames (they are resident) and are marked to stay that way.
3227This means the function may cause page-ins and have to wait for them.
3228
3229When the process is in @code{MCL_FUTURE} mode because it successfully
3230executed this function and specified @code{MCL_CURRENT}, any system call
3231by the process that requires space be added to its virtual address space
3232fails with @code{errno} = @code{ENOMEM} if locking the additional space
3233would cause the process to exceed its locked page limit. In the case
3234that the address space addition that can't be accommodated is stack
3235expansion, the stack expansion fails and the kernel sends a
3236@code{SIGSEGV} signal to the process.
3237
3238When the function fails, it does not affect the lock status of any pages
3239or the future locking mode.
3240
3241The return value is zero if the function succeeds. Otherwise, it is
3242@code{-1} and @code{errno} is set accordingly. @code{errno} values
3243specific to this function are:
3244
3245@table @code
3246@item ENOMEM
3247@itemize @bullet
3248@item
3249At least some of the specified address range does not exist in the
3250calling process' virtual address space.
3251@item
3252The locking would cause the process to exceed its locked page limit.
3253@end itemize
3254
3255@item EPERM
3256The calling process is not superuser.
3257
3258@item EINVAL
3259Undefined bits in @var{flags} are not zero.
3260
3261@item ENOSYS
3262The kernel does not provide @code{mlockall} capability.
3263
3264@end table
3265
3266You can lock just specific pages with @code{mlock}. You unlock pages
3267with @code{munlockall} and @code{munlock}.
3268
3269@end deftypefun
3270
3271
3272@comment sys/mman.h
3273@comment POSIX.1b
3274@deftypefun int munlockall (void)
3275@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3276
3277@code{munlockall} unlocks every page in the calling process' virtual
3278address space and turn off @code{MCL_FUTURE} future locking mode.
3279
3280The return value is zero if the function succeeds. Otherwise, it is
3281@code{-1} and @code{errno} is set accordingly. The only way this
3282function can fail is for generic reasons that all functions and system
3283calls can fail, so there are no specific @code{errno} values.
3284
3285@end deftypefun
3286
3287
3288
3289
3290@ignore
3291@c This was never actually implemented. -zw
3292@node Relocating Allocator
3293@section Relocating Allocator
3294
3295@cindex relocating memory allocator
3296Any system of dynamic memory allocation has overhead: the amount of
3297space it uses is more than the amount the program asks for. The
3298@dfn{relocating memory allocator} achieves very low overhead by moving
3299blocks in memory as necessary, on its own initiative.
3300
3301@c @menu
3302@c * Relocator Concepts:: How to understand relocating allocation.
3303@c * Using Relocator:: Functions for relocating allocation.
3304@c @end menu
3305
3306@node Relocator Concepts
3307@subsection Concepts of Relocating Allocation
3308
3309@ifinfo
3310The @dfn{relocating memory allocator} achieves very low overhead by
3311moving blocks in memory as necessary, on its own initiative.
3312@end ifinfo
3313
3314When you allocate a block with @code{malloc}, the address of the block
3315never changes unless you use @code{realloc} to change its size. Thus,
3316you can safely store the address in various places, temporarily or
3317permanently, as you like. This is not safe when you use the relocating
3318memory allocator, because any and all relocatable blocks can move
3319whenever you allocate memory in any fashion. Even calling @code{malloc}
3320or @code{realloc} can move the relocatable blocks.
3321
3322@cindex handle
3323For each relocatable block, you must make a @dfn{handle}---a pointer
3324object in memory, designated to store the address of that block. The
3325relocating allocator knows where each block's handle is, and updates the
3326address stored there whenever it moves the block, so that the handle
3327always points to the block. Each time you access the contents of the
3328block, you should fetch its address anew from the handle.
3329
3330To call any of the relocating allocator functions from a signal handler
3331is almost certainly incorrect, because the signal could happen at any
3332time and relocate all the blocks. The only way to make this safe is to
3333block the signal around any access to the contents of any relocatable
3334block---not a convenient mode of operation. @xref{Nonreentrancy}.
3335
3336@node Using Relocator
3337@subsection Allocating and Freeing Relocatable Blocks
3338
3339@pindex malloc.h
3340In the descriptions below, @var{handleptr} designates the address of the
3341handle. All the functions are declared in @file{malloc.h}; all are GNU
3342extensions.
3343
3344@comment malloc.h
3345@comment GNU
3346@c @deftypefun {void *} r_alloc (void **@var{handleptr}, size_t @var{size})
3347This function allocates a relocatable block of size @var{size}. It
3348stores the block's address in @code{*@var{handleptr}} and returns
3349a non-null pointer to indicate success.
3350
3351If @code{r_alloc} can't get the space needed, it stores a null pointer
3352in @code{*@var{handleptr}}, and returns a null pointer.
3353@end deftypefun
3354
3355@comment malloc.h
3356@comment GNU
3357@c @deftypefun void r_alloc_free (void **@var{handleptr})
3358This function is the way to free a relocatable block. It frees the
3359block that @code{*@var{handleptr}} points to, and stores a null pointer
3360in @code{*@var{handleptr}} to show it doesn't point to an allocated
3361block any more.
3362@end deftypefun
3363
3364@comment malloc.h
3365@comment GNU
3366@c @deftypefun {void *} r_re_alloc (void **@var{handleptr}, size_t @var{size})
3367The function @code{r_re_alloc} adjusts the size of the block that
3368@code{*@var{handleptr}} points to, making it @var{size} bytes long. It
3369stores the address of the resized block in @code{*@var{handleptr}} and
3370returns a non-null pointer to indicate success.
3371
3372If enough memory is not available, this function returns a null pointer
3373and does not modify @code{*@var{handleptr}}.
3374@end deftypefun
3375@end ignore
3376
3377
3378
3379
3380@ignore
3381@comment No longer available...
3382
3383@comment @node Memory Warnings
3384@comment @section Memory Usage Warnings
3385@comment @cindex memory usage warnings
3386@comment @cindex warnings of memory almost full
3387
3388@pindex malloc.c
3389You can ask for warnings as the program approaches running out of memory
3390space, by calling @code{memory_warnings}. This tells @code{malloc} to
3391check memory usage every time it asks for more memory from the operating
3392system. This is a GNU extension declared in @file{malloc.h}.
3393
3394@comment malloc.h
3395@comment GNU
3396@comment @deftypefun void memory_warnings (void *@var{start}, void (*@var{warn-func}) (const char *))
3397Call this function to request warnings for nearing exhaustion of virtual
3398memory.
3399
3400The argument @var{start} says where data space begins, in memory. The
3401allocator compares this against the last address used and against the
3402limit of data space, to determine the fraction of available memory in
3403use. If you supply zero for @var{start}, then a default value is used
3404which is right in most circumstances.
3405
3406For @var{warn-func}, supply a function that @code{malloc} can call to
3407warn you. It is called with a string (a warning message) as argument.
3408Normally it ought to display the string for the user to read.
3409@end deftypefun
3410
3411The warnings come when memory becomes 75% full, when it becomes 85%
3412full, and when it becomes 95% full. Above 95% you get another warning
3413each time memory usage increases.
3414
3415@end ignore