1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
4 * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Basic mallinfo() test. Refer to glibc test mallinfo2 test
11 * https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c
12 */
13 #include "mallinfo_common.h"
14 #include "tst_safe_macros.h"
15
16 #ifdef HAVE_MALLINFO
17 #define M_NUM 20
18 static struct mallinfo info1;
19 static void *buf[M_NUM + 1];
20
cleanup(void)21 static void cleanup(void)
22 {
23 int i;
24
25 for (i = M_NUM; i > 0; i--) {
26 if (buf[i]) {
27 free(buf[i]);
28 buf[i] = NULL;
29 }
30 }
31 }
32
test_mallinfo(void)33 void test_mallinfo(void)
34 {
35 int i;
36 int total = 0;
37 struct mallinfo info2;
38
39 for (i = 1; i <= M_NUM; i++) {
40 buf[i] = SAFE_MALLOC(160 * i);
41 total += 160 * i;
42 }
43 info2 = mallinfo();
44 print_mallinfo("Test uordblks", &info2);
45 if (info2.uordblks >= info1.uordblks + total)
46 tst_res(TPASS, "mallinfo() uordblks passed");
47 else
48 tst_res(TFAIL, "mallinfo() uordblks failed");
49
50 //Create another free chunk
51 free(buf[M_NUM/2]);
52 buf[M_NUM/2] = NULL;
53 info2 = mallinfo();
54 print_mallinfo("Test ordblks", &info2);
55 if (info2.ordblks == info1.ordblks + 1)
56 tst_res(TPASS, "mallinfo() ordblks passed");
57 else
58 tst_res(TFAIL, "mallinfo() ordblks failed");
59
60 cleanup();
61 }
62
setup(void)63 static void setup(void)
64 {
65 if (sizeof(info1.arena) != sizeof(int))
66 tst_res(TFAIL, "The member of mallinfo struct is not int");
67
68 info1 = mallinfo();
69 print_mallinfo("Start", &info1);
70 }
71
72 static struct tst_test test = {
73 .setup = setup,
74 .test_all = test_mallinfo,
75 .cleanup = cleanup,
76 };
77
78 #else
79 TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
80 #endif
81