1 /* Shared pool of memory blocks for pool allocators.
2 Copyright (C) 2015-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
23 #include "memory-block.h"
26 /* Global singleton-like instance. */
27 memory_block_pool
memory_block_pool::instance
;
29 memory_block_pool::memory_block_pool () : m_blocks (NULL
) {}
31 /* Reduce free list to NUM blocks and return remaining to malloc. */
33 memory_block_pool::reduce_free_list (int num
)
35 block_list
**blocks
= &m_blocks
;
37 /* First skip NUM blocks. */
39 for (;num
> 0 && *blocks
; num
--)
40 blocks
= &(*blocks
)->m_next
;
45 /* And free the remainder of them. */
47 block_list
*to_free
= *blocks
;
52 block_list
*next
= to_free
->m_next
;
58 /* Allocate a chunk for obstack. Use the pool if requested chunk size matches
59 the size of blocks in the pool. */
61 mempool_obstack_chunk_alloc (size_t size
)
63 if (size
== memory_block_pool::block_size
)
64 return memory_block_pool::allocate ();
66 return XNEWVEC (char, size
);
69 /* Free previously allocated obstack chunk. */
71 mempool_obstack_chunk_free (void *chunk
)
73 size_t size
= (reinterpret_cast<_obstack_chunk
*> (chunk
)->limit
74 - reinterpret_cast<char *> (chunk
));
75 if (size
== memory_block_pool::block_size
)
76 memory_block_pool::release (chunk
);
81 /* Return allocated memory back to malloc (and to system). */
83 memory_block_pool::trim (int num
)
85 instance
.reduce_free_list (num
);