Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <sys/time.h>
  5. #include <math.h>
  6. #include "mymalloc.h"
  7.  
  8. //malloc() 1 byte and immediately free it - do this 150 times
  9. int memgrindA() {
  10. void* pointerArray[150];
  11. int i = 0;
  12. while(i < 150){
  13. pointerArray[i] = (void *)(uintptr_t)malloc(1);
  14. free(pointerArray[i]);
  15. i++;
  16. }
  17. return 0;
  18. }
  19.  
  20. //malloc() 1 byte, store the pointer in an array - do this 150 times.
  21. //Once you've malloc()ed 150 byte chunks, then free() the 150 1 byte pointers one by one.
  22. int memgrindB() {
  23. void *pointerArray[150];
  24. int i = 0;
  25. while(i < 150){
  26. pointerArray[i] = (void *)(uintptr_t)malloc(1);
  27. i++;
  28. }
  29. i = 0;
  30. while(i < 150){
  31. free(pointerArray[i]);
  32. i++;
  33. }
  34. return 0;
  35. }
  36.  
  37. //Randomly choose between a 1 byte malloc() or free()ing a 1 byte pointer - do this 150 times
  38. //Keep track of each operation so that you eventually malloc() 150 bytes, in total
  39. //Keep track of each operation so that you eventually free() all pointers
  40. //(don't allow a free() if you have no pointers to free())
  41. int memgrindC() {
  42. void* pointerArray[150];
  43. int malcount = 0;
  44. int freecount = 0;
  45.  
  46. while(malcount < 150){
  47. if(rand() % 2 == 0 && malcount < 150){
  48. pointerArray[malcount] = (void *)(uintptr_t)malloc(1);
  49. if(pointerArray[malcount] != NULL){
  50. malcount++;
  51. }
  52. }
  53. else{
  54. if(freecount < malcount){
  55. free(pointerArray[freecount]);
  56. freecount++;
  57. }
  58. }
  59. }
  60. while(freecount < malcount){
  61. free(pointerArray[freecount]);
  62. freecount++;
  63. }
  64. return 0;
  65. }
  66.  
  67. //Randomly choose between a randomly-sized malloc() or free()ing a pointer do this many times
  68. //Keep track of each malloc so that all mallocs do not exceed your total memory capacity
  69. //Keep track of each operation so that you eventually malloc() 150 times
  70. //Keep track of each operation so that you eventually free() all pointers
  71. //Choose a random allocation size between 1 and 64 bytes
  72. int memgrindD() {
  73. void* pointerArray[150];
  74. int malSizeArray[150];
  75. int malcount = 0;
  76. int freecount = 0;
  77. int malsize = 0;
  78.  
  79. while(malcount < 150){
  80. if(rand() % 2 == 0 && malcount < 150 && malsize < memoryBlockSize){
  81. int randomSize = (rand() % 64) + 1;
  82. pointerArray[malcount] = (void *)(uintptr_t)malloc(randomSize);
  83. if(pointerArray[malcount] != NULL){
  84. malSizeArray[malcount] = randomSize + blockSize;
  85. malsize += randomSize + blockSize;
  86. malcount++;
  87. }
  88. }
  89. else{
  90. if(freecount < malcount){
  91. malsize -= malSizeArray[freecount];
  92. free(pointerArray[freecount]);
  93. freecount++;
  94. }
  95. }
  96. }
  97. while(freecount < malcount){
  98. free(pointerArray[freecount]);
  99. malsize -= malSizeArray[freecount];
  100. freecount++;
  101. }
  102. }
  103.  
  104. //malloc until full
  105. //free every other block
  106. //remalloc freed blocks
  107. //free all the blocks
  108. //test how malloc how efficient it handles empty space
  109. int memgrindE() {
  110. int numBlocks = 1;
  111. int malcount = 0;
  112. int freecount = 0;
  113. void* pointerArray[150];
  114. while((pointerArray[malcount] = (void *)(uintptr_t)malloc(2)) != NULL){
  115. numBlocks++;
  116. malcount++;
  117. }
  118. for(freecount = 0; freecount < malcount; freecount += 2){
  119. free(pointerArray[freecount]);
  120. pointerArray[freecount] = NULL;
  121. }
  122. for(malcount = 0; malcount < numBlocks - 1; malcount +=2){
  123. pointerArray[malcount] = (void *)(uintptr_t)malloc(1);
  124. }
  125. for(freecount = 0; freecount < numBlocks - 1; freecount++){
  126. free(pointerArray[freecount]);
  127. pointerArray[freecount] = NULL;
  128. }
  129.  
  130. return 0;
  131. }
  132.  
  133. //malloc blocks with bytes that increase in powers of two
  134. //test to see if it can malloc bytes efficiently when it is doubling in size
  135. //test to see if it can free bytes efficiently when it is doubling in size
  136. int memgrindF() {
  137. void* pointerArray[150];
  138. int i = 0;
  139. int numBytes = 1;
  140.  
  141. while(i < 150){
  142. pointerArray[i] = (void *)(uintptr_t)malloc(numBytes);
  143. if(pointerArray[i] == NULL){
  144. break;
  145. }
  146. else{
  147. numBytes = numBytes * 2;
  148. i++;
  149. }
  150. }
  151. i = 0;
  152. while(i < 150){
  153. free(pointerArray[i]);
  154. i++;
  155. }
  156. return 0;
  157. }
  158.  
  159. void averageRunTime(int(memgrind)()){
  160. struct timeval t0;
  161. struct timeval t1;
  162. long int averageTime = 0;
  163. int t = 0;
  164. while(t < 100){
  165. gettimeofday(&t0,0);
  166. memgrind();
  167. gettimeofday(&t1,0);
  168. averageTime += (t1.tv_sec - t0.tv_sec) * 1000000 + (t1.tv_usec - t0.tv_usec);
  169. }
  170. averageTime /= 100;
  171. int grind = 1;
  172. for(grind = 1; grind < 7; grind++){
  173. print("The average running time of memgrind %d was %ld ms.\n", grind, averageTime);
  174. }
  175. }
  176.  
  177. int main() {
  178. averageRunTime(memgrindA);
  179. averageRunTime(memgrindB);
  180. averageRunTime(memgrindC);
  181. averageRunTime(memgrindD);
  182. averageRunTime(memgrindE);
  183. averageRunTime(memgrindF);
  184. return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement