Blender  V2.93
memtest.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
25 /* To compile run:
26  * gcc -DWITH_GUARDEDALLOC -I../../ -I../../../atomic/ memtest.c ../../intern/mallocn.c -o memtest
27  */
28 
29 /* Number of chunks to test with */
30 #define NUM_BLOCKS 10
31 
32 #include "MEM_guardedalloc.h"
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 static void mem_error_cb(const char *errorStr)
38 {
39  fprintf(stderr, "%s", errorStr);
40  fflush(stderr);
41 }
42 
43 int main(int argc, char *argv[])
44 {
45  int verbose = 0;
46  int error_status = 0;
47  int retval = 0;
48  int *ip;
49 
50  void *p[NUM_BLOCKS];
51  int i = 0;
52 
53  /* ----------------------------------------------------------------- */
54  switch (argc) {
55  case 2:
56  verbose = atoi(argv[1]);
57  if (verbose < 0)
58  verbose = 0;
59  break;
60  case 1:
61  default:
62  verbose = 0;
63  }
64  if (verbose) {
65  fprintf(stderr, "\n*** Simple memory test\n|\n");
66  }
67 
68  /* ----------------------------------------------------------------- */
69  /* Round one, do a normal allocation, and free the blocks again. */
70  /* ----------------------------------------------------------------- */
71  /* flush mem lib output to stderr */
73 
74  for (i = 0; i < NUM_BLOCKS; i++) {
75  int blocksize = 10000;
76  char tagstring[1000];
77  if (verbose > 1)
78  printf("|--* Allocating block %d\n", i);
79  sprintf(tagstring, "Memblock no. %d : ", i);
80  p[i] = MEM_callocN(blocksize, strdup(tagstring));
81  }
82 
83  /* report on that */
84  if (verbose > 1)
86 
87  /* memory is there: test it */
88  error_status = MEM_consistency_check();
89 
90  if (verbose) {
91  if (error_status) {
92  fprintf(stderr, "|--* Memory test FAILED\n|\n");
93  }
94  else {
95  fprintf(stderr, "|--* Memory tested as good (as it should be)\n|\n");
96  }
97  }
98 
99  for (i = 0; i < NUM_BLOCKS; i++) {
100  MEM_freeN(p[i]);
101  }
102 
103  /* ----------------------------------------------------------------- */
104  /* Round two, do a normal allocation, and corrupt some blocks. */
105  /* ----------------------------------------------------------------- */
106  /* switch off, because it will complain about some things. */
108 
109  for (i = 0; i < NUM_BLOCKS; i++) {
110  int blocksize = 10000;
111  char tagstring[1000];
112  if (verbose > 1)
113  printf("|--* Allocating block %d\n", i);
114  sprintf(tagstring, "Memblock no. %d : ", i);
115  p[i] = MEM_callocN(blocksize, strdup(tagstring));
116  }
117 
118  /* now corrupt a few blocks...*/
119  ip = (int *)p[5] - 50;
120  for (i = 0; i < 1000; i++, ip++)
121  *ip = i + 1;
122  ip = (int *)p[6];
123  *(ip + 10005) = 0;
124 
125  retval = MEM_consistency_check();
126 
127  /* the test should have failed */
128  error_status |= !retval;
129  if (verbose) {
130  if (retval) {
131  fprintf(stderr, "|--* Memory test failed (as it should be)\n");
132  }
133  else {
134  fprintf(stderr, "|--* Memory test FAILED to find corrupted blocks \n");
135  }
136  }
137 
138  for (i = 0; i < NUM_BLOCKS; i++) {
139  MEM_freeN(p[i]);
140  }
141 
142  if (verbose && error_status) {
143  fprintf(stderr, "|--* Memory was corrupted\n");
144  }
145  /* ----------------------------------------------------------------- */
146  if (verbose) {
147  if (error_status) {
148  fprintf(stderr, "|\n|--* Errors were detected\n");
149  }
150  else {
151  fprintf(stderr, "|\n|--* Test exited successfully\n");
152  }
153 
154  fprintf(stderr, "|\n*** Finished test\n\n");
155  }
156  return error_status;
157 }
Read Guarded memory(de)allocation.
static int verbose
Definition: cineonlib.c:44
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void(* MEM_set_error_callback)(void(*func)(const char *))
Definition: mallocn.c:56
bool(* MEM_consistency_check)(void)
Definition: mallocn.c:57
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void(* MEM_printmemlist)(void)
Definition: mallocn.c:53
int main(int argc, char *argv[])
Definition: memtest.c:43
static void mem_error_cb(const char *errorStr)
Definition: memtest.c:37
#define NUM_BLOCKS
Definition: memtest.c:30