msgbuf.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2002 Internet Software Consortium.
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
  9. * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: msgbuf.c,v 1.2 2002/12/06 21:06:27 lidl Exp $ */
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <isc/mem.h>
  22. #include <isc/util.h>
  23. #include "msgbuf.h"
  24. #include "cc.h"
  25. msgbuf_t *
  26. msgbuf_create(isc_mem_t *mctx, unsigned int length)
  27. {
  28. msgbuf_t *mb;
  29. unsigned int len;
  30. len = sizeof(msgbuf_t);
  31. len += length;
  32. mb = isc_mem_get(mctx, len);
  33. RUNTIME_CHECK(mb != NULL);
  34. mb->ref = 1;
  35. mb->r.base = (unsigned char *)(mb + 1);
  36. mb->r.length = length;
  37. return (mb);
  38. }
  39. void
  40. msgbuf_detach(isc_mem_t *mctx, msgbuf_t **mbp)
  41. {
  42. msgbuf_t *mb = *mbp;
  43. unsigned int len;
  44. *mbp = NULL;
  45. INSIST(mb->ref > 0);
  46. mb->ref--;
  47. if (mb->ref == 0) {
  48. len = sizeof(msgbuf_t);
  49. len += mb->r.length;
  50. isc_mem_put(mctx, mb, len);
  51. }
  52. }
  53. msgbuf_t *
  54. msgbuf_attach(msgbuf_t *mb)
  55. {
  56. INSIST(mb->ref > 0);
  57. mb->ref++;
  58. return (mb);
  59. }