tstheap.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <limits.h>
  5. #include <memory.h>
  6. #ifndef DUMA_SO_LIBRARY
  7. #include "../duma.h"
  8. #endif
  9. /*
  10. * This is a simple program to exercise the allocator. It allocates and frees
  11. * memory in a pseudo-random fashion. It should run silently, using up time
  12. * and resources on your system until you stop it or until it has gone
  13. * through TEST_DURATION (or the argument) iterations of the loop.
  14. */
  15. #ifdef __cplusplus
  16. extern "C"
  17. #else
  18. extern
  19. #endif
  20. double drand48(void); /* For pre-ANSI C systems */
  21. #ifdef WIN32
  22. #define FAKE_DRAND48
  23. #endif
  24. #define ALIGNMENT 8192
  25. #undef ALIGNMENT
  26. #define POOL_SIZE 1024
  27. #define LARGEST_BUFFER 30000
  28. #define TEST_DURATION 1000000
  29. struct POOL_ELEM
  30. {
  31. void * addr;
  32. size_t size;
  33. }
  34. pool[POOL_SIZE];
  35. #ifdef FAKE_DRAND48
  36. /*
  37. * Add -DFAKE_DRAND48 to your compile flags if your system doesn't
  38. * provide drand48().
  39. */
  40. #ifndef ULONG_MAX
  41. #define ULONG_MAX ~(1L)
  42. #endif
  43. double
  44. drand48(void)
  45. {
  46. #ifdef WIN32
  47. return (double)rand()/((double)RAND_MAX);
  48. #else
  49. return (random() / (double)ULONG_MAX);
  50. #endif
  51. }
  52. #endif
  53. int
  54. main(int argc, char * * argv)
  55. {
  56. int count;
  57. int duration = TEST_DURATION;
  58. #ifdef DUMA_EXPLICIT_INIT
  59. duma_init();
  60. #endif
  61. if ( argc >= 2 )
  62. duration = atoi(argv[1]);
  63. for ( count = 0; count < POOL_SIZE; count++ )
  64. {
  65. pool[count].addr = (void*)0;
  66. pool[count].size = (size_t)0;
  67. }
  68. for ( count = 0; count < duration; count++ )
  69. {
  70. int pool_idx;
  71. struct POOL_ELEM * element;
  72. size_t size;
  73. pool_idx =(int)(drand48() * POOL_SIZE);
  74. if (pool_idx >=0 && pool_idx<POOL_SIZE)
  75. {
  76. element = &pool[pool_idx];
  77. size = (size_t)(drand48() * (LARGEST_BUFFER + 1));
  78. if ( element->addr )
  79. {
  80. /* check if memory is accessible */
  81. memset( element->addr, 0, element->size );
  82. free( element->addr );
  83. element->addr = (void*)0;
  84. }
  85. if ( size > 0 )
  86. {
  87. #ifdef ALIGNMENT
  88. element->addr = memalign(ALIGNMENT,size);
  89. #else
  90. element->addr = malloc(size);
  91. #endif
  92. element->size = size;
  93. /* really use it, so that the system has to use real memory */
  94. memset( element->addr, -1, size );
  95. }
  96. }
  97. }
  98. #if 1
  99. /* don't forget to free the allocated memory, else the
  100. confidence test won't pass - without having set
  101. "EF_NO_LEAKDETECTION" preprocessor definition
  102. */
  103. for ( count = 0; count < POOL_SIZE; count++ )
  104. {
  105. if ( pool[count].addr )
  106. {
  107. /* check if memory is accessible */
  108. memset( pool[count].addr, 0, pool[count].size );
  109. free( pool[count].addr );
  110. }
  111. }
  112. #endif
  113. return 0;
  114. }