diff --git a/Makefile b/Makefile index 11d20d6..91aec6c 100644 --- a/Makefile +++ b/Makefile @@ -134,6 +134,7 @@ OBJ:=\ ui/ui.o\ util/print.o\ strpg.o\ + strawk/alloc.o\ strawk/awkgram.tab.o\ strawk/b.o\ strawk/parse.o\ diff --git a/strawk/alloc.c b/strawk/alloc.c index 8c3f510..5865faf 100644 --- a/strawk/alloc.c +++ b/strawk/alloc.c @@ -139,12 +139,12 @@ char *defstrdup(const char *s) size_t n; n = strlen(s); - return resize(s, n+1, n+1, compile_time == RUNNING); + return resize(s, n+1, n+1, compile_time == RUNNING || evalstr != NULL); } void *defrealloc(void *p, size_t old, size_t n) { - return resize(p, old, n, compile_time == RUNNING); + return resize(p, old, n, compile_time == RUNNING || evalstr != NULL); } void *defalloc(size_t n) @@ -152,9 +152,9 @@ void *defalloc(size_t n) return alloc(n, compile_time == RUNNING); } -void egrow(void **buf, int *size, void **pos, int want, int blocksz, char *fn) +void dgrow(void **buf, int *size, void **pos, int want, int blocksz, const char *fn) { - int sz, n; + size_t sz, n; intptr_t off; uschar *p; @@ -167,50 +167,50 @@ void egrow(void **buf, int *size, void **pos, int want, int blocksz, char *fn) want += blocksz - n; p = *buf; off = pos != NULL ? *(uschar**)pos - p : 0; - p = erealloc(p, off > 0 ? off : sz, want, fn); - DPRINTF("%p %d -> %d\n", p, sz, n); + p = drealloc(p, off > 0 ? off : sz, want, fn); + DPRINTF("%p %zu -> %zu\n", p, sz, n); *buf = p; if (pos) *pos = p + off; } -void *erealloc(void *s, size_t old, size_t new, char *fn) +void *drealloc(void *s, size_t old, size_t new, const char *fn) { void *p; - DPRINTF(awkstderr, "erealloc %p %d to %d in %s ", s, old, new, fn); + DPRINTF("drealloc %p %zu to %zu in %s ", s, old, new, fn); if((p = realloc(s, new)) == NULL) FATAL("realloc: out of memory"); if(new > old) memset((uschar *)p + old, 0, new - old); - DPRINTF(awkstderr, "-> %p", p); + DPRINTF("-> %p", p); return p; } -void *emalloc(size_t n, char *fn) +void *dmalloc(size_t n, const char *fn) { void *p; - DPRINTF(awkstderr, "emalloc %d in %s ", n, fn); + DPRINTF("dmalloc %zu in %s ", n, fn); if((p = calloc(1, n)) == NULL) FATAL("calloc: out of memory"); - DPRINTF(awkstderr, "-> %p", p); + DPRINTF("-> %p", p); return p; } -char *estrdup(char *s, char *fn) +char *dstrdup(char *s, const char *fn) { void *p; - DPRINTF(awkstderr, "estrdup %s in %s ", s, fn); + DPRINTF("dstrdup %s in %s ", s, fn); if((p = strdup(s)) == NULL) FATAL("strdup: out of memory"); - DPRINTF(awkstderr, "-> %p", p); + DPRINTF("-> %p", p); return p; } -void efree(void *p, char *fn) +void dfree(void *p, const char *fn) { - DPRINTF(awkstderr, "efree %p in %s\n", p, fn); + DPRINTF("dfree %p in %s\n", p, fn); free(p); } diff --git a/strawk/awk.h b/strawk/awk.h index 7eb4cae..1292cec 100644 --- a/strawk/awk.h +++ b/strawk/awk.h @@ -278,6 +278,7 @@ typedef struct fa { int initstat; int curstat; int accept; + int cst; struct rrow re[1]; /* variable: actual size set by calling malloc */ } fa; diff --git a/strawk/b.c b/strawk/b.c index b307f28..f8bc203 100644 --- a/strawk/b.c +++ b/strawk/b.c @@ -179,8 +179,8 @@ fa *makedfa(const char *s, bool anchor) /* returns dfa for reg expr s */ resizesetvec(__func__); } - if (compile_time != RUNNING) /* a constant for sure */ - return mkdfa(s, anchor); + //if (compile_time != RUNNING) /* a constant for sure */ + // return mkdfa(s, anchor); for (i = 0; i < nfatab; i++) /* is it there already? */ if (fatab[i]->anchor == anchor && strcmp((const char *) fatab[i]->restr, s) == 0) { @@ -188,6 +188,8 @@ fa *makedfa(const char *s, bool anchor) /* returns dfa for reg expr s */ return fatab[i]; } pfa = mkdfa(s, anchor); + if(compile_time != RUNNING) + pfa->cst = 1; if (nfatab < NFA) { /* room for another */ fatab[nfatab] = pfa; fatab[nfatab]->use = now++; @@ -196,8 +198,10 @@ fa *makedfa(const char *s, bool anchor) /* returns dfa for reg expr s */ } use = fatab[0]->use; /* replace least-recently used */ nuse = 0; + + /* FIXME: possible overflow? */ for (i = 1; i < nfatab; i++) - if (fatab[i]->use < use) { + if (!fatab[i]->cst && fatab[i]->use < use) { use = fatab[i]->use; nuse = i; } @@ -223,7 +227,7 @@ fa *mkdfa(const char *s, bool anchor) /* does the real work of making a dfa */ poscnt = 0; penter(p1); /* enter parent pointers and leaf indices */ - f = (fa *) CALLOC(1, sizeof(fa) + poscnt * sizeof(rrow)); + f = (fa *) MALLOC(sizeof(fa) + poscnt * sizeof(rrow)); f->accept = poscnt-1; /* penter has computed number of positions in re */ cfoll(f, p1); /* set up follow sets */ resize_state(f, 1); @@ -233,10 +237,8 @@ fa *mkdfa(const char *s, bool anchor) /* does the real work of making a dfa */ f->initstat = makeinit(f, anchor); f->anchor = anchor; f->restr = (uschar *) tostring(s); - if (firstbasestr != basestr) { - if (basestr) - xfree(basestr); - } + if (firstbasestr != basestr) + basestr = NULL; return f; } @@ -1168,10 +1170,8 @@ replace_repeat(const uschar *reptok, int reptoklen, const uschar *atom, j += suffix_length; buf[j] = '\0'; /* free old basestr */ - if (firstbasestr != basestr) { - if (basestr) - xfree(basestr); - } + if (firstbasestr != basestr) + basestr = NULL; basestr = buf; prestr = buf + prefix_length; if (special_case == REPEAT_ZERO) { diff --git a/strawk/lex.c b/strawk/lex.c index 04de6c0..de66a8c 100644 --- a/strawk/lex.c +++ b/strawk/lex.c @@ -587,14 +587,14 @@ int regexpr(void) { int c; static char *buf = NULL; - static int bufsz = 500; + static int bufsz = 64; char *bp; if (buf == NULL) buf = (char *) MALLOC(bufsz); bp = buf; for ( ; (c = input()) != '/' && c != 0; ) { - if (!adjbuf(&buf, &bufsz, bp-buf+3, 500, &bp, "regexpr")) + if (!adjbuf(&buf, &bufsz, bp-buf+3, 64, &bp, "regexpr")) FATAL("out of space for reg expr %.10s...", buf); if (c == '\n') { *bp = '\0'; diff --git a/strawk/lib.c b/strawk/lib.c index 73e9656..3ff51a4 100644 --- a/strawk/lib.c +++ b/strawk/lib.c @@ -68,55 +68,6 @@ extern jmp_buf evalenv; static Cell dollar0 = { OCELL, CFLD, NULL, EMPTY, {0}, REC|STR|DONTFREE, NULL, NULL }; static Cell dollar1 = { OCELL, CFLD, NULL, EMPTY, {0}, FLD|STR|DONTFREE, NULL, NULL }; -static size_t nalloc, renalloc; - -/* FIXME: use strangepg's emalloc functions when linked? */ -void *dmalloc(size_t size, const char *fn) -{ - void *p; - - nalloc += size; - p = malloc(size); - DPRINTF("ALLOC malloc %s %p %zd / %zd\n", fn, p, size, nalloc); - return p; -} -char *dstrdup(const char *s, const char *fn) -{ - char *p; - size_t size; - - size = strlen(s); - nalloc += size; - p = strdup(s); - DPRINTF("ALLOC strdup %s %p %zd / %zd [%s]\n", fn, (void*)p, size, nalloc, s); - return p; -} -void *dcalloc(size_t nmemb, size_t size, const char *fn) -{ - void *p; - - nalloc += nmemb * size; - p = calloc(nmemb, size); - DPRINTF("ALLOC calloc %s %p %zd / %zd\n", fn, p, nmemb*size, nalloc); - return p; -} -void *drealloc(void *ptr, size_t size, const char *fn) -{ - void *p; - - renalloc += size; - p = realloc(ptr, size); - DPRINTF("ALLOC realloc %s %p→%p %zd / %zd\n", fn, ptr, p, size, renalloc); - return p; -} -void dfree(void *ptr, const char *fn) -{ - if(ptr == NULL) - return; - DPRINTF("ALLOC free %s %p\n", fn, ptr); - free(ptr); -} - void recinit(unsigned int n) { record = (char *) MALLOC(n); diff --git a/strawk/proctab.c b/strawk/proctab.c index b3bed9e..973c4c6 100644 --- a/strawk/proctab.c +++ b/strawk/proctab.c @@ -1,110 +1,110 @@ #include #include "awk.h" -#include "awkgram.plan9.h" +#include "awkgram.tab.h" static const char * const printname[102] = { - "FIRSTTOKEN", /* 57346 */ - "PROGRAM", /* 57347 */ - "PASTAT", /* 57348 */ - "PASTAT2", /* 57349 */ - "XBEGIN", /* 57350 */ - "XEND", /* 57351 */ - "NL", /* 57352 */ - "ARRAY", /* 57353 */ - "MATCH", /* 57354 */ - "NOTMATCH", /* 57355 */ - "MATCHOP", /* 57356 */ - "FINAL", /* 57357 */ - "DOT", /* 57358 */ - "ALL", /* 57359 */ - "CCL", /* 57360 */ - "NCCL", /* 57361 */ - "CHAR", /* 57362 */ - "OR", /* 57363 */ - "STAR", /* 57364 */ - "QUEST", /* 57365 */ - "PLUS", /* 57366 */ - "EMPTYRE", /* 57367 */ - "ZERO", /* 57368 */ - "LAND", /* 57369 */ - "LOR", /* 57370 */ - "EQ", /* 57371 */ - "GE", /* 57372 */ - "GT", /* 57373 */ - "LE", /* 57374 */ - "LT", /* 57375 */ - "NE", /* 57376 */ - "IN", /* 57377 */ - "ARG", /* 57378 */ - "BLTIN", /* 57379 */ - "BREAK", /* 57380 */ - "CONTINUE", /* 57381 */ - "DELETE", /* 57382 */ - "DO", /* 57383 */ - "EXIT", /* 57384 */ - "FOR", /* 57385 */ - "FUNC", /* 57386 */ - "SUB", /* 57387 */ - "GSUB", /* 57388 */ - "IF", /* 57389 */ - "INDEX", /* 57390 */ - "LSUBSTR", /* 57391 */ - "MATCHFCN", /* 57392 */ - "NEXT", /* 57393 */ - "ADD", /* 57394 */ - "MINUS", /* 57395 */ - "MULT", /* 57396 */ - "DIVIDE", /* 57397 */ - "MOD", /* 57398 */ - "LSHIFT", /* 57399 */ - "RSHIFT", /* 57400 */ - "XOR", /* 57401 */ - "BAND", /* 57402 */ - "BOR", /* 57403 */ - "CMPL", /* 57404 */ - "ASSIGN", /* 57405 */ - "ASGNOP", /* 57406 */ - "ADDEQ", /* 57407 */ - "SUBEQ", /* 57408 */ - "MULTEQ", /* 57409 */ - "DIVEQ", /* 57410 */ - "MODEQ", /* 57411 */ - "POWEQ", /* 57412 */ - "BANDEQ", /* 57413 */ - "BOREQ", /* 57414 */ - "XOREQ", /* 57415 */ - "SHLEQ", /* 57416 */ - "SHREQ", /* 57417 */ - "PRINT", /* 57418 */ - "PRINTF", /* 57419 */ - "SPRINTF", /* 57420 */ - "ELSE", /* 57421 */ - "INTEST", /* 57422 */ - "CONDEXPR", /* 57423 */ - "POSTINCR", /* 57424 */ - "PREINCR", /* 57425 */ - "POSTDECR", /* 57426 */ - "PREDECR", /* 57427 */ - "VAR", /* 57428 */ - "IVAR", /* 57429 */ - "VARNF", /* 57430 */ - "CALL", /* 57431 */ - "NUMBER", /* 57432 */ - "STRING", /* 57433 */ - "REGEXPR", /* 57434 */ - "RETURN", /* 57435 */ - "SPLIT", /* 57436 */ - "SUBSTR", /* 57437 */ - "WHILE", /* 57438 */ - "CAT", /* 57439 */ - "NOT", /* 57440 */ - "UMINUS", /* 57441 */ - "UPLUS", /* 57442 */ - "POWER", /* 57443 */ - "DECR", /* 57444 */ - "INCR", /* 57445 */ - "INDIRECT", /* 57446 */ - "LASTTOKEN", /* 57447 */ + "FIRSTTOKEN", /* 258 */ + "PROGRAM", /* 259 */ + "PASTAT", /* 260 */ + "PASTAT2", /* 261 */ + "XBEGIN", /* 262 */ + "XEND", /* 263 */ + "NL", /* 264 */ + "ARRAY", /* 265 */ + "MATCH", /* 266 */ + "NOTMATCH", /* 267 */ + "MATCHOP", /* 268 */ + "FINAL", /* 269 */ + "DOT", /* 270 */ + "ALL", /* 271 */ + "CCL", /* 272 */ + "NCCL", /* 273 */ + "CHAR", /* 274 */ + "OR", /* 275 */ + "STAR", /* 276 */ + "QUEST", /* 277 */ + "PLUS", /* 278 */ + "EMPTYRE", /* 279 */ + "ZERO", /* 280 */ + "LAND", /* 281 */ + "LOR", /* 282 */ + "EQ", /* 283 */ + "GE", /* 284 */ + "GT", /* 285 */ + "LE", /* 286 */ + "LT", /* 287 */ + "NE", /* 288 */ + "IN", /* 289 */ + "ARG", /* 290 */ + "BLTIN", /* 291 */ + "BREAK", /* 292 */ + "CONTINUE", /* 293 */ + "DELETE", /* 294 */ + "DO", /* 295 */ + "EXIT", /* 296 */ + "FOR", /* 297 */ + "FUNC", /* 298 */ + "SUB", /* 299 */ + "GSUB", /* 300 */ + "IF", /* 301 */ + "INDEX", /* 302 */ + "LSUBSTR", /* 303 */ + "MATCHFCN", /* 304 */ + "NEXT", /* 305 */ + "ADD", /* 306 */ + "MINUS", /* 307 */ + "MULT", /* 308 */ + "DIVIDE", /* 309 */ + "MOD", /* 310 */ + "LSHIFT", /* 311 */ + "RSHIFT", /* 312 */ + "XOR", /* 313 */ + "BAND", /* 314 */ + "BOR", /* 315 */ + "CMPL", /* 316 */ + "ASSIGN", /* 317 */ + "ASGNOP", /* 318 */ + "ADDEQ", /* 319 */ + "SUBEQ", /* 320 */ + "MULTEQ", /* 321 */ + "DIVEQ", /* 322 */ + "MODEQ", /* 323 */ + "POWEQ", /* 324 */ + "BANDEQ", /* 325 */ + "BOREQ", /* 326 */ + "XOREQ", /* 327 */ + "SHLEQ", /* 328 */ + "SHREQ", /* 329 */ + "PRINT", /* 330 */ + "PRINTF", /* 331 */ + "SPRINTF", /* 332 */ + "ELSE", /* 333 */ + "INTEST", /* 334 */ + "CONDEXPR", /* 335 */ + "POSTINCR", /* 336 */ + "PREINCR", /* 337 */ + "POSTDECR", /* 338 */ + "PREDECR", /* 339 */ + "VAR", /* 340 */ + "IVAR", /* 341 */ + "VARNF", /* 342 */ + "CALL", /* 343 */ + "NUMBER", /* 344 */ + "STRING", /* 345 */ + "REGEXPR", /* 346 */ + "RETURN", /* 347 */ + "SPLIT", /* 348 */ + "SUBSTR", /* 349 */ + "WHILE", /* 350 */ + "CAT", /* 351 */ + "NOT", /* 352 */ + "UMINUS", /* 353 */ + "UPLUS", /* 354 */ + "POWER", /* 355 */ + "DECR", /* 356 */ + "INCR", /* 357 */ + "INDIRECT", /* 358 */ + "LASTTOKEN", /* 359 */ }; diff --git a/strawk/proto.h b/strawk/proto.h index e0464df..46616ce 100644 --- a/strawk/proto.h +++ b/strawk/proto.h @@ -76,18 +76,18 @@ extern void *palloc(size_t); extern char *defstrdup(const char *); extern void *defrealloc(void *, size_t, size_t); extern void *defalloc(size_t); -extern void *erealloc(void *, size_t, size_t, char *); -extern void egrow(void **, int *, void **, int , int, char *); -extern char *estrdup(char *, char *); -extern void *emalloc(size_t, char *); -extern void efree(void *, char *); +extern void *drealloc(void *, size_t, size_t, const char *); +extern void dgrow(void **, int *, void **, int , int, const char *); +extern char *dstrdup(char *, const char *); +extern void *dmalloc(size_t, const char *); +extern void dfree(void *, const char *); /* FIXME */ -#define MALLOC(a) emalloc((a), __func__) -#define CALLOC(a, b) emalloc((a)*(b), __func__) -#define REALLOC(a, b, c) erealloc((a), (b), (c), __func__) -#define STRDUP(a) estrdup((a), __func__) -#define FREE(a) efree((a), __func__) +#define MALLOC(a) dmalloc((a), __func__) +#define CALLOC(a, b) dmalloc((a)*(b), __func__) +#define REALLOC(a, b, c) drealloc((a), (b), (c), __func__) +#define STRDUP(a) dstrdup((a), __func__) +#define FREE(a) dfree((a), __func__) #define xfree(a) { \ if((char*)(a) != EMPTY){ \ FREE((void *)(intptr_t)(a)); \ @@ -222,9 +222,3 @@ extern Cell *nullproc(TNode **, int); extern Cell *dosub(TNode **, int); extern const char *flags2str(int flags); - -extern void *dmalloc(size_t, const char *); -extern void *dcalloc(size_t, size_t, const char *); -extern void *drealloc(void *, size_t, const char *); -extern char *dstrdup(const char *, const char *); -extern void dfree(void *, const char *);