4
0

send-buffer-list.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include <assert.h>
  37. #include "send-buffer-list.h"
  38. void
  39. send_buffer_list_init(struct send_buffer_list *sblist, size_t max_list_entries,
  40. size_t max_buffer_size)
  41. {
  42. memset(sblist, 0, sizeof(*sblist));
  43. sblist->max_list_entries = max_list_entries;
  44. sblist->allocated_list_entries = 0;
  45. sblist->max_buffer_size = max_buffer_size;
  46. TAILQ_INIT(&sblist->list);
  47. TAILQ_INIT(&sblist->free_list);
  48. }
  49. struct send_buffer_list_entry *
  50. send_buffer_list_get_new(struct send_buffer_list *sblist)
  51. {
  52. struct send_buffer_list_entry *entry;
  53. if (!TAILQ_EMPTY(&sblist->free_list)) {
  54. /*
  55. * Use free list entry
  56. */
  57. entry = TAILQ_FIRST(&sblist->free_list);
  58. TAILQ_REMOVE(&sblist->free_list, entry, entries);
  59. dynar_clean(&entry->buffer);
  60. dynar_set_max_size(&entry->buffer, sblist->max_buffer_size);
  61. } else {
  62. if (sblist->allocated_list_entries + 1 > sblist->max_list_entries) {
  63. return (NULL);
  64. }
  65. sblist->allocated_list_entries++;
  66. /*
  67. * Alloc new entry
  68. */
  69. entry = malloc(sizeof(*entry));
  70. if (entry == NULL) {
  71. return (NULL);
  72. }
  73. dynar_init(&entry->buffer, sblist->max_buffer_size);
  74. }
  75. entry->msg_already_sent_bytes = 0;
  76. return (entry);
  77. }
  78. void
  79. send_buffer_list_put(struct send_buffer_list *sblist, struct send_buffer_list_entry *sblist_entry)
  80. {
  81. TAILQ_INSERT_TAIL(&sblist->list, sblist_entry, entries);
  82. }
  83. void
  84. send_buffer_list_discard_new(struct send_buffer_list *sblist, struct send_buffer_list_entry *sblist_entry)
  85. {
  86. TAILQ_INSERT_HEAD(&sblist->free_list, sblist_entry, entries);
  87. }
  88. struct send_buffer_list_entry *
  89. send_buffer_list_get_active(const struct send_buffer_list *sblist)
  90. {
  91. struct send_buffer_list_entry *entry;
  92. entry = TAILQ_FIRST(&sblist->list);
  93. return (entry);
  94. }
  95. void
  96. send_buffer_list_delete(struct send_buffer_list *sblist,
  97. struct send_buffer_list_entry *sblist_entry)
  98. {
  99. /*
  100. * Move item to free list
  101. */
  102. TAILQ_REMOVE(&sblist->list, sblist_entry, entries);
  103. TAILQ_INSERT_HEAD(&sblist->free_list, sblist_entry, entries);
  104. }
  105. int
  106. send_buffer_list_empty(const struct send_buffer_list *sblist)
  107. {
  108. return (TAILQ_EMPTY(&sblist->list));
  109. }
  110. void
  111. send_buffer_list_free(struct send_buffer_list *sblist)
  112. {
  113. struct send_buffer_list_entry *entry;
  114. struct send_buffer_list_entry *entry_next;
  115. entry = TAILQ_FIRST(&sblist->list);
  116. while (entry != NULL) {
  117. entry_next = TAILQ_NEXT(entry, entries);
  118. dynar_destroy(&entry->buffer);
  119. free(entry);
  120. entry = entry_next;
  121. }
  122. entry = TAILQ_FIRST(&sblist->free_list);
  123. while (entry != NULL) {
  124. entry_next = TAILQ_NEXT(entry, entries);
  125. dynar_destroy(&entry->buffer);
  126. free(entry);
  127. entry = entry_next;
  128. }
  129. sblist->allocated_list_entries = 0;
  130. TAILQ_INIT(&sblist->list);
  131. TAILQ_INIT(&sblist->free_list);
  132. }
  133. void
  134. send_buffer_list_set_max_buffer_size(struct send_buffer_list *sblist, size_t max_buffer_size)
  135. {
  136. sblist->max_buffer_size = max_buffer_size;
  137. }
  138. void
  139. send_buffer_list_set_max_list_entries(struct send_buffer_list *sblist, size_t max_list_entries)
  140. {
  141. sblist->max_list_entries = max_list_entries;
  142. }