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