OSDN Git Service

* config/h8300/h8300.c (h8300_tiny_constant_address_p): Use a
[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
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 2, 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 COPYING.  If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "alloc-pool.h"
26
27 /* Redefine abort to report an internal error w/o coredump, and
28    reporting the location of the error in the source file.  This logic
29    is duplicated in rtl.h and tree.h because every file that needs the
30    special abort includes one or both.  toplev.h gets too few files,
31    system.h gets too many.  */
32
33 extern void fancy_abort (const char *, int, const char *)
34     ATTRIBUTE_NORETURN;
35 #define abort() fancy_abort (__FILE__, __LINE__, __FUNCTION__)
36
37 #define align_eight(x) (((x+7) >> 3) << 3)
38
39 /* The internal allocation object.  */
40 typedef struct allocation_object_def
41 {
42 #ifdef ENABLE_CHECKING
43   /* The ID of alloc pool which the object was allocated from.  */
44   ALLOC_POOL_ID_TYPE id;
45 #endif
46
47   union
48     {
49       /* The data of the object.  */
50       char data[1];
51
52       /* Because we want any type of data to be well aligned after the ID,
53          the following elements are here.  They are never accessed so
54          the allocated object may be even smaller than this structure.  */
55       char *align_p;
56       HOST_WIDEST_INT align_i;
57       long double align_ld;
58     } u;
59 } allocation_object;
60
61 /* Convert a pointer to allocation_object from a pointer to user data.  */
62 #define ALLOCATION_OBJECT_PTR_FROM_USER_PTR(X)                          \
63    ((allocation_object *) (((char *) (X))                               \
64                            - offsetof (allocation_object, u.data)))
65
66 /* Convert a pointer to user data from a pointer to allocation_object.  */
67 #define USER_PTR_FROM_ALLOCATION_OBJECT_PTR(X)                          \
68    ((void *) (((allocation_object *) (X))->u.data))
69
70 #ifdef ENABLE_CHECKING
71 /* Last used ID.  */
72 static ALLOC_POOL_ID_TYPE last_id;
73 #endif
74
75 /* Create a pool of things of size SIZE, with NUM in each block we
76    allocate.  */
77
78 alloc_pool
79 create_alloc_pool (const char *name, size_t size, size_t num)
80 {
81   alloc_pool pool;
82   size_t pool_size, header_size;
83
84   if (!name)
85     abort ();
86
87   /* Make size large enough to store the list header.  */
88   if (size < sizeof (alloc_pool_list))
89     size = sizeof (alloc_pool_list);
90
91   /* Now align the size to a multiple of 4.  */
92   size = align_eight (size);
93
94 #ifdef ENABLE_CHECKING
95   /* Add the aligned size of ID.  */
96   size += offsetof (allocation_object, u.data);
97 #endif
98
99   /* Um, we can't really allocate 0 elements per block.  */
100   if (num == 0)
101     abort ();
102
103   /* Find the size of the pool structure, and the name.  */
104   pool_size = sizeof (struct alloc_pool_def);
105
106   /* and allocate that much memory.  */
107   pool = xmalloc (pool_size);
108
109   /* Now init the various pieces of our pool structure.  */
110   pool->name = xstrdup (name);
111   pool->elt_size = size;
112   pool->elts_per_block = num;
113
114   /* List header size should be a multiple of 8.  */
115   header_size = align_eight (sizeof (struct alloc_pool_list_def));
116
117   pool->block_size = (size * num) + header_size;
118   pool->free_list = NULL;
119   pool->elts_allocated = 0;
120   pool->elts_free = 0;
121   pool->blocks_allocated = 0;
122   pool->block_list = NULL;
123
124 #ifdef ENABLE_CHECKING
125   /* Increase the last used ID and use it for this pool.
126      ID == 0 is used for free elements of pool so skip it.  */
127   last_id++;
128   if (last_id == 0)
129     last_id++;
130
131   pool->id = last_id;
132 #endif
133
134   return (pool);
135 }
136
137 /* Free all memory allocated for the given memory pool.  */
138 void
139 free_alloc_pool (alloc_pool pool)
140 {
141   alloc_pool_list block, next_block;
142
143 #ifdef ENABLE_CHECKING
144   if (!pool)
145     abort ();
146 #endif
147
148   /* Free each block allocated to the pool.  */
149   for (block = pool->block_list; block != NULL; block = next_block)
150     {
151       next_block = block->next;
152       free (block);
153     }
154   /* Lastly, free the pool and the name.  */
155   free (pool->name);
156 #ifdef ENABLE_CHECKING
157   memset (pool, 0xaf, sizeof (*pool));
158 #endif
159   free (pool);
160 }
161
162 /* Allocates one element from the pool specified.  */
163 void *
164 pool_alloc (alloc_pool pool)
165 {
166   alloc_pool_list header;
167   char *block;
168
169 #ifdef ENABLE_CHECKING
170   if (!pool)
171     abort ();
172 #endif
173
174   /* If there are no more free elements, make some more!.  */
175   if (!pool->free_list)
176     {
177       size_t i;
178       alloc_pool_list block_header;
179
180       /* Make the block.  */
181       block = xmalloc (pool->block_size);
182       block_header = (alloc_pool_list) block;
183       block += align_eight (sizeof (struct alloc_pool_list_def));
184
185       /* Throw it on the block list.  */
186       block_header->next = pool->block_list;
187       pool->block_list = block_header;
188
189       /* Now put the actual block pieces onto the free list.  */
190       for (i = 0; i < pool->elts_per_block; i++, block += pool->elt_size)
191       {
192 #ifdef ENABLE_CHECKING
193         /* Mark the element to be free.  */
194         ((allocation_object *) block)->id = 0;
195 #endif
196         header = (alloc_pool_list) USER_PTR_FROM_ALLOCATION_OBJECT_PTR (block);
197         header->next = pool->free_list;
198         pool->free_list = header;
199       }
200       /* Also update the number of elements we have free/allocated, and
201          increment the allocated block count.  */
202       pool->elts_allocated += pool->elts_per_block;
203       pool->elts_free += pool->elts_per_block;
204       pool->blocks_allocated += 1;
205     }
206
207   /* Pull the first free element from the free list, and return it.  */
208   header = pool->free_list;
209   pool->free_list = header->next;
210   pool->elts_free--;
211
212 #ifdef ENABLE_CHECKING
213   /* Set the ID for element.  */
214   ALLOCATION_OBJECT_PTR_FROM_USER_PTR (header)->id = pool->id;
215 #endif
216
217   return ((void *) header);
218 }
219
220 /* Puts PTR back on POOL's free list.  */
221 void
222 pool_free (alloc_pool pool, void *ptr)
223 {
224   alloc_pool_list header;
225
226 #ifdef ENABLE_CHECKING
227   if (!ptr)
228     abort ();
229
230   memset (ptr, 0xaf, pool->elt_size - offsetof (allocation_object, u.data));
231
232   /* Check whether the PTR was allocated from POOL.  */
233   if (pool->id != ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr)->id)
234     abort ();
235
236   /* Mark the element to be free.  */
237   ALLOCATION_OBJECT_PTR_FROM_USER_PTR (ptr)->id = 0;
238 #else
239   /* Check if we free more than we allocated, which is Bad (TM).  */
240   if (pool->elts_free + 1 > pool->elts_allocated)
241     abort ();
242 #endif
243
244   header = (alloc_pool_list) ptr;
245   header->next = pool->free_list;
246   pool->free_list = header;
247   pool->elts_free++;
248 }