Clear hardware capabilities for gcc.dg/vect/vect-simd-clone-*.c
[official-gcc.git] / gcc / alloc-pool.c
blobdfb13ce55fb1c13a854c1ec90a1c1fc733d509f4
1 /* Functions to support a pool of allocatable objects.
2 Copyright (C) 1987-2014 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 it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "alloc-pool.h"
24 #include "hash-table.h"
26 #define align_eight(x) (((x+7) >> 3) << 3)
28 /* The internal allocation object. */
29 typedef struct allocation_object_def
31 #ifdef ENABLE_CHECKING
32 /* The ID of alloc pool which the object was allocated from. */
33 ALLOC_POOL_ID_TYPE id;
34 #endif
36 union
38 /* The data of the object. */
39 char data[1];
41 /* Because we want any type of data to be well aligned after the ID,
42 the following elements are here. They are never accessed so
43 the allocated object may be even smaller than this structure.
44 We do not care about alignment for floating-point types. */
45 char *align_p;
46 HOST_WIDEST_INT align_i;
47 } u;
48 } allocation_object;
50 /* Convert a pointer to allocation_object from a pointer to user data. */
51 #define ALLOCATION_OBJECT_PTR_FROM_USER_PTR(X) \
52 ((allocation_object *) (((char *) (X)) \
53 - offsetof (allocation_object, u.data)))
55 /* Convert a pointer to user data from a pointer to allocation_object. */
56 #define USER_PTR_FROM_ALLOCATION_OBJECT_PTR(X) \
57 ((void *) (((allocation_object *) (X))->u.data))
59 #ifdef ENABLE_CHECKING
60 /* Last used ID. */
61 static ALLOC_POOL_ID_TYPE last_id;
62 #endif
64 /* Store information about each particular alloc_pool. Note that this
65 will underestimate the amount the amount of storage used by a small amount:
66 1) The overhead in a pool is not accounted for.
67 2) The unallocated elements in a block are not accounted for. Note
68 that this can at worst case be one element smaller that the block
69 size for that pool. */
70 struct alloc_pool_descriptor
72 const char *name;
73 /* Number of pools allocated. */
74 unsigned long created;
75 /* Gross allocated storage. */
76 unsigned long allocated;
77 /* Amount of currently active storage. */
78 unsigned long current;
79 /* Peak amount of storage used. */
80 unsigned long peak;
81 /* Size of element in the pool. */
82 int elt_size;
85 /* Hashtable helpers. */
86 struct alloc_pool_hasher : typed_noop_remove <alloc_pool_descriptor>
88 typedef alloc_pool_descriptor value_type;
89 typedef char compare_type;
90 static inline hashval_t hash (const alloc_pool_descriptor *);
91 static inline bool equal (const value_type *, const compare_type *);
94 inline hashval_t
95 alloc_pool_hasher::hash (const value_type *d)
97 return htab_hash_pointer (d->name);
100 inline bool
101 alloc_pool_hasher::equal (const value_type *d,
102 const compare_type *p2)
104 return d->name == p2;
107 /* Hashtable mapping alloc_pool names to descriptors. */
108 static hash_table <alloc_pool_hasher> alloc_pool_hash;
110 /* For given name, return descriptor, create new if needed. */
111 static struct alloc_pool_descriptor *
112 allocate_pool_descriptor (const char *name)
114 struct alloc_pool_descriptor **slot;
116 if (!alloc_pool_hash.is_created ())
117 alloc_pool_hash.create (10);
119 slot = alloc_pool_hash.find_slot_with_hash (name,
120 htab_hash_pointer (name), INSERT);
121 if (*slot)
122 return *slot;
123 *slot = XCNEW (struct alloc_pool_descriptor);
124 (*slot)->name = name;
125 return *slot;
128 /* Create a pool of things of size SIZE, with NUM in each block we
129 allocate. */
131 alloc_pool
132 create_alloc_pool (const char *name, size_t size, size_t num)
134 alloc_pool pool;
135 size_t header_size;
137 gcc_checking_assert (name);
139 /* Make size large enough to store the list header. */
140 if (size < sizeof (alloc_pool_list))
141 size = sizeof (alloc_pool_list);
143 /* Now align the size to a multiple of 4. */
144 size = align_eight (size);
146 #ifdef ENABLE_CHECKING
147 /* Add the aligned size of ID. */
148 size += offsetof (allocation_object, u.data);
149 #endif
151 /* Um, we can't really allocate 0 elements per block. */
152 gcc_checking_assert (num);
154 /* Allocate memory for the pool structure. */
155 pool = XNEW (struct alloc_pool_def);
157 /* Now init the various pieces of our pool structure. */
158 pool->name = /*xstrdup (name)*/name;
159 pool->elt_size = size;
160 pool->elts_per_block = num;
162 if (GATHER_STATISTICS)
164 struct alloc_pool_descriptor *desc = allocate_pool_descriptor (name);
165 desc->elt_size = size;
166 desc->created++;
169 /* List header size should be a multiple of 8. */
170 header_size = align_eight (sizeof (struct alloc_pool_list_def));
172 pool->block_size = (size * num) + header_size;
173 pool->returned_free_list = NULL;
174 pool->virgin_free_list = NULL;
175 pool->virgin_elts_remaining = 0;
176 pool->elts_allocated = 0;
177 pool->elts_free = 0;
178 pool->blocks_allocated = 0;
179 pool->block_list = NULL;
181 #ifdef ENABLE_CHECKING
182 /* Increase the last used ID and use it for this pool.
183 ID == 0 is used for free elements of pool so skip it. */
184 last_id++;
185 if (last_id == 0)
186 last_id++;
188 pool->id = last_id;
189 #endif
191 return (pool);
194 /* Free all memory allocated for the given memory pool. */
195 void
196 empty_alloc_pool (alloc_pool pool)
198 alloc_pool_list block, next_block;
200 gcc_checking_assert (pool);
202 /* Free each block allocated to the pool. */
203 for (block = pool->block_list; block != NULL; block = next_block)
205 next_block = block->next;
206 free (block);
209 if (GATHER_STATISTICS)
211 struct alloc_pool_descriptor *desc = allocate_pool_descriptor (pool->name);
212 desc->current -= (pool->elts_allocated - pool->elts_free) * pool->elt_size;
215 pool->returned_free_list = NULL;
216 pool->virgin_free_list = NULL;
217 pool->virgin_elts_remaining = 0;
218 pool->elts_allocated = 0;
219 pool->elts_free = 0;
220 pool->blocks_allocated = 0;
221 pool->block_list = NULL;
224 /* Free all memory allocated for the given memory pool and the pool itself. */
225 void
226 free_alloc_pool (alloc_pool pool)
228 /* First empty the pool. */
229 empty_alloc_pool (pool);
230 #ifdef ENABLE_CHECKING
231 memset (pool, 0xaf, sizeof (*pool));
232 #endif
233 /* Lastly, free the pool. */
234 free (pool);
237 /* Frees the alloc_pool, if it is empty and zero *POOL in this case. */
238 void
239 free_alloc_pool_if_empty (alloc_pool *pool)
241 if ((*pool)->elts_free == (*pool)->elts_allocated)
243 free_alloc_pool (*pool);
244 *pool = NULL;
248 /* Allocates one element from the pool specified. */
249 void *
250 pool_alloc (alloc_pool pool)
252 alloc_pool_list header;
253 #ifdef ENABLE_VALGRIND_CHECKING
254 int size;
255 #endif
257 if (GATHER_STATISTICS)
259 struct alloc_pool_descriptor *desc = allocate_pool_descriptor (pool->name);
261 desc->allocated += pool->elt_size;
262 desc->current += pool->elt_size;
263 if (desc->peak < desc->current)
264 desc->peak = desc->current;
267 gcc_checking_assert (pool);
268 #ifdef ENABLE_VALGRIND_CHECKING
269 size = pool->elt_size - offsetof (allocation_object, u.data);
270 #endif
272 /* If there are no more free elements, make some more!. */
273 if (!pool->returned_free_list)
275 char *block;
276 if (!pool->virgin_elts_remaining)
278 alloc_pool_list block_header;
280 /* Make the block. */
281 block = XNEWVEC (char, pool->block_size);
282 block_header = (alloc_pool_list) block;
283 block += align_eight (sizeof (struct alloc_pool_list_def));
285 /* Throw it on the block list. */
286 block_header->next = pool->block_list;
287 pool->block_list = block_header;
289 /* Make the block available for allocation. */
290 pool->virgin_free_list = block;
291 pool->virgin_elts_remaining = pool->elts_per_block;
293 /* Also update the number of elements we have free/allocated, and
294 increment the allocated block count. */
295 pool->elts_allocated += pool->elts_per_block;
296 pool->elts_free += pool->elts_per_block;
297 pool->blocks_allocated += 1;
301 /* We now know that we can take the first elt off the virgin list and
302 put it on the returned list. */
303 block = pool->virgin_free_list;
304 header = (alloc_pool_list) USER_PTR_FROM_ALLOCATION_OBJECT_PTR (block);
305 header->next = NULL;
306 #ifdef ENABLE_CHECKING
307 /* Mark the element to be free. */
308 ((allocation_object *) block)->id = 0;
309 #endif
310 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (header,size));
311 pool->returned_free_list = header;
312 pool->virgin_free_list += pool->elt_size;
313 pool->virgin_elts_remaining--;
317 /* Pull the first free element from the free list, and return it. */
318 header = pool->returned_free_list;
319 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (header, sizeof (*header)));
320 pool->returned_free_list = header->next;
321 pool->elts_free--;
323 #ifdef ENABLE_CHECKING
324 /* Set the ID for element. */
325 ALLOCATION_OBJECT_PTR_FROM_USER_PTR (header)->id = pool->id;
326 #endif
327 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (header, size));
329 return ((void *) header);
332 /* Puts PTR back on POOL's free list. */
333 void
334 pool_free (alloc_pool pool, void *ptr)
336 alloc_pool_list header;
337 #if defined(ENABLE_VALGRIND_CHECKING) || defined(ENABLE_CHECKING)
338 int size;
339 size = pool->elt_size - offsetof (allocation_object, u.data);
340 #endif
342 #ifdef ENABLE_CHECKING
343 gcc_assert (ptr
344 /* Check if we free more than we allocated, which is Bad (TM). */
345 && pool->elts_free < pool->elts_allocated
346 /* Check whether the PTR was allocated from POOL. */
347 && pool->id == ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr)->id);
349 memset (ptr, 0xaf, size);
351 /* Mark the element to be free. */
352 ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr)->id = 0;
353 #endif
355 header = (alloc_pool_list) ptr;
356 header->next = pool->returned_free_list;
357 pool->returned_free_list = header;
358 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (ptr, size));
359 pool->elts_free++;
361 if (GATHER_STATISTICS)
363 struct alloc_pool_descriptor *desc = allocate_pool_descriptor (pool->name);
364 desc->current -= pool->elt_size;
368 /* Output per-alloc_pool statistics. */
370 /* Used to accumulate statistics about alloc_pool sizes. */
371 struct output_info
373 unsigned long total_created;
374 unsigned long total_allocated;
377 /* Called via hash_table.traverse. Output alloc_pool descriptor pointed out by
378 SLOT and update statistics. */
380 print_alloc_pool_statistics (alloc_pool_descriptor **slot,
381 struct output_info *i)
383 struct alloc_pool_descriptor *d = *slot;
385 if (d->allocated)
387 fprintf (stderr,
388 "%-22s %6d %10lu %10lu(%10lu) %10lu(%10lu) %10lu(%10lu)\n",
389 d->name, d->elt_size, d->created, d->allocated,
390 d->allocated / d->elt_size, d->peak, d->peak / d->elt_size,
391 d->current, d->current / d->elt_size);
392 i->total_allocated += d->allocated;
393 i->total_created += d->created;
395 return 1;
398 /* Output per-alloc_pool memory usage statistics. */
399 void
400 dump_alloc_pool_statistics (void)
402 struct output_info info;
404 if (! GATHER_STATISTICS)
405 return;
407 if (!alloc_pool_hash.is_created ())
408 return;
410 fprintf (stderr, "\nAlloc-pool Kind Elt size Pools Allocated (elts) Peak (elts) Leak (elts)\n");
411 fprintf (stderr, "--------------------------------------------------------------------------------------------------------------\n");
412 info.total_created = 0;
413 info.total_allocated = 0;
414 alloc_pool_hash.traverse <struct output_info *,
415 print_alloc_pool_statistics> (&info);
416 fprintf (stderr, "--------------------------------------------------------------------------------------------------------------\n");
417 fprintf (stderr, "%-22s %7lu %10lu\n",
418 "Total", info.total_created, info.total_allocated);
419 fprintf (stderr, "--------------------------------------------------------------------------------------------------------------\n");