ref 843 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. .TH REF 9
  2. .SH NAME
  3. Ref, incref, decref \- reference counts
  4. .SH SYNOPSIS
  5. .ta \w'\fLchar* 'u
  6. .PP
  7. .B
  8. int incref(Ref *r)
  9. .PP
  10. .B
  11. int decref(Ref *r)
  12. .SH DESCRIPTION
  13. A
  14. .B Ref
  15. structure holds a reference count for a data structure:
  16. .IP
  17. .EX
  18. typedef struct
  19. struct Ref
  20. {
  21. Lock;
  22. long ref;
  23. } Ref;
  24. .EE
  25. .PP
  26. The reference count proper is found in
  27. .BR ref ;
  28. the
  29. .B Lock
  30. prevents concurrent updates
  31. (see
  32. .IR lock (9)).
  33. .PP
  34. .I Incref
  35. atomically increments the reference count
  36. .IR r ,
  37. and returns the new count.
  38. .PP
  39. .I Decref
  40. atomically decrements the reference count
  41. .IR r ,
  42. and returns the new count.
  43. .SH EXAMPLES
  44. Release a structure containing a
  45. .B Ref
  46. on last use.
  47. .IP
  48. .EX
  49. if(decref(s) == 0)
  50. free(s);
  51. .EE
  52. .SH SOURCE
  53. .B /sys/src/9/port/chan.c
  54. .SH DIAGNOSTICS
  55. .I Decref
  56. will
  57. .IR panic (9)
  58. if the count goes negative,
  59. revealing a reference counting bug.