xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mallopt/mallopt01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Linux Test Project, 2003-2021
4  * Copyright (c) International Business Machines  Corp., 2002
5  * 01/02/2003	Port to LTP	<[email protected]>
6  * 06/30/2001	Port to Linux <[email protected]>
7  */
8 
9 /*\
10  * [Description]
11  *
12  * Basic mallinfo() and mallopt() testing.
13  */
14 
15 
16 #include "../mallinfo/mallinfo_common.h"
17 #include "tst_safe_macros.h"
18 
19 #ifdef HAVE_MALLOPT
20 
21 #define MAX_FAST_SIZE	(80 * sizeof(size_t) / 4)
22 
23 struct mallinfo info;
24 
test_mallopt(void)25 void test_mallopt(void)
26 {
27 	char *buf;
28 
29 	buf = SAFE_MALLOC(20480);
30 
31 	info = mallinfo();
32 	if (info.uordblks < 20480) {
33 		print_mallinfo("Test uordblks", &info);
34 		tst_res(TFAIL, "mallinfo() failed: uordblks < 20K");
35 	}
36 	if (info.smblks != 0) {
37 		print_mallinfo("Test smblks", &info);
38 		tst_res(TFAIL, "mallinfo() failed: smblks != 0");
39 	}
40 	if (info.uordblks >= 20480 && info.smblks == 0)
41 		tst_res(TPASS, "mallinfo() succeeded");
42 
43 	free(buf);
44 
45 	if (mallopt(M_MXFAST, MAX_FAST_SIZE) == 0)
46 		tst_res(TFAIL, "mallopt(M_MXFAST, %d) failed", (int)MAX_FAST_SIZE);
47 	else
48 		tst_res(TPASS, "mallopt(M_MXFAST, %d) succeeded", (int)MAX_FAST_SIZE);
49 
50 	if ((buf = malloc(1024)) == NULL) {
51 		tst_res(TFAIL, "malloc(1024) failed");
52 	} else {
53 		tst_res(TPASS, "malloc(1024) succeeded");
54 		free(buf);
55 	}
56 
57 	if (mallopt(M_MXFAST, 0) == 0)
58 		tst_res(TFAIL, "mallopt(M_MXFAST, 0) failed");
59 	else
60 		tst_res(TPASS, "mallopt(M_MXFAST, 0) succeeded");
61 
62 	if ((buf = malloc(1024)) == NULL) {
63 		tst_res(TFAIL, "malloc(1024) failed");
64 	} else {
65 		tst_res(TPASS, "malloc(1024) succeeded");
66 		free(buf);
67 	}
68 }
69 
70 static struct tst_test test = {
71 	.test_all = test_mallopt,
72 };
73 
74 #else
75 TST_TEST_TCONF("system doesn't implement non-POSIX mallopt()");
76 #endif
77