1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /* 01/02/2003 Port to LTP avenkat@us.ibm.com*/
21 /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
22
23 /*
24 * NAME
25 * mallopt
26 *
27 * CALLS
28 * malloc(3x), mallopt(3x), mallinfo(3x).
29 *
30 * ALGORITHM
31 * Set options, malloc memory, and check resource ussage.
32 *
33 * RESTRICTIONS
34 */
35
36 #ifdef CONFIG_COLDFIRE
37 #define __MALLOC_STANDARD__
38 #endif
39 #include <errno.h>
40 /*
41 * NOTE: struct mallinfo is only exported via malloc.h (not stdlib.h), even
42 * though it's an obsolete header for malloc(3).
43 *
44 * Inconsistencies rock.
45 */
46 #include <malloc.h>
47 #include <stdio.h>
48
49 #include "test.h"
50 #include "safe_macros.h"
51
52 #define FAILED 0
53 #define PASSED 1
54 #define MAX_FAST_SIZE (80 * sizeof(size_t) / 4)
55
56 int local_flag = PASSED;
57
58 char *TCID = "mallopt01";
59 int block_number;
60 FILE *temp;
61 int TST_TOTAL = 6;
62 extern int tst_COUNT; /* Test Case counter for tst_routines */
63
64 void printinfo();
65
66 #if !defined(UCLINUX)
67 struct mallinfo info;
68
main(int argc,char * argv[])69 int main(int argc, char *argv[])
70 {
71 char *buf;
72
73 tst_tmpdir();
74
75 buf = SAFE_MALLOC(NULL, 20480);
76
77 /*
78 * Check space usage.
79 */
80
81 info = mallinfo();
82 if (info.uordblks < 20480) {
83 printinfo();
84 tst_resm(TFAIL, "mallinfo failed: uordblks < 20K");
85 }
86 if (info.smblks != 0) {
87 printinfo();
88 tst_resm(TFAIL, "mallinfo failed: smblks != 0");
89 }
90 if (info.uordblks >= 20480 && info.smblks == 0)
91 tst_resm(TPASS, "mallinfo() succeeded");
92 free(buf);
93
94 /*
95 * Test mallopt's M_MXFAST and M_NLBLKS cmds.
96 */
97
98 if (mallopt(M_MXFAST, MAX_FAST_SIZE) == 0)
99 tst_resm(TFAIL, "mallopt(M_MXFAST, %d) failed", (int)MAX_FAST_SIZE);
100 else
101 tst_resm(TPASS, "mallopt(M_MXFAST, %d) succeeded", (int)MAX_FAST_SIZE);
102
103 if (mallopt(M_NLBLKS, 50) == 0)
104 tst_resm(TFAIL, "mallopt(M_NLBLKS, 50) failed");
105 else
106 tst_resm(TPASS, "mallopt(M_NLBLKS, 50) succeeded");
107
108 if ((buf = malloc(1024)) == NULL) {
109 tst_resm(TFAIL, "malloc(1024) failed");
110 } else {
111 tst_resm(TPASS, "malloc(1024) succeeded");
112 free(buf);
113 }
114
115 if (mallopt(M_MXFAST, 0) == 0)
116 tst_resm(TFAIL, "mallopt(M_MXFAST, 0) failed");
117 else
118 tst_resm(TPASS, "mallopt(M_MXFAST, 0) succeeded");
119
120 if ((buf = malloc(1024)) == NULL) {
121 tst_resm(TFAIL, "malloc(1024) failed");
122 } else {
123 tst_resm(TPASS, "malloc(1024) succeeded");
124 free(buf);
125 }
126
127 unlink("core");
128 tst_rmdir();
129 tst_exit();
130 }
131
printinfo(void)132 void printinfo(void)
133 {
134
135 fprintf(stderr, "mallinfo structure:\n");
136 fprintf(stderr, "mallinfo.arena = %d\n", info.arena);
137 fprintf(stderr, "mallinfo.ordblks = %d\n", info.ordblks);
138 fprintf(stderr, "mallinfo.smblks = %d\n", info.smblks);
139 fprintf(stderr, "mallinfo.hblkhd = %d\n", info.hblkhd);
140 fprintf(stderr, "mallinfo.hblks = %d\n", info.hblks);
141 fprintf(stderr, "mallinfo.usmblks = %d\n", info.usmblks);
142 fprintf(stderr, "mallinfo.fsmblks = %d\n", info.fsmblks);
143 fprintf(stderr, "mallinfo.uordblks = %d\n", info.uordblks);
144 fprintf(stderr, "mallinfo.fordblks = %d\n", info.fordblks);
145 fprintf(stderr, "mallinfo.keepcost = %d\n", info.keepcost);
146 }
147
148 #else
main(void)149 int main(void)
150 {
151 tst_brkm(TCONF, NULL, "test is not available on uClinux");
152 }
153 #endif /* if !defined(UCLINUX) */
154