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>
6 This file is part of GCC.
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
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
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/>. */
24 #include "alloc-pool.h"
27 #define align_eight(x) (((x+7) >> 3) << 3)
29 /* The internal allocation object. */
30 typedef struct allocation_object_def
32 #ifdef ENABLE_CHECKING
33 /* The ID of alloc pool which the object was allocated from. */
34 ALLOC_POOL_ID_TYPE id
;
39 /* The data of the object. */
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. */
46 HOST_WIDEST_INT align_i
;
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)))
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))
60 #ifdef ENABLE_CHECKING
62 static ALLOC_POOL_ID_TYPE last_id
;
65 #ifdef GATHER_STATISTICS
67 /* Store information about each particular alloc_pool. */
68 struct alloc_pool_descriptor
77 /* Hashtable mapping alloc_pool names to descriptors. */
78 static htab_t alloc_pool_hash
;
80 /* Hashtable helpers. */
82 hash_descriptor (const void *p
)
84 const struct alloc_pool_descriptor
*d
= p
;
85 return htab_hash_pointer (d
->name
);
88 eq_descriptor (const void *p1
, const void *p2
)
90 const struct alloc_pool_descriptor
*d
= p1
;
94 /* For given name, return descriptor, create new if needed. */
95 static struct alloc_pool_descriptor
*
96 alloc_pool_descriptor (const char *name
)
98 struct alloc_pool_descriptor
**slot
;
100 if (!alloc_pool_hash
)
101 alloc_pool_hash
= htab_create (10, hash_descriptor
, eq_descriptor
, NULL
);
103 slot
= (struct alloc_pool_descriptor
**)
104 htab_find_slot_with_hash (alloc_pool_hash
, name
,
105 htab_hash_pointer (name
),
109 *slot
= xcalloc (sizeof (**slot
), 1);
110 (*slot
)->name
= name
;
115 /* Create a pool of things of size SIZE, with NUM in each block we
119 create_alloc_pool (const char *name
, size_t size
, size_t num
)
122 size_t pool_size
, header_size
;
123 #ifdef GATHER_STATISTICS
124 struct alloc_pool_descriptor
*desc
;
129 /* Make size large enough to store the list header. */
130 if (size
< sizeof (alloc_pool_list
))
131 size
= sizeof (alloc_pool_list
);
133 /* Now align the size to a multiple of 4. */
134 size
= align_eight (size
);
136 #ifdef ENABLE_CHECKING
137 /* Add the aligned size of ID. */
138 size
+= offsetof (allocation_object
, u
.data
);
141 /* Um, we can't really allocate 0 elements per block. */
144 /* Find the size of the pool structure, and the name. */
145 pool_size
= sizeof (struct alloc_pool_def
);
147 /* and allocate that much memory. */
148 pool
= xmalloc (pool_size
);
150 /* Now init the various pieces of our pool structure. */
151 pool
->name
= /*xstrdup (name)*/name
;
152 #ifdef GATHER_STATISTICS
153 desc
= alloc_pool_descriptor (name
);
156 pool
->elt_size
= size
;
157 pool
->elts_per_block
= num
;
159 /* List header size should be a multiple of 8. */
160 header_size
= align_eight (sizeof (struct alloc_pool_list_def
));
162 pool
->block_size
= (size
* num
) + header_size
;
163 pool
->returned_free_list
= NULL
;
164 pool
->virgin_free_list
= NULL
;
165 pool
->virgin_elts_remaining
= 0;
166 pool
->elts_allocated
= 0;
168 pool
->blocks_allocated
= 0;
169 pool
->block_list
= NULL
;
171 #ifdef ENABLE_CHECKING
172 /* Increase the last used ID and use it for this pool.
173 ID == 0 is used for free elements of pool so skip it. */
184 /* Free all memory allocated for the given memory pool. */
186 free_alloc_pool (alloc_pool pool
)
188 alloc_pool_list block
, next_block
;
189 #ifdef GATHER_STATISTICS
190 struct alloc_pool_descriptor
*desc
= alloc_pool_descriptor (pool
->name
);
195 /* Free each block allocated to the pool. */
196 for (block
= pool
->block_list
; block
!= NULL
; block
= next_block
)
198 next_block
= block
->next
;
200 #ifdef GATHER_STATISTICS
201 desc
->current
-= pool
->block_size
;
204 #ifdef ENABLE_CHECKING
205 memset (pool
, 0xaf, sizeof (*pool
));
207 /* Lastly, free the pool. */
211 /* Frees the alloc_pool, if it is empty and zero *POOL in this case. */
213 free_alloc_pool_if_empty (alloc_pool
*pool
)
215 if ((*pool
)->elts_free
== (*pool
)->elts_allocated
)
217 free_alloc_pool (*pool
);
222 /* Allocates one element from the pool specified. */
224 pool_alloc (alloc_pool pool
)
226 alloc_pool_list header
;
227 #ifdef GATHER_STATISTICS
228 struct alloc_pool_descriptor
*desc
= alloc_pool_descriptor (pool
->name
);
230 desc
->allocated
+=pool
->elt_size
;
235 /* If there are no more free elements, make some more!. */
236 if (!pool
->returned_free_list
)
239 if (!pool
->virgin_elts_remaining
)
241 alloc_pool_list block_header
;
243 /* Make the block. */
244 block
= XNEWVEC (char, pool
->block_size
);
245 block_header
= (alloc_pool_list
) block
;
246 block
+= align_eight (sizeof (struct alloc_pool_list_def
));
247 #ifdef GATHER_STATISTICS
248 desc
->current
+= pool
->block_size
;
249 if (desc
->peak
< desc
->current
)
250 desc
->peak
= desc
->current
;
253 /* Throw it on the block list. */
254 block_header
->next
= pool
->block_list
;
255 pool
->block_list
= block_header
;
257 /* Make the block available for allocation. */
258 pool
->virgin_free_list
= block
;
259 pool
->virgin_elts_remaining
= pool
->elts_per_block
;
261 /* Also update the number of elements we have free/allocated, and
262 increment the allocated block count. */
263 pool
->elts_allocated
+= pool
->elts_per_block
;
264 pool
->elts_free
+= pool
->elts_per_block
;
265 pool
->blocks_allocated
+= 1;
269 /* We now know that we can take the first elt off the virgin list and
270 put it on the returned list. */
271 block
= pool
->virgin_free_list
;
272 header
= (alloc_pool_list
) USER_PTR_FROM_ALLOCATION_OBJECT_PTR (block
);
274 #ifdef ENABLE_CHECKING
275 /* Mark the element to be free. */
276 ((allocation_object
*) block
)->id
= 0;
278 pool
->returned_free_list
= header
;
279 pool
->virgin_free_list
+= pool
->elt_size
;
280 pool
->virgin_elts_remaining
--;
284 /* Pull the first free element from the free list, and return it. */
285 header
= pool
->returned_free_list
;
286 pool
->returned_free_list
= header
->next
;
289 #ifdef ENABLE_CHECKING
290 /* Set the ID for element. */
291 ALLOCATION_OBJECT_PTR_FROM_USER_PTR (header
)->id
= pool
->id
;
294 return ((void *) header
);
297 /* Puts PTR back on POOL's free list. */
299 pool_free (alloc_pool pool
, void *ptr
)
301 alloc_pool_list header
;
305 #ifdef ENABLE_CHECKING
306 /* Check whether the PTR was allocated from POOL. */
307 gcc_assert (pool
->id
== ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr
)->id
);
309 memset (ptr
, 0xaf, pool
->elt_size
- offsetof (allocation_object
, u
.data
));
311 /* Mark the element to be free. */
312 ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr
)->id
= 0;
314 /* Check if we free more than we allocated, which is Bad (TM). */
315 gcc_assert (pool
->elts_free
< pool
->elts_allocated
);
318 header
= (alloc_pool_list
) ptr
;
319 header
->next
= pool
->returned_free_list
;
320 pool
->returned_free_list
= header
;
323 /* Output per-alloc_pool statistics. */
324 #ifdef GATHER_STATISTICS
326 /* Used to accumulate statistics about alloc_pool sizes. */
333 /* Called via htab_traverse. Output alloc_pool descriptor pointed out by SLOT
334 and update statistics. */
336 print_statistics (void **slot
, void *b
)
338 struct alloc_pool_descriptor
*d
= (struct alloc_pool_descriptor
*) *slot
;
339 struct output_info
*i
= (struct output_info
*) b
;
343 fprintf (stderr
, "%-21s %6d %10d %10d %10d\n", d
->name
,
344 d
->created
, d
->allocated
, d
->peak
, d
->current
);
345 i
->size
+= d
->allocated
;
346 i
->count
+= d
->created
;
352 /* Output per-alloc_pool memory usage statistics. */
354 dump_alloc_pool_statistics (void)
356 #ifdef GATHER_STATISTICS
357 struct output_info info
;
359 if (!alloc_pool_hash
)
362 fprintf (stderr
, "\nAlloc-pool Kind Pools Allocated Peak Leak\n");
363 fprintf (stderr
, "-------------------------------------------------------------\n");
366 htab_traverse (alloc_pool_hash
, print_statistics
, &info
);
367 fprintf (stderr
, "-------------------------------------------------------------\n");
368 fprintf (stderr
, "%-20s %7d %10d\n",
369 "Total", info
.count
, info
.size
);
370 fprintf (stderr
, "-------------------------------------------------------------\n");