alloc-pool.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /* Functions to support a pool of allocatable objects
  2. Copyright (C) 1997-2020 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
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License 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. #ifndef ALLOC_POOL_H
  17. #define ALLOC_POOL_H
  18. #include "memory-block.h"
  19. #include "options.h" // for flag_checking
  20. extern void dump_alloc_pool_statistics (void);
  21. /* Flag indicates whether memory statistics are gathered any longer. */
  22. extern bool after_memory_report;
  23. typedef unsigned long ALLOC_POOL_ID_TYPE;
  24. /* Last used ID. */
  25. extern ALLOC_POOL_ID_TYPE last_id;
  26. /* Pool allocator memory usage. */
  27. class pool_usage: public mem_usage
  28. {
  29. public:
  30. /* Default contructor. */
  31. pool_usage (): m_element_size (0), m_pool_name ("") {}
  32. /* Constructor. */
  33. pool_usage (size_t allocated, size_t times, size_t peak,
  34. size_t instances, size_t element_size,
  35. const char *pool_name)
  36. : mem_usage (allocated, times, peak, instances),
  37. m_element_size (element_size),
  38. m_pool_name (pool_name) {}
  39. /* Sum the usage with SECOND usage. */
  40. pool_usage
  41. operator+ (const pool_usage &second)
  42. {
  43. return pool_usage (m_allocated + second.m_allocated,
  44. m_times + second.m_times,
  45. m_peak + second.m_peak,
  46. m_instances + second.m_instances,
  47. m_element_size, m_pool_name);
  48. }
  49. /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
  50. inline void
  51. dump (mem_location *loc, mem_usage &total) const
  52. {
  53. char *location_string = loc->to_string ();
  54. fprintf (stderr, "%-32s%-48s " PRsa(5) PRsa(9) ":%5.1f%%"
  55. PRsa(9) PRsa(9) ":%5.1f%%%12" PRIu64 "\n",
  56. m_pool_name, location_string,
  57. SIZE_AMOUNT (m_instances),
  58. SIZE_AMOUNT (m_allocated),
  59. get_percent (m_allocated, total.m_allocated),
  60. SIZE_AMOUNT (m_peak),
  61. SIZE_AMOUNT (m_times),
  62. get_percent (m_times, total.m_times),
  63. (uint64_t)m_element_size);
  64. free (location_string);
  65. }
  66. /* Dump header with NAME. */
  67. static inline void
  68. dump_header (const char *name)
  69. {
  70. fprintf (stderr, "%-32s%-48s %6s%11s%16s%17s%12s\n", "Pool name", name,
  71. "Pools", "Leak", "Peak", "Times", "Elt size");
  72. }
  73. /* Dump footer. */
  74. inline void
  75. dump_footer ()
  76. {
  77. fprintf (stderr, "%s" PRsa(82) PRsa(10) "\n", "Total",
  78. SIZE_AMOUNT (m_instances), SIZE_AMOUNT (m_allocated));
  79. }
  80. /* Element size. */
  81. size_t m_element_size;
  82. /* Pool name. */
  83. const char *m_pool_name;
  84. };
  85. extern mem_alloc_description<pool_usage> pool_allocator_usage;
  86. #if 0
  87. /* If a pool with custom block size is needed, one might use the following
  88. template. An instance of this template can be used as a parameter for
  89. instantiating base_pool_allocator template:
  90. typedef custom_block_allocator <128*1024> huge_block_allocator;
  91. ...
  92. static base_pool_allocator <huge_block_allocator>
  93. value_pool ("value", 16384);
  94. Right now it's not used anywhere in the code, and is given here as an
  95. example). */
  96. template <size_t BlockSize>
  97. class custom_block_allocator
  98. {
  99. public:
  100. static const size_t block_size = BlockSize;
  101. static inline void *
  102. allocate () ATTRIBUTE_MALLOC
  103. {
  104. return XNEWVEC (char, BlockSize);
  105. }
  106. static inline void
  107. release (void *block)
  108. {
  109. XDELETEVEC (block);
  110. }
  111. };
  112. #endif
  113. /* Generic pool allocator. */
  114. template <typename TBlockAllocator>
  115. class base_pool_allocator
  116. {
  117. public:
  118. /* Default constructor for pool allocator called NAME. */
  119. base_pool_allocator (const char *name, size_t size CXX_MEM_STAT_INFO);
  120. ~base_pool_allocator ();
  121. void release ();
  122. void release_if_empty ();
  123. void *allocate () ATTRIBUTE_MALLOC;
  124. void remove (void *object);
  125. size_t num_elts_current ();
  126. private:
  127. struct allocation_pool_list
  128. {
  129. allocation_pool_list *next;
  130. };
  131. /* Initialize a pool allocator. */
  132. void initialize ();
  133. struct allocation_object
  134. {
  135. #if CHECKING_P
  136. /* The ID of alloc pool which the object was allocated from. */
  137. ALLOC_POOL_ID_TYPE id;
  138. #endif
  139. union
  140. {
  141. /* The data of the object. */
  142. char data[1];
  143. /* Because we want any type of data to be well aligned after the ID,
  144. the following elements are here. They are never accessed so
  145. the allocated object may be even smaller than this structure.
  146. We do not care about alignment for floating-point types. */
  147. char *align_p;
  148. int64_t align_i;
  149. } u;
  150. #if CHECKING_P
  151. static inline allocation_object*
  152. get_instance (void *data_ptr)
  153. {
  154. return (allocation_object *)(((char *)(data_ptr))
  155. - offsetof (allocation_object,
  156. u.data));
  157. }
  158. #endif
  159. static inline void*
  160. get_data (void *instance_ptr)
  161. {
  162. return (void*)(((allocation_object *) instance_ptr)->u.data);
  163. }
  164. };
  165. /* Align X to 8. */
  166. static inline size_t
  167. align_eight (size_t x)
  168. {
  169. return (((x+7) >> 3) << 3);
  170. }
  171. const char *m_name;
  172. ALLOC_POOL_ID_TYPE m_id;
  173. size_t m_elts_per_block;
  174. /* These are the elements that have been allocated at least once
  175. and freed. */
  176. allocation_pool_list *m_returned_free_list;
  177. /* These are the elements that have not yet been allocated out of
  178. the last block obtained from XNEWVEC. */
  179. char* m_virgin_free_list;
  180. /* The number of elements in the virgin_free_list that can be
  181. allocated before needing another block. */
  182. size_t m_virgin_elts_remaining;
  183. /* The number of elements that are allocated. */
  184. size_t m_elts_allocated;
  185. /* The number of elements that are released. */
  186. size_t m_elts_free;
  187. /* The number of allocated blocks. */
  188. size_t m_blocks_allocated;
  189. /* List of blocks that are used to allocate new objects. */
  190. allocation_pool_list *m_block_list;
  191. /* Size of a pool elements in bytes. */
  192. size_t m_elt_size;
  193. /* Size in bytes that should be allocated for each element. */
  194. size_t m_size;
  195. /* Flag if a pool allocator is initialized. */
  196. bool m_initialized;
  197. /* Memory allocation location. */
  198. mem_location m_location;
  199. };
  200. template <typename TBlockAllocator>
  201. inline
  202. base_pool_allocator <TBlockAllocator>::base_pool_allocator (
  203. const char *name, size_t size MEM_STAT_DECL):
  204. m_name (name), m_id (0), m_elts_per_block (0), m_returned_free_list (NULL),
  205. m_virgin_free_list (NULL), m_virgin_elts_remaining (0), m_elts_allocated (0),
  206. m_elts_free (0), m_blocks_allocated (0), m_block_list (NULL), m_elt_size (0),
  207. m_size (size), m_initialized (false),
  208. m_location (ALLOC_POOL_ORIGIN, false PASS_MEM_STAT) {}
  209. /* Initialize a pool allocator. */
  210. template <typename TBlockAllocator>
  211. inline void
  212. base_pool_allocator <TBlockAllocator>::initialize ()
  213. {
  214. gcc_checking_assert (!m_initialized);
  215. m_initialized = true;
  216. size_t size = m_size;
  217. gcc_checking_assert (m_name);
  218. gcc_checking_assert (m_size);
  219. /* Make size large enough to store the list header. */
  220. if (size < sizeof (allocation_pool_list*))
  221. size = sizeof (allocation_pool_list*);
  222. /* Now align the size to a multiple of 8. */
  223. size = align_eight (size);
  224. /* Add the aligned size of ID. */
  225. size += offsetof (allocation_object, u.data);
  226. m_elt_size = size;
  227. if (GATHER_STATISTICS)
  228. {
  229. pool_usage *u = pool_allocator_usage.register_descriptor
  230. (this, new mem_location (m_location));
  231. u->m_element_size = m_elt_size;
  232. u->m_pool_name = m_name;
  233. }
  234. /* List header size should be a multiple of 8. */
  235. size_t header_size = align_eight (sizeof (allocation_pool_list));
  236. m_elts_per_block = (TBlockAllocator::block_size - header_size) / size;
  237. gcc_checking_assert (m_elts_per_block != 0);
  238. /* Increase the last used ID and use it for this pool.
  239. ID == 0 is used for free elements of pool so skip it. */
  240. last_id++;
  241. if (last_id == 0)
  242. last_id++;
  243. m_id = last_id;
  244. }
  245. /* Free all memory allocated for the given memory pool. */
  246. template <typename TBlockAllocator>
  247. inline void
  248. base_pool_allocator <TBlockAllocator>::release ()
  249. {
  250. if (!m_initialized)
  251. return;
  252. allocation_pool_list *block, *next_block;
  253. /* Free each block allocated to the pool. */
  254. for (block = m_block_list; block != NULL; block = next_block)
  255. {
  256. next_block = block->next;
  257. TBlockAllocator::release (block);
  258. }
  259. if (GATHER_STATISTICS && !after_memory_report)
  260. {
  261. pool_allocator_usage.release_instance_overhead
  262. (this, (m_elts_allocated - m_elts_free) * m_elt_size);
  263. }
  264. m_returned_free_list = NULL;
  265. m_virgin_free_list = NULL;
  266. m_virgin_elts_remaining = 0;
  267. m_elts_allocated = 0;
  268. m_elts_free = 0;
  269. m_blocks_allocated = 0;
  270. m_block_list = NULL;
  271. }
  272. template <typename TBlockAllocator>
  273. inline void
  274. base_pool_allocator <TBlockAllocator>::release_if_empty ()
  275. {
  276. if (m_elts_free == m_elts_allocated)
  277. release ();
  278. }
  279. template <typename TBlockAllocator>
  280. inline base_pool_allocator <TBlockAllocator>::~base_pool_allocator ()
  281. {
  282. release ();
  283. }
  284. /* Allocates one element from the pool specified. */
  285. template <typename TBlockAllocator>
  286. inline void*
  287. base_pool_allocator <TBlockAllocator>::allocate ()
  288. {
  289. if (!m_initialized)
  290. initialize ();
  291. allocation_pool_list *header;
  292. #ifdef ENABLE_VALGRIND_ANNOTATIONS
  293. int size;
  294. #endif
  295. if (GATHER_STATISTICS)
  296. {
  297. pool_allocator_usage.register_instance_overhead (m_elt_size, this);
  298. }
  299. #ifdef ENABLE_VALGRIND_ANNOTATIONS
  300. size = m_elt_size - offsetof (allocation_object, u.data);
  301. #endif
  302. /* If there are no more free elements, make some more!. */
  303. if (!m_returned_free_list)
  304. {
  305. char *block;
  306. if (!m_virgin_elts_remaining)
  307. {
  308. allocation_pool_list *block_header;
  309. /* Make the block. */
  310. block = reinterpret_cast<char *> (TBlockAllocator::allocate ());
  311. block_header = new (block) allocation_pool_list;
  312. block += align_eight (sizeof (allocation_pool_list));
  313. /* Throw it on the block list. */
  314. block_header->next = m_block_list;
  315. m_block_list = block_header;
  316. /* Make the block available for allocation. */
  317. m_virgin_free_list = block;
  318. m_virgin_elts_remaining = m_elts_per_block;
  319. /* Also update the number of elements we have free/allocated, and
  320. increment the allocated block count. */
  321. m_elts_allocated += m_elts_per_block;
  322. m_elts_free += m_elts_per_block;
  323. m_blocks_allocated += 1;
  324. }
  325. /* We now know that we can take the first elt off the virgin list and
  326. put it on the returned list. */
  327. block = m_virgin_free_list;
  328. header = (allocation_pool_list*) allocation_object::get_data (block);
  329. header->next = NULL;
  330. /* Mark the element to be free. */
  331. #if CHECKING_P
  332. ((allocation_object*) block)->id = 0;
  333. #endif
  334. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (header,size));
  335. m_returned_free_list = header;
  336. m_virgin_free_list += m_elt_size;
  337. m_virgin_elts_remaining--;
  338. }
  339. /* Pull the first free element from the free list, and return it. */
  340. header = m_returned_free_list;
  341. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (header, sizeof (*header)));
  342. m_returned_free_list = header->next;
  343. m_elts_free--;
  344. /* Set the ID for element. */
  345. #if CHECKING_P
  346. allocation_object::get_instance (header)->id = m_id;
  347. #endif
  348. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (header, size));
  349. return (void *)(header);
  350. }
  351. /* Puts PTR back on POOL's free list. */
  352. template <typename TBlockAllocator>
  353. inline void
  354. base_pool_allocator <TBlockAllocator>::remove (void *object)
  355. {
  356. int size = m_elt_size - offsetof (allocation_object, u.data);
  357. if (flag_checking)
  358. {
  359. gcc_assert (m_initialized);
  360. gcc_assert (object
  361. /* Check if we free more than we allocated. */
  362. && m_elts_free < m_elts_allocated);
  363. #if CHECKING_P
  364. /* Check whether the PTR was allocated from POOL. */
  365. gcc_assert (m_id == allocation_object::get_instance (object)->id);
  366. #endif
  367. memset (object, 0xaf, size);
  368. }
  369. #if CHECKING_P
  370. /* Mark the element to be free. */
  371. allocation_object::get_instance (object)->id = 0;
  372. #endif
  373. allocation_pool_list *header = new (object) allocation_pool_list;
  374. header->next = m_returned_free_list;
  375. m_returned_free_list = header;
  376. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (object, size));
  377. m_elts_free++;
  378. if (GATHER_STATISTICS)
  379. {
  380. pool_allocator_usage.release_instance_overhead (this, m_elt_size);
  381. }
  382. }
  383. /* Number of elements currently active (not returned to pool). Used for cheap
  384. consistency checks. */
  385. template <typename TBlockAllocator>
  386. inline size_t
  387. base_pool_allocator <TBlockAllocator>::num_elts_current ()
  388. {
  389. return m_elts_allocated - m_elts_free;
  390. }
  391. /* Specialization of base_pool_allocator which should be used in most cases.
  392. Another specialization may be needed, if object size is greater than
  393. memory_block_pool::block_size (64 KB). */
  394. typedef base_pool_allocator <memory_block_pool> pool_allocator;
  395. /* Type based memory pool allocator. */
  396. template <typename T>
  397. class object_allocator
  398. {
  399. public:
  400. /* Default constructor for pool allocator called NAME. */
  401. object_allocator (const char *name CXX_MEM_STAT_INFO):
  402. m_allocator (name, sizeof (T) PASS_MEM_STAT) {}
  403. inline void
  404. release ()
  405. {
  406. m_allocator.release ();
  407. }
  408. inline void release_if_empty ()
  409. {
  410. m_allocator.release_if_empty ();
  411. }
  412. /* Allocate memory for instance of type T and call a default constructor. */
  413. inline T *
  414. allocate () ATTRIBUTE_MALLOC
  415. {
  416. return ::new (m_allocator.allocate ()) T;
  417. }
  418. /* Allocate memory for instance of type T and return void * that
  419. could be used in situations where a default constructor is not provided
  420. by the class T. */
  421. inline void *
  422. allocate_raw () ATTRIBUTE_MALLOC
  423. {
  424. return m_allocator.allocate ();
  425. }
  426. inline void
  427. remove (T *object)
  428. {
  429. /* Call destructor. */
  430. object->~T ();
  431. m_allocator.remove (object);
  432. }
  433. inline size_t
  434. num_elts_current ()
  435. {
  436. return m_allocator.num_elts_current ();
  437. }
  438. private:
  439. pool_allocator m_allocator;
  440. };
  441. /* Store information about each particular alloc_pool. Note that this
  442. will underestimate the amount the amount of storage used by a small amount:
  443. 1) The overhead in a pool is not accounted for.
  444. 2) The unallocated elements in a block are not accounted for. Note
  445. that this can at worst case be one element smaller that the block
  446. size for that pool. */
  447. struct alloc_pool_descriptor
  448. {
  449. /* Number of pools allocated. */
  450. unsigned long created;
  451. /* Gross allocated storage. */
  452. unsigned long allocated;
  453. /* Amount of currently active storage. */
  454. unsigned long current;
  455. /* Peak amount of storage used. */
  456. unsigned long peak;
  457. /* Size of element in the pool. */
  458. int elt_size;
  459. };
  460. /* Helper for classes that do not provide default ctor. */
  461. template <typename T>
  462. inline void *
  463. operator new (size_t, object_allocator<T> &a)
  464. {
  465. return a.allocate_raw ();
  466. }
  467. /* Hashtable mapping alloc_pool names to descriptors. */
  468. extern hash_map<const char *, alloc_pool_descriptor> *alloc_pool_hash;
  469. #endif