annotate gcc/alloc-pool.h @ 136:4627f235cf2a

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