Home | History | Annotate | Download | only in mallopt
      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 (at) us.ibm.com*/
     21 /* 06/30/2001	Port to Linux	nsharoff (at) 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 
     69 int main(int argc, char *argv[])
     70 {
     71 	char *buf;
     72 
     73 	tst_parse_opts(argc, argv, NULL, NULL);
     74 
     75 	tst_tmpdir();
     76 
     77 	buf = SAFE_MALLOC(NULL, 20480);
     78 
     79 	/*
     80 	 * Check space usage.
     81 	 */
     82 
     83 	info = mallinfo();
     84 	if (info.uordblks < 20480) {
     85 		printinfo();
     86 		tst_resm(TFAIL, "mallinfo failed: uordblks < 20K");
     87 	}
     88 	if (info.smblks != 0) {
     89 		printinfo();
     90 		tst_resm(TFAIL, "mallinfo failed: smblks != 0");
     91 	}
     92 	if (info.uordblks >= 20480 && info.smblks == 0)
     93 		tst_resm(TPASS, "mallinfo() succeeded");
     94 	free(buf);
     95 
     96 	/*
     97 	 * Test mallopt's M_MXFAST and M_NLBLKS cmds.
     98 	 */
     99 
    100 	if (mallopt(M_MXFAST, MAX_FAST_SIZE) == 0)
    101 		tst_resm(TFAIL, "mallopt(M_MXFAST, %d) failed", (int)MAX_FAST_SIZE);
    102 	else
    103 		tst_resm(TPASS, "mallopt(M_MXFAST, %d) succeeded", (int)MAX_FAST_SIZE);
    104 
    105 	if (mallopt(M_NLBLKS, 50) == 0)
    106 		tst_resm(TFAIL, "mallopt(M_NLBLKS, 50) failed");
    107 	else
    108 		tst_resm(TPASS, "mallopt(M_NLBLKS, 50) succeeded");
    109 
    110 	if ((buf = malloc(1024)) == NULL) {
    111 		tst_resm(TFAIL, "malloc(1024) failed");
    112 	} else {
    113 		tst_resm(TPASS, "malloc(1024) succeeded");
    114 		free(buf);
    115 	}
    116 
    117 	if (mallopt(M_MXFAST, 0) == 0)
    118 		tst_resm(TFAIL, "mallopt(M_MXFAST, 0) failed");
    119 	else
    120 		tst_resm(TPASS, "mallopt(M_MXFAST, 0) succeeded");
    121 
    122 	if ((buf = malloc(1024)) == NULL) {
    123 		tst_resm(TFAIL, "malloc(1024) failed");
    124 	} else {
    125 		tst_resm(TPASS, "malloc(1024) succeeded");
    126 		free(buf);
    127 	}
    128 
    129 	unlink("core");
    130 	tst_rmdir();
    131 	tst_exit();
    132 }
    133 
    134 void printinfo(void)
    135 {
    136 
    137 	fprintf(stderr, "mallinfo structure:\n");
    138 	fprintf(stderr, "mallinfo.arena = %d\n", info.arena);
    139 	fprintf(stderr, "mallinfo.ordblks = %d\n", info.ordblks);
    140 	fprintf(stderr, "mallinfo.smblks = %d\n", info.smblks);
    141 	fprintf(stderr, "mallinfo.hblkhd = %d\n", info.hblkhd);
    142 	fprintf(stderr, "mallinfo.hblks = %d\n", info.hblks);
    143 	fprintf(stderr, "mallinfo.usmblks = %d\n", info.usmblks);
    144 	fprintf(stderr, "mallinfo.fsmblks = %d\n", info.fsmblks);
    145 	fprintf(stderr, "mallinfo.uordblks = %d\n", info.uordblks);
    146 	fprintf(stderr, "mallinfo.fordblks = %d\n", info.fordblks);
    147 	fprintf(stderr, "mallinfo.keepcost = %d\n", info.keepcost);
    148 }
    149 
    150 #else
    151 int main(void)
    152 {
    153 	tst_brkm(TCONF, NULL, "test is not available on uClinux");
    154 }
    155 #endif /* if !defined(UCLINUX) */
    156