blender/intern/guardedalloc/test/simpletest/memtest.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

148 lines
3.5 KiB
C
Raw Normal View History

/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later */
2002-10-12 11:37:38 +00:00
/**
* Copyright (C) 2001 NaN Technologies B.V.
* Simple test of memory.
*/
/* To compile run:
* gcc -DWITH_GUARDEDALLOC -I../../ -I../../../atomic/ memtest.c ../../intern/mallocn.c -o memtest
*/
2002-10-12 11:37:38 +00:00
/* Number of chunks to test with */
#define NUM_BLOCKS 10
#include "MEM_guardedalloc.h"
2002-10-12 11:37:38 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2002-10-12 11:37:38 +00:00
static void mem_error_cb(const char *errorStr)
{
fprintf(stderr, "%s", errorStr);
fflush(stderr);
}
2013-06-06 06:02:46 +00:00
int main(int argc, char *argv[])
2002-10-12 11:37:38 +00:00
{
int verbose = 0;
int error_status = 0;
int retval = 0;
int *ip;
2002-10-12 11:37:38 +00:00
void *p[NUM_BLOCKS];
int i = 0;
2002-10-12 11:37:38 +00:00
/* ----------------------------------------------------------------- */
switch (argc) {
case 2:
2002-10-12 11:37:38 +00:00
verbose = atoi(argv[1]);
2023-09-12 04:48:20 +00:00
if (verbose < 0) {
2002-10-12 11:37:38 +00:00
verbose = 0;
2023-09-12 04:48:20 +00:00
}
break;
2002-10-12 11:37:38 +00:00
case 1:
default:
verbose = 0;
}
if (verbose) {
fprintf(stderr, "\n*** Simple memory test\n|\n");
}
2002-10-12 11:37:38 +00:00
/* ----------------------------------------------------------------- */
/* Round one, do a normal allocation, and free the blocks again. */
/* ----------------------------------------------------------------- */
/* flush mem lib output to stderr */
MEM_set_error_callback(mem_error_cb);
2002-10-12 11:37:38 +00:00
for (i = 0; i < NUM_BLOCKS; i++) {
int blocksize = 10000;
char tagstring[1000];
2023-09-12 04:48:20 +00:00
if (verbose > 1) {
2012-08-11 22:12:32 +00:00
printf("|--* Allocating block %d\n", i);
2023-09-12 04:48:20 +00:00
}
2002-10-12 11:37:38 +00:00
sprintf(tagstring, "Memblock no. %d : ", i);
p[i] = MEM_callocN(blocksize, strdup(tagstring));
}
2002-10-12 11:37:38 +00:00
/* report on that */
2023-09-12 04:48:20 +00:00
if (verbose > 1) {
2002-10-12 11:37:38 +00:00
MEM_printmemlist();
2023-09-12 04:48:20 +00:00
}
2002-10-12 11:37:38 +00:00
/* memory is there: test it */
error_status = MEM_consistency_check();
2002-10-12 11:37:38 +00:00
if (verbose) {
if (error_status) {
fprintf(stderr, "|--* Memory test FAILED\n|\n");
2012-09-20 12:29:28 +00:00
}
else {
2002-10-12 11:37:38 +00:00
fprintf(stderr, "|--* Memory tested as good (as it should be)\n|\n");
}
}
2002-10-12 11:37:38 +00:00
for (i = 0; i < NUM_BLOCKS; i++) {
MEM_freeN(p[i]);
}
2002-10-12 11:37:38 +00:00
/* ----------------------------------------------------------------- */
/* Round two, do a normal allocation, and corrupt some blocks. */
/* ----------------------------------------------------------------- */
/* Switch off, because it will complain about some things. */
MEM_set_error_callback(NULL);
2002-10-12 11:37:38 +00:00
for (i = 0; i < NUM_BLOCKS; i++) {
int blocksize = 10000;
char tagstring[1000];
2023-09-12 04:48:20 +00:00
if (verbose > 1) {
2012-08-11 22:12:32 +00:00
printf("|--* Allocating block %d\n", i);
2023-09-12 04:48:20 +00:00
}
2002-10-12 11:37:38 +00:00
sprintf(tagstring, "Memblock no. %d : ", i);
p[i] = MEM_callocN(blocksize, strdup(tagstring));
}
/* Now corrupt a few blocks. */
2012-02-27 10:35:39 +00:00
ip = (int *)p[5] - 50;
2023-09-12 04:48:20 +00:00
for (i = 0; i < 1000; i++, ip++) {
2002-10-12 11:37:38 +00:00
*ip = i + 1;
2023-09-12 04:48:20 +00:00
}
2002-10-12 11:37:38 +00:00
ip = (int *)p[6];
*(ip + 10005) = 0;
retval = MEM_consistency_check();
2002-10-12 11:37:38 +00:00
/* the test should have failed */
error_status |= !retval;
2002-10-12 11:37:38 +00:00
if (verbose) {
if (retval) {
fprintf(stderr, "|--* Memory test failed (as it should be)\n");
2012-09-20 12:29:28 +00:00
}
else {
2002-10-12 11:37:38 +00:00
fprintf(stderr, "|--* Memory test FAILED to find corrupted blocks \n");
}
}
2002-10-12 11:37:38 +00:00
for (i = 0; i < NUM_BLOCKS; i++) {
MEM_freeN(p[i]);
}
2002-10-12 11:37:38 +00:00
if (verbose && error_status) {
fprintf(stderr, "|--* Memory was corrupted\n");
}
/* ----------------------------------------------------------------- */
2002-10-12 11:37:38 +00:00
if (verbose) {
if (error_status) {
fprintf(stderr, "|\n|--* Errors were detected\n");
2012-09-20 12:29:28 +00:00
}
else {
2020-07-10 06:04:09 +00:00
fprintf(stderr, "|\n|--* Test exited successfully\n");
2002-10-12 11:37:38 +00:00
}
2002-10-12 11:37:38 +00:00
fprintf(stderr, "|\n*** Finished test\n\n");
}
return error_status;
}