annotate gcc/memory-block.h @ 16:04ced10e8804

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