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");
92 fprintf (stderr
, "%s" PRsa(82) PRsa(10) "\n", "Total",
93 SIZE_AMOUNT (m_instances
), SIZE_AMOUNT (m_allocated
));
97 size_t m_element_size
;
99 const char *m_pool_name
;
102 extern mem_alloc_description
<pool_usage
> pool_allocator_usage
;
105 /* If a pool with custom block size is needed, one might use the following
106 template. An instance of this template can be used as a parameter for
107 instantiating base_pool_allocator template:
109 typedef custom_block_allocator <128*1024> huge_block_allocator;
111 static base_pool_allocator <huge_block_allocator>
112 value_pool ("value", 16384);
114 Right now it's not used anywhere in the code, and is given here as an
117 template <size_t BlockSize
>
118 class custom_block_allocator
121 static const size_t block_size
= BlockSize
;
124 allocate () ATTRIBUTE_MALLOC
126 return XNEWVEC (char, BlockSize
);
130 release (void *block
)
137 /* Generic pool allocator. */
139 template <typename TBlockAllocator
>
140 class base_pool_allocator
143 /* Default constructor for pool allocator called NAME. */
144 base_pool_allocator (const char *name
, size_t size CXX_MEM_STAT_INFO
);
145 ~base_pool_allocator ();
147 void release_if_empty ();
148 void *allocate () ATTRIBUTE_MALLOC
;
149 void remove (void *object
);
150 size_t num_elts_current ();
153 struct allocation_pool_list
155 allocation_pool_list
*next
;
158 /* Initialize a pool allocator. */
161 struct allocation_object
164 /* The ID of alloc pool which the object was allocated from. */
165 ALLOC_POOL_ID_TYPE id
;
170 /* The data of the object. */
173 /* Because we want any type of data to be well aligned after the ID,
174 the following elements are here. They are never accessed so
175 the allocated object may be even smaller than this structure.
176 We do not care about alignment for floating-point types. */
182 static inline allocation_object
*
183 get_instance (void *data_ptr
)
185 return (allocation_object
*)(((char *)(data_ptr
))
186 - offsetof (allocation_object
,
192 get_data (void *instance_ptr
)
194 return (void*)(((allocation_object
*) instance_ptr
)->u
.data
);
200 align_eight (size_t x
)
202 return (((x
+7) >> 3) << 3);
206 ALLOC_POOL_ID_TYPE m_id
;
207 size_t m_elts_per_block
;
209 /* These are the elements that have been allocated at least once
211 allocation_pool_list
*m_returned_free_list
;
213 /* These are the elements that have not yet been allocated out of
214 the last block obtained from XNEWVEC. */
215 char* m_virgin_free_list
;
217 /* The number of elements in the virgin_free_list that can be
218 allocated before needing another block. */
219 size_t m_virgin_elts_remaining
;
220 /* The number of elements that are allocated. */
221 size_t m_elts_allocated
;
222 /* The number of elements that are released. */
224 /* The number of allocated blocks. */
225 size_t m_blocks_allocated
;
226 /* List of blocks that are used to allocate new objects. */
227 allocation_pool_list
*m_block_list
;
228 /* Size of a pool elements in bytes. */
230 /* Size in bytes that should be allocated for each element. */
232 /* Flag if a pool allocator is initialized. */
234 /* Memory allocation location. */
235 mem_location m_location
;
238 template <typename TBlockAllocator
>
240 base_pool_allocator
<TBlockAllocator
>::base_pool_allocator (
241 const char *name
, size_t size MEM_STAT_DECL
):
242 m_name (name
), m_id (0), m_elts_per_block (0), m_returned_free_list (NULL
),
243 m_virgin_free_list (NULL
), m_virgin_elts_remaining (0), m_elts_allocated (0),
244 m_elts_free (0), m_blocks_allocated (0), m_block_list (NULL
), m_elt_size (0),
245 m_size (size
), m_initialized (false),
246 m_location (ALLOC_POOL_ORIGIN
, false PASS_MEM_STAT
) {}
248 /* Initialize a pool allocator. */
250 template <typename TBlockAllocator
>
252 base_pool_allocator
<TBlockAllocator
>::initialize ()
254 gcc_checking_assert (!m_initialized
);
255 m_initialized
= true;
257 size_t size
= m_size
;
259 gcc_checking_assert (m_name
);
260 gcc_checking_assert (m_size
);
262 /* Make size large enough to store the list header. */
263 if (size
< sizeof (allocation_pool_list
*))
264 size
= sizeof (allocation_pool_list
*);
266 /* Now align the size to a multiple of 8. */
267 size
= align_eight (size
);
269 /* Add the aligned size of ID. */
270 size
+= offsetof (allocation_object
, u
.data
);
274 if (GATHER_STATISTICS
)
276 pool_usage
*u
= pool_allocator_usage
.register_descriptor
277 (this, new mem_location (m_location
));
279 u
->m_element_size
= m_elt_size
;
280 u
->m_pool_name
= m_name
;
283 /* List header size should be a multiple of 8. */
284 size_t header_size
= align_eight (sizeof (allocation_pool_list
));
286 m_elts_per_block
= (TBlockAllocator::block_size
- header_size
) / size
;
287 gcc_checking_assert (m_elts_per_block
!= 0);
289 /* Increase the last used ID and use it for this pool.
290 ID == 0 is used for free elements of pool so skip it. */
298 /* Free all memory allocated for the given memory pool. */
299 template <typename TBlockAllocator
>
301 base_pool_allocator
<TBlockAllocator
>::release ()
306 allocation_pool_list
*block
, *next_block
;
308 /* Free each block allocated to the pool. */
309 for (block
= m_block_list
; block
!= NULL
; block
= next_block
)
311 next_block
= block
->next
;
312 TBlockAllocator::release (block
);
315 if (GATHER_STATISTICS
&& !after_memory_report
)
317 pool_allocator_usage
.release_instance_overhead
318 (this, (m_elts_allocated
- m_elts_free
) * m_elt_size
);
321 m_returned_free_list
= NULL
;
322 m_virgin_free_list
= NULL
;
323 m_virgin_elts_remaining
= 0;
324 m_elts_allocated
= 0;
326 m_blocks_allocated
= 0;
330 template <typename TBlockAllocator
>
332 base_pool_allocator
<TBlockAllocator
>::release_if_empty ()
334 if (m_elts_free
== m_elts_allocated
)
338 template <typename TBlockAllocator
>
339 inline base_pool_allocator
<TBlockAllocator
>::~base_pool_allocator ()
344 /* Allocates one element from the pool specified. */
345 template <typename TBlockAllocator
>
347 base_pool_allocator
<TBlockAllocator
>::allocate ()
352 allocation_pool_list
*header
;
353 #ifdef ENABLE_VALGRIND_ANNOTATIONS
357 if (GATHER_STATISTICS
)
359 pool_allocator_usage
.register_instance_overhead (m_elt_size
, this);
362 #ifdef ENABLE_VALGRIND_ANNOTATIONS
363 size
= m_elt_size
- offsetof (allocation_object
, u
.data
);
366 /* If there are no more free elements, make some more!. */
367 if (!m_returned_free_list
)
370 if (!m_virgin_elts_remaining
)
372 allocation_pool_list
*block_header
;
374 /* Make the block. */
375 block
= reinterpret_cast<char *> (TBlockAllocator::allocate ());
376 block_header
= new (block
) allocation_pool_list
;
377 block
+= align_eight (sizeof (allocation_pool_list
));
379 /* Throw it on the block list. */
380 block_header
->next
= m_block_list
;
381 m_block_list
= block_header
;
383 /* Make the block available for allocation. */
384 m_virgin_free_list
= block
;
385 m_virgin_elts_remaining
= m_elts_per_block
;
387 /* Also update the number of elements we have free/allocated, and
388 increment the allocated block count. */
389 m_elts_allocated
+= m_elts_per_block
;
390 m_elts_free
+= m_elts_per_block
;
391 m_blocks_allocated
+= 1;
394 /* We now know that we can take the first elt off the virgin list and
395 put it on the returned list. */
396 block
= m_virgin_free_list
;
397 header
= (allocation_pool_list
*) allocation_object::get_data (block
);
400 /* Mark the element to be free. */
402 ((allocation_object
*) block
)->id
= 0;
404 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (header
,size
));
405 m_returned_free_list
= header
;
406 m_virgin_free_list
+= m_elt_size
;
407 m_virgin_elts_remaining
--;
411 /* Pull the first free element from the free list, and return it. */
412 header
= m_returned_free_list
;
413 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (header
, sizeof (*header
)));
414 m_returned_free_list
= header
->next
;
417 /* Set the ID for element. */
419 allocation_object::get_instance (header
)->id
= m_id
;
421 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (header
, size
));
423 return (void *)(header
);
426 /* Puts PTR back on POOL's free list. */
427 template <typename TBlockAllocator
>
429 base_pool_allocator
<TBlockAllocator
>::remove (void *object
)
431 int size
= m_elt_size
- offsetof (allocation_object
, u
.data
);
435 gcc_assert (m_initialized
);
437 /* Check if we free more than we allocated. */
438 && m_elts_free
< m_elts_allocated
);
440 /* Check whether the PTR was allocated from POOL. */
441 gcc_assert (m_id
== allocation_object::get_instance (object
)->id
);
444 memset (object
, 0xaf, size
);
448 /* Mark the element to be free. */
449 allocation_object::get_instance (object
)->id
= 0;
452 allocation_pool_list
*header
= new (object
) allocation_pool_list
;
453 header
->next
= m_returned_free_list
;
454 m_returned_free_list
= header
;
455 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (object
, size
));
458 if (GATHER_STATISTICS
)
460 pool_allocator_usage
.release_instance_overhead (this, m_elt_size
);
464 /* Number of elements currently active (not returned to pool). Used for cheap
465 consistency checks. */
466 template <typename TBlockAllocator
>
468 base_pool_allocator
<TBlockAllocator
>::num_elts_current ()
470 return m_elts_allocated
- m_elts_free
;
473 /* Specialization of base_pool_allocator which should be used in most cases.
474 Another specialization may be needed, if object size is greater than
475 memory_block_pool::block_size (64 KB). */
476 typedef base_pool_allocator
<memory_block_pool
> pool_allocator
;
478 /* Type based memory pool allocator. */
479 template <typename T
>
480 class object_allocator
483 /* Default constructor for pool allocator called NAME. */
484 object_allocator (const char *name CXX_MEM_STAT_INFO
):
485 m_allocator (name
, sizeof (T
) PASS_MEM_STAT
) {}
490 m_allocator
.release ();
493 inline void release_if_empty ()
495 m_allocator
.release_if_empty ();
499 /* Allocate memory for instance of type T and call a default constructor. */
502 allocate () ATTRIBUTE_MALLOC
504 return ::new (m_allocator
.allocate ()) T
;
507 /* Allocate memory for instance of type T and return void * that
508 could be used in situations where a default constructor is not provided
512 allocate_raw () ATTRIBUTE_MALLOC
514 return m_allocator
.allocate ();
520 /* Call destructor. */
523 m_allocator
.remove (object
);
529 return m_allocator
.num_elts_current ();
533 pool_allocator m_allocator
;
536 /* Store information about each particular alloc_pool. Note that this
537 will underestimate the amount the amount of storage used by a small amount:
538 1) The overhead in a pool is not accounted for.
539 2) The unallocated elements in a block are not accounted for. Note
540 that this can at worst case be one element smaller that the block
541 size for that pool. */
542 struct alloc_pool_descriptor
544 /* Number of pools allocated. */
545 unsigned long created
;
546 /* Gross allocated storage. */
547 unsigned long allocated
;
548 /* Amount of currently active storage. */
549 unsigned long current
;
550 /* Peak amount of storage used. */
552 /* Size of element in the pool. */
556 /* Helper for classes that do not provide default ctor. */
558 template <typename T
>
560 operator new (size_t, object_allocator
<T
> &a
)
562 return a
.allocate_raw ();
565 /* Hashtable mapping alloc_pool names to descriptors. */
566 extern hash_map
<const char *, alloc_pool_descriptor
> *alloc_pool_hash
;