dynar.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2015-2017 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 <stdlib.h>
  36. #include <string.h>
  37. #include "dynar.h"
  38. void
  39. dynar_init(struct dynar *array, size_t maximum_size)
  40. {
  41. memset(array, 0, sizeof(*array));
  42. array->maximum_size = maximum_size;
  43. }
  44. void
  45. dynar_set_max_size(struct dynar *array, size_t maximum_size)
  46. {
  47. array->maximum_size = maximum_size;
  48. }
  49. int
  50. dynar_set_size(struct dynar *array, size_t size)
  51. {
  52. if (size > dynar_max_size(array)) {
  53. dynar_set_max_size(array, size);
  54. }
  55. if (size > dynar_size(array)) {
  56. if (dynar_prealloc(array, size - dynar_size(array)) == -1) {
  57. return (-1);
  58. }
  59. }
  60. array->size = size;
  61. return (0);
  62. }
  63. void
  64. dynar_destroy(struct dynar *array)
  65. {
  66. free(array->data);
  67. dynar_init(array, array->maximum_size);
  68. }
  69. void
  70. dynar_clean(struct dynar *array)
  71. {
  72. array->size = 0;
  73. }
  74. size_t
  75. dynar_size(const struct dynar *array)
  76. {
  77. return (array->size);
  78. }
  79. size_t
  80. dynar_max_size(const struct dynar *array)
  81. {
  82. return (array->maximum_size);
  83. }
  84. char *
  85. dynar_data(const struct dynar *array)
  86. {
  87. return (array->data);
  88. }
  89. static int
  90. dynar_realloc(struct dynar *array, size_t new_array_size)
  91. {
  92. char *new_data;
  93. if (new_array_size > array->maximum_size) {
  94. return (-1);
  95. }
  96. new_data = realloc(array->data, new_array_size);
  97. if (new_data == NULL) {
  98. return (-1);
  99. }
  100. array->allocated = new_array_size;
  101. array->data = new_data;
  102. return (0);
  103. }
  104. int
  105. dynar_prealloc(struct dynar *array, size_t size)
  106. {
  107. size_t new_size;
  108. if (array->size + size > array->maximum_size) {
  109. return (-1);
  110. }
  111. if (array->size + size > array->allocated) {
  112. new_size = (array->allocated + size) * 2;
  113. if (new_size > array->maximum_size) {
  114. new_size = array->maximum_size;
  115. }
  116. if (dynar_realloc(array, new_size) == -1) {
  117. return (-1);
  118. }
  119. }
  120. return (0);
  121. }
  122. int
  123. dynar_cat(struct dynar *array, const void *src, size_t size)
  124. {
  125. if (dynar_prealloc(array, size) != 0) {
  126. return (-1);
  127. }
  128. memmove(array->data + array->size, src, size);
  129. array->size += size;
  130. return (0);
  131. }
  132. int
  133. dynar_prepend(struct dynar *array, const void *src, size_t size)
  134. {
  135. if (dynar_prealloc(array, size) != 0) {
  136. return (-1);
  137. }
  138. memmove(array->data + size, array->data, array->size);
  139. memmove(array->data, src, size);
  140. array->size += size;
  141. return (0);
  142. }