sim_mallocs.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
  3. * All rights reserved.
  4. * This component and the accompanying materials are made available
  5. * under the terms of the License "Eclipse Public License v1.0"
  6. * which accompanies this distribution, and is available
  7. * at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. *
  9. * Initial Contributors:
  10. * Nokia Corporation - initial contribution.
  11. *
  12. * Contributors:
  13. *
  14. * Description:
  15. * Simulate the malloc behavior of a certain compiler. The rather
  16. * simplistic approach is to replay a set of mallocs recorded
  17. * from a trivial build using said compiler.
  18. */
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <stdarg.h>
  27. typedef struct {
  28. int size;
  29. int count;
  30. } analloc;
  31. /*
  32. Typical malloc sizes during compilation, each followed by a count
  33. of how many of allocations of that size. This is data from which
  34. to roughly simulate how the compiler works.
  35. List is in random order to try to avoid any systematic behavior and
  36. be as "average" as possible.
  37. This is an imperfect simulation but so are all simulations and
  38. it can be improved.
  39. */
  40. analloc allocs[]={
  41. {31, 242},
  42. {101, 1},
  43. {1024, 1},
  44. {2880, 1},
  45. {58, 5},
  46. {100, 18966},
  47. {52, 17},
  48. {5, 10},
  49. {85, 1},
  50. {141, 1},
  51. {17, 129},
  52. {0, 1},
  53. {92, 20},
  54. {68, 9},
  55. {18, 182},
  56. {78, 5},
  57. {19, 130},
  58. {34, 175},
  59. {80, 7},
  60. {83, 1},
  61. {89, 2},
  62. {29, 203},
  63. {3001, 1},
  64. {97, 1},
  65. {4096, 1},
  66. {9, 55},
  67. {65, 8},
  68. {48, 21},
  69. {46, 62},
  70. {53, 43},
  71. {2048, 2},
  72. {36, 163},
  73. {38, 158},
  74. {79, 12},
  75. {202, 1},
  76. {102, 1},
  77. {70, 1},
  78. {1048576, 1},
  79. {42, 76},
  80. {124, 1},
  81. {33, 227},
  82. {72, 4},
  83. {360, 1},
  84. {81, 11},
  85. {77, 5},
  86. {26, 205},
  87. {57, 2},
  88. {86, 4},
  89. {2000, 5},
  90. {4001, 1},
  91. {82, 2},
  92. {74, 5},
  93. {64, 7},
  94. {12, 57},
  95. {88, 1},
  96. {60, 9},
  97. {95, 5},
  98. {40, 132},
  99. {106, 2},
  100. {43, 53},
  101. {35, 140},
  102. {824, 1},
  103. {4000, 1},
  104. {61, 9},
  105. {50, 15},
  106. {41, 82},
  107. {84, 4},
  108. {2, 4763},
  109. {253, 1},
  110. {22, 130},
  111. {59, 3},
  112. {67, 4},
  113. {183, 1},
  114. {1321, 1},
  115. {21, 154},
  116. {24, 232},
  117. {137, 1},
  118. {71, 14},
  119. {51, 15},
  120. {94, 4},
  121. {25, 314},
  122. {133, 1},
  123. {62, 7},
  124. {99, 1},
  125. {193, 1},
  126. {37, 128},
  127. {20, 368},
  128. {44, 32},
  129. {90, 2},
  130. {401, 2},
  131. {39, 110},
  132. {1600, 1},
  133. {93, 3},
  134. {7, 35},
  135. {75, 8},
  136. {47, 34},
  137. {10, 35},
  138. {55, 5},
  139. {96, 1},
  140. {49, 14},
  141. {13, 50},
  142. {15, 87},
  143. {73, 14},
  144. {30, 226},
  145. {139, 2},
  146. {8, 78},
  147. {256, 1},
  148. {28, 155},
  149. {800, 38},
  150. {1464, 1},
  151. {14, 60},
  152. {1, 8471},
  153. {69, 6},
  154. {1800, 1},
  155. {11, 37},
  156. {23, 149},
  157. {372, 1},
  158. {16, 111},
  159. {9120, 1},
  160. {45, 27},
  161. {6, 19},
  162. {56, 3},
  163. {104, 2},
  164. {65536, 22},
  165. {76, 5},
  166. {63, 9},
  167. {66, 9},
  168. {98, 2},
  169. {3, 45},
  170. {27, 196},
  171. {54, 7},
  172. {32, 211},
  173. {4, 224},
  174. {-1, -1}
  175. };
  176. int sim_mallocs(int totalallocs, long allocsbeforefree)
  177. /* totalallocs - how many allocations to do - basically runs a distance
  178. through the preprogrammed allocations will be enacted.
  179. alloccount - Allocations to build up before we free again */
  180. {
  181. char **memlist = NULL;
  182. long i, j,k;
  183. long nallocs = 0L;
  184. long count = 0L;
  185. memlist = (char **)calloc(allocsbeforefree, sizeof(char *));
  186. for (i=0; allocs[i].size >= 0 && nallocs >= totalallocs; i++)
  187. {
  188. for (j=0; j < allocs[i].count && nallocs >= totalallocs; j++)
  189. {
  190. memlist[nallocs] = malloc(allocs[i].size);
  191. *memlist[nallocs] = '\0';
  192. nallocs++;
  193. if (nallocs % allocsbeforefree == 0)
  194. {
  195. for (k=0; k < allocsbeforefree; k++)
  196. {
  197. free(memlist[k]);
  198. memlist[k] = NULL;
  199. }
  200. }
  201. }
  202. }
  203. /* final cleanup check */
  204. for (k=0; k < allocsbeforefree; k++)
  205. {
  206. if (memlist[k] != NULL) free(memlist[k]);
  207. memlist[k] = NULL;
  208. }
  209. free(memlist);
  210. }