-/* $OpenBSD: look.c,v 1.23 2014/05/12 19:11:19 espie Exp $ */
+/* $OpenBSD: look.c,v 1.24 2014/12/21 09:33:12 espie Exp $ */
/*
* Copyright (c) 1989, 1993
static void *element_alloc(size_t, void *);
static void setup_definition(struct macro_definition *, const char *,
const char *);
+static void free_definition(char *);
+static void keep(char *);
+static int string_in_use(const char *);
static struct ohash_info macro_info = {
offsetof(struct ndblock, name),
ndptr n = create_entry(name);
if (n->d != NULL) {
if (n->d->defn != null)
- free(n->d->defn);
+ free_definition(n->d->defn);
} else {
n->d = xalloc(sizeof(struct macro_definition), NULL);
n->d->next = NULL;
else
return p;
}
+
+/* XXX things are slightly more complicated than they seem.
+ * a macro may actually be "live" (in the middle of an expansion
+ * on the stack.
+ * So we actually may need to place it in an array for later...
+ */
+
+static int kept_capacity = 0;
+static int kept_size = 0;
+static char **kept = NULL;
+
+static void
+keep(char *ptr)
+{
+ if (kept_capacity <= kept_size) {
+ if (kept_capacity)
+ kept_capacity *= 2;
+ else
+ kept_capacity = 50;
+ kept = xreallocarray(kept, kept_capacity,
+ sizeof(char *), "Out of memory while saving %d strings\n",
+ kept_capacity);
+ }
+ kept[kept_size++] = ptr;
+}
+
+static int
+string_in_use(const char *ptr)
+{
+ int i;
+ for (i = 0; i <= sp; i++) {
+ if (sstack[i] == STORAGE_MACRO && mstack[i].sstr == ptr)
+ return 1;
+ }
+ return 0;
+}
+
+
+static void
+free_definition(char *ptr)
+{
+ int i;
+
+ /* first try to free old strings */
+ for (i = 0; i < kept_size; i++) {
+ if (!string_in_use(kept[i])) {
+ kept_size--;
+ free(kept[i]);
+ if (i != kept_size)
+ kept[i] = kept[kept_size];
+ i--;
+ }
+ }
+
+ /* then deal with us */
+ if (string_in_use(ptr))
+ keep(ptr);
+ else
+ free(ptr);
+}
+
-/* $OpenBSD: main.c,v 1.83 2014/05/12 19:11:19 espie Exp $ */
+/* $OpenBSD: main.c,v 1.84 2014/12/21 09:33:12 espie Exp $ */
/* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
/*-
/*
* now push the string arguments:
*/
- pushs1(macro_getdef(p)->defn); /* defn string */
+ pushdef(p); /* defn string */
pushs1((char *)macro_name(p)); /* macro name */
pushs(ep); /* start next..*/
-/* $OpenBSD: mdef.h,v 1.31 2011/09/27 07:24:02 espie Exp $ */
+/* $OpenBSD: mdef.h,v 1.32 2014/12/21 09:33:12 espie Exp $ */
/* $NetBSD: mdef.h,v 1.7 1996/01/13 23:25:27 pk Exp $ */
/*
int c;
};
+#define STORAGE_STRSPACE 0
+#define STORAGE_MACRO 1
+#define STORAGE_OTHER 2
+
#define CURRENT_NAME (infile[ilevel].name)
#define CURRENT_LINE (infile[ilevel].lineno)
/*
if (++sp == STACKMAX) \
enlarge_stack();\
mstack[sp].sfra = (x); \
- sstack[sp] = 0; \
+ sstack[sp] = STORAGE_OTHER; \
} while (0)
#define pushs(x) \
if (++sp == STACKMAX) \
enlarge_stack();\
mstack[sp].sstr = (x); \
- sstack[sp] = 1; \
+ sstack[sp] = STORAGE_STRSPACE; \
} while (0)
#define pushs1(x) \
if (++sp == STACKMAX) \
enlarge_stack();\
mstack[sp].sstr = (x); \
- sstack[sp] = 0; \
+ sstack[sp] = STORAGE_OTHER; \
+ } while (0)
+
+#define pushdef(p) \
+ do { \
+ if (++sp == STACKMAX) \
+ enlarge_stack();\
+ mstack[sp].sstr = macro_getdef(p)->defn;\
+ sstack[sp] = STORAGE_MACRO; \
} while (0)
+
/*
* . .
-/* $OpenBSD: misc.c,v 1.44 2014/05/12 19:11:19 espie Exp $ */
+/* $OpenBSD: misc.c,v 1.45 2014/12/21 09:33:12 espie Exp $ */
/* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */
/*
errx(1, "string space overflow");
memcpy(newstrspace, strspace, strsize/2);
for (i = 0; i <= sp; i++)
- if (sstack[i])
+ if (sstack[i] == STORAGE_STRSPACE)
mstack[i].sstr = (mstack[i].sstr - strspace)
+ newstrspace;
ep = (ep-strspace) + newstrspace;