1 /* Functions to support a pool of allocatable objects
2 Copyright (C) 1997-2018 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 %6li%10li:%5.1f%%%10li%10li:%5.1f%%%12li\n",
67 m_pool_name
, location_string
, (long)m_instances
,
68 (long)m_allocated
, get_percent (m_allocated
, total
.m_allocated
),
69 (long)m_peak
, (long)m_times
,
70 get_percent (m_times
, total
.m_times
),
71 (long)m_element_size
);
73 free (location_string
);
76 /* Dump header with NAME. */
78 dump_header (const char *name
)
80 fprintf (stderr
, "%-32s%-48s %6s%11s%16s%17s%12s\n", "Pool name", name
,
81 "Pools", "Leak", "Peak", "Times", "Elt size");
90 fprintf (stderr
, "%s%82li%10li\n", "Total", (long)m_instances
,
96 size_t m_element_size
;
98 const char *m_pool_name
;
101 extern mem_alloc_description
<pool_usage
> pool_allocator_usage
;
104 /* If a pool with custom block size is needed, one might use the following
105 template. An instance of this template can be used as a parameter for
106 instantiating base_pool_allocator template:
108 typedef custom_block_allocator <128*1024> huge_block_allocator;
110 static base_pool_allocator <huge_block_allocator>
111 value_pool ("value", 16384);
113 Right now it's not used anywhere in the code, and is given here as an
116 template <size_t BlockSize
>
117 class custom_block_allocator
120 static const size_t block_size
= BlockSize
;
123 allocate () ATTRIBUTE_MALLOC
125 return XNEWVEC (char, BlockSize
);
129 release (void *block
)
136 /* Generic pool allocator. */
138 template <typename TBlockAllocator
>
139 class base_pool_allocator
142 /* Default constructor for pool allocator called NAME. */
143 base_pool_allocator (const char *name
, size_t size CXX_MEM_STAT_INFO
);
144 ~base_pool_allocator ();
146 void release_if_empty ();
147 void *allocate () ATTRIBUTE_MALLOC
;
148 void remove (void *object
);
149 size_t num_elts_current ();
152 struct allocation_pool_list
154 allocation_pool_list
*next
;
157 /* Initialize a pool allocator. */
160 struct allocation_object
163 /* The ID of alloc pool which the object was allocated from. */
164 ALLOC_POOL_ID_TYPE id
;
169 /* The data of the object. */
172 /* Because we want any type of data to be well aligned after the ID,
173 the following elements are here. They are never accessed so
174 the allocated object may be even smaller than this structure.
175 We do not care about alignment for floating-point types. */
181 static inline allocation_object
*
182 get_instance (void *data_ptr
)
184 return (allocation_object
*)(((char *)(data_ptr
))
185 - offsetof (allocation_object
,
191 get_data (void *instance_ptr
)
193 return (void*)(((allocation_object
*) instance_ptr
)->u
.data
);
199 align_eight (size_t x
)
201 return (((x
+7) >> 3) << 3);
205 ALLOC_POOL_ID_TYPE m_id
;
206 size_t m_elts_per_block
;
208 /* These are the elements that have been allocated at least once
210 allocation_pool_list
*m_returned_free_list
;
212 /* These are the elements that have not yet been allocated out of
213 the last block obtained from XNEWVEC. */
214 char* m_virgin_free_list
;
216 /* The number of elements in the virgin_free_list that can be
217 allocated before needing another block. */
218 size_t m_virgin_elts_remaining
;
219 /* The number of elements that are allocated. */
220 size_t m_elts_allocated
;
221 /* The number of elements that are released. */
223 /* The number of allocated blocks. */
224 size_t m_blocks_allocated
;
225 /* List of blocks that are used to allocate new objects. */
226 allocation_pool_list
*m_block_list
;
227 /* Size of a pool elements in bytes. */
229 /* Size in bytes that should be allocated for each element. */
231 /* Flag if a pool allocator is initialized. */
233 /* Memory allocation location. */
234 mem_location m_location
;
237 template <typename TBlockAllocator
>
239 base_pool_allocator
<TBlockAllocator
>::base_pool_allocator (
240 const char *name
, size_t size MEM_STAT_DECL
):
241 m_name (name
), m_id (0), m_elts_per_block (0), m_returned_free_list (NULL
),
242 m_virgin_free_list (NULL
), m_virgin_elts_remaining (0), m_elts_allocated (0),
243 m_elts_free (0), m_blocks_allocated (0), m_block_list (NULL
), m_elt_size (0),
244 m_size (size
), m_initialized (false),
245 m_location (ALLOC_POOL_ORIGIN
, false PASS_MEM_STAT
) {}
247 /* Initialize a pool allocator. */
249 template <typename TBlockAllocator
>
251 base_pool_allocator
<TBlockAllocator
>::initialize ()
253 gcc_checking_assert (!m_initialized
);
254 m_initialized
= true;
256 size_t size
= m_size
;
258 gcc_checking_assert (m_name
);
260 /* Make size large enough to store the list header. */
261 if (size
< sizeof (allocation_pool_list
*))
262 size
= sizeof (allocation_pool_list
*);
264 /* Now align the size to a multiple of 8. */
265 size
= align_eight (size
);
267 /* Add the aligned size of ID. */
268 size
+= offsetof (allocation_object
, u
.data
);
272 if (GATHER_STATISTICS
)
274 pool_usage
*u
= pool_allocator_usage
.register_descriptor
275 (this, new mem_location (m_location
));
277 u
->m_element_size
= m_elt_size
;
278 u
->m_pool_name
= m_name
;
281 /* List header size should be a multiple of 8. */
282 size_t header_size
= align_eight (sizeof (allocation_pool_list
));
284 m_elts_per_block
= (TBlockAllocator::block_size
- header_size
) / size
;
285 gcc_checking_assert (m_elts_per_block
!= 0);
287 /* Increase the last used ID and use it for this pool.
288 ID == 0 is used for free elements of pool so skip it. */
296 /* Free all memory allocated for the given memory pool. */
297 template <typename TBlockAllocator
>
299 base_pool_allocator
<TBlockAllocator
>::release ()
304 allocation_pool_list
*block
, *next_block
;
306 /* Free each block allocated to the pool. */
307 for (block
= m_block_list
; block
!= NULL
; block
= next_block
)
309 next_block
= block
->next
;
310 TBlockAllocator::release (block
);
313 if (GATHER_STATISTICS
&& !after_memory_report
)
315 pool_allocator_usage
.release_instance_overhead
316 (this, (m_elts_allocated
- m_elts_free
) * m_elt_size
);
319 m_returned_free_list
= NULL
;
320 m_virgin_free_list
= NULL
;
321 m_virgin_elts_remaining
= 0;
322 m_elts_allocated
= 0;
324 m_blocks_allocated
= 0;
328 template <typename TBlockAllocator
>
330 base_pool_allocator
<TBlockAllocator
>::release_if_empty ()
332 if (m_elts_free
== m_elts_allocated
)
336 template <typename TBlockAllocator
>
337 inline base_pool_allocator
<TBlockAllocator
>::~base_pool_allocator ()
342 /* Allocates one element from the pool specified. */
343 template <typename TBlockAllocator
>
345 base_pool_allocator
<TBlockAllocator
>::allocate ()
350 allocation_pool_list
*header
;
351 #ifdef ENABLE_VALGRIND_ANNOTATIONS
355 if (GATHER_STATISTICS
)
357 pool_allocator_usage
.register_instance_overhead (m_elt_size
, this);
360 #ifdef ENABLE_VALGRIND_ANNOTATIONS
361 size
= m_elt_size
- offsetof (allocation_object
, u
.data
);
364 /* If there are no more free elements, make some more!. */
365 if (!m_returned_free_list
)
368 if (!m_virgin_elts_remaining
)
370 allocation_pool_list
*block_header
;
372 /* Make the block. */
373 block
= reinterpret_cast<char *> (TBlockAllocator::allocate ());
374 block_header
= new (block
) allocation_pool_list
;
375 block
+= align_eight (sizeof (allocation_pool_list
));
377 /* Throw it on the block list. */
378 block_header
->next
= m_block_list
;
379 m_block_list
= block_header
;
381 /* Make the block available for allocation. */
382 m_virgin_free_list
= block
;
383 m_virgin_elts_remaining
= m_elts_per_block
;
385 /* Also update the number of elements we have free/allocated, and
386 increment the allocated block count. */
387 m_elts_allocated
+= m_elts_per_block
;
388 m_elts_free
+= m_elts_per_block
;
389 m_blocks_allocated
+= 1;
392 /* We now know that we can take the first elt off the virgin list and
393 put it on the returned list. */
394 block
= m_virgin_free_list
;
395 header
= (allocation_pool_list
*) allocation_object::get_data (block
);
398 /* Mark the element to be free. */
400 ((allocation_object
*) block
)->id
= 0;
402 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (header
,size
));
403 m_returned_free_list
= header
;
404 m_virgin_free_list
+= m_elt_size
;
405 m_virgin_elts_remaining
--;
409 /* Pull the first free element from the free list, and return it. */
410 header
= m_returned_free_list
;
411 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (header
, sizeof (*header
)));
412 m_returned_free_list
= header
->next
;
415 /* Set the ID for element. */
417 allocation_object::get_instance (header
)->id
= m_id
;
419 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (header
, size
));
421 return (void *)(header
);
424 /* Puts PTR back on POOL's free list. */
425 template <typename TBlockAllocator
>
427 base_pool_allocator
<TBlockAllocator
>::remove (void *object
)
429 int size
= m_elt_size
- offsetof (allocation_object
, u
.data
);
433 gcc_assert (m_initialized
);
435 /* Check if we free more than we allocated. */
436 && m_elts_free
< m_elts_allocated
);
438 /* Check whether the PTR was allocated from POOL. */
439 gcc_assert (m_id
== allocation_object::get_instance (object
)->id
);
442 memset (object
, 0xaf, size
);
446 /* Mark the element to be free. */
447 allocation_object::get_instance (object
)->id
= 0;
450 allocation_pool_list
*header
= new (object
) allocation_pool_list
;
451 header
->next
= m_returned_free_list
;
452 m_returned_free_list
= header
;
453 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (object
, size
));
456 if (GATHER_STATISTICS
)
458 pool_allocator_usage
.release_instance_overhead (this, m_elt_size
);
462 /* Number of elements currently active (not returned to pool). Used for cheap
463 consistency checks. */
464 template <typename TBlockAllocator
>
466 base_pool_allocator
<TBlockAllocator
>::num_elts_current ()
468 return m_elts_allocated
- m_elts_free
;
471 /* Specialization of base_pool_allocator which should be used in most cases.
472 Another specialization may be needed, if object size is greater than
473 memory_block_pool::block_size (64 KB). */
474 typedef base_pool_allocator
<memory_block_pool
> pool_allocator
;
476 /* Type based memory pool allocator. */
477 template <typename T
>
478 class object_allocator
481 /* Default constructor for pool allocator called NAME. */
482 object_allocator (const char *name CXX_MEM_STAT_INFO
):
483 m_allocator (name
, sizeof (T
) PASS_MEM_STAT
) {}
488 m_allocator
.release ();
491 inline void release_if_empty ()
493 m_allocator
.release_if_empty ();
497 /* Allocate memory for instance of type T and call a default constructor. */
500 allocate () ATTRIBUTE_MALLOC
502 return ::new (m_allocator
.allocate ()) T
;
505 /* Allocate memory for instance of type T and return void * that
506 could be used in situations where a default constructor is not provided
510 allocate_raw () ATTRIBUTE_MALLOC
512 return m_allocator
.allocate ();
518 /* Call destructor. */
521 m_allocator
.remove (object
);
527 return m_allocator
.num_elts_current ();
531 pool_allocator m_allocator
;
534 /* Store information about each particular alloc_pool. Note that this
535 will underestimate the amount the amount of storage used by a small amount:
536 1) The overhead in a pool is not accounted for.
537 2) The unallocated elements in a block are not accounted for. Note
538 that this can at worst case be one element smaller that the block
539 size for that pool. */
540 struct alloc_pool_descriptor
542 /* Number of pools allocated. */
543 unsigned long created
;
544 /* Gross allocated storage. */
545 unsigned long allocated
;
546 /* Amount of currently active storage. */
547 unsigned long current
;
548 /* Peak amount of storage used. */
550 /* Size of element in the pool. */
554 /* Helper for classes that do not provide default ctor. */
556 template <typename T
>
558 operator new (size_t, object_allocator
<T
> &a
)
560 return a
.allocate_raw ();
563 /* Hashtable mapping alloc_pool names to descriptors. */
564 extern hash_map
<const char *, alloc_pool_descriptor
> *alloc_pool_hash
;