OSDN Git Service

* alloc-pool.c (create_alloc_pool): Fix -Wc++-compat warnings.
[pf3gnuchains/gcc-fork.git] / gcc / alloc-pool.c
1 /* Functions to support a pool of allocatable objects.
2    Copyright (C) 1987, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007
3    Free Software Foundation, Inc.
4    Contributed by Daniel Berlin <dan@cgsoftware.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "alloc-pool.h"
25 #include "hashtab.h"
26
27 #define align_eight(x) (((x+7) >> 3) << 3)
28
29 /* The internal allocation object.  */
30 typedef struct allocation_object_def
31 {
32 #ifdef ENABLE_CHECKING
33   /* The ID of alloc pool which the object was allocated from.  */
34   ALLOC_POOL_ID_TYPE id;
35 #endif
36
37   union
38     {
39       /* The data of the object.  */
40       char data[1];
41
42       /* Because we want any type of data to be well aligned after the ID,
43          the following elements are here.  They are never accessed so
44          the allocated object may be even smaller than this structure.  */
45       char *align_p;
46       HOST_WIDEST_INT align_i;
47       long double align_ld;
48     } u;
49 } allocation_object;
50
51 /* Convert a pointer to allocation_object from a pointer to user data.  */
52 #define ALLOCATION_OBJECT_PTR_FROM_USER_PTR(X)                          \
53    ((allocation_object *) (((char *) (X))                               \
54                            - offsetof (allocation_object, u.data)))
55
56 /* Convert a pointer to user data from a pointer to allocation_object.  */
57 #define USER_PTR_FROM_ALLOCATION_OBJECT_PTR(X)                          \
58    ((void *) (((allocation_object *) (X))->u.data))
59
60 #ifdef ENABLE_CHECKING
61 /* Last used ID.  */
62 static ALLOC_POOL_ID_TYPE last_id;
63 #endif
64
65 #ifdef GATHER_STATISTICS
66
67 /* Store information about each particular alloc_pool.  */
68 struct alloc_pool_descriptor
69 {
70   const char *name;
71   int allocated;
72   int created;
73   int peak;
74   int current;
75 };
76
77 /* Hashtable mapping alloc_pool names to descriptors.  */
78 static htab_t alloc_pool_hash;
79
80 /* Hashtable helpers.  */
81 static hashval_t
82 hash_descriptor (const void *p)
83 {
84   const struct alloc_pool_descriptor *d = p;
85   return htab_hash_pointer (d->name);
86 }
87 static int
88 eq_descriptor (const void *p1, const void *p2)
89 {
90   const struct alloc_pool_descriptor *d = p1;
91   return d->name == p2;
92 }
93
94 /* For given name, return descriptor, create new if needed.  */
95 static struct alloc_pool_descriptor *
96 alloc_pool_descriptor (const char *name)
97 {
98   struct alloc_pool_descriptor **slot;
99
100   if (!alloc_pool_hash)
101     alloc_pool_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL);
102
103   slot = (struct alloc_pool_descriptor **)
104     htab_find_slot_with_hash (alloc_pool_hash, name,
105                               htab_hash_pointer (name),
106                               1);
107   if (*slot)
108     return *slot;
109   *slot = xcalloc (sizeof (**slot), 1);
110   (*slot)->name = name;
111   return *slot;
112 }
113 #endif
114
115 /* Create a pool of things of size SIZE, with NUM in each block we
116    allocate.  */
117
118 alloc_pool
119 create_alloc_pool (const char *name, size_t size, size_t num)
120 {
121   alloc_pool pool;
122   size_t header_size;
123 #ifdef GATHER_STATISTICS
124   struct alloc_pool_descriptor *desc;
125 #endif
126
127   gcc_assert (name);
128
129   /* Make size large enough to store the list header.  */
130   if (size < sizeof (alloc_pool_list))
131     size = sizeof (alloc_pool_list);
132
133   /* Now align the size to a multiple of 4.  */
134   size = align_eight (size);
135
136 #ifdef ENABLE_CHECKING
137   /* Add the aligned size of ID.  */
138   size += offsetof (allocation_object, u.data);
139 #endif
140
141   /* Um, we can't really allocate 0 elements per block.  */
142   gcc_assert (num);
143
144   /* Allocate memory for the pool structure.  */
145   pool = XNEW (struct alloc_pool_def);
146
147   /* Now init the various pieces of our pool structure.  */
148   pool->name = /*xstrdup (name)*/name;
149 #ifdef GATHER_STATISTICS
150   desc = alloc_pool_descriptor (name);
151   desc->created++;
152 #endif
153   pool->elt_size = size;
154   pool->elts_per_block = num;
155
156   /* List header size should be a multiple of 8.  */
157   header_size = align_eight (sizeof (struct alloc_pool_list_def));
158
159   pool->block_size = (size * num) + header_size;
160   pool->returned_free_list = NULL;
161   pool->virgin_free_list = NULL;
162   pool->virgin_elts_remaining = 0;
163   pool->elts_allocated = 0;
164   pool->elts_free = 0;
165   pool->blocks_allocated = 0;
166   pool->block_list = NULL;
167
168 #ifdef ENABLE_CHECKING
169   /* Increase the last used ID and use it for this pool.
170      ID == 0 is used for free elements of pool so skip it.  */
171   last_id++;
172   if (last_id == 0)
173     last_id++;
174
175   pool->id = last_id;
176 #endif
177
178   return (pool);
179 }
180
181 /* Free all memory allocated for the given memory pool.  */
182 void
183 empty_alloc_pool (alloc_pool pool)
184 {
185   alloc_pool_list block, next_block;
186 #ifdef GATHER_STATISTICS
187   struct alloc_pool_descriptor *desc = alloc_pool_descriptor (pool->name);
188 #endif
189
190   gcc_assert (pool);
191
192   /* Free each block allocated to the pool.  */
193   for (block = pool->block_list; block != NULL; block = next_block)
194     {
195       next_block = block->next;
196       free (block);
197 #ifdef GATHER_STATISTICS
198       desc->current -= pool->block_size;
199 #endif
200     }
201
202   pool->returned_free_list = NULL;
203   pool->virgin_free_list = NULL;
204   pool->virgin_elts_remaining = 0;
205   pool->elts_allocated = 0;
206   pool->elts_free = 0;
207   pool->blocks_allocated = 0;
208   pool->block_list = NULL;
209 }
210
211 /* Free all memory allocated for the given memory pool and the pool itself.  */
212 void
213 free_alloc_pool (alloc_pool pool)
214 {
215   /* First empty the pool.  */
216   empty_alloc_pool (pool);
217 #ifdef ENABLE_CHECKING
218   memset (pool, 0xaf, sizeof (*pool));
219 #endif
220   /* Lastly, free the pool.  */
221   free (pool);
222 }
223
224 /* Frees the alloc_pool, if it is empty and zero *POOL in this case.  */
225 void
226 free_alloc_pool_if_empty (alloc_pool *pool)
227 {
228   if ((*pool)->elts_free == (*pool)->elts_allocated)
229     {
230       free_alloc_pool (*pool);
231       *pool = NULL;
232     }
233 }
234
235 /* Allocates one element from the pool specified.  */
236 void *
237 pool_alloc (alloc_pool pool)
238 {
239   alloc_pool_list header;
240 #ifdef GATHER_STATISTICS
241   struct alloc_pool_descriptor *desc = alloc_pool_descriptor (pool->name);
242
243   desc->allocated+=pool->elt_size;
244 #endif
245
246   gcc_assert (pool);
247
248   /* If there are no more free elements, make some more!.  */
249   if (!pool->returned_free_list)
250     {
251       char *block;
252       if (!pool->virgin_elts_remaining)
253         {
254           alloc_pool_list block_header;
255
256           /* Make the block.  */
257           block = XNEWVEC (char, pool->block_size);
258           block_header = (alloc_pool_list) block;
259           block += align_eight (sizeof (struct alloc_pool_list_def));
260 #ifdef GATHER_STATISTICS
261           desc->current += pool->block_size;
262           if (desc->peak < desc->current)
263             desc->peak = desc->current;
264 #endif
265           
266           /* Throw it on the block list.  */
267           block_header->next = pool->block_list;
268           pool->block_list = block_header;
269
270           /* Make the block available for allocation.  */
271           pool->virgin_free_list = block;
272           pool->virgin_elts_remaining = pool->elts_per_block;
273
274           /* Also update the number of elements we have free/allocated, and
275              increment the allocated block count.  */
276           pool->elts_allocated += pool->elts_per_block;
277           pool->elts_free += pool->elts_per_block;
278           pool->blocks_allocated += 1;
279         }
280
281       
282       /* We now know that we can take the first elt off the virgin list and
283          put it on the returned list. */
284       block = pool->virgin_free_list;
285       header = (alloc_pool_list) USER_PTR_FROM_ALLOCATION_OBJECT_PTR (block);
286       header->next = NULL;
287 #ifdef ENABLE_CHECKING
288       /* Mark the element to be free.  */
289       ((allocation_object *) block)->id = 0;
290 #endif
291       pool->returned_free_list = header;
292       pool->virgin_free_list += pool->elt_size;
293       pool->virgin_elts_remaining--;
294
295     }
296
297   /* Pull the first free element from the free list, and return it.  */
298   header = pool->returned_free_list;
299   pool->returned_free_list = header->next;
300   pool->elts_free--;
301
302 #ifdef ENABLE_CHECKING
303   /* Set the ID for element.  */
304   ALLOCATION_OBJECT_PTR_FROM_USER_PTR (header)->id = pool->id;
305 #endif
306
307   return ((void *) header);
308 }
309
310 /* Puts PTR back on POOL's free list.  */
311 void
312 pool_free (alloc_pool pool, void *ptr)
313 {
314   alloc_pool_list header;
315
316   gcc_assert (ptr);
317
318 #ifdef ENABLE_CHECKING
319   /* Check whether the PTR was allocated from POOL.  */
320   gcc_assert (pool->id == ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr)->id);
321
322   memset (ptr, 0xaf, pool->elt_size - offsetof (allocation_object, u.data));
323
324   /* Mark the element to be free.  */
325   ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr)->id = 0;
326 #else
327   /* Check if we free more than we allocated, which is Bad (TM).  */
328   gcc_assert (pool->elts_free < pool->elts_allocated);
329 #endif
330
331   header = (alloc_pool_list) ptr;
332   header->next = pool->returned_free_list;
333   pool->returned_free_list = header;
334   pool->elts_free++;
335 }
336 /* Output per-alloc_pool statistics.  */
337 #ifdef GATHER_STATISTICS
338
339 /* Used to accumulate statistics about alloc_pool sizes.  */
340 struct output_info
341 {
342   int count;
343   int size;
344 };
345
346 /* Called via htab_traverse.  Output alloc_pool descriptor pointed out by SLOT
347    and update statistics.  */
348 static int
349 print_statistics (void **slot, void *b)
350 {
351   struct alloc_pool_descriptor *d = (struct alloc_pool_descriptor *) *slot;
352   struct output_info *i = (struct output_info *) b;
353
354   if (d->allocated)
355     {
356       fprintf (stderr, "%-21s %6d %10d %10d %10d\n", d->name,
357                d->created, d->allocated, d->peak, d->current);
358       i->size += d->allocated;
359       i->count += d->created;
360     }
361   return 1;
362 }
363 #endif
364
365 /* Output per-alloc_pool memory usage statistics.  */
366 void
367 dump_alloc_pool_statistics (void)
368 {
369 #ifdef GATHER_STATISTICS
370   struct output_info info;
371
372   if (!alloc_pool_hash)
373     return;
374
375   fprintf (stderr, "\nAlloc-pool Kind        Pools  Allocated      Peak        Leak\n");
376   fprintf (stderr, "-------------------------------------------------------------\n");
377   info.count = 0;
378   info.size = 0;
379   htab_traverse (alloc_pool_hash, print_statistics, &info);
380   fprintf (stderr, "-------------------------------------------------------------\n");
381   fprintf (stderr, "%-20s %7d %10d\n",
382            "Total", info.count, info.size);
383   fprintf (stderr, "-------------------------------------------------------------\n");
384 #endif
385 }