1 /* Functions to support a pool of allocatable objects
2 Copyright (C) 1997-2019 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@cgsoftware.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "memory-block.h"
24 #include "options.h" // for flag_checking
26 extern void dump_alloc_pool_statistics (void);
28 /* Flag indicates whether memory statistics are gathered any longer. */
29 extern bool after_memory_report
;
31 typedef unsigned long ALLOC_POOL_ID_TYPE
;
34 extern ALLOC_POOL_ID_TYPE last_id
;
36 /* Pool allocator memory usage. */
37 struct pool_usage
: public mem_usage
39 /* Default contructor. */
40 pool_usage (): m_element_size (0), m_pool_name ("") {}
42 pool_usage (size_t allocated
, size_t times
, size_t peak
,
43 size_t instances
, size_t element_size
,
44 const char *pool_name
)
45 : mem_usage (allocated
, times
, peak
, instances
),
46 m_element_size (element_size
),
47 m_pool_name (pool_name
) {}
49 /* Sum the usage with SECOND usage. */
51 operator+ (const pool_usage
&second
)
53 return pool_usage (m_allocated
+ second
.m_allocated
,
54 m_times
+ second
.m_times
,
55 m_peak
+ second
.m_peak
,
56 m_instances
+ second
.m_instances
,
57 m_element_size
, m_pool_name
);
60 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
62 dump (mem_location
*loc
, mem_usage
&total
) const
64 char *location_string
= loc
->to_string ();
66 fprintf (stderr
, "%-32s%-48s " PRsa(5) PRsa(9) ":%5.1f%%"
67 PRsa(9) PRsa(9) ":%5.1f%%%12" PRIu64
"\n",
68 m_pool_name
, location_string
,
69 SIZE_AMOUNT (m_instances
),
70 SIZE_AMOUNT (m_allocated
),
71 get_percent (m_allocated
, total
.m_allocated
),
73 SIZE_AMOUNT (m_times
),
74 get_percent (m_times
, total
.m_times
),
75 (uint64_t)m_element_size
);
77 free (location_string
);
80 /* Dump header with NAME. */
82 dump_header (const char *name
)
84 fprintf (stderr
, "%-32s%-48s %6s%11s%16s%17s%12s\n", "Pool name", name
,
85 "Pools", "Leak", "Peak", "Times", "Elt size");
94 fprintf (stderr
, "%s" PRsa(82) PRsa(10) "\n", "Total",
95 SIZE_AMOUNT (m_instances
), SIZE_AMOUNT (m_allocated
));
100 size_t m_element_size
;
102 const char *m_pool_name
;
105 extern mem_alloc_description
<pool_usage
> pool_allocator_usage
;
108 /* If a pool with custom block size is needed, one might use the following
109 template. An instance of this template can be used as a parameter for
110 instantiating base_pool_allocator template:
112 typedef custom_block_allocator <128*1024> huge_block_allocator;
114 static base_pool_allocator <huge_block_allocator>
115 value_pool ("value", 16384);
117 Right now it's not used anywhere in the code, and is given here as an
120 template <size_t BlockSize
>
121 class custom_block_allocator
124 static const size_t block_size
= BlockSize
;
127 allocate () ATTRIBUTE_MALLOC
129 return XNEWVEC (char, BlockSize
);
133 release (void *block
)
140 /* Generic pool allocator. */
142 template <typename TBlockAllocator
>
143 class base_pool_allocator
146 /* Default constructor for pool allocator called NAME. */
147 base_pool_allocator (const char *name
, size_t size CXX_MEM_STAT_INFO
);
148 ~base_pool_allocator ();
150 void release_if_empty ();
151 void *allocate () ATTRIBUTE_MALLOC
;
152 void remove (void *object
);
153 size_t num_elts_current ();
156 struct allocation_pool_list
158 allocation_pool_list
*next
;
161 /* Initialize a pool allocator. */
164 struct allocation_object
167 /* The ID of alloc pool which the object was allocated from. */
168 ALLOC_POOL_ID_TYPE id
;
173 /* The data of the object. */
176 /* Because we want any type of data to be well aligned after the ID,
177 the following elements are here. They are never accessed so
178 the allocated object may be even smaller than this structure.
179 We do not care about alignment for floating-point types. */
185 static inline allocation_object
*
186 get_instance (void *data_ptr
)
188 return (allocation_object
*)(((char *)(data_ptr
))
189 - offsetof (allocation_object
,
195 get_data (void *instance_ptr
)
197 return (void*)(((allocation_object
*) instance_ptr
)->u
.data
);
203 align_eight (size_t x
)
205 return (((x
+7) >> 3) << 3);
209 ALLOC_POOL_ID_TYPE m_id
;
210 size_t m_elts_per_block
;
212 /* These are the elements that have been allocated at least once
214 allocation_pool_list
*m_returned_free_list
;
216 /* These are the elements that have not yet been allocated out of
217 the last block obtained from XNEWVEC. */
218 char* m_virgin_free_list
;
220 /* The number of elements in the virgin_free_list that can be
221 allocated before needing another block. */
222 size_t m_virgin_elts_remaining
;
223 /* The number of elements that are allocated. */
224 size_t m_elts_allocated
;
225 /* The number of elements that are released. */
227 /* The number of allocated blocks. */
228 size_t m_blocks_allocated
;
229 /* List of blocks that are used to allocate new objects. */
230 allocation_pool_list
*m_block_list
;
231 /* Size of a pool elements in bytes. */
233 /* Size in bytes that should be allocated for each element. */
235 /* Flag if a pool allocator is initialized. */
237 /* Memory allocation location. */
238 mem_location m_location
;
241 template <typename TBlockAllocator
>
243 base_pool_allocator
<TBlockAllocator
>::base_pool_allocator (
244 const char *name
, size_t size MEM_STAT_DECL
):
245 m_name (name
), m_id (0), m_elts_per_block (0), m_returned_free_list (NULL
),
246 m_virgin_free_list (NULL
), m_virgin_elts_remaining (0), m_elts_allocated (0),
247 m_elts_free (0), m_blocks_allocated (0), m_block_list (NULL
), m_elt_size (0),
248 m_size (size
), m_initialized (false),
249 m_location (ALLOC_POOL_ORIGIN
, false PASS_MEM_STAT
) {}
251 /* Initialize a pool allocator. */
253 template <typename TBlockAllocator
>
255 base_pool_allocator
<TBlockAllocator
>::initialize ()
257 gcc_checking_assert (!m_initialized
);
258 m_initialized
= true;
260 size_t size
= m_size
;
262 gcc_checking_assert (m_name
);
263 gcc_checking_assert (m_size
);
265 /* Make size large enough to store the list header. */
266 if (size
< sizeof (allocation_pool_list
*))
267 size
= sizeof (allocation_pool_list
*);
269 /* Now align the size to a multiple of 8. */
270 size
= align_eight (size
);
272 /* Add the aligned size of ID. */
273 size
+= offsetof (allocation_object
, u
.data
);
277 if (GATHER_STATISTICS
)
279 pool_usage
*u
= pool_allocator_usage
.register_descriptor
280 (this, new mem_location (m_location
));
282 u
->m_element_size
= m_elt_size
;
283 u
->m_pool_name
= m_name
;
286 /* List header size should be a multiple of 8. */
287 size_t header_size
= align_eight (sizeof (allocation_pool_list
));
289 m_elts_per_block
= (TBlockAllocator::block_size
- header_size
) / size
;
290 gcc_checking_assert (m_elts_per_block
!= 0);
292 /* Increase the last used ID and use it for this pool.
293 ID == 0 is used for free elements of pool so skip it. */
301 /* Free all memory allocated for the given memory pool. */
302 template <typename TBlockAllocator
>
304 base_pool_allocator
<TBlockAllocator
>::release ()
309 allocation_pool_list
*block
, *next_block
;
311 /* Free each block allocated to the pool. */
312 for (block
= m_block_list
; block
!= NULL
; block
= next_block
)
314 next_block
= block
->next
;
315 TBlockAllocator::release (block
);
318 if (GATHER_STATISTICS
&& !after_memory_report
)
320 pool_allocator_usage
.release_instance_overhead
321 (this, (m_elts_allocated
- m_elts_free
) * m_elt_size
);
324 m_returned_free_list
= NULL
;
325 m_virgin_free_list
= NULL
;
326 m_virgin_elts_remaining
= 0;
327 m_elts_allocated
= 0;
329 m_blocks_allocated
= 0;
333 template <typename TBlockAllocator
>
335 base_pool_allocator
<TBlockAllocator
>::release_if_empty ()
337 if (m_elts_free
== m_elts_allocated
)
341 template <typename TBlockAllocator
>
342 inline base_pool_allocator
<TBlockAllocator
>::~base_pool_allocator ()
347 /* Allocates one element from the pool specified. */
348 template <typename TBlockAllocator
>
350 base_pool_allocator
<TBlockAllocator
>::allocate ()
355 allocation_pool_list
*header
;
356 #ifdef ENABLE_VALGRIND_ANNOTATIONS
360 if (GATHER_STATISTICS
)
362 pool_allocator_usage
.register_instance_overhead (m_elt_size
, this);
365 #ifdef ENABLE_VALGRIND_ANNOTATIONS
366 size
= m_elt_size
- offsetof (allocation_object
, u
.data
);
369 /* If there are no more free elements, make some more!. */
370 if (!m_returned_free_list
)
373 if (!m_virgin_elts_remaining
)
375 allocation_pool_list
*block_header
;
377 /* Make the block. */
378 block
= reinterpret_cast<char *> (TBlockAllocator::allocate ());
379 block_header
= new (block
) allocation_pool_list
;
380 block
+= align_eight (sizeof (allocation_pool_list
));
382 /* Throw it on the block list. */
383 block_header
->next
= m_block_list
;
384 m_block_list
= block_header
;
386 /* Make the block available for allocation. */
387 m_virgin_free_list
= block
;
388 m_virgin_elts_remaining
= m_elts_per_block
;
390 /* Also update the number of elements we have free/allocated, and
391 increment the allocated block count. */
392 m_elts_allocated
+= m_elts_per_block
;
393 m_elts_free
+= m_elts_per_block
;
394 m_blocks_allocated
+= 1;
397 /* We now know that we can take the first elt off the virgin list and
398 put it on the returned list. */
399 block
= m_virgin_free_list
;
400 header
= (allocation_pool_list
*) allocation_object::get_data (block
);
403 /* Mark the element to be free. */
405 ((allocation_object
*) block
)->id
= 0;
407 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (header
,size
));
408 m_returned_free_list
= header
;
409 m_virgin_free_list
+= m_elt_size
;
410 m_virgin_elts_remaining
--;
414 /* Pull the first free element from the free list, and return it. */
415 header
= m_returned_free_list
;
416 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (header
, sizeof (*header
)));
417 m_returned_free_list
= header
->next
;
420 /* Set the ID for element. */
422 allocation_object::get_instance (header
)->id
= m_id
;
424 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (header
, size
));
426 return (void *)(header
);
429 /* Puts PTR back on POOL's free list. */
430 template <typename TBlockAllocator
>
432 base_pool_allocator
<TBlockAllocator
>::remove (void *object
)
434 int size
= m_elt_size
- offsetof (allocation_object
, u
.data
);
438 gcc_assert (m_initialized
);
440 /* Check if we free more than we allocated. */
441 && m_elts_free
< m_elts_allocated
);
443 /* Check whether the PTR was allocated from POOL. */
444 gcc_assert (m_id
== allocation_object::get_instance (object
)->id
);
447 memset (object
, 0xaf, size
);
451 /* Mark the element to be free. */
452 allocation_object::get_instance (object
)->id
= 0;
455 allocation_pool_list
*header
= new (object
) allocation_pool_list
;
456 header
->next
= m_returned_free_list
;
457 m_returned_free_list
= header
;
458 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (object
, size
));
461 if (GATHER_STATISTICS
)
463 pool_allocator_usage
.release_instance_overhead (this, m_elt_size
);
467 /* Number of elements currently active (not returned to pool). Used for cheap
468 consistency checks. */
469 template <typename TBlockAllocator
>
471 base_pool_allocator
<TBlockAllocator
>::num_elts_current ()
473 return m_elts_allocated
- m_elts_free
;
476 /* Specialization of base_pool_allocator which should be used in most cases.
477 Another specialization may be needed, if object size is greater than
478 memory_block_pool::block_size (64 KB). */
479 typedef base_pool_allocator
<memory_block_pool
> pool_allocator
;
481 /* Type based memory pool allocator. */
482 template <typename T
>
483 class object_allocator
486 /* Default constructor for pool allocator called NAME. */
487 object_allocator (const char *name CXX_MEM_STAT_INFO
):
488 m_allocator (name
, sizeof (T
) PASS_MEM_STAT
) {}
493 m_allocator
.release ();
496 inline void release_if_empty ()
498 m_allocator
.release_if_empty ();
502 /* Allocate memory for instance of type T and call a default constructor. */
505 allocate () ATTRIBUTE_MALLOC
507 return ::new (m_allocator
.allocate ()) T
;
510 /* Allocate memory for instance of type T and return void * that
511 could be used in situations where a default constructor is not provided
515 allocate_raw () ATTRIBUTE_MALLOC
517 return m_allocator
.allocate ();
523 /* Call destructor. */
526 m_allocator
.remove (object
);
532 return m_allocator
.num_elts_current ();
536 pool_allocator m_allocator
;
539 /* Store information about each particular alloc_pool. Note that this
540 will underestimate the amount the amount of storage used by a small amount:
541 1) The overhead in a pool is not accounted for.
542 2) The unallocated elements in a block are not accounted for. Note
543 that this can at worst case be one element smaller that the block
544 size for that pool. */
545 struct alloc_pool_descriptor
547 /* Number of pools allocated. */
548 unsigned long created
;
549 /* Gross allocated storage. */
550 unsigned long allocated
;
551 /* Amount of currently active storage. */
552 unsigned long current
;
553 /* Peak amount of storage used. */
555 /* Size of element in the pool. */
559 /* Helper for classes that do not provide default ctor. */
561 template <typename T
>
563 operator new (size_t, object_allocator
<T
> &a
)
565 return a
.allocate_raw ();
568 /* Hashtable mapping alloc_pool names to descriptors. */
569 extern hash_map
<const char *, alloc_pool_descriptor
> *alloc_pool_hash
;