NCDBuf.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * @file NCDBuf.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "NCDBuf.h"
  30. #include <misc/balloc.h>
  31. #include <misc/offset.h>
  32. #include <misc/debug.h>
  33. static void ref_target_func_release (BRefTarget *ref_target)
  34. {
  35. NCDBuf *o = UPPER_OBJECT(ref_target, NCDBuf, ref_target);
  36. NCDBufStore *store = o->store;
  37. if (store) {
  38. LinkedList0_Remove(&store->used_bufs_list, &o->list_node);
  39. LinkedList0_Prepend(&store->free_bufs_list, &o->list_node);
  40. } else {
  41. BFree(o);
  42. }
  43. }
  44. void NCDBufStore_Init (NCDBufStore *o, size_t buf_size)
  45. {
  46. o->buf_size = buf_size;
  47. LinkedList0_Init(&o->used_bufs_list);
  48. LinkedList0_Init(&o->free_bufs_list);
  49. DebugObject_Init(&o->d_obj);
  50. }
  51. void NCDBufStore_Free (NCDBufStore *o)
  52. {
  53. DebugObject_Free(&o->d_obj);
  54. LinkedList0Node *ln;
  55. ln = LinkedList0_GetFirst(&o->used_bufs_list);
  56. while (ln) {
  57. NCDBuf *buf = UPPER_OBJECT(ln, NCDBuf, list_node);
  58. ASSERT(buf->store == o)
  59. buf->store = NULL;
  60. ln = LinkedList0Node_Next(ln);
  61. }
  62. ln = LinkedList0_GetFirst(&o->free_bufs_list);
  63. while (ln) {
  64. LinkedList0Node *next_ln = LinkedList0Node_Next(ln);
  65. NCDBuf *buf = UPPER_OBJECT(ln, NCDBuf, list_node);
  66. ASSERT(buf->store == o)
  67. BFree(buf);
  68. ln = next_ln;
  69. }
  70. }
  71. size_t NCDBufStore_BufSize (NCDBufStore *o)
  72. {
  73. DebugObject_Access(&o->d_obj);
  74. return o->buf_size;
  75. }
  76. NCDBuf * NCDBufStore_GetBuf (NCDBufStore *o)
  77. {
  78. DebugObject_Access(&o->d_obj);
  79. NCDBuf *buf;
  80. LinkedList0Node *ln = LinkedList0_GetFirst(&o->free_bufs_list);
  81. if (ln) {
  82. buf = UPPER_OBJECT(ln, NCDBuf, list_node);
  83. ASSERT(buf->store == o)
  84. LinkedList0_Remove(&o->free_bufs_list, &buf->list_node);
  85. } else {
  86. bsize_t size = bsize_add(bsize_fromsize(sizeof(NCDBuf)), bsize_fromsize(o->buf_size));
  87. buf = BAllocSize(size);
  88. if (!buf) {
  89. return NULL;
  90. }
  91. buf->store = o;
  92. }
  93. LinkedList0_Prepend(&o->used_bufs_list, &buf->list_node);
  94. BRefTarget_Init(&buf->ref_target, ref_target_func_release);
  95. return buf;
  96. }
  97. BRefTarget * NCDBuf_RefTarget (NCDBuf *o)
  98. {
  99. return &o->ref_target;
  100. }
  101. char * NCDBuf_Data (NCDBuf *o)
  102. {
  103. return o->data;
  104. }