mallocTest.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. int test_numbers[] =
  5. {
  6. 100,
  7. 200,
  8. 300,
  9. 400,
  10. 500,
  11. 1000,
  12. 2000,
  13. 3000,
  14. 4000,
  15. 5000,
  16. 10000,
  17. 15000,
  18. 20000,
  19. 25000,
  20. 30000,
  21. 35000,
  22. 40000,
  23. 45000,
  24. 50000,
  25. 60000,
  26. 70000,
  27. 80000,
  28. 90000,
  29. 100000,
  30. 200000,
  31. 0
  32. };
  33. typedef struct test_node_s test_node_t;
  34. struct test_node_s
  35. {
  36. test_node_t *next;
  37. };
  38. static test_node_t *first_node;
  39. /*--------------------------------------------------------- */
  40. /*
  41. * Returns a pseudo random number based on the input.
  42. * This will given 2^15 concequtive numbers generate the entire
  43. * number space in a psedo random manner.
  44. */
  45. static int
  46. pseudo_random(int i)
  47. {
  48. /* multiply the first prime after 2^14 and mask to 15 bits */
  49. return (16411*i) & (32767);
  50. }
  51. /*--------------------------------------------------------- */
  52. static void
  53. create_nodes(unsigned count,int verbose)
  54. {
  55. test_node_t *node;
  56. int i;
  57. if (verbose) printf("Create %d random sized blocks...",count);
  58. for(i = 0; i < count; i++)
  59. {
  60. /* pick a random size upto 4096 bytes */
  61. size_t size = 1 + 4096 * pseudo_random(i) / 32768;
  62. node = malloc(size);
  63. if(NULL != node)
  64. {
  65. /* put this into the linked list for later */
  66. node->next = first_node;
  67. first_node = node;
  68. }
  69. else
  70. {
  71. break;
  72. }
  73. }
  74. if (verbose) printf("done\n");
  75. }
  76. /*--------------------------------------------------------- */
  77. static void
  78. destroy_nodes(int count,
  79. int skip,
  80. int verbose)
  81. {
  82. test_node_t **ptr = &first_node;
  83. int i;
  84. if (verbose) printf("Fragment by destroying %d blocks...",count / skip);
  85. for(i = 0;
  86. *ptr && (*ptr)->next && (i < count);
  87. i++, ptr = &(*ptr)->next)
  88. {
  89. if(i % skip)
  90. {
  91. char *tmp = (char*)*ptr;
  92. /* remove from the linked list */
  93. *ptr = (*ptr)->next;
  94. /* and free the node */
  95. free(tmp);
  96. }
  97. }
  98. if (verbose) printf("done\n");
  99. }
  100. /*--------------------------------------------------------- */
  101. void
  102. mallocTest(int number)
  103. {
  104. struct timespec start_time;
  105. struct timespec end_time;
  106. struct timespec delta_time;
  107. int *num_allocs;
  108. int single_shot[] =
  109. {
  110. 0,0
  111. };
  112. if(0 == number)
  113. {
  114. num_allocs = test_numbers;
  115. }
  116. else
  117. {
  118. num_allocs = single_shot;
  119. single_shot[0] = number;
  120. }
  121. while(*num_allocs)
  122. {
  123. int count = *num_allocs++;
  124. clock_gettime(CLOCK_REALTIME,&start_time);
  125. create_nodes(count,single_shot[0]);
  126. if (single_shot[0]) printf("Now free every other node\n");
  127. destroy_nodes(count,2,single_shot[0]);
  128. create_nodes(count >> 1,single_shot[0]);
  129. if(single_shot[0])
  130. {
  131. printf("Now free all the nodes...");
  132. }
  133. while(first_node)
  134. {
  135. test_node_t *node = first_node;
  136. first_node = node->next;
  137. free(node);
  138. }
  139. if (single_shot[0]) printf("done\n");
  140. clock_gettime(CLOCK_REALTIME,&end_time);
  141. delta_time.tv_sec = end_time.tv_sec - start_time.tv_sec;
  142. delta_time.tv_nsec = end_time.tv_nsec - start_time.tv_nsec;
  143. printf("*** %6d in %10lu milliseconds\n",count,
  144. delta_time.tv_sec * 1000 + delta_time.tv_nsec/1000000);
  145. }
  146. }
  147. /*--------------------------------------------------------- */
  148. int
  149. main(int argc, char **argv)
  150. {
  151. unsigned number = 0;
  152. if(argc > 1)
  153. {
  154. number = atoi(argv[1]);
  155. }
  156. mallocTest(number);
  157. return 0;
  158. }
  159. /*--------------------------------------------------------- */