OSDN Git Service

(split) LDP: Update original to LDP v3.40.
[linuxjm/LDP_man-pages.git] / original / man3 / mallopt.3
1 '\" t
2 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .TH MALLOPT 3  2012-04-18 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 mallopt \- set memory allocation parameters
27 .SH SYNOPSIS
28 .B #include <malloc.h>
29
30 .BI "int mallopt(int " param ", int " value );
31 .SH DESCRIPTION
32 The
33 .BR mallopt ()
34 function adjusts parameters that control the behavior of the
35 memory-allocation functions (see
36 .BR malloc (3)).
37 The
38 .IR param
39 argument specifies the parameter to be modified, and
40 .I value
41 specifies the new value for that parameter.
42
43 The following values can be specified for
44 .IR param :
45 .TP
46 .BR M_CHECK_ACTION
47 Setting this parameter controls how glibc responds when various kinds
48 of programming errors are detected (e.g., freeing the same pointer twice).
49 The 3 least significant bits (2, 1, and 0) of the value assigned
50 to this parameter determine the glibc behavior, as follows:
51 .RS
52 .TP
53 Bit 0
54 If this bit is set, then print a one-line message on
55 .I stderr
56 that provides details about the error.
57 The message starts with the string "***\ glibc detected\ ***",
58 followed by the program name,
59 the name of the memory-allocation function in which the error was detected,
60 a brief description of the error,
61 and the memory address where the error was detected.
62 .TP
63 Bit 1
64 If this bit is set, then,
65 after printing any error message specified by bit 0,
66 the program is terminated by calling
67 .BR abort (3).
68 In glibc versions since 2.4,
69 if bit 0 is also set,
70 then, between printing the error message and aborting,
71 the program also prints a stack trace in the manner of
72 .BR backtrace (3),
73 and prints the process's memory mapping in the style of
74 .IR /proc/[pid]/maps
75 (see
76 .BR proc (5)).
77 .TP
78 Bit 2 (since glibc 2.4)
79 This bit has an effect only if bit 0 is also set.
80 If this bit is set,
81 then the one-line message describing the error is simplified
82 to contain just the name of the function where the error
83 was detected and the brief description of the error.
84 .RE
85 .IP
86 The remaining bits in
87 .I value
88 are ignored.
89 .IP
90 Combining the above details,
91 the following numeric values are meaningful for
92 .BR M_CHECK_ACTION :
93 .RS 12
94 .IP 0 3
95 Ignore error conditions; continue execution (with undefined results).
96 .IP 1
97 Print a detailed error message and continue execution.
98 .IP 2
99 Abort the program.
100 .IP 3
101 Print detailed error message, stack trace, and memory mappings,
102 and abort the program.
103 .IP 5
104 Print a simple error message and continue execution.
105 .IP 7
106 Print simple error message, stack trace, and memory mappings,
107 and abort the program.
108 .RE
109 .IP
110 Since glibc 2.3.4, the default value for the
111 .BR M_CHECK_ACTION
112 parameter is 3.
113 In glibc version 2.3.3 and earlier, the default value is 1.
114 .IP
115 Using a nonzero
116 .B M_CHECK_ACTION
117 value can be useful because otherwise a crash may happen much later,
118 and the true cause of the problem is then very hard to track down.
119 .TP
120 .BR M_MMAP_MAX
121 .\" The following text adapted from comments in the glibc source:
122 This parameter specifies the maximum number of allocation requests that
123 may be simultaneously serviced using
124 .BR mmap (2).
125 This parameter exists because some systems have a limited number
126 of internal tables for use by
127 .BR mmap (2),
128 and using more than a few of them may degrade performance.
129 .IP
130 The default value is 65,536,
131 a value which has no special significance and
132 which servers only as a safeguard.
133 Setting this parameter to 0 disables the use of
134 .BR mmap (2)
135 for servicing large allocation requests.
136 .TP
137 .BR M_MMAP_THRESHOLD
138 For allocations greater than or equal to the limit specified (in bytes) by
139 .BR M_MMAP_THRESHOLD
140 that can't be satisfied from the free list,
141 the memory-allocation functions employ
142 .BR mmap (2)
143 instead of increasing the program break using
144 .BR sbrk (2).
145 .IP
146 Allocating memory using
147 .BR mmap (2)
148 has the significant advantage that the allocated memory blocks
149 can always be independently released back to the system.
150 (By contrast,
151 the heap can be trimmed only if memory is freed at the top end.)
152 On the other hand, there are some disadvantages to the use of
153 .BR mmap (2):
154 deallocated space is not placed on the free list
155 for reuse by later allocations;
156 memory may be wasted because
157 .BR mmap (2)
158 allocations must be page-aligned;
159 and the kernel must perform the expensive task of zeroing out
160 memory allocated via
161 .BR mmap (2).
162 Balancing these factors leads to a default setting of 128*1024 for the
163 .BR M_MMAP_THRESHOLD
164 parameter.
165 .IP
166 The lower limit for this parameter is 0.
167 The upper limit is
168 .BR DEFAULT_MMAP_THRESHOLD_MAX :
169 512*1024 on 32-bit systems or
170 .IR 4*1024*1024*sizeof(long)
171 on 64-bit systems.
172 .IP
173 .IR Note:
174 Nowadays, glibc uses a dynamic mmap threshold by default.
175 The initial value of the threshold is 128*1024,
176 but when blocks larger than the current threshold and less than or equal to
177 .BR DEFAULT_MMAP_THRESHOLD_MAX
178 are freed,
179 the threshold is adjusted upwards to the size of the freed block.
180 When dynamic mmap thresholding is in effect,
181 the threshold for trimming the heap is also dynamically adjusted
182 to be twice the dynamic mmap threshold.
183 Dynamic adjustment of the mmap threshold is disabled if any of the
184 .BR M_TRIM_THRESHOLD ,
185 .BR M_TOP_PAD ,
186 .BR M_MMAP_THRESHOLD ,
187 or
188 .BR M_MMAP_MAX
189 parameters is set.
190 .TP
191 .BR M_MXFAST " (since glibc 2.3)"
192 .\" The following text adapted from comments in the glibc sources:
193 Set the upper limit for memory allocation requests that are satisfied
194 using "fastbins".
195 (The measurement unit for this parameter is bytes.)
196 Fastbins are storage areas that hold deallocated blocks of memory
197 of the same size without merging adjacent free blocks.
198 Subsequent reallocation of blocks of the same size can be handled
199 very quickly by allocating from the fastbin,
200 although memory fragmentation and the overall memory footprint
201 of the program can increase.
202 The default value for this parameter is
203 .IR "64*sizeof(size_t)/4"
204 (i.e., 64 on 32-bit architectures).
205 The range for this parameter is 0 to
206 .IR "80*sizeof(size_t)/4" .
207 Setting
208 .B M_MXFAST
209 to 0 disables the use of fastbins.
210 .TP
211 .BR M_PERTURB " (since glibc 2.4)"
212 If this parameter is set to a nonzero value,
213 then bytes of allocated memory (other than allocations via
214 .BR calloc (3))
215 are initialized to the complement of the value
216 in the least significant byte of
217 .IR value ,
218 and when allocated memory is released using
219 .BR free (3),
220 the freed bytes are set to the least significant byte of
221 .IR value .
222 This can be useful for detecting errors where programs
223 incorrectly rely on allocated memory being initialized to zero,
224 or reuse values in memory that has already been freed.
225 .TP
226 .BR M_TOP_PAD
227 This parameter defines the amount of padding to employ when calling
228 .BR sbrk (2)
229 to modify the program break.
230 (The measurement unit for this parameter is bytes.)
231 This parameter has an effect in the following circumstances:
232 .RS
233 .IP * 3
234 When the program break is increased, then
235 .BR M_TOP_PAD
236 bytes are added to the
237 .BR sbrk (2)
238 request.
239 .IP *
240 When the heap is trimmed as a consequence of calling
241 .BR free (3)
242 (see the discussion of
243 .BR M_TRIM_THRESHOLD )
244 this much free space is preserved at the top of the heap.
245 .RE
246 .IP
247 In either case,
248 the amount of padding is always rounded to a system page boundary.
249 .IP
250 Modifying
251 .BR M_TOP_PAD
252 is a trade-off between increasing the number of system calls
253 (when the parameter is set low)
254 and wasting unused memory at the top of the heap
255 (when the parameter is set high).
256 .IP
257 The default value for this parameter is 128*1024.
258 .\" DEFAULT_TOP_PAD in glibc source
259 .TP
260 .BR M_TRIM_THRESHOLD
261 When the amount of contiguous free memory at the top of the heap
262 grows sufficiently large,
263 .BR free (3)
264 employs
265 .BR sbrk (2)
266 to release this memory back to the system.
267 (This can be useful in programs that continue to execute for
268 a long period after freeing a significant amount of memory.)
269 The
270 .BR M_TRIM_THRESHOLD
271 parameter specifies the minimum size (in bytes) that
272 this block of memory must reach before
273 .BR sbrk (2)
274 is used to trim the heap.
275 .IP
276 The default value for this parameter is 128*1024.
277 Setting
278 .BR M_TRIM_THRESHOLD
279 to \-1 disables trimming completely.
280 .IP
281 Modifying
282 .BR M_TRIM_THRESHOLD
283 is a trade-off between increasing the number of system calls
284 (when the parameter is set low)
285 and wasting unused memory at the top of the heap
286 (when the parameter is set high).
287 .\" FIXME Do the arena parameters need to be documented?
288 .\" .TP
289 .\" .BR M_ARENA_TEST " (since glibc 2.10)"
290 .\" .TP
291 .\" .BR M_ARENA_MAX " (since glibc 2.10)"
292 .\"
293 .\" Environment variables
294 .\"     MALLOC_ARENA_MAX_
295 .\"     MALLOC_ARENA_TEST_
296 .\"
297 .\" http://udrepper.livejournal.com/20948.html describes some details
298 .\"     of the MALLOC_ARENA_* environment variables.
299 .\"
300 .\" These macros aren't enabled in production releases until 2.15?
301 .\" (see glibc malloc/Makefile)
302 .\"
303 .SS Environment Variables
304 A number of environment variables can be defined
305 to modify some of the same parameters as are controlled by
306 .BR mallopt ().
307 Using these variables has the advantage that the source code
308 of the program need not be changed.
309 To be effective, these variables must be defined before the
310 first call to a memory-allocation function.
311 (If the same parameters are adjusted via
312 .BR mallopt ()
313 then the
314 .BR mallopt ()
315 settings take precedence.)
316 For security reasons,
317 these variables are ignored in set-user-ID and set-group-ID programs.
318
319 The environment variables are as follows
320 (note the trailing underscore at the end of the name of each variable):
321 .TP
322 .BR MALLOC_CHECK_
323 This environment variable controls the same parameter as
324 .BR mallopt ()
325 .BR M_CHECK_ACTION .
326 If this variable is set to a nonzero value,
327 then a special implementation of the memory-allocation functions is used.
328 (This is accomplished using the
329 .BR malloc_hook (3)
330 feature.)
331 This implementation performs additional error checking,
332 but is slower
333 .\" On glibc 2.12/x86, a simple malloc()+free() loop is about 70% slower
334 .\" when MALLOC_CHECK_ was set.
335 than the standard set of memory-allocation functions.
336 (This implementation does not detect all possible errors;
337 memory leaks can still occur.)
338 .IP
339 The value assigned to this environment variable should be a single digit,
340 whose meaning is as described for
341 .BR M_CHECK_ACTION .
342 Any characters beyond the initial digit are ignored.
343 .IP
344 For security reasons, the effect of
345 .BR MALLOC_CHECK_
346 is disabled by default for set-user-ID and set-group-ID programs.
347 However, if the file
348 .IR /etc/suid\-debug
349 exists (the content of the file is irrelevant), then
350 .BR MALLOC_CHECK_
351 also has an effect for set-user-ID and set-group-ID programs.
352 .TP
353 .BR MALLOC_MMAP_MAX_
354 Controls the same parameter as
355 .BR mallopt ()
356 .BR M_MMAP_MAX .
357 .TP
358 .BR MALLOC_MMAP_THRESHOLD_
359 Controls the same parameter as
360 .BR mallopt ()
361 .BR M_MMAP_THRESHOLD .
362 .TP
363 .BR MALLOC_PERTURB_
364 Controls the same parameter as
365 .BR mallopt ()
366 .BR M_PERTURB .
367 .TP
368 .BR MALLOC_TRIM_THRESHOLD_
369 Controls the same parameter as
370 .BR mallopt ()
371 .BR M_TRIM_THRESHOLD .
372 .TP
373 .BR MALLOC_TOP_PAD_
374 Controls the same parameter as
375 .BR mallopt ()
376 .BR M_TOP_PAD .
377 .SH RETURN VALUE
378 On success,
379 .BR mallopt ()
380 returns 1.
381 On error, it returns 0.
382 .SH ERRORS
383 On error,
384 .I errno
385 is
386 .I not
387 set.
388 .\" .SH VERSIONS
389 .\" Available already in glibc 2.0, possibly earlier
390 .SH CONFORMING TO
391 This function is not specified by POSIX or the C standards.
392 A similar function exists on many System V derivatives,
393 but the range of values for
394 .IR param
395 varies across systems.
396 The SVID defined options
397 .BR M_MXFAST ,
398 .BR M_NLBLKS ,
399 .BR M_GRAIN ,
400 and
401 .BR M_KEEP ,
402 but only the first of these is implemented in glibc.
403 .\" .SH NOTES
404 .SH BUGS
405 Specifying an invalid value for
406 .I param
407 does not generate an error.
408
409 A calculation error within the glibc implementation means that
410 a call of the form:
411 .\" FIXME This looks buggy:
412 .\" setting the M_MXFAST limit rounds up:    (s + SIZE_SZ) & ~MALLOC_ALIGN_MASK)
413 .\" malloc requests are rounded up:
414 .\"    (req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK
415 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=12129
416 .nf
417
418     mallopt(M_MXFAST, n)
419
420 .fi
421 does not result in fastbins being employed for all allocations of size up to
422 .IR n .
423 To ensure desired results,
424 .I n
425 should be rounded up to the next multiple greater than or equal to
426 .IR (2k+1)*sizeof(size_t) ,
427 where
428 .I k
429 is an integer.
430 .\" Bins are multiples of 2 * sizeof(size_t) + sizeof(size_t)
431
432 The
433 .BR MALLOC_MMAP_THRESHOLD_
434 and
435 .BR MALLOC_MMAP_MAX_
436 variables are
437 .I not
438 ignored in set-group-ID programs.
439 .\" FIXME MALLOC_MMAP_THRESHOLD_ and MALLOC_MMAP_MAX_
440 .\" do have an effect for set-user-ID programs (but not
441 .\" set-group-ID programs).
442 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=12155
443
444 If
445 .BR mallopt ()
446 is used to set
447 .BR M_PERTURB ,
448 then, as expected, the bytes of allocated memory are initialized
449 to the complement of the byte in
450 .IR value ,
451 and when that memory is freed,
452 the bytes of the region are initialized to the byte specified in
453 .IR value .
454 However, there is an
455 .RI off-by- sizeof(size_t)
456 error in the implementation:
457 .\" FIXME http://sources.redhat.com/bugzilla/show_bug.cgi?id=12140
458 instead of initializing precisely the block of memory
459 being freed by the call
460 .IR free(p) ,
461 the block starting at
462 .I p+sizeof(size_t)
463 is initialized.
464 .SH EXAMPLE
465 The program below demonstrates the use of
466 .BR M_CHECK_ACTION .
467 If the program is supplied with an (integer) command-line argument,
468 then that argument is used to set the
469 .BR M_CHECK_ACTION
470 parameter.
471 The program then allocates a block of memory,
472 and frees it twice (an error).
473
474 The following shell session shows what happens when we run this program
475 under glibc, with the default value for
476 .BR M_CHECK_ACTION :
477 .in +4n
478 .nf
479
480 $ \fB./a.out\fP
481 *** glibc detected *** ./a.out: double free or corruption (top): 0x09d30008 ***
482 ======= Backtrace: =========
483 /lib/libc.so.6(+0x6c501)[0x523501]
484 /lib/libc.so.6(+0x6dd70)[0x524d70]
485 /lib/libc.so.6(cfree+0x6d)[0x527e5d]
486 \&./a.out[0x80485db]
487 /lib/libc.so.6(__libc_start_main+0xe7)[0x4cdce7]
488 \&./a.out[0x8048471]
489 ======= Memory map: ========
490 001e4000\-001fe000 r\-xp 00000000 08:06 1083555    /lib/libgcc_s.so.1
491 001fe000\-001ff000 r\-\-p 00019000 08:06 1083555    /lib/libgcc_s.so.1
492 [some lines omitted]
493 b7814000\-b7817000 rw\-p 00000000 00:00 0
494 bff53000\-bff74000 rw\-p 00000000 00:00 0          [stack]
495 Aborted (core dumped)
496 .fi
497 .in
498 .PP
499 The following runs show the results when employing other values for
500 .BR M_CHECK_ACTION :
501 .PP
502 .in +4n
503 .nf
504 $ \fB./a.out 1\fP             # Diagnose error and continue
505 main(): returned from first free() call
506 *** glibc detected *** ./a.out: double free or corruption (top): 0x09cbe008 ***
507 main(): returned from second free() call
508 $ \fB./a.out 2\fP             # Abort without error message
509 main(): returned from first free() call
510 Aborted (core dumped)
511 $ \fB./a.out 0\fP             # Ignore error and continue
512 main(): returned from first free() call
513 main(): returned from second free() call
514 .fi
515 .in
516 .PP
517 The next run shows how to set the same parameter using the
518 .B MALLOC_CHECK_
519 environment variable:
520 .PP
521 .in +4n
522 .nf
523 $ \fBMALLOC_CHECK_=1 ./a.out\fP
524 main(): returned from first free() call
525 *** glibc detected *** ./a.out: free(): invalid pointer: 0x092c2008 ***
526 main(): returned from second free() call
527 .fi
528 .in
529 .SS Program source
530 \&
531 .nf
532 #include <malloc.h>
533 #include <stdio.h>
534 #include <stdlib.h>
535
536 int
537 main(int argc, char *argv[])
538 {
539     char *p;
540
541     if (argc > 1) {
542         if (argc != 2) {
543             fprintf(stderr, "%s <M_CHECK_ACTION\-value>\\n", argv[0]);
544             exit(EXIT_FAILURE);
545         }
546     }
547
548     if (mallopt(M_CHECK_ACTION, atoi(argv[1])) != 1) {
549         fprintf(stderr, "mallopt() failed");
550         exit(EXIT_FAILURE);
551     }
552
553     p = malloc(1000);
554     if (p == NULL) {
555         fprintf(stderr, "malloc() failed");
556         exit(EXIT_FAILURE);
557     }
558
559     free(p);
560     printf("main(): returned from first free() call\\n");
561
562     free(p);
563     printf("main(): returned from second free() call\\n");
564
565     exit(EXIT_SUCCESS);
566 }
567 .fi
568 .SH SEE ALSO
569 .ad l
570 .nh
571 .BR mmap (2)
572 .BR sbrk (2),
573 .BR mallinfo (3),
574 .BR malloc (3),
575 .BR malloc_hook (3),
576 .BR malloc_trim (3),
577 .BR mcheck (3),
578 .BR mtrace (3),
579 .BR posix_memalign (3)