alloc-pool.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Functions to support a pool of allocatable objects.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. Contributed by Daniel Berlin <dan@cgsoftware.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "alloc-pool.h"
  20. #include "hash-table.h"
  21. #include "hash-map.h"
  22. #define align_eight(x) (((x+7) >> 3) << 3)
  23. /* The internal allocation object. */
  24. typedef struct allocation_object_def
  25. {
  26. #ifdef ENABLE_CHECKING
  27. /* The ID of alloc pool which the object was allocated from. */
  28. ALLOC_POOL_ID_TYPE id;
  29. #endif
  30. union
  31. {
  32. /* The data of the object. */
  33. char data[1];
  34. /* Because we want any type of data to be well aligned after the ID,
  35. the following elements are here. They are never accessed so
  36. the allocated object may be even smaller than this structure.
  37. We do not care about alignment for floating-point types. */
  38. char *align_p;
  39. int64_t align_i;
  40. } u;
  41. } allocation_object;
  42. /* Convert a pointer to allocation_object from a pointer to user data. */
  43. #define ALLOCATION_OBJECT_PTR_FROM_USER_PTR(X) \
  44. ((allocation_object *) (((char *) (X)) \
  45. - offsetof (allocation_object, u.data)))
  46. /* Convert a pointer to user data from a pointer to allocation_object. */
  47. #define USER_PTR_FROM_ALLOCATION_OBJECT_PTR(X) \
  48. ((void *) (((allocation_object *) (X))->u.data))
  49. #ifdef ENABLE_CHECKING
  50. /* Last used ID. */
  51. static ALLOC_POOL_ID_TYPE last_id;
  52. #endif
  53. /* Store information about each particular alloc_pool. Note that this
  54. will underestimate the amount the amount of storage used by a small amount:
  55. 1) The overhead in a pool is not accounted for.
  56. 2) The unallocated elements in a block are not accounted for. Note
  57. that this can at worst case be one element smaller that the block
  58. size for that pool. */
  59. struct alloc_pool_descriptor
  60. {
  61. /* Number of pools allocated. */
  62. unsigned long created;
  63. /* Gross allocated storage. */
  64. unsigned long allocated;
  65. /* Amount of currently active storage. */
  66. unsigned long current;
  67. /* Peak amount of storage used. */
  68. unsigned long peak;
  69. /* Size of element in the pool. */
  70. int elt_size;
  71. };
  72. /* Hashtable mapping alloc_pool names to descriptors. */
  73. static hash_map<const char *, alloc_pool_descriptor> *alloc_pool_hash;
  74. /* For given name, return descriptor, create new if needed. */
  75. static struct alloc_pool_descriptor *
  76. allocate_pool_descriptor (const char *name)
  77. {
  78. if (!alloc_pool_hash)
  79. alloc_pool_hash = new hash_map<const char *, alloc_pool_descriptor> (10);
  80. return &alloc_pool_hash->get_or_insert (name);
  81. }
  82. /* Create a pool of things of size SIZE, with NUM in each block we
  83. allocate. */
  84. alloc_pool
  85. create_alloc_pool (const char *name, size_t size, size_t num)
  86. {
  87. alloc_pool pool;
  88. size_t header_size;
  89. gcc_checking_assert (name);
  90. /* Make size large enough to store the list header. */
  91. if (size < sizeof (alloc_pool_list))
  92. size = sizeof (alloc_pool_list);
  93. /* Now align the size to a multiple of 4. */
  94. size = align_eight (size);
  95. #ifdef ENABLE_CHECKING
  96. /* Add the aligned size of ID. */
  97. size += offsetof (allocation_object, u.data);
  98. #endif
  99. /* Um, we can't really allocate 0 elements per block. */
  100. gcc_checking_assert (num);
  101. /* Allocate memory for the pool structure. */
  102. pool = XNEW (struct alloc_pool_def);
  103. /* Now init the various pieces of our pool structure. */
  104. pool->name = /*xstrdup (name)*/name;
  105. pool->elt_size = size;
  106. pool->elts_per_block = num;
  107. if (GATHER_STATISTICS)
  108. {
  109. struct alloc_pool_descriptor *desc = allocate_pool_descriptor (name);
  110. desc->elt_size = size;
  111. desc->created++;
  112. }
  113. /* List header size should be a multiple of 8. */
  114. header_size = align_eight (sizeof (struct alloc_pool_list_def));
  115. pool->block_size = (size * num) + header_size;
  116. pool->returned_free_list = NULL;
  117. pool->virgin_free_list = NULL;
  118. pool->virgin_elts_remaining = 0;
  119. pool->elts_allocated = 0;
  120. pool->elts_free = 0;
  121. pool->blocks_allocated = 0;
  122. pool->block_list = NULL;
  123. #ifdef ENABLE_CHECKING
  124. /* Increase the last used ID and use it for this pool.
  125. ID == 0 is used for free elements of pool so skip it. */
  126. last_id++;
  127. if (last_id == 0)
  128. last_id++;
  129. pool->id = last_id;
  130. #endif
  131. return (pool);
  132. }
  133. /* Free all memory allocated for the given memory pool. */
  134. void
  135. empty_alloc_pool (alloc_pool pool)
  136. {
  137. alloc_pool_list block, next_block;
  138. gcc_checking_assert (pool);
  139. /* Free each block allocated to the pool. */
  140. for (block = pool->block_list; block != NULL; block = next_block)
  141. {
  142. next_block = block->next;
  143. free (block);
  144. }
  145. if (GATHER_STATISTICS)
  146. {
  147. struct alloc_pool_descriptor *desc = allocate_pool_descriptor (pool->name);
  148. desc->current -= (pool->elts_allocated - pool->elts_free) * pool->elt_size;
  149. }
  150. pool->returned_free_list = NULL;
  151. pool->virgin_free_list = NULL;
  152. pool->virgin_elts_remaining = 0;
  153. pool->elts_allocated = 0;
  154. pool->elts_free = 0;
  155. pool->blocks_allocated = 0;
  156. pool->block_list = NULL;
  157. }
  158. /* Free all memory allocated for the given memory pool and the pool itself. */
  159. void
  160. free_alloc_pool (alloc_pool pool)
  161. {
  162. /* First empty the pool. */
  163. empty_alloc_pool (pool);
  164. #ifdef ENABLE_CHECKING
  165. memset (pool, 0xaf, sizeof (*pool));
  166. #endif
  167. /* Lastly, free the pool. */
  168. free (pool);
  169. }
  170. /* Frees the alloc_pool, if it is empty and zero *POOL in this case. */
  171. void
  172. free_alloc_pool_if_empty (alloc_pool *pool)
  173. {
  174. if ((*pool)->elts_free == (*pool)->elts_allocated)
  175. {
  176. free_alloc_pool (*pool);
  177. *pool = NULL;
  178. }
  179. }
  180. /* Allocates one element from the pool specified. */
  181. void *
  182. pool_alloc (alloc_pool pool)
  183. {
  184. alloc_pool_list header;
  185. #ifdef ENABLE_VALGRIND_ANNOTATIONS
  186. int size;
  187. #endif
  188. if (GATHER_STATISTICS)
  189. {
  190. struct alloc_pool_descriptor *desc = allocate_pool_descriptor (pool->name);
  191. desc->allocated += pool->elt_size;
  192. desc->current += pool->elt_size;
  193. if (desc->peak < desc->current)
  194. desc->peak = desc->current;
  195. }
  196. gcc_checking_assert (pool);
  197. #ifdef ENABLE_VALGRIND_ANNOTATIONS
  198. size = pool->elt_size - offsetof (allocation_object, u.data);
  199. #endif
  200. /* If there are no more free elements, make some more!. */
  201. if (!pool->returned_free_list)
  202. {
  203. char *block;
  204. if (!pool->virgin_elts_remaining)
  205. {
  206. alloc_pool_list block_header;
  207. /* Make the block. */
  208. block = XNEWVEC (char, pool->block_size);
  209. block_header = (alloc_pool_list) block;
  210. block += align_eight (sizeof (struct alloc_pool_list_def));
  211. /* Throw it on the block list. */
  212. block_header->next = pool->block_list;
  213. pool->block_list = block_header;
  214. /* Make the block available for allocation. */
  215. pool->virgin_free_list = block;
  216. pool->virgin_elts_remaining = pool->elts_per_block;
  217. /* Also update the number of elements we have free/allocated, and
  218. increment the allocated block count. */
  219. pool->elts_allocated += pool->elts_per_block;
  220. pool->elts_free += pool->elts_per_block;
  221. pool->blocks_allocated += 1;
  222. }
  223. /* We now know that we can take the first elt off the virgin list and
  224. put it on the returned list. */
  225. block = pool->virgin_free_list;
  226. header = (alloc_pool_list) USER_PTR_FROM_ALLOCATION_OBJECT_PTR (block);
  227. header->next = NULL;
  228. #ifdef ENABLE_CHECKING
  229. /* Mark the element to be free. */
  230. ((allocation_object *) block)->id = 0;
  231. #endif
  232. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (header,size));
  233. pool->returned_free_list = header;
  234. pool->virgin_free_list += pool->elt_size;
  235. pool->virgin_elts_remaining--;
  236. }
  237. /* Pull the first free element from the free list, and return it. */
  238. header = pool->returned_free_list;
  239. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (header, sizeof (*header)));
  240. pool->returned_free_list = header->next;
  241. pool->elts_free--;
  242. #ifdef ENABLE_CHECKING
  243. /* Set the ID for element. */
  244. ALLOCATION_OBJECT_PTR_FROM_USER_PTR (header)->id = pool->id;
  245. #endif
  246. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (header, size));
  247. return ((void *) header);
  248. }
  249. /* Puts PTR back on POOL's free list. */
  250. void
  251. pool_free (alloc_pool pool, void *ptr)
  252. {
  253. alloc_pool_list header;
  254. #if defined(ENABLE_VALGRIND_ANNOTATIONS) || defined(ENABLE_CHECKING)
  255. int size;
  256. size = pool->elt_size - offsetof (allocation_object, u.data);
  257. #endif
  258. #ifdef ENABLE_CHECKING
  259. gcc_assert (ptr
  260. /* Check if we free more than we allocated, which is Bad (TM). */
  261. && pool->elts_free < pool->elts_allocated
  262. /* Check whether the PTR was allocated from POOL. */
  263. && pool->id == ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr)->id);
  264. memset (ptr, 0xaf, size);
  265. /* Mark the element to be free. */
  266. ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr)->id = 0;
  267. #endif
  268. header = (alloc_pool_list) ptr;
  269. header->next = pool->returned_free_list;
  270. pool->returned_free_list = header;
  271. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (ptr, size));
  272. pool->elts_free++;
  273. if (GATHER_STATISTICS)
  274. {
  275. struct alloc_pool_descriptor *desc = allocate_pool_descriptor (pool->name);
  276. desc->current -= pool->elt_size;
  277. }
  278. }
  279. /* Output per-alloc_pool statistics. */
  280. /* Used to accumulate statistics about alloc_pool sizes. */
  281. struct pool_output_info
  282. {
  283. unsigned long total_created;
  284. unsigned long total_allocated;
  285. };
  286. /* Called via hash_map.traverse. Output alloc_pool descriptor pointed out by
  287. SLOT and update statistics. */
  288. bool
  289. print_alloc_pool_statistics (const char *const &name,
  290. const alloc_pool_descriptor &d,
  291. struct pool_output_info *i)
  292. {
  293. if (d.allocated)
  294. {
  295. fprintf (stderr,
  296. "%-22s %6d %10lu %10lu(%10lu) %10lu(%10lu) %10lu(%10lu)\n",
  297. name, d.elt_size, d.created, d.allocated,
  298. d.allocated / d.elt_size, d.peak, d.peak / d.elt_size,
  299. d.current, d.current / d.elt_size);
  300. i->total_allocated += d.allocated;
  301. i->total_created += d.created;
  302. }
  303. return 1;
  304. }
  305. /* Output per-alloc_pool memory usage statistics. */
  306. void
  307. dump_alloc_pool_statistics (void)
  308. {
  309. struct pool_output_info info;
  310. if (! GATHER_STATISTICS)
  311. return;
  312. if (!alloc_pool_hash)
  313. return;
  314. fprintf (stderr, "\nAlloc-pool Kind Elt size Pools Allocated (elts) Peak (elts) Leak (elts)\n");
  315. fprintf (stderr, "--------------------------------------------------------------------------------------------------------------\n");
  316. info.total_created = 0;
  317. info.total_allocated = 0;
  318. alloc_pool_hash->traverse <struct pool_output_info *,
  319. print_alloc_pool_statistics> (&info);
  320. fprintf (stderr, "--------------------------------------------------------------------------------------------------------------\n");
  321. fprintf (stderr, "%-22s %7lu %10lu\n",
  322. "Total", info.total_created, info.total_allocated);
  323. fprintf (stderr, "--------------------------------------------------------------------------------------------------------------\n");
  324. }