1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include <ndb.h>
6 #include "ndbhf.h"
7
8 /*
9 * free a parsed entry
10 */
11 void
ndbfree(Ndbtuple * t)12 ndbfree(Ndbtuple *t)
13 {
14 Ndbtuple *tn;
15
16 for(; t; t = tn){
17 tn = t->entry;
18 if(t->val != t->valbuf){
19 free(t->val);
20 }
21 free(t);
22 }
23 }
24
25 /*
26 * set a value in a tuple
27 */
28 void
ndbsetval(Ndbtuple * t,char * val,int n)29 ndbsetval(Ndbtuple *t, char *val, int n)
30 {
31 if(n < Ndbvlen){
32 if(t->val != t->valbuf){
33 free(t->val);
34 t->val = t->valbuf;
35 }
36 } else {
37 if(t->val != t->valbuf)
38 t->val = realloc(t->val, n+1);
39 else
40 t->val = malloc(n+1);
41 if(t->val == nil)
42 sysfatal("ndbsetval %r");
43 }
44 strncpy(t->val, val, n);
45 t->val[n] = 0;
46 }
47
48 /*
49 * allocate a tuple
50 */
51 Ndbtuple*
ndbnew(char * attr,char * val)52 ndbnew(char *attr, char *val)
53 {
54 Ndbtuple *t;
55
56 t = mallocz(sizeof(*t), 1);
57 if(t == nil)
58 sysfatal("ndbnew %r");
59 if(attr != nil)
60 strncpy(t->attr, attr, sizeof(t->attr)-1);
61 t->val = t->valbuf;
62 if(val != nil)
63 ndbsetval(t, val, strlen(val));
64 ndbsetmalloctag(t, getcallerpc(&attr));
65 return t;
66 }
67
68 /*
69 * set owner of a tuple
70 */
71 void
ndbsetmalloctag(Ndbtuple * t,uintptr tag)72 ndbsetmalloctag(Ndbtuple *t, uintptr tag)
73 {
74 for(; t; t=t->entry)
75 setmalloctag(t, tag);
76 }
77