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