annotate gcc/memory-block.cc @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Shared pool of memory blocks for pool allocators.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2015-2020 Free Software Foundation, Inc.
111
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 it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 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 #include "config.h"
kono
parents:
diff changeset
21 #include "system.h"
kono
parents:
diff changeset
22 #include "coretypes.h"
kono
parents:
diff changeset
23 #include "memory-block.h"
kono
parents:
diff changeset
24 #include "obstack.h"
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 /* Global singleton-like instance. */
kono
parents:
diff changeset
27 memory_block_pool memory_block_pool::instance;
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 memory_block_pool::memory_block_pool () : m_blocks (NULL) {}
kono
parents:
diff changeset
30
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
31 /* Reduce free list to NUM blocks and return remaining to malloc. */
111
kono
parents:
diff changeset
32 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
33 memory_block_pool::reduce_free_list (int num)
111
kono
parents:
diff changeset
34 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
35 block_list **blocks = &m_blocks;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
36
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
37 /* First skip NUM blocks. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
38
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
39 for (;num > 0 && *blocks; num--)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
40 blocks = &(*blocks)->m_next;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
41
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
42 if (!*blocks)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
43 return;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
44
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
45 /* And free the remainder of them. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
46
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
47 block_list *to_free = *blocks;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
48 *blocks = NULL;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
49
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
50 while (to_free)
111
kono
parents:
diff changeset
51 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
52 block_list *next = to_free->m_next;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
53 XDELETEVEC (to_free);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
54 to_free = next;
111
kono
parents:
diff changeset
55 }
kono
parents:
diff changeset
56 }
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 /* Allocate a chunk for obstack. Use the pool if requested chunk size matches
kono
parents:
diff changeset
59 the size of blocks in the pool. */
kono
parents:
diff changeset
60 void *
kono
parents:
diff changeset
61 mempool_obstack_chunk_alloc (size_t size)
kono
parents:
diff changeset
62 {
kono
parents:
diff changeset
63 if (size == memory_block_pool::block_size)
kono
parents:
diff changeset
64 return memory_block_pool::allocate ();
kono
parents:
diff changeset
65 else
kono
parents:
diff changeset
66 return XNEWVEC (char, size);
kono
parents:
diff changeset
67 }
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 /* Free previously allocated obstack chunk. */
kono
parents:
diff changeset
70 void
kono
parents:
diff changeset
71 mempool_obstack_chunk_free (void *chunk)
kono
parents:
diff changeset
72 {
kono
parents:
diff changeset
73 size_t size = (reinterpret_cast<_obstack_chunk *> (chunk)->limit
kono
parents:
diff changeset
74 - reinterpret_cast<char *> (chunk));
kono
parents:
diff changeset
75 if (size == memory_block_pool::block_size)
kono
parents:
diff changeset
76 memory_block_pool::release (chunk);
kono
parents:
diff changeset
77 else
kono
parents:
diff changeset
78 XDELETEVEC (chunk);
kono
parents:
diff changeset
79 }
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
80
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
81 /* Return allocated memory back to malloc (and to system). */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
82 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
83 memory_block_pool::trim (int num)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
84 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
85 instance.reduce_free_list (num);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
86 }