From dbfb32056693662b9843b47c70a672f32ea55d8b Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sat, 28 Apr 2018 04:38:34 +0200 Subject: [PATCH] libndb: cleanup --- sys/src/libndb/csgetval.c | 46 ++++++++++-------------------- sys/src/libndb/dnsquery.c | 7 ++--- sys/src/libndb/ndbdiscard.c | 1 - sys/src/libndb/ndbfree.c | 2 +- sys/src/libndb/ndbgetval.c | 37 +++++++----------------- sys/src/libndb/ndbhash.c | 56 +++++++++++++++---------------------- sys/src/libndb/ndbparse.c | 10 +++---- sys/src/libndb/ndbreorder.c | 2 +- 8 files changed, 57 insertions(+), 104 deletions(-) diff --git a/sys/src/libndb/csgetval.c b/sys/src/libndb/csgetval.c index 9bd6123c1..a1f6a9052 100644 --- a/sys/src/libndb/csgetval.c +++ b/sys/src/libndb/csgetval.c @@ -14,15 +14,12 @@ char* csgetvalue(char *netroot, char *attr, char *val, char *rattr, Ndbtuple **pp) { Ndbtuple *t, *first, *last; - int n, linefound; char line[1024]; - int fd; - int oops = 0; + int fd, n; char *rv; - if(pp) + if(pp != nil) *pp = nil; - rv = nil; if(netroot) snprint(line, sizeof(line), "%s/cs", netroot); @@ -30,17 +27,17 @@ csgetvalue(char *netroot, char *attr, char *val, char *rattr, Ndbtuple **pp) strcpy(line, "/net/cs"); fd = open(line, ORDWR); if(fd < 0) - return 0; + return nil; seek(fd, 0, 0); snprint(line, sizeof(line), "!%s=%s %s=*", attr, val, rattr); if(write(fd, line, strlen(line)) < 0){ close(fd); - return 0; + return nil; } seek(fd, 0, 0); - first = last = 0; - linefound = 0; + rv = nil; + first = last = nil; for(;;){ n = read(fd, line, sizeof(line)-2); if(n <= 0) @@ -49,35 +46,22 @@ csgetvalue(char *netroot, char *attr, char *val, char *rattr, Ndbtuple **pp) line[n+1] = 0; t = _ndbparseline(line); - if(t == 0) + if(t == nil) continue; - if(first) + if(first != nil) last->entry = t; else first = t; - last = t; - - while(last->entry) - last = last->entry; - - for(; t; t = t->entry){ - if(linefound == 0){ - if(strcmp(rattr, t->attr) == 0){ - linefound = 1; - rv = strdup(t->val); - } - } - } + do { + last = t; + if(rv == nil && strcmp(rattr, t->attr) == 0) + rv = strdup(t->val); + t = t->entry; + } while(t != nil); } close(fd); - if(oops){ - werrstr("buffer too short"); - ndbfree(first); - return nil; - } - - if(pp){ + if(pp != nil){ setmalloctag(first, getcallerpc(&netroot)); *pp = first; } else diff --git a/sys/src/libndb/dnsquery.c b/sys/src/libndb/dnsquery.c index fbb9680e1..634756b76 100644 --- a/sys/src/libndb/dnsquery.c +++ b/sys/src/libndb/dnsquery.c @@ -129,17 +129,14 @@ doquery(int fd, char *dn, char *type) t = _ndbparseline(buf); if(t != nil){ - if(first) + if(first != nil) last->entry = t; else first = t; last = t; - - while(last->entry) + while(last->entry != nil) last = last->entry; } } - - ndbsetmalloctag(first, getcallerpc(&fd)); return first; } diff --git a/sys/src/libndb/ndbdiscard.c b/sys/src/libndb/ndbdiscard.c index ea557ffc5..9b2fc0c98 100644 --- a/sys/src/libndb/ndbdiscard.c +++ b/sys/src/libndb/ndbdiscard.c @@ -25,6 +25,5 @@ ndbdiscard(Ndbtuple *t, Ndbtuple *a) a->entry = nil; ndbfree(a); - ndbsetmalloctag(t, getcallerpc(&t)); return t; } diff --git a/sys/src/libndb/ndbfree.c b/sys/src/libndb/ndbfree.c index d3f27feb5..f78d38554 100644 --- a/sys/src/libndb/ndbfree.c +++ b/sys/src/libndb/ndbfree.c @@ -71,6 +71,6 @@ ndbnew(char *attr, char *val) void ndbsetmalloctag(Ndbtuple *t, uintptr tag) { - for(; t; t=t->entry) + for(; t != nil; t=t->entry) setmalloctag(t, tag); } diff --git a/sys/src/libndb/ndbgetval.c b/sys/src/libndb/ndbgetval.c index 749cc1564..9bc8058c8 100644 --- a/sys/src/libndb/ndbgetval.c +++ b/sys/src/libndb/ndbgetval.c @@ -7,7 +7,7 @@ * search for a tuple that has the given 'attr=val' and also 'rattr=x'. * copy 'x' into 'buf' and return the whole tuple. * - * return 0 if not found. + * return nil if not found. */ char* ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, Ndbtuple **pp) @@ -21,32 +21,15 @@ ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, Ndbtuple **pp) if(pp) *pp = nil; t = ndbsearch(db, s, attr, val); - while(t){ - /* first look on same line (closer binding) */ - nt = s->t; - for(;;){ - if(strcmp(rattr, nt->attr) == 0){ - rv = strdup(nt->val); - if(pp != nil) - *pp = t; - else - ndbfree(t); - return rv; - } - nt = nt->line; - if(nt == s->t) - break; - } - /* search whole tuple */ - for(nt = t; nt; nt = nt->entry){ - if(strcmp(rattr, nt->attr) == 0){ - rv = strdup(nt->val); - if(pp != nil) - *pp = t; - else - ndbfree(t); - return rv; - } + while(t != nil){ + nt = ndbfindattr(t, s->t, rattr); + if(nt != nil){ + rv = strdup(nt->val); + if(pp != nil) + *pp = t; + else + ndbfree(t); + return rv; } ndbfree(t); t = ndbsnext(s, attr, val); diff --git a/sys/src/libndb/ndbhash.c b/sys/src/libndb/ndbhash.c index 94dfd67c7..63b0839de 100644 --- a/sys/src/libndb/ndbhash.c +++ b/sys/src/libndb/ndbhash.c @@ -123,26 +123,22 @@ ndbsearch(Ndb *db, Ndbs *s, char *attr, char *val) memset(s, 0, sizeof(*s)); if(_ndbcachesearch(db, s, attr, val, &t) == 0){ /* found in cache */ - if(t != nil){ - ndbsetmalloctag(t, getcallerpc(&db)); - return t; /* answer from this file */ - } + if(t != nil) + goto out; if(db->next == nil) return nil; t = ndbsearch(db->next, s, attr, val); - ndbsetmalloctag(t, getcallerpc(&db)); - return t; + goto out; } s->db = db; s->hf = hf; - if(s->hf){ + if(s->hf != nil){ s->ptr = ndbhash(val, s->hf->hlen)*NDBPLEN; p = hfread(s->hf, s->ptr+NDBHLEN, NDBPLEN); - if(p == 0){ + if(p == nil){ t = _ndbcacheadd(db, s, attr, val, nil); - ndbsetmalloctag(t, getcallerpc(&db)); - return t; + goto out; } s->ptr = NDBGETP(p); s->type = Cptr1; @@ -153,17 +149,17 @@ ndbsearch(Ndb *db, Ndbs *s, char *attr, char *val) /* advance search to next db file */ s->ptr = NDBNAP; _ndbcacheadd(db, s, attr, val, nil); - if(db->next == 0) + if(db->next == nil) return nil; t = ndbsearch(db->next, s, attr, val); - ndbsetmalloctag(t, getcallerpc(&db)); - return t; + goto out; } else { s->ptr = 0; s->type = Dptr; } t = ndbsnext(s, attr, val); _ndbcacheadd(db, s, attr, val, (t != nil && s->db == db)?t:nil); +out: ndbsetmalloctag(t, getcallerpc(&db)); return t; } @@ -173,7 +169,7 @@ match(Ndbtuple *t, char *attr, char *val) { Ndbtuple *nt; - for(nt = t; nt; nt = nt->entry) + for(nt = t; nt != nil; nt = nt->entry) if(strcmp(attr, nt->attr) == 0 && strcmp(val, nt->val) == 0) return nt; @@ -200,12 +196,10 @@ ndbsnext(Ndbs *s, char *attr, char *val) break; t = ndbparse(db); s->ptr = Boffset(&db->b); - if(t == 0) + if(t == nil) break; - if(s->t = match(t, attr, val)){ - ndbsetmalloctag(t, getcallerpc(&s)); - return t; - } + if((s->t = match(t, attr, val)) != nil) + goto out; ndbfree(t); } else if(s->type == Cptr){ if(Bseek(&db->b, s->ptr, 0) < 0) @@ -213,18 +207,16 @@ ndbsnext(Ndbs *s, char *attr, char *val) s->ptr = s->ptr1; s->type = Cptr1; t = ndbparse(db); - if(t == 0) + if(t == nil) break; - if(s->t = match(t, attr, val)){ - ndbsetmalloctag(t, getcallerpc(&s)); - return t; - } + if((s->t = match(t, attr, val)) != nil) + goto out; ndbfree(t); } else if(s->type == Cptr1){ if(s->ptr & NDBCHAIN){ /* hash chain continuation */ s->ptr &= ~NDBCHAIN; p = hfread(s->hf, s->ptr+NDBHLEN, 2*NDBPLEN); - if(p == 0) + if(p == nil) break; s->ptr = NDBGETP(p); s->ptr1 = NDBGETP(p+NDBPLEN); @@ -234,12 +226,10 @@ ndbsnext(Ndbs *s, char *attr, char *val) break; s->ptr = NDBNAP; t = ndbparse(db); - if(t == 0) + if(t == nil) break; - if(s->t = match(t, attr, val)){ - ndbsetmalloctag(t, getcallerpc(&s)); - return t; - } + if((s->t = match(t, attr, val)) != nil) + goto out; ndbfree(t); break; } @@ -247,14 +237,14 @@ ndbsnext(Ndbs *s, char *attr, char *val) } nextfile: - /* nothing left to search? */ s->ptr = NDBNAP; - if(db->next == 0) - return 0; + if(db->next == nil) + return nil; /* advance search to next db file */ t = ndbsearch(db->next, s, attr, val); +out: ndbsetmalloctag(t, getcallerpc(&s)); return t; } diff --git a/sys/src/libndb/ndbparse.c b/sys/src/libndb/ndbparse.c index e551ada9d..415118b4a 100644 --- a/sys/src/libndb/ndbparse.c +++ b/sys/src/libndb/ndbparse.c @@ -30,9 +30,9 @@ ndbparse(Ndb *db) Ndbtuple *first, *last; int len; - first = last = 0; + first = last = nil; for(;;){ - if((line = Brdline(&db->b, '\n')) == 0) + if((line = Brdline(&db->b, '\n')) == nil) break; len = Blinelen(&db->b); if(line[len-1] != '\n') @@ -42,15 +42,15 @@ ndbparse(Ndb *db) break; } t = _ndbparseline(line); - if(t == 0) + if(t == nil) continue; setmalloctag(t, getcallerpc(&db)); - if(first) + if(first != nil) last->entry = t; else first = t; last = t; - while(last->entry) + while(last->entry != nil) last = last->entry; } ndbsetmalloctag(first, getcallerpc(&db)); diff --git a/sys/src/libndb/ndbreorder.c b/sys/src/libndb/ndbreorder.c index 167d0a0ac..f68f7a74f 100644 --- a/sys/src/libndb/ndbreorder.c +++ b/sys/src/libndb/ndbreorder.c @@ -4,7 +4,7 @@ #include /* - * reorder the tuple to put x's line first in the entry and x fitst in its line + * reorder the tuple to put x's line first in the entry and x first in its line */ Ndbtuple* ndbreorder(Ndbtuple *t, Ndbtuple *x)