1 /* Functions to support a pool of allocatable objects.
2 Copyright (C) 1987, 1997, 1998, 1999, 2000, 2001 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 2, or (at your option) any later
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
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "alloc-pool.h"
26 #define align_four(x) (((x+3) >> 2) << 2)
27 #define align_eight(x) (((x+7) >> 3) << 3)
29 /* Create a pool of things of size SIZE, with NUM in each block we
33 create_alloc_pool (name
, size
, num
)
39 size_t pool_size
, header_size
;
44 /* Make size large enough to store the list header. */
45 if (size
< sizeof (alloc_pool_list
))
46 size
= sizeof (alloc_pool_list
);
48 /* Now align the size to a multiple of 4. */
49 size
= align_four (size
);
51 /* Um, we can't really allocate 0 elements per block. */
55 /* Find the size of the pool structure, and the name. */
56 pool_size
= sizeof (struct alloc_pool_def
);
58 /* and allocate that much memory. */
59 pool
= (alloc_pool
) xmalloc (pool_size
);
61 /* Now init the various pieces of our pool structure. */
62 pool
->name
= xstrdup (name
);
63 pool
->elt_size
= size
;
64 pool
->elts_per_block
= num
;
66 /* List header size should be a multiple of 8 */
67 header_size
= align_eight (sizeof (struct alloc_pool_list_def
));
69 pool
->block_size
= (size
* num
) + header_size
;
70 pool
->free_list
= NULL
;
71 pool
->elts_allocated
= 0;
73 pool
->blocks_allocated
= 0;
74 pool
->block_list
= NULL
;
79 /* Free all memory allocated for the given memory pool. */
81 free_alloc_pool (pool
)
84 alloc_pool_list block
, next_block
;
89 /* Free each block allocated to the pool. */
90 for (block
= pool
->block_list
; block
!= NULL
; block
= next_block
)
92 next_block
= block
->next
;
95 /* Lastly, free the pool and the name. */
100 /* Allocates one element from the pool specified. */
105 alloc_pool_list header
;
111 /* If there are no more free elements, make some more!. */
112 if (!pool
->free_list
)
115 alloc_pool_list block_header
;
118 block
= (char *) xmalloc (pool
->block_size
);
119 block_header
= (alloc_pool_list
) block
;
120 block
+= align_eight (sizeof (struct alloc_pool_list_def
));
122 /* Throw it on the block list */
123 block_header
->next
= pool
->block_list
;
124 pool
->block_list
= block_header
;
126 /* Now put the actual block pieces onto the free list. */
127 for (i
= 0; i
< pool
->elts_per_block
; i
++, block
+= pool
->elt_size
)
129 header
= (alloc_pool_list
) block
;
130 header
->next
= pool
->free_list
;
131 pool
->free_list
= header
;
133 /* Also update the number of elements we have free/allocated, and
134 increment the allocated block count. */
135 pool
->elts_allocated
+= pool
->elts_per_block
;
136 pool
->elts_free
+= pool
->elts_per_block
;
137 pool
->blocks_allocated
+= 1;
140 /* Pull the first free element from the free list, and return it. */
141 header
= pool
->free_list
;
142 pool
->free_list
= header
->next
;
144 return ((void *) header
);
147 /* Puts PTR back on POOL's free list. */
149 pool_free (pool
, ptr
)
153 alloc_pool_list header
;
158 /* Check if we free more than we allocated, which is Bad (TM). */
159 if (pool
->elts_free
+ 1 > pool
->elts_allocated
)
161 header
= (alloc_pool_list
) ptr
;
162 header
->next
= pool
->free_list
;
163 pool
->free_list
= header
;