annotate gcc/alloc-pool.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 /* Functions to support a pool of allocatable objects
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 1997-2020 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 Contributed by Daniel Berlin <dan@cgsoftware.com>
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 This file is part of GCC.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 it under the terms of the GNU General Public License as published by
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 the Free Software Foundation; either version 3, or (at your option)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 any later version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 GNU General Public License for more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #ifndef ALLOC_POOL_H
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #define ALLOC_POOL_H
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
111
kono
parents: 55
diff changeset
23 #include "memory-block.h"
kono
parents: 55
diff changeset
24 #include "options.h" // for flag_checking
kono
parents: 55
diff changeset
25
kono
parents: 55
diff changeset
26 extern void dump_alloc_pool_statistics (void);
kono
parents: 55
diff changeset
27
kono
parents: 55
diff changeset
28 /* Flag indicates whether memory statistics are gathered any longer. */
kono
parents: 55
diff changeset
29 extern bool after_memory_report;
kono
parents: 55
diff changeset
30
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 typedef unsigned long ALLOC_POOL_ID_TYPE;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32
111
kono
parents: 55
diff changeset
33 /* Last used ID. */
kono
parents: 55
diff changeset
34 extern ALLOC_POOL_ID_TYPE last_id;
kono
parents: 55
diff changeset
35
kono
parents: 55
diff changeset
36 /* Pool allocator memory usage. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
37 class pool_usage: public mem_usage
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
39 public:
111
kono
parents: 55
diff changeset
40 /* Default contructor. */
kono
parents: 55
diff changeset
41 pool_usage (): m_element_size (0), m_pool_name ("") {}
kono
parents: 55
diff changeset
42 /* Constructor. */
kono
parents: 55
diff changeset
43 pool_usage (size_t allocated, size_t times, size_t peak,
kono
parents: 55
diff changeset
44 size_t instances, size_t element_size,
kono
parents: 55
diff changeset
45 const char *pool_name)
kono
parents: 55
diff changeset
46 : mem_usage (allocated, times, peak, instances),
kono
parents: 55
diff changeset
47 m_element_size (element_size),
kono
parents: 55
diff changeset
48 m_pool_name (pool_name) {}
kono
parents: 55
diff changeset
49
kono
parents: 55
diff changeset
50 /* Sum the usage with SECOND usage. */
kono
parents: 55
diff changeset
51 pool_usage
kono
parents: 55
diff changeset
52 operator+ (const pool_usage &second)
kono
parents: 55
diff changeset
53 {
kono
parents: 55
diff changeset
54 return pool_usage (m_allocated + second.m_allocated,
kono
parents: 55
diff changeset
55 m_times + second.m_times,
kono
parents: 55
diff changeset
56 m_peak + second.m_peak,
kono
parents: 55
diff changeset
57 m_instances + second.m_instances,
kono
parents: 55
diff changeset
58 m_element_size, m_pool_name);
kono
parents: 55
diff changeset
59 }
kono
parents: 55
diff changeset
60
kono
parents: 55
diff changeset
61 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
kono
parents: 55
diff changeset
62 inline void
kono
parents: 55
diff changeset
63 dump (mem_location *loc, mem_usage &total) const
kono
parents: 55
diff changeset
64 {
kono
parents: 55
diff changeset
65 char *location_string = loc->to_string ();
kono
parents: 55
diff changeset
66
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
67 fprintf (stderr, "%-32s%-48s " PRsa(5) PRsa(9) ":%5.1f%%"
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
68 PRsa(9) PRsa(9) ":%5.1f%%%12" PRIu64 "\n",
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
69 m_pool_name, location_string,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
70 SIZE_AMOUNT (m_instances),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
71 SIZE_AMOUNT (m_allocated),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
72 get_percent (m_allocated, total.m_allocated),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
73 SIZE_AMOUNT (m_peak),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
74 SIZE_AMOUNT (m_times),
111
kono
parents: 55
diff changeset
75 get_percent (m_times, total.m_times),
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
76 (uint64_t)m_element_size);
111
kono
parents: 55
diff changeset
77
kono
parents: 55
diff changeset
78 free (location_string);
kono
parents: 55
diff changeset
79 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80
111
kono
parents: 55
diff changeset
81 /* Dump header with NAME. */
kono
parents: 55
diff changeset
82 static inline void
kono
parents: 55
diff changeset
83 dump_header (const char *name)
kono
parents: 55
diff changeset
84 {
kono
parents: 55
diff changeset
85 fprintf (stderr, "%-32s%-48s %6s%11s%16s%17s%12s\n", "Pool name", name,
kono
parents: 55
diff changeset
86 "Pools", "Leak", "Peak", "Times", "Elt size");
kono
parents: 55
diff changeset
87 }
kono
parents: 55
diff changeset
88
kono
parents: 55
diff changeset
89 /* Dump footer. */
kono
parents: 55
diff changeset
90 inline void
kono
parents: 55
diff changeset
91 dump_footer ()
kono
parents: 55
diff changeset
92 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
93 fprintf (stderr, "%s" PRsa(82) PRsa(10) "\n", "Total",
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
94 SIZE_AMOUNT (m_instances), SIZE_AMOUNT (m_allocated));
111
kono
parents: 55
diff changeset
95 }
kono
parents: 55
diff changeset
96
kono
parents: 55
diff changeset
97 /* Element size. */
kono
parents: 55
diff changeset
98 size_t m_element_size;
kono
parents: 55
diff changeset
99 /* Pool name. */
kono
parents: 55
diff changeset
100 const char *m_pool_name;
kono
parents: 55
diff changeset
101 };
kono
parents: 55
diff changeset
102
kono
parents: 55
diff changeset
103 extern mem_alloc_description<pool_usage> pool_allocator_usage;
kono
parents: 55
diff changeset
104
kono
parents: 55
diff changeset
105 #if 0
kono
parents: 55
diff changeset
106 /* If a pool with custom block size is needed, one might use the following
kono
parents: 55
diff changeset
107 template. An instance of this template can be used as a parameter for
kono
parents: 55
diff changeset
108 instantiating base_pool_allocator template:
kono
parents: 55
diff changeset
109
kono
parents: 55
diff changeset
110 typedef custom_block_allocator <128*1024> huge_block_allocator;
kono
parents: 55
diff changeset
111 ...
kono
parents: 55
diff changeset
112 static base_pool_allocator <huge_block_allocator>
kono
parents: 55
diff changeset
113 value_pool ("value", 16384);
kono
parents: 55
diff changeset
114
kono
parents: 55
diff changeset
115 Right now it's not used anywhere in the code, and is given here as an
kono
parents: 55
diff changeset
116 example). */
kono
parents: 55
diff changeset
117
kono
parents: 55
diff changeset
118 template <size_t BlockSize>
kono
parents: 55
diff changeset
119 class custom_block_allocator
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 {
111
kono
parents: 55
diff changeset
121 public:
kono
parents: 55
diff changeset
122 static const size_t block_size = BlockSize;
kono
parents: 55
diff changeset
123
kono
parents: 55
diff changeset
124 static inline void *
kono
parents: 55
diff changeset
125 allocate () ATTRIBUTE_MALLOC
kono
parents: 55
diff changeset
126 {
kono
parents: 55
diff changeset
127 return XNEWVEC (char, BlockSize);
kono
parents: 55
diff changeset
128 }
kono
parents: 55
diff changeset
129
kono
parents: 55
diff changeset
130 static inline void
kono
parents: 55
diff changeset
131 release (void *block)
kono
parents: 55
diff changeset
132 {
kono
parents: 55
diff changeset
133 XDELETEVEC (block);
kono
parents: 55
diff changeset
134 }
kono
parents: 55
diff changeset
135 };
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 #endif
111
kono
parents: 55
diff changeset
137
kono
parents: 55
diff changeset
138 /* Generic pool allocator. */
kono
parents: 55
diff changeset
139
kono
parents: 55
diff changeset
140 template <typename TBlockAllocator>
kono
parents: 55
diff changeset
141 class base_pool_allocator
kono
parents: 55
diff changeset
142 {
kono
parents: 55
diff changeset
143 public:
kono
parents: 55
diff changeset
144 /* Default constructor for pool allocator called NAME. */
kono
parents: 55
diff changeset
145 base_pool_allocator (const char *name, size_t size CXX_MEM_STAT_INFO);
kono
parents: 55
diff changeset
146 ~base_pool_allocator ();
kono
parents: 55
diff changeset
147 void release ();
kono
parents: 55
diff changeset
148 void release_if_empty ();
kono
parents: 55
diff changeset
149 void *allocate () ATTRIBUTE_MALLOC;
kono
parents: 55
diff changeset
150 void remove (void *object);
kono
parents: 55
diff changeset
151 size_t num_elts_current ();
kono
parents: 55
diff changeset
152
kono
parents: 55
diff changeset
153 private:
kono
parents: 55
diff changeset
154 struct allocation_pool_list
kono
parents: 55
diff changeset
155 {
kono
parents: 55
diff changeset
156 allocation_pool_list *next;
kono
parents: 55
diff changeset
157 };
kono
parents: 55
diff changeset
158
kono
parents: 55
diff changeset
159 /* Initialize a pool allocator. */
kono
parents: 55
diff changeset
160 void initialize ();
kono
parents: 55
diff changeset
161
kono
parents: 55
diff changeset
162 struct allocation_object
kono
parents: 55
diff changeset
163 {
kono
parents: 55
diff changeset
164 #if CHECKING_P
kono
parents: 55
diff changeset
165 /* The ID of alloc pool which the object was allocated from. */
kono
parents: 55
diff changeset
166 ALLOC_POOL_ID_TYPE id;
kono
parents: 55
diff changeset
167 #endif
kono
parents: 55
diff changeset
168
kono
parents: 55
diff changeset
169 union
kono
parents: 55
diff changeset
170 {
kono
parents: 55
diff changeset
171 /* The data of the object. */
kono
parents: 55
diff changeset
172 char data[1];
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
173
111
kono
parents: 55
diff changeset
174 /* Because we want any type of data to be well aligned after the ID,
kono
parents: 55
diff changeset
175 the following elements are here. They are never accessed so
kono
parents: 55
diff changeset
176 the allocated object may be even smaller than this structure.
kono
parents: 55
diff changeset
177 We do not care about alignment for floating-point types. */
kono
parents: 55
diff changeset
178 char *align_p;
kono
parents: 55
diff changeset
179 int64_t align_i;
kono
parents: 55
diff changeset
180 } u;
kono
parents: 55
diff changeset
181
kono
parents: 55
diff changeset
182 #if CHECKING_P
kono
parents: 55
diff changeset
183 static inline allocation_object*
kono
parents: 55
diff changeset
184 get_instance (void *data_ptr)
kono
parents: 55
diff changeset
185 {
kono
parents: 55
diff changeset
186 return (allocation_object *)(((char *)(data_ptr))
kono
parents: 55
diff changeset
187 - offsetof (allocation_object,
kono
parents: 55
diff changeset
188 u.data));
kono
parents: 55
diff changeset
189 }
kono
parents: 55
diff changeset
190 #endif
kono
parents: 55
diff changeset
191
kono
parents: 55
diff changeset
192 static inline void*
kono
parents: 55
diff changeset
193 get_data (void *instance_ptr)
kono
parents: 55
diff changeset
194 {
kono
parents: 55
diff changeset
195 return (void*)(((allocation_object *) instance_ptr)->u.data);
kono
parents: 55
diff changeset
196 }
kono
parents: 55
diff changeset
197 };
kono
parents: 55
diff changeset
198
kono
parents: 55
diff changeset
199 /* Align X to 8. */
kono
parents: 55
diff changeset
200 static inline size_t
kono
parents: 55
diff changeset
201 align_eight (size_t x)
kono
parents: 55
diff changeset
202 {
kono
parents: 55
diff changeset
203 return (((x+7) >> 3) << 3);
kono
parents: 55
diff changeset
204 }
kono
parents: 55
diff changeset
205
kono
parents: 55
diff changeset
206 const char *m_name;
kono
parents: 55
diff changeset
207 ALLOC_POOL_ID_TYPE m_id;
kono
parents: 55
diff changeset
208 size_t m_elts_per_block;
kono
parents: 55
diff changeset
209
kono
parents: 55
diff changeset
210 /* These are the elements that have been allocated at least once
kono
parents: 55
diff changeset
211 and freed. */
kono
parents: 55
diff changeset
212 allocation_pool_list *m_returned_free_list;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
213
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 /* These are the elements that have not yet been allocated out of
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 the last block obtained from XNEWVEC. */
111
kono
parents: 55
diff changeset
216 char* m_virgin_free_list;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
217
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 /* The number of elements in the virgin_free_list that can be
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
219 allocated before needing another block. */
111
kono
parents: 55
diff changeset
220 size_t m_virgin_elts_remaining;
kono
parents: 55
diff changeset
221 /* The number of elements that are allocated. */
kono
parents: 55
diff changeset
222 size_t m_elts_allocated;
kono
parents: 55
diff changeset
223 /* The number of elements that are released. */
kono
parents: 55
diff changeset
224 size_t m_elts_free;
kono
parents: 55
diff changeset
225 /* The number of allocated blocks. */
kono
parents: 55
diff changeset
226 size_t m_blocks_allocated;
kono
parents: 55
diff changeset
227 /* List of blocks that are used to allocate new objects. */
kono
parents: 55
diff changeset
228 allocation_pool_list *m_block_list;
kono
parents: 55
diff changeset
229 /* Size of a pool elements in bytes. */
kono
parents: 55
diff changeset
230 size_t m_elt_size;
kono
parents: 55
diff changeset
231 /* Size in bytes that should be allocated for each element. */
kono
parents: 55
diff changeset
232 size_t m_size;
kono
parents: 55
diff changeset
233 /* Flag if a pool allocator is initialized. */
kono
parents: 55
diff changeset
234 bool m_initialized;
kono
parents: 55
diff changeset
235 /* Memory allocation location. */
kono
parents: 55
diff changeset
236 mem_location m_location;
kono
parents: 55
diff changeset
237 };
kono
parents: 55
diff changeset
238
kono
parents: 55
diff changeset
239 template <typename TBlockAllocator>
kono
parents: 55
diff changeset
240 inline
kono
parents: 55
diff changeset
241 base_pool_allocator <TBlockAllocator>::base_pool_allocator (
kono
parents: 55
diff changeset
242 const char *name, size_t size MEM_STAT_DECL):
kono
parents: 55
diff changeset
243 m_name (name), m_id (0), m_elts_per_block (0), m_returned_free_list (NULL),
kono
parents: 55
diff changeset
244 m_virgin_free_list (NULL), m_virgin_elts_remaining (0), m_elts_allocated (0),
kono
parents: 55
diff changeset
245 m_elts_free (0), m_blocks_allocated (0), m_block_list (NULL), m_elt_size (0),
kono
parents: 55
diff changeset
246 m_size (size), m_initialized (false),
kono
parents: 55
diff changeset
247 m_location (ALLOC_POOL_ORIGIN, false PASS_MEM_STAT) {}
kono
parents: 55
diff changeset
248
kono
parents: 55
diff changeset
249 /* Initialize a pool allocator. */
kono
parents: 55
diff changeset
250
kono
parents: 55
diff changeset
251 template <typename TBlockAllocator>
kono
parents: 55
diff changeset
252 inline void
kono
parents: 55
diff changeset
253 base_pool_allocator <TBlockAllocator>::initialize ()
kono
parents: 55
diff changeset
254 {
kono
parents: 55
diff changeset
255 gcc_checking_assert (!m_initialized);
kono
parents: 55
diff changeset
256 m_initialized = true;
kono
parents: 55
diff changeset
257
kono
parents: 55
diff changeset
258 size_t size = m_size;
kono
parents: 55
diff changeset
259
kono
parents: 55
diff changeset
260 gcc_checking_assert (m_name);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
261 gcc_checking_assert (m_size);
111
kono
parents: 55
diff changeset
262
kono
parents: 55
diff changeset
263 /* Make size large enough to store the list header. */
kono
parents: 55
diff changeset
264 if (size < sizeof (allocation_pool_list*))
kono
parents: 55
diff changeset
265 size = sizeof (allocation_pool_list*);
kono
parents: 55
diff changeset
266
kono
parents: 55
diff changeset
267 /* Now align the size to a multiple of 8. */
kono
parents: 55
diff changeset
268 size = align_eight (size);
kono
parents: 55
diff changeset
269
kono
parents: 55
diff changeset
270 /* Add the aligned size of ID. */
kono
parents: 55
diff changeset
271 size += offsetof (allocation_object, u.data);
kono
parents: 55
diff changeset
272
kono
parents: 55
diff changeset
273 m_elt_size = size;
kono
parents: 55
diff changeset
274
kono
parents: 55
diff changeset
275 if (GATHER_STATISTICS)
kono
parents: 55
diff changeset
276 {
kono
parents: 55
diff changeset
277 pool_usage *u = pool_allocator_usage.register_descriptor
kono
parents: 55
diff changeset
278 (this, new mem_location (m_location));
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
279
111
kono
parents: 55
diff changeset
280 u->m_element_size = m_elt_size;
kono
parents: 55
diff changeset
281 u->m_pool_name = m_name;
kono
parents: 55
diff changeset
282 }
kono
parents: 55
diff changeset
283
kono
parents: 55
diff changeset
284 /* List header size should be a multiple of 8. */
kono
parents: 55
diff changeset
285 size_t header_size = align_eight (sizeof (allocation_pool_list));
kono
parents: 55
diff changeset
286
kono
parents: 55
diff changeset
287 m_elts_per_block = (TBlockAllocator::block_size - header_size) / size;
kono
parents: 55
diff changeset
288 gcc_checking_assert (m_elts_per_block != 0);
kono
parents: 55
diff changeset
289
kono
parents: 55
diff changeset
290 /* Increase the last used ID and use it for this pool.
kono
parents: 55
diff changeset
291 ID == 0 is used for free elements of pool so skip it. */
kono
parents: 55
diff changeset
292 last_id++;
kono
parents: 55
diff changeset
293 if (last_id == 0)
kono
parents: 55
diff changeset
294 last_id++;
kono
parents: 55
diff changeset
295
kono
parents: 55
diff changeset
296 m_id = last_id;
kono
parents: 55
diff changeset
297 }
kono
parents: 55
diff changeset
298
kono
parents: 55
diff changeset
299 /* Free all memory allocated for the given memory pool. */
kono
parents: 55
diff changeset
300 template <typename TBlockAllocator>
kono
parents: 55
diff changeset
301 inline void
kono
parents: 55
diff changeset
302 base_pool_allocator <TBlockAllocator>::release ()
kono
parents: 55
diff changeset
303 {
kono
parents: 55
diff changeset
304 if (!m_initialized)
kono
parents: 55
diff changeset
305 return;
kono
parents: 55
diff changeset
306
kono
parents: 55
diff changeset
307 allocation_pool_list *block, *next_block;
kono
parents: 55
diff changeset
308
kono
parents: 55
diff changeset
309 /* Free each block allocated to the pool. */
kono
parents: 55
diff changeset
310 for (block = m_block_list; block != NULL; block = next_block)
kono
parents: 55
diff changeset
311 {
kono
parents: 55
diff changeset
312 next_block = block->next;
kono
parents: 55
diff changeset
313 TBlockAllocator::release (block);
kono
parents: 55
diff changeset
314 }
kono
parents: 55
diff changeset
315
kono
parents: 55
diff changeset
316 if (GATHER_STATISTICS && !after_memory_report)
kono
parents: 55
diff changeset
317 {
kono
parents: 55
diff changeset
318 pool_allocator_usage.release_instance_overhead
kono
parents: 55
diff changeset
319 (this, (m_elts_allocated - m_elts_free) * m_elt_size);
kono
parents: 55
diff changeset
320 }
kono
parents: 55
diff changeset
321
kono
parents: 55
diff changeset
322 m_returned_free_list = NULL;
kono
parents: 55
diff changeset
323 m_virgin_free_list = NULL;
kono
parents: 55
diff changeset
324 m_virgin_elts_remaining = 0;
kono
parents: 55
diff changeset
325 m_elts_allocated = 0;
kono
parents: 55
diff changeset
326 m_elts_free = 0;
kono
parents: 55
diff changeset
327 m_blocks_allocated = 0;
kono
parents: 55
diff changeset
328 m_block_list = NULL;
kono
parents: 55
diff changeset
329 }
kono
parents: 55
diff changeset
330
kono
parents: 55
diff changeset
331 template <typename TBlockAllocator>
kono
parents: 55
diff changeset
332 inline void
kono
parents: 55
diff changeset
333 base_pool_allocator <TBlockAllocator>::release_if_empty ()
kono
parents: 55
diff changeset
334 {
kono
parents: 55
diff changeset
335 if (m_elts_free == m_elts_allocated)
kono
parents: 55
diff changeset
336 release ();
kono
parents: 55
diff changeset
337 }
kono
parents: 55
diff changeset
338
kono
parents: 55
diff changeset
339 template <typename TBlockAllocator>
kono
parents: 55
diff changeset
340 inline base_pool_allocator <TBlockAllocator>::~base_pool_allocator ()
kono
parents: 55
diff changeset
341 {
kono
parents: 55
diff changeset
342 release ();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 }
111
kono
parents: 55
diff changeset
344
kono
parents: 55
diff changeset
345 /* Allocates one element from the pool specified. */
kono
parents: 55
diff changeset
346 template <typename TBlockAllocator>
kono
parents: 55
diff changeset
347 inline void*
kono
parents: 55
diff changeset
348 base_pool_allocator <TBlockAllocator>::allocate ()
kono
parents: 55
diff changeset
349 {
kono
parents: 55
diff changeset
350 if (!m_initialized)
kono
parents: 55
diff changeset
351 initialize ();
kono
parents: 55
diff changeset
352
kono
parents: 55
diff changeset
353 allocation_pool_list *header;
kono
parents: 55
diff changeset
354 #ifdef ENABLE_VALGRIND_ANNOTATIONS
kono
parents: 55
diff changeset
355 int size;
kono
parents: 55
diff changeset
356 #endif
kono
parents: 55
diff changeset
357
kono
parents: 55
diff changeset
358 if (GATHER_STATISTICS)
kono
parents: 55
diff changeset
359 {
kono
parents: 55
diff changeset
360 pool_allocator_usage.register_instance_overhead (m_elt_size, this);
kono
parents: 55
diff changeset
361 }
kono
parents: 55
diff changeset
362
kono
parents: 55
diff changeset
363 #ifdef ENABLE_VALGRIND_ANNOTATIONS
kono
parents: 55
diff changeset
364 size = m_elt_size - offsetof (allocation_object, u.data);
kono
parents: 55
diff changeset
365 #endif
kono
parents: 55
diff changeset
366
kono
parents: 55
diff changeset
367 /* If there are no more free elements, make some more!. */
kono
parents: 55
diff changeset
368 if (!m_returned_free_list)
kono
parents: 55
diff changeset
369 {
kono
parents: 55
diff changeset
370 char *block;
kono
parents: 55
diff changeset
371 if (!m_virgin_elts_remaining)
kono
parents: 55
diff changeset
372 {
kono
parents: 55
diff changeset
373 allocation_pool_list *block_header;
kono
parents: 55
diff changeset
374
kono
parents: 55
diff changeset
375 /* Make the block. */
kono
parents: 55
diff changeset
376 block = reinterpret_cast<char *> (TBlockAllocator::allocate ());
kono
parents: 55
diff changeset
377 block_header = new (block) allocation_pool_list;
kono
parents: 55
diff changeset
378 block += align_eight (sizeof (allocation_pool_list));
kono
parents: 55
diff changeset
379
kono
parents: 55
diff changeset
380 /* Throw it on the block list. */
kono
parents: 55
diff changeset
381 block_header->next = m_block_list;
kono
parents: 55
diff changeset
382 m_block_list = block_header;
kono
parents: 55
diff changeset
383
kono
parents: 55
diff changeset
384 /* Make the block available for allocation. */
kono
parents: 55
diff changeset
385 m_virgin_free_list = block;
kono
parents: 55
diff changeset
386 m_virgin_elts_remaining = m_elts_per_block;
kono
parents: 55
diff changeset
387
kono
parents: 55
diff changeset
388 /* Also update the number of elements we have free/allocated, and
kono
parents: 55
diff changeset
389 increment the allocated block count. */
kono
parents: 55
diff changeset
390 m_elts_allocated += m_elts_per_block;
kono
parents: 55
diff changeset
391 m_elts_free += m_elts_per_block;
kono
parents: 55
diff changeset
392 m_blocks_allocated += 1;
kono
parents: 55
diff changeset
393 }
kono
parents: 55
diff changeset
394
kono
parents: 55
diff changeset
395 /* We now know that we can take the first elt off the virgin list and
kono
parents: 55
diff changeset
396 put it on the returned list. */
kono
parents: 55
diff changeset
397 block = m_virgin_free_list;
kono
parents: 55
diff changeset
398 header = (allocation_pool_list*) allocation_object::get_data (block);
kono
parents: 55
diff changeset
399 header->next = NULL;
kono
parents: 55
diff changeset
400
kono
parents: 55
diff changeset
401 /* Mark the element to be free. */
kono
parents: 55
diff changeset
402 #if CHECKING_P
kono
parents: 55
diff changeset
403 ((allocation_object*) block)->id = 0;
kono
parents: 55
diff changeset
404 #endif
kono
parents: 55
diff changeset
405 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (header,size));
kono
parents: 55
diff changeset
406 m_returned_free_list = header;
kono
parents: 55
diff changeset
407 m_virgin_free_list += m_elt_size;
kono
parents: 55
diff changeset
408 m_virgin_elts_remaining--;
kono
parents: 55
diff changeset
409
kono
parents: 55
diff changeset
410 }
kono
parents: 55
diff changeset
411
kono
parents: 55
diff changeset
412 /* Pull the first free element from the free list, and return it. */
kono
parents: 55
diff changeset
413 header = m_returned_free_list;
kono
parents: 55
diff changeset
414 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (header, sizeof (*header)));
kono
parents: 55
diff changeset
415 m_returned_free_list = header->next;
kono
parents: 55
diff changeset
416 m_elts_free--;
kono
parents: 55
diff changeset
417
kono
parents: 55
diff changeset
418 /* Set the ID for element. */
kono
parents: 55
diff changeset
419 #if CHECKING_P
kono
parents: 55
diff changeset
420 allocation_object::get_instance (header)->id = m_id;
kono
parents: 55
diff changeset
421 #endif
kono
parents: 55
diff changeset
422 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (header, size));
kono
parents: 55
diff changeset
423
kono
parents: 55
diff changeset
424 return (void *)(header);
kono
parents: 55
diff changeset
425 }
kono
parents: 55
diff changeset
426
kono
parents: 55
diff changeset
427 /* Puts PTR back on POOL's free list. */
kono
parents: 55
diff changeset
428 template <typename TBlockAllocator>
kono
parents: 55
diff changeset
429 inline void
kono
parents: 55
diff changeset
430 base_pool_allocator <TBlockAllocator>::remove (void *object)
kono
parents: 55
diff changeset
431 {
kono
parents: 55
diff changeset
432 int size = m_elt_size - offsetof (allocation_object, u.data);
kono
parents: 55
diff changeset
433
kono
parents: 55
diff changeset
434 if (flag_checking)
kono
parents: 55
diff changeset
435 {
kono
parents: 55
diff changeset
436 gcc_assert (m_initialized);
kono
parents: 55
diff changeset
437 gcc_assert (object
kono
parents: 55
diff changeset
438 /* Check if we free more than we allocated. */
kono
parents: 55
diff changeset
439 && m_elts_free < m_elts_allocated);
kono
parents: 55
diff changeset
440 #if CHECKING_P
kono
parents: 55
diff changeset
441 /* Check whether the PTR was allocated from POOL. */
kono
parents: 55
diff changeset
442 gcc_assert (m_id == allocation_object::get_instance (object)->id);
kono
parents: 55
diff changeset
443 #endif
kono
parents: 55
diff changeset
444
kono
parents: 55
diff changeset
445 memset (object, 0xaf, size);
kono
parents: 55
diff changeset
446 }
kono
parents: 55
diff changeset
447
kono
parents: 55
diff changeset
448 #if CHECKING_P
kono
parents: 55
diff changeset
449 /* Mark the element to be free. */
kono
parents: 55
diff changeset
450 allocation_object::get_instance (object)->id = 0;
kono
parents: 55
diff changeset
451 #endif
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
452
111
kono
parents: 55
diff changeset
453 allocation_pool_list *header = new (object) allocation_pool_list;
kono
parents: 55
diff changeset
454 header->next = m_returned_free_list;
kono
parents: 55
diff changeset
455 m_returned_free_list = header;
kono
parents: 55
diff changeset
456 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (object, size));
kono
parents: 55
diff changeset
457 m_elts_free++;
kono
parents: 55
diff changeset
458
kono
parents: 55
diff changeset
459 if (GATHER_STATISTICS)
kono
parents: 55
diff changeset
460 {
kono
parents: 55
diff changeset
461 pool_allocator_usage.release_instance_overhead (this, m_elt_size);
kono
parents: 55
diff changeset
462 }
kono
parents: 55
diff changeset
463 }
kono
parents: 55
diff changeset
464
kono
parents: 55
diff changeset
465 /* Number of elements currently active (not returned to pool). Used for cheap
kono
parents: 55
diff changeset
466 consistency checks. */
kono
parents: 55
diff changeset
467 template <typename TBlockAllocator>
kono
parents: 55
diff changeset
468 inline size_t
kono
parents: 55
diff changeset
469 base_pool_allocator <TBlockAllocator>::num_elts_current ()
kono
parents: 55
diff changeset
470 {
kono
parents: 55
diff changeset
471 return m_elts_allocated - m_elts_free;
kono
parents: 55
diff changeset
472 }
kono
parents: 55
diff changeset
473
kono
parents: 55
diff changeset
474 /* Specialization of base_pool_allocator which should be used in most cases.
kono
parents: 55
diff changeset
475 Another specialization may be needed, if object size is greater than
kono
parents: 55
diff changeset
476 memory_block_pool::block_size (64 KB). */
kono
parents: 55
diff changeset
477 typedef base_pool_allocator <memory_block_pool> pool_allocator;
kono
parents: 55
diff changeset
478
kono
parents: 55
diff changeset
479 /* Type based memory pool allocator. */
kono
parents: 55
diff changeset
480 template <typename T>
kono
parents: 55
diff changeset
481 class object_allocator
kono
parents: 55
diff changeset
482 {
kono
parents: 55
diff changeset
483 public:
kono
parents: 55
diff changeset
484 /* Default constructor for pool allocator called NAME. */
kono
parents: 55
diff changeset
485 object_allocator (const char *name CXX_MEM_STAT_INFO):
kono
parents: 55
diff changeset
486 m_allocator (name, sizeof (T) PASS_MEM_STAT) {}
kono
parents: 55
diff changeset
487
kono
parents: 55
diff changeset
488 inline void
kono
parents: 55
diff changeset
489 release ()
kono
parents: 55
diff changeset
490 {
kono
parents: 55
diff changeset
491 m_allocator.release ();
kono
parents: 55
diff changeset
492 }
kono
parents: 55
diff changeset
493
kono
parents: 55
diff changeset
494 inline void release_if_empty ()
kono
parents: 55
diff changeset
495 {
kono
parents: 55
diff changeset
496 m_allocator.release_if_empty ();
kono
parents: 55
diff changeset
497 }
kono
parents: 55
diff changeset
498
kono
parents: 55
diff changeset
499
kono
parents: 55
diff changeset
500 /* Allocate memory for instance of type T and call a default constructor. */
kono
parents: 55
diff changeset
501
kono
parents: 55
diff changeset
502 inline T *
kono
parents: 55
diff changeset
503 allocate () ATTRIBUTE_MALLOC
kono
parents: 55
diff changeset
504 {
kono
parents: 55
diff changeset
505 return ::new (m_allocator.allocate ()) T;
kono
parents: 55
diff changeset
506 }
kono
parents: 55
diff changeset
507
kono
parents: 55
diff changeset
508 /* Allocate memory for instance of type T and return void * that
kono
parents: 55
diff changeset
509 could be used in situations where a default constructor is not provided
kono
parents: 55
diff changeset
510 by the class T. */
kono
parents: 55
diff changeset
511
kono
parents: 55
diff changeset
512 inline void *
kono
parents: 55
diff changeset
513 allocate_raw () ATTRIBUTE_MALLOC
kono
parents: 55
diff changeset
514 {
kono
parents: 55
diff changeset
515 return m_allocator.allocate ();
kono
parents: 55
diff changeset
516 }
kono
parents: 55
diff changeset
517
kono
parents: 55
diff changeset
518 inline void
kono
parents: 55
diff changeset
519 remove (T *object)
kono
parents: 55
diff changeset
520 {
kono
parents: 55
diff changeset
521 /* Call destructor. */
kono
parents: 55
diff changeset
522 object->~T ();
kono
parents: 55
diff changeset
523
kono
parents: 55
diff changeset
524 m_allocator.remove (object);
kono
parents: 55
diff changeset
525 }
kono
parents: 55
diff changeset
526
kono
parents: 55
diff changeset
527 inline size_t
kono
parents: 55
diff changeset
528 num_elts_current ()
kono
parents: 55
diff changeset
529 {
kono
parents: 55
diff changeset
530 return m_allocator.num_elts_current ();
kono
parents: 55
diff changeset
531 }
kono
parents: 55
diff changeset
532
kono
parents: 55
diff changeset
533 private:
kono
parents: 55
diff changeset
534 pool_allocator m_allocator;
kono
parents: 55
diff changeset
535 };
kono
parents: 55
diff changeset
536
kono
parents: 55
diff changeset
537 /* Store information about each particular alloc_pool. Note that this
kono
parents: 55
diff changeset
538 will underestimate the amount the amount of storage used by a small amount:
kono
parents: 55
diff changeset
539 1) The overhead in a pool is not accounted for.
kono
parents: 55
diff changeset
540 2) The unallocated elements in a block are not accounted for. Note
kono
parents: 55
diff changeset
541 that this can at worst case be one element smaller that the block
kono
parents: 55
diff changeset
542 size for that pool. */
kono
parents: 55
diff changeset
543 struct alloc_pool_descriptor
kono
parents: 55
diff changeset
544 {
kono
parents: 55
diff changeset
545 /* Number of pools allocated. */
kono
parents: 55
diff changeset
546 unsigned long created;
kono
parents: 55
diff changeset
547 /* Gross allocated storage. */
kono
parents: 55
diff changeset
548 unsigned long allocated;
kono
parents: 55
diff changeset
549 /* Amount of currently active storage. */
kono
parents: 55
diff changeset
550 unsigned long current;
kono
parents: 55
diff changeset
551 /* Peak amount of storage used. */
kono
parents: 55
diff changeset
552 unsigned long peak;
kono
parents: 55
diff changeset
553 /* Size of element in the pool. */
kono
parents: 55
diff changeset
554 int elt_size;
kono
parents: 55
diff changeset
555 };
kono
parents: 55
diff changeset
556
kono
parents: 55
diff changeset
557 /* Helper for classes that do not provide default ctor. */
kono
parents: 55
diff changeset
558
kono
parents: 55
diff changeset
559 template <typename T>
kono
parents: 55
diff changeset
560 inline void *
kono
parents: 55
diff changeset
561 operator new (size_t, object_allocator<T> &a)
kono
parents: 55
diff changeset
562 {
kono
parents: 55
diff changeset
563 return a.allocate_raw ();
kono
parents: 55
diff changeset
564 }
kono
parents: 55
diff changeset
565
kono
parents: 55
diff changeset
566 /* Hashtable mapping alloc_pool names to descriptors. */
kono
parents: 55
diff changeset
567 extern hash_map<const char *, alloc_pool_descriptor> *alloc_pool_hash;
kono
parents: 55
diff changeset
568
kono
parents: 55
diff changeset
569
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
570 #endif