* config/arm/elf.h (ASM_OUTPUT_ALIGNED_COMMON): Remove definition.
[official-gcc.git] / gcc / alloc-pool.c
blob2f5d04dab516d6a5b7eeed63a05dafaa2141966f
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
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 COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
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
30 allocate. */
32 alloc_pool
33 create_alloc_pool (name, size, num)
34 const char *name;
35 size_t size;
36 size_t num;
38 alloc_pool pool;
39 size_t pool_size, header_size;
41 if (!name)
42 abort ();
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. */
52 if (num == 0)
53 abort ();
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;
72 pool->elts_free = 0;
73 pool->blocks_allocated = 0;
74 pool->block_list = NULL;
76 return (pool);
79 /* Free all memory allocated for the given memory pool. */
80 void
81 free_alloc_pool (pool)
82 alloc_pool pool;
84 alloc_pool_list block, next_block;
86 if (!pool)
87 abort ();
89 /* Free each block allocated to the pool. */
90 for (block = pool->block_list; block != NULL; block = next_block)
92 next_block = block->next;
93 free (block);
95 /* Lastly, free the pool and the name. */
96 free (pool->name);
97 free (pool);
100 /* Allocates one element from the pool specified. */
101 void *
102 pool_alloc (pool)
103 alloc_pool pool;
105 alloc_pool_list header;
106 char *block;
108 if (!pool)
109 abort ();
111 /* If there are no more free elements, make some more!. */
112 if (!pool->free_list)
114 size_t i;
115 alloc_pool_list block_header;
117 /* Make the block */
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;
143 pool->elts_free--;
144 return ((void *) header);
147 /* Puts PTR back on POOL's free list. */
148 void
149 pool_free (pool, ptr)
150 alloc_pool pool;
151 void *ptr;
153 alloc_pool_list header;
155 if (!ptr)
156 abort ();
158 /* Check if we free more than we allocated, which is Bad (TM). */
159 if (pool->elts_free + 1 > pool->elts_allocated)
160 abort ();
161 header = (alloc_pool_list) ptr;
162 header->next = pool->free_list;
163 pool->free_list = header;
164 pool->elts_free++;