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