OSDN Git Service

2003-01-30 Ralf Corsepius <corsepiu@faw.uni-ulm.de>
[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 Free Software Foundation, Inc.
3    Contributed by Daniel Berlin <dan@cgsoftware.com>
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "alloc-pool.h"
25
26 #define align_four(x) (((x+3) >> 2) << 2)
27 #define align_eight(x) (((x+7) >> 3) << 3)
28
29 /* Create a pool of things of size SIZE, with NUM in each block we
30    allocate.  */
31
32 alloc_pool
33 create_alloc_pool (name, size, num)
34      const char *name;
35      size_t size;
36      size_t num;
37 {
38   alloc_pool pool;
39   size_t pool_size, header_size;
40
41   if (!name)
42     abort ();
43
44   /* Make size large enough to store the list header.  */
45   if (size < sizeof (alloc_pool_list))
46     size = sizeof (alloc_pool_list);
47
48   /* Now align the size to a multiple of 4.  */
49   size = align_four (size);
50
51   /* Um, we can't really allocate 0 elements per block.  */
52   if (num == 0)
53     abort ();
54
55   /* Find the size of the pool structure, and the name.  */
56   pool_size = sizeof (struct alloc_pool_def);
57
58   /* and allocate that much memory.  */
59   pool = (alloc_pool) xmalloc (pool_size);
60
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;
65
66   /* List header size should be a multiple of 8 */
67   header_size = align_eight (sizeof (struct alloc_pool_list_def));
68
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;
75
76   return (pool);
77 }
78
79 /* Free all memory allocated for the given memory pool.  */
80 void
81 free_alloc_pool (pool)
82      alloc_pool pool;
83 {
84   alloc_pool_list block, next_block;
85
86   if (!pool)
87     abort ();
88
89   /* Free each block allocated to the pool.  */
90   for (block = pool->block_list; block != NULL; block = next_block)
91     {
92       next_block = block->next;
93       free (block);
94     }
95   /* Lastly, free the pool and the name.  */
96   free (pool->name);
97   free (pool);
98 }
99
100 /* Allocates one element from the pool specified.  */
101 void *
102 pool_alloc (pool)
103      alloc_pool pool;
104 {
105   alloc_pool_list header;
106   char *block;
107
108   if (!pool)
109     abort ();
110
111   /* If there are no more free elements, make some more!.  */
112   if (!pool->free_list)
113     {
114       size_t i;
115       alloc_pool_list block_header;
116
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));
121
122       /* Throw it on the block list */
123       block_header->next = pool->block_list;
124       pool->block_list = block_header;
125
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)
128       {
129         header = (alloc_pool_list) block;
130         header->next = pool->free_list;
131         pool->free_list = header;
132       }
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;
138     }
139
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);
145 }
146
147 /* Puts PTR back on POOL's free list.  */
148 void
149 pool_free (pool, ptr)
150      alloc_pool pool;
151      void *ptr;
152 {
153   alloc_pool_list header;
154
155   if (!ptr)
156     abort ();
157
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++;
165 }