pr-poll-array.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <sys/types.h>
  35. #include <arpa/inet.h>
  36. #include <inttypes.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include "pr-poll-array.h"
  40. void
  41. pr_poll_array_init(struct pr_poll_array *poll_array, size_t user_data_size)
  42. {
  43. memset(poll_array, 0, sizeof(*poll_array));
  44. poll_array->user_data_size = user_data_size;
  45. }
  46. void
  47. pr_poll_array_destroy(struct pr_poll_array *poll_array)
  48. {
  49. free(poll_array->array);
  50. free(poll_array->user_data_array);
  51. pr_poll_array_init(poll_array, poll_array->user_data_size);
  52. }
  53. void
  54. pr_poll_array_clean(struct pr_poll_array *poll_array)
  55. {
  56. poll_array->items = 0;
  57. }
  58. static int
  59. pr_poll_array_realloc(struct pr_poll_array *poll_array,
  60. ssize_t new_array_size)
  61. {
  62. PRPollDesc *new_array;
  63. char *new_user_data_array;
  64. new_array = realloc(poll_array->array,
  65. sizeof(PRPollDesc) * new_array_size);
  66. if (new_array == NULL) {
  67. return (-1);
  68. }
  69. poll_array->allocated = new_array_size;
  70. poll_array->array = new_array;
  71. if (poll_array->user_data_size > 0) {
  72. new_user_data_array = realloc(poll_array->user_data_array,
  73. poll_array->user_data_size * new_array_size);
  74. if (new_user_data_array == NULL) {
  75. return (-1);
  76. }
  77. poll_array->user_data_array = new_user_data_array;
  78. }
  79. return (0);
  80. }
  81. ssize_t
  82. pr_poll_array_size(struct pr_poll_array *poll_array)
  83. {
  84. return (poll_array->items);
  85. }
  86. ssize_t
  87. pr_poll_array_add(struct pr_poll_array *poll_array, PRPollDesc **pfds, void **user_data)
  88. {
  89. if (pr_poll_array_size(poll_array) >= poll_array->allocated) {
  90. if (pr_poll_array_realloc(poll_array, (poll_array->allocated * 2) + 1)) {
  91. return (-1);
  92. }
  93. }
  94. *pfds = &poll_array->array[pr_poll_array_size(poll_array)];
  95. memset(*pfds, 0, sizeof(**pfds));
  96. *user_data = poll_array->user_data_array + (poll_array->items * poll_array->user_data_size);
  97. memset(*user_data, 0, poll_array->user_data_size);
  98. poll_array->items++;
  99. return (poll_array->items - 1);
  100. }
  101. void
  102. pr_poll_array_gc(struct pr_poll_array *poll_array)
  103. {
  104. if (poll_array->allocated > (pr_poll_array_size(poll_array) * 3) + 1) {
  105. pr_poll_array_realloc(poll_array, (pr_poll_array_size(poll_array) * 2) + 1);
  106. }
  107. }
  108. PRPollDesc *
  109. pr_poll_array_get(const struct pr_poll_array *poll_array, ssize_t pos)
  110. {
  111. if (pos >= poll_array->items) {
  112. return (NULL);
  113. }
  114. return (&poll_array->array[pos]);
  115. }
  116. void *
  117. pr_poll_array_get_user_data(const struct pr_poll_array *poll_array, ssize_t pos)
  118. {
  119. if (pos >= poll_array->items) {
  120. return (NULL);
  121. }
  122. return (poll_array->user_data_array + (pos * poll_array->user_data_size));
  123. }