xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mallinfo/mallinfo02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
4  * Author: Yang Xu <[email protected]>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Basic mallinfo() test for malloc() using sbrk or mmap.
11  * It size > MMAP_THRESHOLD, it will use mmap. Otherwise, use sbrk.
12  */
13 
14 #include "mallinfo_common.h"
15 #include "tst_safe_macros.h"
16 
17 #ifdef HAVE_MALLINFO
test_mallinfo(void)18 void test_mallinfo(void)
19 {
20 	struct mallinfo info;
21 	int size;
22 	char *buf;
23 
24 	buf = SAFE_MALLOC(20480);
25 
26 	info = mallinfo();
27 	if (info.uordblks > 20480 && info.hblkhd == 0) {
28 		tst_res(TPASS, "malloc() uses sbrk when size < 128k");
29 	} else {
30 		tst_res(TFAIL, "malloc() use mmap when size < 128k");
31 		print_mallinfo("Test malloc(20480)", &info);
32 	}
33 	free(buf);
34 
35 	info = mallinfo();
36 	size = MAX(info.fordblks, 131072);
37 
38 	buf = SAFE_MALLOC(size);
39 	info = mallinfo();
40 	if (info.hblkhd > size && info.hblks > 0) {
41 		tst_res(TPASS, "malloc() uses mmap when size >= 128k");
42 	} else {
43 		tst_res(TFAIL, "malloc uses sbrk when size >= 128k");
44 		print_mallinfo("Test malloc(1024*128)", &info);
45 	}
46 
47 	free(buf);
48 }
49 
setup(void)50 static void setup(void)
51 {
52 	if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)
53 		tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");
54 }
55 
56 static struct tst_test test = {
57 	.setup = setup,
58 	.test_all = test_mallinfo,
59 };
60 
61 #else
62 TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
63 #endif
64