You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.4KB

  1. /* Shared pool of memory blocks for pool allocators.
  2. Copyright (C) 2015-2020 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef MEMORY_BLOCK_H
  16. #define MEMORY_BLOCK_H
  17. /* Shared pool which allows other memory pools to reuse each others' allocated
  18. memory blocks instead of calling free/malloc again. */
  19. class memory_block_pool
  20. {
  21. public:
  22. /* Blocks have fixed size. This is necessary for sharing. */
  23. static const size_t block_size = 64 * 1024;
  24. /* Number of blocks we keep in the freelists. */
  25. static const size_t freelist_size = 1024 * 1024 / block_size;
  26. memory_block_pool ();
  27. static inline void *allocate () ATTRIBUTE_MALLOC;
  28. static inline void release (void *);
  29. static void trim (int nblocks = freelist_size);
  30. void reduce_free_list (int);
  31. private:
  32. /* memory_block_pool singleton instance, defined in memory-block.cc. */
  33. static memory_block_pool instance;
  34. struct block_list
  35. {
  36. block_list *m_next;
  37. };
  38. /* Free list. */
  39. block_list *m_blocks;
  40. };
  41. /* Allocate a single block. Reuse a previously returned block, if possible. */
  42. inline void *
  43. memory_block_pool::allocate ()
  44. {
  45. if (instance.m_blocks == NULL)
  46. return XNEWVEC (char, block_size);
  47. void *result = instance.m_blocks;
  48. instance.m_blocks = instance.m_blocks->m_next;
  49. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, block_size));
  50. return result;
  51. }
  52. /* Return UNCAST_BLOCK to the pool. */
  53. inline void
  54. memory_block_pool::release (void *uncast_block)
  55. {
  56. block_list *block = new (uncast_block) block_list;
  57. block->m_next = instance.m_blocks;
  58. instance.m_blocks = block;
  59. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *)uncast_block
  60. + sizeof (block_list),
  61. block_size
  62. - sizeof (block_list)));
  63. }
  64. extern void *mempool_obstack_chunk_alloc (size_t) ATTRIBUTE_MALLOC;
  65. extern void mempool_obstack_chunk_free (void *);
  66. #endif /* MEMORY_BLOCK_H */