Blender  V3.3
memtest.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
9 /* To compile run:
10  * gcc -DWITH_GUARDEDALLOC -I../../ -I../../../atomic/ memtest.c ../../intern/mallocn.c -o memtest
11  */
12 
13 /* Number of chunks to test with */
14 #define NUM_BLOCKS 10
15 
16 #include "MEM_guardedalloc.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 
21 static void mem_error_cb(const char *errorStr)
22 {
23  fprintf(stderr, "%s", errorStr);
24  fflush(stderr);
25 }
26 
27 int main(int argc, char *argv[])
28 {
29  int verbose = 0;
30  int error_status = 0;
31  int retval = 0;
32  int *ip;
33 
34  void *p[NUM_BLOCKS];
35  int i = 0;
36 
37  /* ----------------------------------------------------------------- */
38  switch (argc) {
39  case 2:
40  verbose = atoi(argv[1]);
41  if (verbose < 0)
42  verbose = 0;
43  break;
44  case 1:
45  default:
46  verbose = 0;
47  }
48  if (verbose) {
49  fprintf(stderr, "\n*** Simple memory test\n|\n");
50  }
51 
52  /* ----------------------------------------------------------------- */
53  /* Round one, do a normal allocation, and free the blocks again. */
54  /* ----------------------------------------------------------------- */
55  /* flush mem lib output to stderr */
57 
58  for (i = 0; i < NUM_BLOCKS; i++) {
59  int blocksize = 10000;
60  char tagstring[1000];
61  if (verbose > 1)
62  printf("|--* Allocating block %d\n", i);
63  sprintf(tagstring, "Memblock no. %d : ", i);
64  p[i] = MEM_callocN(blocksize, strdup(tagstring));
65  }
66 
67  /* report on that */
68  if (verbose > 1)
70 
71  /* memory is there: test it */
72  error_status = MEM_consistency_check();
73 
74  if (verbose) {
75  if (error_status) {
76  fprintf(stderr, "|--* Memory test FAILED\n|\n");
77  }
78  else {
79  fprintf(stderr, "|--* Memory tested as good (as it should be)\n|\n");
80  }
81  }
82 
83  for (i = 0; i < NUM_BLOCKS; i++) {
84  MEM_freeN(p[i]);
85  }
86 
87  /* ----------------------------------------------------------------- */
88  /* Round two, do a normal allocation, and corrupt some blocks. */
89  /* ----------------------------------------------------------------- */
90  /* Switch off, because it will complain about some things. */
92 
93  for (i = 0; i < NUM_BLOCKS; i++) {
94  int blocksize = 10000;
95  char tagstring[1000];
96  if (verbose > 1)
97  printf("|--* Allocating block %d\n", i);
98  sprintf(tagstring, "Memblock no. %d : ", i);
99  p[i] = MEM_callocN(blocksize, strdup(tagstring));
100  }
101 
102  /* Now corrupt a few blocks. */
103  ip = (int *)p[5] - 50;
104  for (i = 0; i < 1000; i++, ip++)
105  *ip = i + 1;
106  ip = (int *)p[6];
107  *(ip + 10005) = 0;
108 
109  retval = MEM_consistency_check();
110 
111  /* the test should have failed */
112  error_status |= !retval;
113  if (verbose) {
114  if (retval) {
115  fprintf(stderr, "|--* Memory test failed (as it should be)\n");
116  }
117  else {
118  fprintf(stderr, "|--* Memory test FAILED to find corrupted blocks \n");
119  }
120  }
121 
122  for (i = 0; i < NUM_BLOCKS; i++) {
123  MEM_freeN(p[i]);
124  }
125 
126  if (verbose && error_status) {
127  fprintf(stderr, "|--* Memory was corrupted\n");
128  }
129  /* ----------------------------------------------------------------- */
130  if (verbose) {
131  if (error_status) {
132  fprintf(stderr, "|\n|--* Errors were detected\n");
133  }
134  else {
135  fprintf(stderr, "|\n|--* Test exited successfully\n");
136  }
137 
138  fprintf(stderr, "|\n*** Finished test\n\n");
139  }
140  return error_status;
141 }
Read Guarded memory(de)allocation.
static int verbose
Definition: cineonlib.c:29
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void(* MEM_set_error_callback)(void(*func)(const char *))
Definition: mallocn.c:42
bool(* MEM_consistency_check)(void)
Definition: mallocn.c:43
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void(* MEM_printmemlist)(void)
Definition: mallocn.c:39
int main(int argc, char *argv[])
Definition: memtest.c:27
static void mem_error_cb(const char *errorStr)
Definition: memtest.c:21
#define NUM_BLOCKS
Definition: memtest.c:14