ndbsubstitute.c 867 B

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