Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
1.8 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <malloc.h>
  6. //
  7. // 'tstheap.c'
  8. // Tests out malloc, calloc, realloc, and mfree.
  9. //
  10. // 05/22/92 DarekM Created
  11. //
  12. #define max(a,b) ((a > b) ? a : b )
  13. int
  14. main(int argc, char *argv[])
  15. {
  16. int l; // loop counter
  17. int t; // total memory allocated
  18. int c; // count of blocks
  19. int i; // current block index
  20. void *p; // address of block
  21. int numblocks;
  22. int delta;
  23. void **rgp; // array of memory pointers
  24. int fDEBUG = 0;
  25. if (argc < 3)
  26. {
  27. printf("Usage: tstheap <numblocks> <delta> [DUMP]\n\n");
  28. return;
  29. }
  30. else if (argc > 3)
  31. fDEBUG = 1;
  32. numblocks = max(1, atoi(argv[1]));
  33. delta = max(1, atoi(argv[2]));
  34. rgp = malloc(numblocks * sizeof(void *));
  35. printf("TstHeap: numblocks = %d, delta = %d\n\n", numblocks, delta);
  36. for (l = 0; ; l++)
  37. {
  38. t = c = 0;
  39. printf("PASS #%d\n", l);
  40. for (i = 0; i < numblocks; i++)
  41. {
  42. int cb;
  43. if (i & 1)
  44. p = malloc(cb = i + l*delta + 1);
  45. else
  46. p = calloc(cb = i + l*delta + (rand() & 255) + 1, 1);
  47. if (p == NULL)
  48. {
  49. printf("p == NULL\n");
  50. break;
  51. }
  52. if (((int)p < 0x1000) || ((int)p < 0))
  53. {
  54. printf("WIERD P == %d\n", p);
  55. break;
  56. }
  57. rgp[i] = p;
  58. t += cb;
  59. if (fDEBUG)
  60. printf(" %d,%02d: Alloced $%08X\n", l, i, p);
  61. }
  62. if ((c = i) == 0)
  63. break;
  64. printf(" Blocks alloced: %d Bytes: %d\n", c, t);
  65. for (i = 0; i < c; i++)
  66. {
  67. rgp[i] = p = realloc(rgp[i], 1);
  68. if (fDEBUG)
  69. printf(" %d,%02d: Realloced $%08X\n", l, i, p);
  70. }
  71. printf(" Blocks realloced: %d\n", i);
  72. for (i = 0; i < c; i++)
  73. {
  74. free(rgp[i]);
  75. if (fDEBUG)
  76. printf(" %d,%02d: Freed $%08X\n", l, i, rgp[i]);
  77. }
  78. printf(" Blocks freed: %d\n\n", i);
  79. }
  80. printf("\n\n");
  81. free(rgp);
  82. return 1;
  83. }