memtest.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /**
  20. * Copyright (C) 2001 NaN Technologies B.V.
  21. * Simple test of memory.
  22. */
  23. /* To compile run:
  24. * gcc -DWITH_GUARDEDALLOC -I../../ -I../../../atomic/ memtest.c ../../intern/mallocn.c -o memtest
  25. */
  26. /* Number of chunks to test with */
  27. #define NUM_BLOCKS 10
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include "MEM_guardedalloc.h"
  32. static void mem_error_cb(const char *errorStr)
  33. {
  34. fprintf(stderr, "%s", errorStr);
  35. fflush(stderr);
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39. int verbose = 0;
  40. int error_status = 0;
  41. int retval = 0;
  42. int *ip;
  43. void *p[NUM_BLOCKS];
  44. int i = 0;
  45. /* ----------------------------------------------------------------- */
  46. switch (argc) {
  47. case 2:
  48. verbose = atoi(argv[1]);
  49. if (verbose < 0)
  50. verbose = 0;
  51. break;
  52. case 1:
  53. default:
  54. verbose = 0;
  55. }
  56. if (verbose) {
  57. fprintf(stderr, "\n*** Simple memory test\n|\n");
  58. }
  59. /* ----------------------------------------------------------------- */
  60. /* Round one, do a normal allocation, and free the blocks again. */
  61. /* ----------------------------------------------------------------- */
  62. /* flush mem lib output to stderr */
  63. MEM_set_error_callback(mem_error_cb);
  64. for (i = 0; i < NUM_BLOCKS; i++) {
  65. int blocksize = 10000;
  66. char tagstring[1000];
  67. if (verbose > 1)
  68. printf("|--* Allocating block %d\n", i);
  69. sprintf(tagstring, "Memblock no. %d : ", i);
  70. p[i] = MEM_callocN(blocksize, strdup(tagstring));
  71. }
  72. /* report on that */
  73. if (verbose > 1)
  74. MEM_printmemlist();
  75. /* memory is there: test it */
  76. error_status = MEM_consistency_check();
  77. if (verbose) {
  78. if (error_status) {
  79. fprintf(stderr, "|--* Memory test FAILED\n|\n");
  80. }
  81. else {
  82. fprintf(stderr, "|--* Memory tested as good (as it should be)\n|\n");
  83. }
  84. }
  85. for (i = 0; i < NUM_BLOCKS; i++) {
  86. MEM_freeN(p[i]);
  87. }
  88. /* ----------------------------------------------------------------- */
  89. /* Round two, do a normal allocation, and corrupt some blocks. */
  90. /* ----------------------------------------------------------------- */
  91. /* switch off, because it will complain about some things. */
  92. MEM_set_error_callback(NULL);
  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. /* now corrupt a few blocks...*/
  102. ip = (int *)p[5] - 50;
  103. for (i = 0; i < 1000; i++, ip++)
  104. *ip = i + 1;
  105. ip = (int *)p[6];
  106. *(ip + 10005) = 0;
  107. retval = MEM_consistency_check();
  108. /* the test should have failed */
  109. error_status |= !retval;
  110. if (verbose) {
  111. if (retval) {
  112. fprintf(stderr, "|--* Memory test failed (as it should be)\n");
  113. }
  114. else {
  115. fprintf(stderr, "|--* Memory test FAILED to find corrupted blocks \n");
  116. }
  117. }
  118. for (i = 0; i < NUM_BLOCKS; i++) {
  119. MEM_freeN(p[i]);
  120. }
  121. if (verbose && error_status) {
  122. fprintf(stderr, "|--* Memory was corrupted\n");
  123. }
  124. /* ----------------------------------------------------------------- */
  125. if (verbose) {
  126. if (error_status) {
  127. fprintf(stderr, "|\n|--* Errors were detected\n");
  128. }
  129. else {
  130. fprintf(stderr, "|\n|--* Test exited succesfully\n");
  131. }
  132. fprintf(stderr, "|\n*** Finished test\n\n");
  133. }
  134. return error_status;
  135. }