File: malloctest.c

package info (click to toggle)
mpb 1.11.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,856 kB
  • sloc: ansic: 13,270; javascript: 9,901; makefile: 212; lisp: 44; sh: 4
file content (77 lines) | stat: -rw-r--r-- 2,279 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Copyright (C) 1999-2020 Massachusetts Institute of Technology.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "config.h"
#include <check.h>

#define NUM_POINTERS 1000
#define MAX_SIZE 65536
#define NUM_MALLOCS 3000000

int main(void)
{
     char *pointers[NUM_POINTERS];
     int i, iter;
     clock_t start_time;
     double elapsed_time;

#ifdef DEBUG_MALLOC
     printf("Using debug_malloc and debug_free routines.\n");
#else
     fprintf(stderr, 
	     "***** NOTE: malloctest is designed to be run when the package\n"
	     "            is configured with --enable-debug, to test the\n"
	     "            debugging malloc/free routines.\n");
#endif

     srand(time(NULL));

     for (i = 0; i < NUM_POINTERS; ++i)
	  pointers[i] = NULL;

     printf("Doing %d malloc/free calls...\n", NUM_MALLOCS);
     start_time = clock();
     for (iter = 0; iter < NUM_MALLOCS; ++iter) {
	  i = rand() % NUM_POINTERS;
	  if (pointers[i])
	       free(pointers[i]);
	  CHK_MALLOC(pointers[i], char, rand() % MAX_SIZE + 1);
	  if ((iter + 1) % (NUM_MALLOCS / 20) == 0)
	       printf("...completed %d...\n", iter + 1);
     }
     elapsed_time = (clock() - start_time) * 1.0 / CLOCKS_PER_SEC;
     printf("Done.\n");

     printf("Total time = %g seconds, %g us per iteration\n",
	    elapsed_time, elapsed_time * 1e6 / NUM_MALLOCS);

     for (i = 0; i < NUM_POINTERS; ++i)
	  if (pointers[i])
	       free(pointers[i]);

#ifdef DEBUG_MALLOC
     debug_check_memory_leaks();
#endif
     
     printf("Okay.\n");

     return EXIT_SUCCESS;
}