mallopt(3) Library Functions Manual mallopt(3) mallopt - LIBRARY Standard C library (libc, -lc) #include int mallopt(int param, int value); mallopt() , ( malloc(3)). param , value -- . param : M_ARENA_MAX , (arenas), . (pool) , malloc(3) ( ) . . . , , . 0, M_ARENA_TEST. glibc 2.10 --enable-experimental-malloc, glibc 2.15 -- . (, CentOS 5, RHEL 5). glibc , , . M_ARENA_MAX . , tcmalloc jemalloc (, ). M_ARENA_TEST ; ( M_ARENA_MAX). , , . , . M_ARENA_TEST 2 , sizeof(long) 4; 8. glibc 2.10 --enable-experimental-malloc, glibc 2.15 -- . M_ARENA_TEST , M_ARENA_MAX . M_CHECK_ACTION , glibc (, ). glibc (2, 1 0) : 0 , stderr . <<*** glibc detected ***>>, , , , , . 1 If this bit is set, then, after printing any error message specified by bit 0, the program is terminated by calling abort(3). Since glibc 2.4, if bit 0 is also set, then, between printing the error message and aborting, the program also prints a stack trace in the manner of backtrace(3), and prints the process's memory mapping in the style of /proc/pid/maps (see proc(5)). 2 ( glibc 2.4) 0. , , . value . , , M_CHECK_ACTION: 0 ; ( ). 1 . 2 . 3 , , . 5 . 7 , , . Since glibc 2.3.4, the default value for the M_CHECK_ACTION parameter is 3. In glibc 2.3.3 and earlier, the default value is 1. M_CHECK_ACTION , . M_MMAP_MAX , mmap(2). , , mmap(2), . 65536; - . 0 mmap(2) . M_MMAP_THRESHOLD , M_MMAP_THRESHOLD ( ) , mmap(2) sbrk(2) . mmap(2) , ( , , ). , mmap(2): ; , mmap(2) ; , mmap(2). M_MMAP_THRESHOLD -- 128*1024. 0. DEFAULT_MMAP_THRESHOLD_MAX: 512*1024 32- 4*1024*1024*sizeof(long) 64- . Note: Nowadays, glibc uses a dynamic mmap threshold by default. The initial value of the threshold is 128*1024, but when blocks larger than the current threshold and less than or equal to DEFAULT_MMAP_THRESHOLD_MAX are freed, the threshold is adjusted upward to the size of the freed block. When dynamic mmap thresholding is in effect, the threshold for trimming the heap is also dynamically adjusted to be twice the dynamic mmap threshold. Dynamic adjustment of the mmap threshold is disabled if any of the M_TRIM_THRESHOLD, M_TOP_PAD, M_MMAP_THRESHOLD, or M_MMAP_MAX parameters is set. M_MXFAST ( glibc 2.3) , <> ( ). Fastbins -- , . fastbin , - . 64*sizeof(size_t)/4 (. ., 64 32- ). : 0 - 80*sizeof(size_t)/4. M_MXFAST 0 fastbins. M_PERTURB ( glibc 2.4) , ( calloc(3)) value, free(3) value. , , . 0. M_TOP_PAD sbrk(2) ( ). : o , M_TOP_PAD sbrk(2). o free(3) ( M_TRIM_THRESHOLD), . , . M_TOP_PAD -- ( ) ( ). 128*1024. M_TRIM_THRESHOLD free(3) sbrk(2) ( , ). M_TRIM_THRESHOLD ( ), , sbrk(2) . 128*1024. M_TRIM_THRESHOLD -1 . M_TRIM_THRESHOLD -- ( ) ( ). , mallopt(), . . ( mallopt(), mallopt() ). , set-user-ID set-group-ID. ( ): MALLOC_ARENA_MAX M_ARENA_MAX, mallopt(). MALLOC_ARENA_TEST M_ARENA_TEST, mallopt(). MALLOC_CHECK_ mallopt() M_CHECK_ACTION. , ( malloc_hook(3)). , ( ; ). , ; M_CHECK_ACTION. . MALLOC_CHECK_ set-user-ID set-group-ID. , /etc/suid-debug ( ), MALLOC_CHECK_ set-user-ID set-group-ID. MALLOC_MMAP_MAX_ M_MMAP_MAX, mallopt(). MALLOC_MMAP_THRESHOLD_ M_MMAP_THRESHOLD, mallopt(). MALLOC_PERTURB_ M_PERTURB, mallopt(). MALLOC_TRIM_THRESHOLD_ M_TRIM_THRESHOLD, mallopt(). MALLOC_TOP_PAD_ M_TOP_PAD, mallopt(). mallopt() 1. 0. errno . A similar function exists on many System V derivatives, but the range of values for param varies across systems. The SVID defined options M_MXFAST, M_NLBLKS, M_GRAIN, and M_KEEP, but only the first of these is implemented in glibc. None. glibc 2.0. param . glibc , : mallopt(M_MXFAST, n) , fastbins n. , n (2k+1)*sizeof(size_t), k -- . mallopt() M_PERTURB, , , value, , , value. , sizeof(size_t): free(p), p+sizeof(size_t). M_CHECK_ACTION. ( ), M_CHECK_ACTION. (). glibc M_CHECK_ACTION: $ ./a.out main(): free() *** glibc detected *** ./a.out: double free or corruption (top): 0x09d30008 *** ======= Backtrace: ========= /lib/libc.so.6(+0x6c501)[0x523501] /lib/libc.so.6(+0x6dd70)[0x524d70] /lib/libc.so.6(cfree+0x6d)[0x527e5d] ./a.out[0x80485db] /lib/libc.so.6(__libc_start_main+0xe7)[0x4cdce7] ./a.out[0x8048471] ======= Memory map: ======== 001e4000-001fe000 r-xp 00000000 08:06 1083555 /lib/libgcc_s.so.1 001fe000-001ff000 r--p 00019000 08:06 1083555 /lib/libgcc_s.so.1 [some lines omitted] b7814000-b7817000 rw-p 00000000 00:00 0 bff53000-bff74000 rw-p 00000000 00:00 0 [stack] Aborted (core dumped) M_CHECK_ACTION: $ ./a.out 1 # main(): free() *** glibc detected *** ./a.out: double free or corruption (top): 0x09cbe008 *** main(): free() $ ./a.out 2 # main(): free() Aborted (core dumped) $ ./a.out 0 # main(): free() main(): free() MALLOC_CHECK_: $ MALLOC_CHECK_=1 ./a.out main(): free() *** glibc detected *** ./a.out: free(): invalid pointer: 0x092c2008 *** main(): free() #include #include #include int main(int argc, char *argv[]) { char *p; if (argc > 1) { if (mallopt(M_CHECK_ACTION, atoi(argv[1])) != 1) { fprintf(stderr, "mallopt() failed"); exit(EXIT_FAILURE); } } p = malloc(1000); if (p == NULL) { fprintf(stderr, "malloc() failed"); exit(EXIT_FAILURE); } free(p); printf("%s(): returned from first free() call\n", __func__); free(p); printf("%s(): returned from second free() call\n", __func__); exit(EXIT_SUCCESS); } . mmap(2), sbrk(2), mallinfo(3), malloc(3), malloc_hook(3), malloc_info(3), malloc_stats(3), malloc_trim(3), mcheck(3), mtrace(3), posix_memalign(3) aereiae , Alexey , Azamat Hackimov , Dmitriy S. Seregin , Dmitry Bolkhovskikh , ITriskTI , Max Is , Yuri Kozlov , ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . mallopt(3)