forked from bminor/glibc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.texi
2803 lines (2281 loc) · 105 KB
/
memory.texi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@node Memory, Character Handling, Error Reporting, Top
@chapter Virtual Memory Allocation And Paging
@c %MENU% Allocating virtual memory and controlling paging
@cindex memory allocation
@cindex storage allocation
This chapter describes how processes manage and use memory in a system
that uses the GNU C library.
The GNU C Library has several functions for dynamically allocating
virtual memory in various ways. They vary in generality and in
efficiency. The library also provides functions for controlling paging
and allocation of real memory.
@menu
* Memory Concepts:: An introduction to concepts and terminology.
* Memory Allocation:: Allocating storage for your program data
* Locking Pages:: Preventing page faults
* Resizing the Data Segment:: @code{brk}, @code{sbrk}
@end menu
Memory mapped I/O is not discussed in this chapter. @xref{Memory-mapped I/O}.
@node Memory Concepts
@section Process Memory Concepts
One of the most basic resources a process has available to it is memory.
There are a lot of different ways systems organize memory, but in a
typical one, each process has one linear virtual address space, with
addresses running from zero to some huge maximum. It need not be
contiguous; i.e. not all of these addresses actually can be used to
store data.
The virtual memory is divided into pages (4 kilobytes is typical).
Backing each page of virtual memory is a page of real memory (called a
@dfn{frame}) or some secondary storage, usually disk space. The disk
space might be swap space or just some ordinary disk file. Actually, a
page of all zeroes sometimes has nothing at all backing it -- there's
just a flag saying it is all zeroes.
@cindex page frame
@cindex frame, real memory
@cindex swap space
@cindex page, virtual memory
The same frame of real memory or backing store can back multiple virtual
pages belonging to multiple processes. This is normally the case, for
example, with virtual memory occupied by GNU C library code. The same
real memory frame containing the @code{printf} function backs a virtual
memory page in each of the existing processes that has a @code{printf}
call in its program.
In order for a program to access any part of a virtual page, the page
must at that moment be backed by (``connected to'') a real frame. But
because there is usually a lot more virtual memory than real memory, the
pages must move back and forth between real memory and backing store
regularly, coming into real memory when a process needs to access them
and then retreating to backing store when not needed anymore. This
movement is called @dfn{paging}.
When a program attempts to access a page which is not at that moment
backed by real memory, this is known as a @dfn{page fault}. When a page
fault occurs, the kernel suspends the process, places the page into a
real page frame (this is called ``paging in'' or ``faulting in''), then
resumes the process so that from the process' point of view, the page
was in real memory all along. In fact, to the process, all pages always
seem to be in real memory. Except for one thing: the elapsed execution
time of an instruction that would normally be a few nanoseconds is
suddenly much, much, longer (because the kernel normally has to do I/O
to complete the page-in). For programs sensitive to that, the functions
described in @ref{Locking Pages} can control it.
@cindex page fault
@cindex paging
Within each virtual address space, a process has to keep track of what
is at which addresses, and that process is called memory allocation.
Allocation usually brings to mind meting out scarce resources, but in
the case of virtual memory, that's not a major goal, because there is
generally much more of it than anyone needs. Memory allocation within a
process is mainly just a matter of making sure that the same byte of
memory isn't used to store two different things.
Processes allocate memory in two major ways: by exec and
programmatically. Actually, forking is a third way, but it's not very
interesting. @xref{Creating a Process}.
Exec is the operation of creating a virtual address space for a process,
loading its basic program into it, and executing the program. It is
done by the ``exec'' family of functions (e.g. @code{execl}). The
operation takes a program file (an executable), it allocates space to
load all the data in the executable, loads it, and transfers control to
it. That data is most notably the instructions of the program (the
@dfn{text}), but also literals and constants in the program and even
some variables: C variables with the static storage class (@pxref{Memory
Allocation and C}).
@cindex executable
@cindex literals
@cindex constants
Once that program begins to execute, it uses programmatic allocation to
gain additional memory. In a C program with the GNU C library, there
are two kinds of programmatic allocation: automatic and dynamic.
@xref{Memory Allocation and C}.
Memory-mapped I/O is another form of dynamic virtual memory allocation.
Mapping memory to a file means declaring that the contents of certain
range of a process' addresses shall be identical to the contents of a
specified regular file. The system makes the virtual memory initially
contain the contents of the file, and if you modify the memory, the
system writes the same modification to the file. Note that due to the
magic of virtual memory and page faults, there is no reason for the
system to do I/O to read the file, or allocate real memory for its
contents, until the program accesses the virtual memory.
@xref{Memory-mapped I/O}.
@cindex memory mapped I/O
@cindex memory mapped file
@cindex files, accessing
Just as it programmatically allocates memory, the program can
programmatically deallocate (@dfn{free}) it. You can't free the memory
that was allocated by exec. When the program exits or execs, you might
say that all its memory gets freed, but since in both cases the address
space ceases to exist, the point is really moot. @xref{Program
Termination}.
@cindex execing a program
@cindex freeing memory
@cindex exiting a program
A process' virtual address space is divided into segments. A segment is
a contiguous range of virtual addresses. Three important segments are:
@itemize @bullet
@item
The @dfn{text segment} contains a program's instructions and literals and
static constants. It is allocated by exec and stays the same size for
the life of the virtual address space.
@item
The @dfn{data segment} is working storage for the program. It can be
preallocated and preloaded by exec and the process can extend or shrink
it by calling functions as described in @xref{Resizing the Data
Segment}. Its lower end is fixed.
@item
The @dfn{stack segment} contains a program stack. It grows as the stack
grows, but doesn't shrink when the stack shrinks.
@end itemize
@node Memory Allocation
@section Allocating Storage For Program Data
This section covers how ordinary programs manage storage for their data,
including the famous @code{malloc} function and some fancier facilities
special the GNU C library and GNU Compiler.
@menu
* Memory Allocation and C:: How to get different kinds of allocation in C.
* Unconstrained Allocation:: The @code{malloc} facility allows fully general
dynamic allocation.
* Allocation Debugging:: Finding memory leaks and not freed memory.
* Obstacks:: Obstacks are less general than malloc
but more efficient and convenient.
* Variable Size Automatic:: Allocation of variable-sized blocks
of automatic storage that are freed when the
calling function returns.
@end menu
@node Memory Allocation and C
@subsection Memory Allocation in C Programs
The C language supports two kinds of memory allocation through the
variables in C programs:
@itemize @bullet
@item
@dfn{Static allocation} is what happens when you declare a static or
global variable. Each static or global variable defines one block of
space, of a fixed size. The space is allocated once, when your program
is started (part of the exec operation), and is never freed.
@cindex static memory allocation
@cindex static storage class
@item
@dfn{Automatic allocation} happens when you declare an automatic
variable, such as a function argument or a local variable. The space
for an automatic variable is allocated when the compound statement
containing the declaration is entered, and is freed when that
compound statement is exited.
@cindex automatic memory allocation
@cindex automatic storage class
In GNU C, the size of the automatic storage can be an expression
that varies. In other C implementations, it must be a constant.
@end itemize
A third important kind of memory allocation, @dfn{dynamic allocation},
is not supported by C variables but is available via GNU C library
functions.
@cindex dynamic memory allocation
@subsubsection Dynamic Memory Allocation
@cindex dynamic memory allocation
@dfn{Dynamic memory allocation} is a technique in which programs
determine as they are running where to store some information. You need
dynamic allocation when the amount of memory you need, or how long you
continue to need it, depends on factors that are not known before the
program runs.
For example, you may need a block to store a line read from an input
file; since there is no limit to how long a line can be, you must
allocate the memory dynamically and make it dynamically larger as you
read more of the line.
Or, you may need a block for each record or each definition in the input
data; since you can't know in advance how many there will be, you must
allocate a new block for each record or definition as you read it.
When you use dynamic allocation, the allocation of a block of memory is
an action that the program requests explicitly. You call a function or
macro when you want to allocate space, and specify the size with an
argument. If you want to free the space, you do so by calling another
function or macro. You can do these things whenever you want, as often
as you want.
Dynamic allocation is not supported by C variables; there is no storage
class ``dynamic'', and there can never be a C variable whose value is
stored in dynamically allocated space. The only way to get dynamically
allocated memory is via a system call (which is generally via a GNU C
library function call), and the only way to refer to dynamically
allocated space is through a pointer. Because it is less convenient,
and because the actual process of dynamic allocation requires more
computation time, programmers generally use dynamic allocation only when
neither static nor automatic allocation will serve.
For example, if you want to allocate dynamically some space to hold a
@code{struct foobar}, you cannot declare a variable of type @code{struct
foobar} whose contents are the dynamically allocated space. But you can
declare a variable of pointer type @code{struct foobar *} and assign it the
address of the space. Then you can use the operators @samp{*} and
@samp{->} on this pointer variable to refer to the contents of the space:
@smallexample
@{
struct foobar *ptr
= (struct foobar *) malloc (sizeof (struct foobar));
ptr->name = x;
ptr->next = current_foobar;
current_foobar = ptr;
@}
@end smallexample
@node Unconstrained Allocation
@subsection Unconstrained Allocation
@cindex unconstrained memory allocation
@cindex @code{malloc} function
@cindex heap, dynamic allocation from
The most general dynamic allocation facility is @code{malloc}. It
allows you to allocate blocks of memory of any size at any time, make
them bigger or smaller at any time, and free the blocks individually at
any time (or never).
@menu
* Basic Allocation:: Simple use of @code{malloc}.
* Malloc Examples:: Examples of @code{malloc}. @code{xmalloc}.
* Freeing after Malloc:: Use @code{free} to free a block you
got with @code{malloc}.
* Changing Block Size:: Use @code{realloc} to make a block
bigger or smaller.
* Allocating Cleared Space:: Use @code{calloc} to allocate a
block and clear it.
* Efficiency and Malloc:: Efficiency considerations in use of
these functions.
* Aligned Memory Blocks:: Allocating specially aligned memory.
* Malloc Tunable Parameters:: Use @code{mallopt} to adjust allocation
parameters.
* Heap Consistency Checking:: Automatic checking for errors.
* Hooks for Malloc:: You can use these hooks for debugging
programs that use @code{malloc}.
* Statistics of Malloc:: Getting information about how much
memory your program is using.
* Summary of Malloc:: Summary of @code{malloc} and related functions.
@end menu
@node Basic Allocation
@subsubsection Basic Memory Allocation
@cindex allocation of memory with @code{malloc}
To allocate a block of memory, call @code{malloc}. The prototype for
this function is in @file{stdlib.h}.
@pindex stdlib.h
@comment malloc.h stdlib.h
@comment ISO
@deftypefun {void *} malloc (size_t @var{size})
This function returns a pointer to a newly allocated block @var{size}
bytes long, or a null pointer if the block could not be allocated.
@end deftypefun
The contents of the block are undefined; you must initialize it yourself
(or use @code{calloc} instead; @pxref{Allocating Cleared Space}).
Normally you would cast the value as a pointer to the kind of object
that you want to store in the block. Here we show an example of doing
so, and of initializing the space with zeros using the library function
@code{memset} (@pxref{Copying and Concatenation}):
@smallexample
struct foo *ptr;
@dots{}
ptr = (struct foo *) malloc (sizeof (struct foo));
if (ptr == 0) abort ();
memset (ptr, 0, sizeof (struct foo));
@end smallexample
You can store the result of @code{malloc} into any pointer variable
without a cast, because @w{ISO C} automatically converts the type
@code{void *} to another type of pointer when necessary. But the cast
is necessary in contexts other than assignment operators or if you might
want your code to run in traditional C.
Remember that when allocating space for a string, the argument to
@code{malloc} must be one plus the length of the string. This is
because a string is terminated with a null character that doesn't count
in the ``length'' of the string but does need space. For example:
@smallexample
char *ptr;
@dots{}
ptr = (char *) malloc (length + 1);
@end smallexample
@noindent
@xref{Representation of Strings}, for more information about this.
@node Malloc Examples
@subsubsection Examples of @code{malloc}
If no more space is available, @code{malloc} returns a null pointer.
You should check the value of @emph{every} call to @code{malloc}. It is
useful to write a subroutine that calls @code{malloc} and reports an
error if the value is a null pointer, returning only if the value is
nonzero. This function is conventionally called @code{xmalloc}. Here
it is:
@smallexample
void *
xmalloc (size_t size)
@{
register void *value = malloc (size);
if (value == 0)
fatal ("virtual memory exhausted");
return value;
@}
@end smallexample
Here is a real example of using @code{malloc} (by way of @code{xmalloc}).
The function @code{savestring} will copy a sequence of characters into
a newly allocated null-terminated string:
@smallexample
@group
char *
savestring (const char *ptr, size_t len)
@{
register char *value = (char *) xmalloc (len + 1);
value[len] = '\0';
return (char *) memcpy (value, ptr, len);
@}
@end group
@end smallexample
The block that @code{malloc} gives you is guaranteed to be aligned so
that it can hold any type of data. In the GNU system, the address is
always a multiple of eight on most systems, and a multiple of 16 on
64-bit systems. Only rarely is any higher boundary (such as a page
boundary) necessary; for those cases, use @code{memalign},
@code{posix_memalign} or @code{valloc} (@pxref{Aligned Memory Blocks}).
Note that the memory located after the end of the block is likely to be
in use for something else; perhaps a block already allocated by another
call to @code{malloc}. If you attempt to treat the block as longer than
you asked for it to be, you are liable to destroy the data that
@code{malloc} uses to keep track of its blocks, or you may destroy the
contents of another block. If you have already allocated a block and
discover you want it to be bigger, use @code{realloc} (@pxref{Changing
Block Size}).
@node Freeing after Malloc
@subsubsection Freeing Memory Allocated with @code{malloc}
@cindex freeing memory allocated with @code{malloc}
@cindex heap, freeing memory from
When you no longer need a block that you got with @code{malloc}, use the
function @code{free} to make the block available to be allocated again.
The prototype for this function is in @file{stdlib.h}.
@pindex stdlib.h
@comment malloc.h stdlib.h
@comment ISO
@deftypefun void free (void *@var{ptr})
The @code{free} function deallocates the block of memory pointed at
by @var{ptr}.
@end deftypefun
@comment stdlib.h
@comment Sun
@deftypefun void cfree (void *@var{ptr})
This function does the same thing as @code{free}. It's provided for
backward compatibility with SunOS; you should use @code{free} instead.
@end deftypefun
Freeing a block alters the contents of the block. @strong{Do not expect to
find any data (such as a pointer to the next block in a chain of blocks) in
the block after freeing it.} Copy whatever you need out of the block before
freeing it! Here is an example of the proper way to free all the blocks in
a chain, and the strings that they point to:
@smallexample
struct chain
@{
struct chain *next;
char *name;
@}
void
free_chain (struct chain *chain)
@{
while (chain != 0)
@{
struct chain *next = chain->next;
free (chain->name);
free (chain);
chain = next;
@}
@}
@end smallexample
Occasionally, @code{free} can actually return memory to the operating
system and make the process smaller. Usually, all it can do is allow a
later call to @code{malloc} to reuse the space. In the meantime, the
space remains in your program as part of a free-list used internally by
@code{malloc}.
There is no point in freeing blocks at the end of a program, because all
of the program's space is given back to the system when the process
terminates.
@node Changing Block Size
@subsubsection Changing the Size of a Block
@cindex changing the size of a block (@code{malloc})
Often you do not know for certain how big a block you will ultimately need
at the time you must begin to use the block. For example, the block might
be a buffer that you use to hold a line being read from a file; no matter
how long you make the buffer initially, you may encounter a line that is
longer.
You can make the block longer by calling @code{realloc}. This function
is declared in @file{stdlib.h}.
@pindex stdlib.h
@comment malloc.h stdlib.h
@comment ISO
@deftypefun {void *} realloc (void *@var{ptr}, size_t @var{newsize})
The @code{realloc} function changes the size of the block whose address is
@var{ptr} to be @var{newsize}.
Since the space after the end of the block may be in use, @code{realloc}
may find it necessary to copy the block to a new address where more free
space is available. The value of @code{realloc} is the new address of the
block. If the block needs to be moved, @code{realloc} copies the old
contents.
If you pass a null pointer for @var{ptr}, @code{realloc} behaves just
like @samp{malloc (@var{newsize})}. This can be convenient, but beware
that older implementations (before @w{ISO C}) may not support this
behavior, and will probably crash when @code{realloc} is passed a null
pointer.
@end deftypefun
Like @code{malloc}, @code{realloc} may return a null pointer if no
memory space is available to make the block bigger. When this happens,
the original block is untouched; it has not been modified or relocated.
In most cases it makes no difference what happens to the original block
when @code{realloc} fails, because the application program cannot continue
when it is out of memory, and the only thing to do is to give a fatal error
message. Often it is convenient to write and use a subroutine,
conventionally called @code{xrealloc}, that takes care of the error message
as @code{xmalloc} does for @code{malloc}:
@smallexample
void *
xrealloc (void *ptr, size_t size)
@{
register void *value = realloc (ptr, size);
if (value == 0)
fatal ("Virtual memory exhausted");
return value;
@}
@end smallexample
You can also use @code{realloc} to make a block smaller. The reason you
would do this is to avoid tying up a lot of memory space when only a little
is needed.
@comment The following is no longer true with the new malloc.
@comment But it seems wise to keep the warning for other implementations.
In several allocation implementations, making a block smaller sometimes
necessitates copying it, so it can fail if no other space is available.
If the new size you specify is the same as the old size, @code{realloc}
is guaranteed to change nothing and return the same address that you gave.
@node Allocating Cleared Space
@subsubsection Allocating Cleared Space
The function @code{calloc} allocates memory and clears it to zero. It
is declared in @file{stdlib.h}.
@pindex stdlib.h
@comment malloc.h stdlib.h
@comment ISO
@deftypefun {void *} calloc (size_t @var{count}, size_t @var{eltsize})
This function allocates a block long enough to contain a vector of
@var{count} elements, each of size @var{eltsize}. Its contents are
cleared to zero before @code{calloc} returns.
@end deftypefun
You could define @code{calloc} as follows:
@smallexample
void *
calloc (size_t count, size_t eltsize)
@{
size_t size = count * eltsize;
void *value = malloc (size);
if (value != 0)
memset (value, 0, size);
return value;
@}
@end smallexample
But in general, it is not guaranteed that @code{calloc} calls
@code{malloc} internally. Therefore, if an application provides its own
@code{malloc}/@code{realloc}/@code{free} outside the C library, it
should always define @code{calloc}, too.
@node Efficiency and Malloc
@subsubsection Efficiency Considerations for @code{malloc}
@cindex efficiency and @code{malloc}
@ignore
@c No longer true, see below instead.
To make the best use of @code{malloc}, it helps to know that the GNU
version of @code{malloc} always dispenses small amounts of memory in
blocks whose sizes are powers of two. It keeps separate pools for each
power of two. This holds for sizes up to a page size. Therefore, if
you are free to choose the size of a small block in order to make
@code{malloc} more efficient, make it a power of two.
@c !!! xref getpagesize
Once a page is split up for a particular block size, it can't be reused
for another size unless all the blocks in it are freed. In many
programs, this is unlikely to happen. Thus, you can sometimes make a
program use memory more efficiently by using blocks of the same size for
many different purposes.
When you ask for memory blocks of a page or larger, @code{malloc} uses a
different strategy; it rounds the size up to a multiple of a page, and
it can coalesce and split blocks as needed.
The reason for the two strategies is that it is important to allocate
and free small blocks as fast as possible, but speed is less important
for a large block since the program normally spends a fair amount of
time using it. Also, large blocks are normally fewer in number.
Therefore, for large blocks, it makes sense to use a method which takes
more time to minimize the wasted space.
@end ignore
As opposed to other versions, the @code{malloc} in the GNU C Library
does not round up block sizes to powers of two, neither for large nor
for small sizes. Neighboring chunks can be coalesced on a @code{free}
no matter what their size is. This makes the implementation suitable
for all kinds of allocation patterns without generally incurring high
memory waste through fragmentation.
Very large blocks (much larger than a page) are allocated with
@code{mmap} (anonymous or via @code{/dev/zero}) by this implementation.
This has the great advantage that these chunks are returned to the
system immediately when they are freed. Therefore, it cannot happen
that a large chunk becomes ``locked'' in between smaller ones and even
after calling @code{free} wastes memory. The size threshold for
@code{mmap} to be used can be adjusted with @code{mallopt}. The use of
@code{mmap} can also be disabled completely.
@node Aligned Memory Blocks
@subsubsection Allocating Aligned Memory Blocks
@cindex page boundary
@cindex alignment (with @code{malloc})
@pindex stdlib.h
The address of a block returned by @code{malloc} or @code{realloc} in
the GNU system is always a multiple of eight (or sixteen on 64-bit
systems). If you need a block whose address is a multiple of a higher
power of two than that, use @code{memalign}, @code{posix_memalign}, or
@code{valloc}. These functions are declared in @file{stdlib.h}.
With the GNU library, you can use @code{free} to free the blocks that
@code{memalign}, @code{posix_memalign}, and @code{valloc} return. That
does not work in BSD, however---BSD does not provide any way to free
such blocks.
@comment malloc.h stdlib.h
@comment BSD
@deftypefun {void *} memalign (size_t @var{boundary}, size_t @var{size})
The @code{memalign} function allocates a block of @var{size} bytes whose
address is a multiple of @var{boundary}. The @var{boundary} must be a
power of two! The function @code{memalign} works by allocating a
somewhat larger block, and then returning an address within the block
that is on the specified boundary.
@end deftypefun
@comment stdlib.h
@comment POSIX
@deftypefun int posix_memalign (void **@var{memptr}, size_t @var{alignment}, size_t @var{size})
The @code{posix_memalign} function is similar to the @code{memalign}
function in that it returns a buffer of @var{size} bytes aligned to a
multiple of @var{alignment}. But it adds one requirement to the
parameter @var{alignment}: the value must be a power of two multiple of
@code{sizeof (void *)}.
If the function succeeds in allocation memory a pointer to the allocated
memory is returned in @code{*@var{memptr}} and the return value is zero.
Otherwise the function returns an error value indicating the problem.
This function was introduced in POSIX 1003.1d.
@end deftypefun
@comment malloc.h stdlib.h
@comment BSD
@deftypefun {void *} valloc (size_t @var{size})
Using @code{valloc} is like using @code{memalign} and passing the page size
as the value of the second argument. It is implemented like this:
@smallexample
void *
valloc (size_t size)
@{
return memalign (getpagesize (), size);
@}
@end smallexample
@c !!! xref getpagesize
@end deftypefun
@node Malloc Tunable Parameters
@subsubsection Malloc Tunable Parameters
You can adjust some parameters for dynamic memory allocation with the
@code{mallopt} function. This function is the general SVID/XPG
interface, defined in @file{malloc.h}.
@pindex malloc.h
@deftypefun int mallopt (int @var{param}, int @var{value})
When calling @code{mallopt}, the @var{param} argument specifies the
parameter to be set, and @var{value} the new value to be set. Possible
choices for @var{param}, as defined in @file{malloc.h}, are:
@table @code
@item M_TRIM_THRESHOLD
This is the minimum size (in bytes) of the top-most, releasable chunk
that will cause @code{sbrk} to be called with a negative argument in
order to return memory to the system.
@item M_TOP_PAD
This parameter determines the amount of extra memory to obtain from the
system when a call to @code{sbrk} is required. It also specifies the
number of bytes to retain when shrinking the heap by calling @code{sbrk}
with a negative argument. This provides the necessary hysteresis in
heap size such that excessive amounts of system calls can be avoided.
@item M_MMAP_THRESHOLD
All chunks larger than this value are allocated outside the normal
heap, using the @code{mmap} system call. This way it is guaranteed
that the memory for these chunks can be returned to the system on
@code{free}.
@item M_MMAP_MAX
The maximum number of chunks to allocate with @code{mmap}. Setting this
to zero disables all use of @code{mmap}.
@end table
@end deftypefun
@node Heap Consistency Checking
@subsubsection Heap Consistency Checking
@cindex heap consistency checking
@cindex consistency checking, of heap
You can ask @code{malloc} to check the consistency of dynamic memory by
using the @code{mcheck} function. This function is a GNU extension,
declared in @file{mcheck.h}.
@pindex mcheck.h
@comment mcheck.h
@comment GNU
@deftypefun int mcheck (void (*@var{abortfn}) (enum mcheck_status @var{status}))
Calling @code{mcheck} tells @code{malloc} to perform occasional
consistency checks. These will catch things such as writing
past the end of a block that was allocated with @code{malloc}.
The @var{abortfn} argument is the function to call when an inconsistency
is found. If you supply a null pointer, then @code{mcheck} uses a
default function which prints a message and calls @code{abort}
(@pxref{Aborting a Program}). The function you supply is called with
one argument, which says what sort of inconsistency was detected; its
type is described below.
It is too late to begin allocation checking once you have allocated
anything with @code{malloc}. So @code{mcheck} does nothing in that
case. The function returns @code{-1} if you call it too late, and
@code{0} otherwise (when it is successful).
The easiest way to arrange to call @code{mcheck} early enough is to use
the option @samp{-lmcheck} when you link your program; then you don't
need to modify your program source at all. Alternatively you might use
a debugger to insert a call to @code{mcheck} whenever the program is
started, for example these gdb commands will automatically call @code{mcheck}
whenever the program starts:
@smallexample
(gdb) break main
Breakpoint 1, main (argc=2, argv=0xbffff964) at whatever.c:10
(gdb) command 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".
>call mcheck(0)
>continue
>end
(gdb) ...
@end smallexample
This will however only work if no initialization function of any object
involved calls any of the @code{malloc} functions since @code{mcheck}
must be called before the first such function.
@end deftypefun
@deftypefun {enum mcheck_status} mprobe (void *@var{pointer})
The @code{mprobe} function lets you explicitly check for inconsistencies
in a particular allocated block. You must have already called
@code{mcheck} at the beginning of the program, to do its occasional
checks; calling @code{mprobe} requests an additional consistency check
to be done at the time of the call.
The argument @var{pointer} must be a pointer returned by @code{malloc}
or @code{realloc}. @code{mprobe} returns a value that says what
inconsistency, if any, was found. The values are described below.
@end deftypefun
@deftp {Data Type} {enum mcheck_status}
This enumerated type describes what kind of inconsistency was detected
in an allocated block, if any. Here are the possible values:
@table @code
@item MCHECK_DISABLED
@code{mcheck} was not called before the first allocation.
No consistency checking can be done.
@item MCHECK_OK
No inconsistency detected.
@item MCHECK_HEAD
The data immediately before the block was modified.
This commonly happens when an array index or pointer
is decremented too far.
@item MCHECK_TAIL
The data immediately after the block was modified.
This commonly happens when an array index or pointer
is incremented too far.
@item MCHECK_FREE
The block was already freed.
@end table
@end deftp
Another possibility to check for and guard against bugs in the use of
@code{malloc}, @code{realloc} and @code{free} is to set the environment
variable @code{MALLOC_CHECK_}. When @code{MALLOC_CHECK_} is set, a
special (less efficient) implementation is used which is designed to be
tolerant against simple errors, such as double calls of @code{free} with
the same argument, or overruns of a single byte (off-by-one bugs). Not
all such errors can be protected against, however, and memory leaks can
result. If @code{MALLOC_CHECK_} is set to @code{0}, any detected heap
corruption is silently ignored; if set to @code{1}, a diagnostic is
printed on @code{stderr}; if set to @code{2}, @code{abort} is called
immediately. This can be useful because otherwise a crash may happen
much later, and the true cause for the problem is then very hard to
track down.
There is one problem with @code{MALLOC_CHECK_}: in SUID or SGID binaries
it could possibly be exploited since diverging from the normal programs
behaviour it now writes something to the standard error desriptor.
Therefore the use of @code{MALLOC_CHECK_} is disabled by default for
SUID and SGID binaries. It can be enabled again by the system
administrator by adding a file @file{/etc/suid-debug} (the content is
not important it could be empty).
So, what's the difference between using @code{MALLOC_CHECK_} and linking
with @samp{-lmcheck}? @code{MALLOC_CHECK_} is orthogonal with respect to
@samp{-lmcheck}. @samp{-lmcheck} has been added for backward
compatibility. Both @code{MALLOC_CHECK_} and @samp{-lmcheck} should
uncover the same bugs - but using @code{MALLOC_CHECK_} you don't need to
recompile your application.
@node Hooks for Malloc
@subsubsection Memory Allocation Hooks
@cindex allocation hooks, for @code{malloc}
The GNU C library lets you modify the behavior of @code{malloc},
@code{realloc}, and @code{free} by specifying appropriate hook
functions. You can use these hooks to help you debug programs that use
dynamic memory allocation, for example.
The hook variables are declared in @file{malloc.h}.
@pindex malloc.h
@comment malloc.h
@comment GNU
@defvar __malloc_hook
The value of this variable is a pointer to the function that
@code{malloc} uses whenever it is called. You should define this
function to look like @code{malloc}; that is, like:
@smallexample
void *@var{function} (size_t @var{size}, const void *@var{caller})
@end smallexample
The value of @var{caller} is the return address found on the stack when
the @code{malloc} function was called. This value allows you to trace
the memory consumption of the program.
@end defvar
@comment malloc.h
@comment GNU
@defvar __realloc_hook
The value of this variable is a pointer to function that @code{realloc}
uses whenever it is called. You should define this function to look
like @code{realloc}; that is, like:
@smallexample
void *@var{function} (void *@var{ptr}, size_t @var{size}, const void *@var{caller})
@end smallexample
The value of @var{caller} is the return address found on the stack when
the @code{realloc} function was called. This value allows you to trace the
memory consumption of the program.
@end defvar
@comment malloc.h
@comment GNU
@defvar __free_hook
The value of this variable is a pointer to function that @code{free}
uses whenever it is called. You should define this function to look
like @code{free}; that is, like:
@smallexample
void @var{function} (void *@var{ptr}, const void *@var{caller})
@end smallexample
The value of @var{caller} is the return address found on the stack when
the @code{free} function was called. This value allows you to trace the
memory consumption of the program.
@end defvar
@comment malloc.h
@comment GNU
@defvar __memalign_hook
The value of this variable is a pointer to function that @code{memalign}
uses whenever it is called. You should define this function to look
like @code{memalign}; that is, like:
@smallexample
void *@var{function} (size_t @var{size}, size_t @var{alignment}, const void *@var{caller})
@end smallexample
The value of @var{caller} is the return address found on the stack when
the @code{memalign} function was called. This value allows you to trace the
memory consumption of the program.
@end defvar
You must make sure that the function you install as a hook for one of
these functions does not call that function recursively without restoring
the old value of the hook first! Otherwise, your program will get stuck
in an infinite recursion. Before calling the function recursively, one
should make sure to restore all the hooks to their previous value. When
coming back from the recursive call, all the hooks should be resaved
since a hook might modify itself.
@comment malloc.h
@comment GNU
@defvar __malloc_initialize_hook
The value of this variable is a pointer to a function that is called
once when the malloc implementation is initialized. This is a weak
variable, so it can be overridden in the application with a definition
like the following:
@smallexample
void (*@var{__malloc_initialize_hook}) (void) = my_init_hook;
@end smallexample
@end defvar
An issue to look out for is the time at which the malloc hook functions
can be safely installed. If the hook functions call the malloc-related
functions recursively, it is necessary that malloc has already properly
initialized itself at the time when @code{__malloc_hook} etc. is
assigned to. On the other hand, if the hook functions provide a
complete malloc implementation of their own, it is vital that the hooks
are assigned to @emph{before} the very first @code{malloc} call has
completed, because otherwise a chunk obtained from the ordinary,
un-hooked malloc may later be handed to @code{__free_hook}, for example.
In both cases, the problem can be solved by setting up the hooks from
within a user-defined function pointed to by
@code{__malloc_initialize_hook}---then the hooks will be set up safely
at the right time.
Here is an example showing how to use @code{__malloc_hook} and
@code{__free_hook} properly. It installs a function that prints out
information every time @code{malloc} or @code{free} is called. We just
assume here that @code{realloc} and @code{memalign} are not used in our
program.
@smallexample
/* Prototypes for __malloc_hook, __free_hook */
#include <malloc.h>
/* Prototypes for our hooks. */
static void *my_init_hook (void);
static void *my_malloc_hook (size_t, const void *);
static void my_free_hook (void*, const void *);
/* Override initializing hook from the C library. */
void (*__malloc_initialize_hook) (void) = my_init_hook;
static void
my_init_hook (void)
@{
old_malloc_hook = __malloc_hook;
old_free_hook = __free_hook;
__malloc_hook = my_malloc_hook;
__free_hook = my_free_hook;
@}
static void *
my_malloc_hook (size_t size, const void *caller)
@{
void *result;
/* Restore all old hooks */
__malloc_hook = old_malloc_hook;
__free_hook = old_free_hook;
/* Call recursively */
result = malloc (size);
/* Save underlaying hooks */
old_malloc_hook = __malloc_hook;
old_free_hook = __free_hook;
/* @r{@code{printf} might call @code{malloc}, so protect it too.} */
printf ("malloc (%u) returns %p\n", (unsigned int) size, result);
/* Restore our own hooks */
__malloc_hook = my_malloc_hook;
__free_hook = my_free_hook;
return result;
@}
static void *
my_free_hook (void *ptr, const void *caller)
@{
/* Restore all old hooks */
__malloc_hook = old_malloc_hook;
__free_hook = old_free_hook;
/* Call recursively */
free (ptr);
/* Save underlaying hooks */
old_malloc_hook = __malloc_hook;
old_free_hook = __free_hook;
/* @r{@code{printf} might call @code{free}, so protect it too.} */
printf ("freed pointer %p\n", ptr);
/* Restore our own hooks */
__malloc_hook = my_malloc_hook;
__free_hook = my_free_hook;
@}