]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libndb/ndbsubstitute.c
check program size
[plan9front.git] / sys / src / libndb / ndbsubstitute.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ndb.h>
5
6 /* replace a in t with b, the line structure in b is lost, c'est la vie */
7 Ndbtuple*
8 ndbsubstitute(Ndbtuple *t, Ndbtuple *a, Ndbtuple *b)
9 {
10         Ndbtuple *nt;
11
12         if(a == b){
13                 ndbsetmalloctag(t, getcallerpc(&t));
14                 return t;
15         }
16         if(b == nil){
17                 t = ndbdiscard(t, a);
18                 ndbsetmalloctag(t, getcallerpc(&t));
19                 return t;
20         }
21
22         /* all pointers to a become pointers to b */
23         for(nt = t; nt != nil; nt = nt->entry){
24                 if(nt->line == a)
25                         nt->line = b;
26                 if(nt->entry == a)
27                         nt->entry = b;
28         }
29
30         /* end of b chain points to a's successors */
31         for(nt = b; nt->entry; nt = nt->entry)
32                 nt->line = nt->entry;
33         nt->line = a->line;
34         nt->entry = a->entry;
35
36         a->entry = nil;
37         ndbfree(a);
38
39         if(a == t){
40                 ndbsetmalloctag(b, getcallerpc(&t));
41                 return b;
42         }else{
43                 ndbsetmalloctag(t, getcallerpc(&t));
44                 return t;
45         }
46 }