summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authorPaul Smith <psmith@gnu.org>2007-03-20 03:02:26 +0000
committerPaul Smith <psmith@gnu.org>2007-03-20 03:02:26 +0000
commit6ccf33cdbdfda2aea5d51e4d4991881c74d853d1 (patch)
treece963770c6d0dc0428a6bce65d96da4b710e2831 /misc.c
parente4da30858037b431880263676e8f90b1f8412a38 (diff)
downloadgunmake-6ccf33cdbdfda2aea5d51e4d4991881c74d853d1.tar.gz
This is a major update, which switches virtually every allocated-but-not-freed
string into the strcache. As a side-effect, many more structure members and function arguments can/should be declared const. As mentioned in the changelog, unfortunately measurement shows that this change does not yet reduce memory. The problem is with secondary expansion: because of this we store all the prerequisites in the string cache twice. First we store the prerequisite string after initial expansion but before secondary expansion, then we store each individual file after secondary expansion and expand_deps(). I plan to change expand_deps() to be callable in either context (eval or snap_deps) then have non-second-expansion targets call expand_deps() during eval, so that we only need to store that dependency list once.
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c101
1 files changed, 38 insertions, 63 deletions
diff --git a/misc.c b/misc.c
index 47703d9..df54163 100644
--- a/misc.c
+++ b/misc.c
@@ -158,28 +158,31 @@ print_spaces (unsigned int n)
}
-/* Return a newly-allocated string whose contents
- concatenate those of s1, s2, s3. */
+/* Return a string whose contents concatenate those of s1, s2, s3.
+ This string lives in static, re-used memory. */
char *
concat (const char *s1, const char *s2, const char *s3)
{
unsigned int len1, len2, len3;
- char *result;
+ static unsigned int rlen = 0;
+ static char *result = NULL;
- len1 = *s1 != '\0' ? strlen (s1) : 0;
- len2 = *s2 != '\0' ? strlen (s2) : 0;
- len3 = *s3 != '\0' ? strlen (s3) : 0;
+ len1 = (s1 && *s1 != '\0') ? strlen (s1) : 0;
+ len2 = (s2 && *s2 != '\0') ? strlen (s2) : 0;
+ len3 = (s3 && *s3 != '\0') ? strlen (s3) : 0;
- result = xmalloc (len1 + len2 + len3 + 1);
+ if (len1 + len2 + len3 + 1 > rlen)
+ result = xrealloc (result, (rlen = len1 + len2 + len3 + 10));
- if (*s1 != '\0')
+ if (len1)
memcpy (result, s1, len1);
- if (*s2 != '\0')
+ if (len2)
memcpy (result + len1, s2, len2);
- if (*s3 != '\0')
+ if (len3)
memcpy (result + len1 + len2, s3, len3);
- *(result + len1 + len2 + len3) = '\0';
+
+ result[len1+len2+len3] = '\0';
return result;
}
@@ -426,10 +429,10 @@ end_of_token (const char *s)
* Same as end_of_token, but take into account a stop character
*/
char *
-end_of_token_w32 (char *s, char stopchar)
+end_of_token_w32 (const char *s, char stopchar)
{
- register char *p = s;
- register int backslash = 0;
+ const char *p = s;
+ int backslash = 0;
while (*p != '\0' && *p != stopchar
&& (backslash || !isblank ((unsigned char)*p)))
@@ -447,7 +450,7 @@ end_of_token_w32 (char *s, char stopchar)
backslash = 0;
}
- return p;
+ return (char *)p;
}
#endif
@@ -461,22 +464,23 @@ next_token (const char *s)
return (char *)s;
}
-/* Find the next token in PTR; return the address of it, and store the
- length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
+/* Find the next token in PTR; return the address of it, and store the length
+ of the token into *LENGTHPTR if LENGTHPTR is not nil. Set *PTR to the end
+ of the token, so this function can be called repeatedly in a loop. */
char *
-find_next_token (char **ptr, unsigned int *lengthptr)
+find_next_token (const char **ptr, unsigned int *lengthptr)
{
- char *p = next_token (*ptr);
- char *end;
+ const char *p = next_token (*ptr);
if (*p == '\0')
return 0;
- *ptr = end = end_of_token (p);
+ *ptr = end_of_token (p);
if (lengthptr != 0)
- *lengthptr = end - p;
- return p;
+ *lengthptr = *ptr - p;
+
+ return (char *)p;
}
@@ -496,12 +500,6 @@ alloc_dep ()
void
free_dep (struct dep *d)
{
- if (d->name != 0)
- free (d->name);
-
- if (d->stem != 0)
- free (d->stem);
-
free (d);
}
@@ -511,20 +509,14 @@ free_dep (struct dep *d)
struct dep *
copy_dep_chain (const struct dep *d)
{
- register struct dep *c;
struct dep *firstnew = 0;
struct dep *lastnew = 0;
while (d != 0)
{
- c = xmalloc (sizeof (struct dep));
+ struct dep *c = xmalloc (sizeof (struct dep));
memcpy (c, d, sizeof (struct dep));
- if (c->name != 0)
- c->name = xstrdup (c->name);
- if (c->stem != 0)
- c->stem = xstrdup (c->stem);
-
c->next = 0;
if (firstnew == 0)
firstnew = lastnew = c;
@@ -549,37 +541,20 @@ free_dep_chain (struct dep *d)
free_dep (df);
}
}
-
-/* Free a chain of `struct nameseq'. Each nameseq->name is freed
- as well. For `struct dep' chains use free_dep_chain. */
-
-void
-free_ns_chain (struct nameseq *n)
-{
- register struct nameseq *tmp;
- while (n != 0)
- {
- if (n->name != 0)
- free (n->name);
+/* Free a chain of struct nameseq.
+ For struct dep chains use free_dep_chain. */
- tmp = n;
-
- n = n->next;
-
- free (tmp);
- }
-
-}
-#ifdef iAPX286
-/* The losing compiler on this machine can't handle this macro. */
-
-char *
-dep_name (struct dep *dep)
+void
+free_ns_chain (struct nameseq *ns)
{
- return dep->name == 0 ? dep->file->name : dep->name;
+ while (ns != 0)
+ {
+ struct nameseq *t = ns;
+ ns = ns->next;
+ free (t);
+ }
}
-#endif
#ifdef GETLOADAVG_PRIVILEGED