7a7bf9f950f846dbcc8321f901f541d947f252fd
[platform/upstream/make.git] / packaging / make-3.81-memory.patch
1 diff -Bburpd make-3.81_orig/file.c make-3.81/file.c
2 --- make-3.81_orig/file.c       2006-05-23 13:59:11.000000000 +0200
3 +++ make-3.81/file.c    2006-05-23 14:39:34.000000000 +0200
4 @@ -490,7 +490,7 @@ expand_deps (struct file *f)
5  
6                o = subst_expand (buffer, d->name, "%", "$*", 1, 2, 0);
7  
8 -              free (d->name);
9 +              hash_strfree (d->name);
10                d->name = savestring (buffer, o - buffer);
11                d->staticpattern = 0; /* Clear staticpattern so that we don't
12                                         re-expand %s below. */
13 @@ -549,7 +549,7 @@ expand_deps (struct file *f)
14                          dp->name[0] = '\0';
15                        else
16                          {
17 -                          free (dp->name);
18 +                          hash_strfree (dp->name);
19                            dp->name = savestring (buffer, o - buffer);
20                          }
21                      }
22 @@ -580,7 +580,7 @@ expand_deps (struct file *f)
23            if (d1->file == 0)
24              d1->file = enter_file (d1->name);
25            else
26 -            free (d1->name);
27 +            hash_strfree (d1->name);
28            d1->name = 0;
29            d1->staticpattern = 0;
30            d1->need_2nd_expansion = 0;
31 Only in make-3.81: file.c~
32 diff -Bburpd make-3.81_orig/implicit.c make-3.81/implicit.c
33 --- make-3.81_orig/implicit.c   2006-05-23 13:59:11.000000000 +0200
34 +++ make-3.81/implicit.c        2006-05-23 14:40:01.000000000 +0200
35 @@ -864,7 +864,7 @@ pattern_search (struct file *file, int a
36               dep->file = enter_file (dep->name);
37                /* enter_file uses dep->name _if_ we created a new file.  */
38                if (dep->name != dep->file->name)
39 -                free (dep->name);
40 +                hash_strfree (dep->name);
41               dep->name = 0;
42               dep->file->tried_implicit |= dep->changed;
43             }
44 Only in make-3.81: implicit.c~
45 diff -Bburpd make-3.81_orig/main.c make-3.81/main.c
46 --- make-3.81_orig/main.c       2006-05-23 13:59:11.000000000 +0200
47 +++ make-3.81/main.c    2006-05-23 14:40:49.000000000 +0200
48 @@ -540,6 +540,7 @@ initialize_global_hash_tables (void)
49    init_hash_files ();
50    hash_init_directories ();
51    hash_init_function_table ();
52 +  init_hash_strings ();
53  }
54  
55  static struct file *
56 Only in make-3.81: main.c~
57 diff -Bburpd make-3.81_orig/make.h make-3.81/make.h
58 --- make-3.81_orig/make.h       2006-05-23 13:59:11.000000000 +0200
59 +++ make-3.81/make.h    2006-05-23 14:41:21.000000000 +0200
60 @@ -431,6 +431,11 @@ extern void print_spaces PARAMS ((unsign
61  extern char *find_percent PARAMS ((char *));
62  extern FILE *open_tmpfile PARAMS ((char **, const char *));
63  
64 +extern void init_hash_strings PARAMS ((void));
65 +extern char *hash_strdup PARAMS ((const char *));
66 +extern char *hash_savestring PARAMS ((const char *, unsigned int));
67 +extern void hash_strfree PARAMS ((char *));
68 +
69  #ifndef NO_ARCHIVES
70  extern int ar_name PARAMS ((char *));
71  extern void ar_parse_name PARAMS ((char *, char **, char **));
72 Only in make-3.81: make.h~
73 diff -Bburpd make-3.81_orig/misc.c make-3.81/misc.c
74 --- make-3.81_orig/misc.c       2006-05-23 13:59:11.000000000 +0200
75 +++ make-3.81/misc.c    2006-05-23 14:42:59.000000000 +0200
76 @@ -16,8 +16,10 @@ You should have received a copy of the G
77  GNU Make; see the file COPYING.  If not, write to the Free Software
78  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.  */
79  
80 +#include <assert.h>
81  #include "make.h"
82  #include "dep.h"
83 +#include "hash.h"
84  #include "debug.h"
85  
86  /* Variadic functions.  We go through contortions to allow proper function
87 @@ -511,7 +513,7 @@ void
88  free_dep (struct dep *d)
89  {
90    if (d->name != 0)
91 -    free (d->name);
92 +    hash_strfree (d->name);
93  
94    if (d->stem != 0)
95      free (d->stem);
96 @@ -535,7 +537,7 @@ copy_dep_chain (const struct dep *d)
97        bcopy ((char *) d, (char *) c, sizeof (struct dep));
98  
99        if (c->name != 0)
100 -       c->name = xstrdup (c->name);
101 +       c->name = hash_strdup (c->name);
102        if (c->stem != 0)
103         c->stem = xstrdup (c->stem);
104  
105 @@ -909,3 +911,154 @@ close_stdout (void)
106        exit (EXIT_FAILURE);
107      }
108  }
109 +
110 +/* Hash table of duplicated strings.  */
111 +
112 +struct hash_string
113 +{
114 +  char *string;
115 +  unsigned int count;
116 +};
117 +
118 +static unsigned long
119 +string_hash_1 (key)
120 +    const void *key;
121 +{
122 +  return_ISTRING_HASH_1 (((const struct hash_string *) key)->string);
123 +}
124 +
125 +static unsigned long
126 +string_hash_2 (key)
127 +    const void *key;
128 +{
129 +  return_ISTRING_HASH_2 (((const struct hash_string *) key)->string);
130 +}
131 +
132 +static int
133 +string_hash_cmp (x, y)
134 +    const void *x;
135 +    const void *y;
136 +{
137 +  return_ISTRING_COMPARE (((const struct hash_string *) x)->string,
138 +                         ((const struct hash_string *) y)->string);
139 +}
140 +
141 +static struct hash_table strings;
142 +
143 +void
144 +init_hash_strings ()
145 +{
146 +  hash_init (&strings, 1000, string_hash_1, string_hash_2,
147 +            string_hash_cmp);
148 +}
149 +
150 +/* Keep track duplicated string and return the old one if exists.  */
151 +
152 +char *
153 +hash_strdup (ptr)
154 +     const char *ptr;
155 +{
156 +  struct hash_string *h, key;
157 +
158 +  if (*ptr == '\0')
159 +    return "";
160 +
161 +  key.string = (char *) ptr;
162 +  key.count = 0;
163 +  h = (struct hash_string *) hash_find_item (&strings, &key);
164 +  if (h == NULL)
165 +    {
166 +      char *result = (char *) malloc (strlen (ptr) + 1);
167 +
168 +      if (result == NULL)
169 +       fatal (NILF, _("virtual memory exhausted"));
170 +
171 +      strcpy (result, ptr);
172 +
173 +      h = (struct hash_string *) malloc (sizeof (struct hash_string));
174 +      if (h == NULL)
175 +       fatal (NILF, _("virtual memory exhausted"));
176 +
177 +      h->string = result;
178 +      h->count = 1;
179 +      hash_insert (&strings, h);
180 +    }
181 +  else
182 +    {
183 +      h->count++;
184 +      assert (h->count != 0);
185 +    }
186 +
187 +  return h->string;
188 +}
189 +
190 +char *
191 +hash_savestring (str, length)
192 +     const char *str;
193 +     unsigned int length;
194 +{
195 +  struct hash_string *h, key;
196 +
197 +  if (length == 0 || *str == '\0')
198 +    return "";
199 +
200 +  key.string = alloca (length + 1);
201 +  key.count = 0;
202 +  bcopy (str, key.string, length);
203 +  key.string [length] = '\0';
204 +
205 +  h = (struct hash_string *) hash_find_item (&strings, &key);
206 +  if (h == NULL)
207 +    {
208 +      char *out = (char *) xmalloc (length + 1);
209 +      bcopy (str, out, length);
210 +      out[length] = '\0';
211 +
212 +      h = (struct hash_string *) malloc (sizeof (struct hash_string));
213 +      if (h == NULL)
214 +       fatal (NILF, _("virtual memory exhausted"));
215 +
216 +      h->string = out;
217 +      h->count = 1;
218 +      hash_insert (&strings, h);
219 +    }
220 +  else
221 +    {
222 +      h->count++;
223 +      assert (h->count != 0);
224 +    }
225 +
226 +  return h->string;
227 +}
228 +
229 +void
230 +hash_strfree (ptr)
231 +     char *ptr;
232 +{
233 +  struct hash_string *h, key;
234 +
235 +  if (*ptr == '\0')
236 +    return;
237 +
238 +  key.string = ptr;
239 +  key.count = 0;
240 +  h = (struct hash_string *) hash_find_item (&strings, &key);
241 +
242 +  /* Check if string comes from hash_strdup or hash_savestring.  */
243 +  if (h == NULL || h->string != ptr)
244 +    {
245 +      free (ptr);
246 +      return;
247 +    }
248 +
249 +  h->count--;
250 +  if (h->count == 0)
251 +    {
252 +      struct hash_string *d;
253 +
254 +      d = hash_delete (&strings, h);
255 +      assert (d == h);
256 +      free (h->string);
257 +      free (h);
258 +    }
259 +}
260 Only in make-3.81: misc.c~
261 Only in make-3.81: read.c~