Tweaks and optimizations
This commit is contained in:
parent
db969f34a9
commit
bc813df3d7
2
files.c
2
files.c
@ -186,7 +186,7 @@ void destroy_file(file_t **f)
|
||||
}
|
||||
}
|
||||
|
||||
for (allocated_pat_t *next; (*f)->pats; (*f)->pats = next) {
|
||||
for (pat_t *next; (*f)->pats; (*f)->pats = next) {
|
||||
next = (*f)->pats->next;
|
||||
xfree(&(*f)->pats);
|
||||
}
|
||||
|
4
files.h
4
files.h
@ -10,14 +10,14 @@
|
||||
|
||||
#define file_err(f, ...) do { fprint_line(stderr, f, __VA_ARGS__); exit(EXIT_FAILURE); } while(false)
|
||||
|
||||
struct allocated_pat_s; // declared in types.h
|
||||
struct pat_s; // declared in types.h
|
||||
|
||||
typedef struct file_s {
|
||||
struct file_s *next;
|
||||
const char *filename;
|
||||
char *memory, **lines, *start, *end;
|
||||
size_t nlines;
|
||||
struct allocated_pat_s *pats;
|
||||
struct pat_s *pats;
|
||||
bool mmapped:1;
|
||||
} file_t;
|
||||
|
||||
|
8
match.c
8
match.c
@ -497,14 +497,14 @@ static match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, bool
|
||||
size_t len = (size_t)(m1->end - m1->start);
|
||||
pat_t *backref = new_pat(f, m1->start, m1->end, len, (ssize_t)len, BP_STRING);
|
||||
backref->args.string = m1->start;
|
||||
def_t *defs2 = with_def(defs, m1->pat->args.capture.namelen, m1->pat->args.capture.name, backref);
|
||||
|
||||
def_t *defs2 = with_def(defs, m1->pat->args.capture.namelen, m1->pat->args.capture.name, backref);
|
||||
++m1->refcount; {
|
||||
m2 = match(defs2, f, m1->end, pat->args.multiple.second, ignorecase);
|
||||
if (!m2) { // No need to keep the backref in memory if it didn't match
|
||||
for (struct allocated_pat_s **rem = &f->pats; *rem; rem = &(*rem)->next) {
|
||||
if (&(*rem)->pat == backref) {
|
||||
struct allocated_pat_s *tmp = *rem;
|
||||
for (pat_t **rem = &f->pats; *rem; rem = &(*rem)->next) {
|
||||
if ((*rem) == backref) {
|
||||
pat_t *tmp = *rem;
|
||||
*rem = (*rem)->next;
|
||||
free(tmp);
|
||||
break;
|
||||
|
22
pattern.c
22
pattern.c
@ -34,16 +34,18 @@ static pat_t *bp_simplepattern(file_t *f, const char *str);
|
||||
pat_t *new_pat(file_t *f, const char *start, const char *end, size_t minlen, ssize_t maxlen, enum pattype_e type)
|
||||
{
|
||||
static size_t next_pat_id = 1;
|
||||
allocated_pat_t *tracker = new(allocated_pat_t);
|
||||
tracker->next = f->pats;
|
||||
f->pats = tracker;
|
||||
tracker->pat.type = type;
|
||||
tracker->pat.start = start;
|
||||
tracker->pat.end = end;
|
||||
tracker->pat.min_matchlen = minlen;
|
||||
tracker->pat.max_matchlen = maxlen;
|
||||
tracker->pat.id = next_pat_id++;
|
||||
return &tracker->pat;
|
||||
pat_t *pat = new(pat_t);
|
||||
*pat = (pat_t){
|
||||
.next = f->pats,
|
||||
.type = type,
|
||||
.start = start,
|
||||
.end = end,
|
||||
.min_matchlen = minlen,
|
||||
.max_matchlen = maxlen,
|
||||
.id = next_pat_id++,
|
||||
};
|
||||
f->pats = pat;
|
||||
return pat;
|
||||
}
|
||||
|
||||
//
|
||||
|
10
types.h
10
types.h
@ -46,6 +46,7 @@ struct match_s; // forward declared to resolve circular struct defs
|
||||
// A struct reperesenting a BP virtual machine operation
|
||||
//
|
||||
typedef struct pat_s {
|
||||
struct pat_s *next;
|
||||
enum pattype_e type;
|
||||
const char *start, *end;
|
||||
// The bounds of the match length (used for backtracking)
|
||||
@ -126,14 +127,5 @@ typedef struct def_s {
|
||||
struct def_s *next;
|
||||
} def_t;
|
||||
|
||||
//
|
||||
// Structure used for tracking allocated patterns, which must be freed when the
|
||||
// file is freed.
|
||||
//
|
||||
typedef struct allocated_pat_s {
|
||||
struct allocated_pat_s *next;
|
||||
pat_t pat;
|
||||
} allocated_pat_t;
|
||||
|
||||
#endif
|
||||
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1
|
||||
|
Loading…
Reference in New Issue
Block a user