ndbfree.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <ctype.h>
  13. #include <ndb.h>
  14. #include "ndbhf.h"
  15. /*
  16. * free a parsed entry
  17. */
  18. void
  19. ndbfree(Ndbtuple *t)
  20. {
  21. Ndbtuple *tn;
  22. for(; t; t = tn){
  23. tn = t->entry;
  24. if(t->val != t->valbuf){
  25. free(t->val);
  26. }
  27. free(t);
  28. }
  29. }
  30. /*
  31. * set a value in a tuple
  32. */
  33. void
  34. ndbsetval(Ndbtuple *t, char *val, int n)
  35. {
  36. if(n < Ndbvlen){
  37. if(t->val != t->valbuf){
  38. free(t->val);
  39. t->val = t->valbuf;
  40. }
  41. } else {
  42. if(t->val != t->valbuf)
  43. t->val = realloc(t->val, n+1);
  44. else
  45. t->val = malloc(n+1);
  46. if(t->val == nil)
  47. sysfatal("ndbsetval %r");
  48. }
  49. strncpy(t->val, val, n);
  50. t->val[n] = 0;
  51. }
  52. /*
  53. * allocate a tuple
  54. */
  55. Ndbtuple*
  56. ndbnew(char *attr, char *val)
  57. {
  58. Ndbtuple *t;
  59. t = mallocz(sizeof(*t), 1);
  60. if(t == nil)
  61. sysfatal("ndbnew %r");
  62. if(attr != nil)
  63. strncpy(t->attr, attr, sizeof(t->attr)-1);
  64. t->val = t->valbuf;
  65. if(val != nil)
  66. ndbsetval(t, val, strlen(val));
  67. ndbsetmalloctag(t, getcallerpc(&attr));
  68. return t;
  69. }
  70. /*
  71. * set owner of a tuple
  72. */
  73. void
  74. ndbsetmalloctag(Ndbtuple *t, uintptr tag)
  75. {
  76. for(; t; t=t->entry)
  77. setmalloctag(t, tag);
  78. }