dynar.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <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. void
  50. dynar_destroy(struct dynar *array)
  51. {
  52. free(array->data);
  53. dynar_init(array, array->maximum_size);
  54. }
  55. void
  56. dynar_clean(struct dynar *array)
  57. {
  58. array->size = 0;
  59. }
  60. size_t
  61. dynar_size(const struct dynar *array)
  62. {
  63. return (array->size);
  64. }
  65. size_t
  66. dynar_max_size(const struct dynar *array)
  67. {
  68. return (array->maximum_size);
  69. }
  70. char *
  71. dynar_data(const struct dynar *array)
  72. {
  73. return (array->data);
  74. }
  75. static int
  76. dynar_realloc(struct dynar *array, size_t new_array_size)
  77. {
  78. char *new_data;
  79. if (new_array_size > array->maximum_size) {
  80. return (-1);
  81. }
  82. new_data = realloc(array->data, new_array_size);
  83. if (new_data == NULL) {
  84. return (-1);
  85. }
  86. array->allocated = new_array_size;
  87. array->data = new_data;
  88. return (0);
  89. }
  90. int
  91. dynar_prealloc(struct dynar *array, size_t size)
  92. {
  93. size_t new_size;
  94. if (array->size + size > array->maximum_size) {
  95. return (-1);
  96. }
  97. if (array->size + size > array->allocated) {
  98. new_size = (array->allocated + size) * 2;
  99. if (new_size > array->maximum_size) {
  100. new_size = array->maximum_size;
  101. }
  102. if (dynar_realloc(array, new_size) == -1) {
  103. return (-1);
  104. }
  105. }
  106. return (0);
  107. }
  108. int
  109. dynar_cat(struct dynar *array, const void *src, size_t size)
  110. {
  111. if (dynar_prealloc(array, size) != 0) {
  112. return (-1);
  113. }
  114. memmove(array->data + array->size, src, size);
  115. array->size += size;
  116. return (0);
  117. }
  118. int
  119. dynar_prepend(struct dynar *array, const void *src, size_t size)
  120. {
  121. if (dynar_prealloc(array, size) != 0) {
  122. return (-1);
  123. }
  124. memmove(array->data + size, array->data, array->size);
  125. memmove(array->data, src, size);
  126. array->size += size;
  127. return (0);
  128. }