vector.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * vector.h
  3. *
  4. * Copyright (C) 2018 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __MONOLITHIUM_VECTOR_H__
  20. #define __MONOLITHIUM_VECTOR_H__
  21. #include "defs.h"
  22. #include "list.h"
  23. #ifndef VECTOR_REALLOC
  24. extern void *realloc(void*, size_t);
  25. #define VECTOR_REALLOC(p, s) realloc((p), (s))
  26. #endif
  27. typedef struct
  28. {
  29. void *address;
  30. size_t length;
  31. } vector_buffer_t;
  32. typedef struct
  33. {
  34. list_entry_t link;
  35. vector_buffer_t buf;
  36. } vector_buffer_entry_t;
  37. typedef struct
  38. {
  39. size_t position;
  40. size_t size;
  41. list_entry_t buffer_list;
  42. } vector_t;
  43. static bool_t vector_insert_buffer(vector_t *vector, size_t position, const vector_buffer_t *buffer)
  44. {
  45. if (!buffer->length) return TRUE;
  46. list_entry_t *ptr = &vector->buffer_list;
  47. while (position)
  48. {
  49. list_entry_t *next = ptr->next;
  50. if (next == &vector->buffer_list) return FALSE;
  51. vector_buffer_entry_t *entry = CONTAINER_OF(next, vector_buffer_entry_t, link);
  52. if (entry->buf.length > position)
  53. {
  54. vector_buffer_entry_t *remaining = VECTOR_REALLOC(NULL, sizeof(vector_buffer_entry_t));
  55. if (!remaining) return FALSE;
  56. remaining->buf.address = (void*)((uintptr_t)entry->buf.address + position);
  57. remaining->buf.length = entry->buf.length - position;
  58. entry->buf.length = position;
  59. list_put_after(next, &remaining->link);
  60. }
  61. position -= entry->buf.length;
  62. ptr = next;
  63. }
  64. vector_buffer_entry_t *entry = VECTOR_REALLOC(NULL, sizeof(vector_buffer_entry_t));
  65. entry->buf = *buffer;
  66. list_put_after(ptr, &entry->link);
  67. vector->size += buffer->length;
  68. return TRUE;
  69. }
  70. static inline void vector_clear(vector_t *vector)
  71. {
  72. vector->position = vector->size = 0;
  73. while (vector->buffer_list.next != &vector->buffer_list)
  74. {
  75. list_entry_t *ptr = vector->buffer_list.next;
  76. list_remove(ptr);
  77. ptr = VECTOR_REALLOC(ptr, 0);
  78. }
  79. }
  80. static inline bool_t vector_init(vector_t *vector, vector_buffer_t *buffers, size_t num_buffers)
  81. {
  82. vector->position = vector->size = 0;
  83. list_init(&vector->buffer_list);
  84. int i;
  85. for (i = 0; i < num_buffers; i++)
  86. {
  87. if (!vector_insert_buffer(vector, vector->size, &buffers[i]))
  88. {
  89. vector_clear(vector);
  90. return FALSE;
  91. }
  92. }
  93. return TRUE;
  94. }
  95. static inline void vector_read_gather(vector_t *vector, void *data, size_t size)
  96. {
  97. size_t position = 0;
  98. list_entry_t *ptr;
  99. for (ptr = vector->buffer_list.next; ptr != &vector->buffer_list && position < vector->position + size; ptr = ptr->next)
  100. {
  101. vector_buffer_t *buffer = &CONTAINER_OF(ptr, vector_buffer_entry_t, link)->buf;
  102. uintptr_t start = position > vector->position ? position : vector->position;
  103. uintptr_t end = position + buffer->length < vector->position + size ? position + buffer->length : vector->position + size;
  104. if (start < end)
  105. {
  106. __builtin_memcpy((void*)((uintptr_t)data + start - vector->position),
  107. (void*)((uintptr_t)buffer->address + start - position),
  108. end - start);
  109. }
  110. position += buffer->length;
  111. }
  112. vector->position += size;
  113. }
  114. static inline void vector_write_scatter(vector_t *vector, const void *data, size_t size)
  115. {
  116. size_t position = 0;
  117. list_entry_t *ptr;
  118. for (ptr = vector->buffer_list.next; ptr != &vector->buffer_list && position < vector->position + size; ptr = ptr->next)
  119. {
  120. vector_buffer_t *buffer = &CONTAINER_OF(ptr, vector_buffer_entry_t, link)->buf;
  121. uintptr_t start = position > vector->position ? position : vector->position;
  122. uintptr_t end = position + buffer->length < vector->position + size ? position + buffer->length : vector->position + size;
  123. if (start < end)
  124. {
  125. __builtin_memcpy((void*)((uintptr_t)buffer->address + start - position),
  126. (void*)((uintptr_t)data + start - vector->position),
  127. end - start);
  128. }
  129. position += buffer->length;
  130. }
  131. vector->position += size;
  132. }
  133. static inline void vector_seek(vector_t *vector, size_t position)
  134. {
  135. vector->position = position < vector->size ? position : vector->size;
  136. }
  137. #endif