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