alloc-pool.h 15 KB

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