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