4 * Copyright (C) 2018 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
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.
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.
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/>.
20 #ifndef __MONOLITHIUM_VECTOR_H__
21 #define __MONOLITHIUM_VECTOR_H__
26 #ifndef VECTOR_REALLOC
27 extern void *realloc(void*, size_t);
28 #define VECTOR_REALLOC(p, s) realloc((p), (s))
41 } vector_buffer_entry_t;
47 list_entry_t buffer_list;
50 static bool_t vector_insert_buffer(vector_t *vector, size_t position, const vector_buffer_t *buffer)
52 if (!buffer->length) return TRUE;
53 list_entry_t *ptr = &vector->buffer_list;
57 list_entry_t *next = ptr->next;
58 if (next == &vector->buffer_list) return FALSE;
60 vector_buffer_entry_t *entry = CONTAINER_OF(next, vector_buffer_entry_t, link);
61 if (entry->buf.length > position)
63 vector_buffer_entry_t *remaining = VECTOR_REALLOC(NULL, sizeof(vector_buffer_entry_t));
64 if (!remaining) return FALSE;
66 remaining->buf.address = (void*)((uintptr_t)entry->buf.address + position);
67 remaining->buf.length = entry->buf.length - position;
68 entry->buf.length = position;
70 list_put_after(next, &remaining->link);
73 position -= entry->buf.length;
77 vector_buffer_entry_t *entry = VECTOR_REALLOC(NULL, sizeof(vector_buffer_entry_t));
79 list_put_after(ptr, &entry->link);
80 vector->size += buffer->length;
84 static inline void vector_clear(vector_t *vector)
86 vector->position = vector->size = 0;
88 while (vector->buffer_list.next != &vector->buffer_list)
90 list_entry_t *ptr = vector->buffer_list.next;
92 ptr = VECTOR_REALLOC(ptr, 0);
96 static inline bool_t vector_init(vector_t *vector, vector_buffer_t *buffers, size_t num_buffers)
98 vector->position = vector->size = 0;
99 list_init(&vector->buffer_list);
102 for (i = 0; i < num_buffers; i++)
104 if (!vector_insert_buffer(vector, vector->size, &buffers[i]))
106 vector_clear(vector);
114 static inline void vector_read_gather(vector_t *vector, void *data, size_t size)
119 for (ptr = vector->buffer_list.next; ptr != &vector->buffer_list && position < vector->position + size; ptr = ptr->next)
121 vector_buffer_t *buffer = &CONTAINER_OF(ptr, vector_buffer_entry_t, link)->buf;
122 uintptr_t start = position > vector->position ? position : vector->position;
123 uintptr_t end = position + buffer->length < vector->position + size ? position + buffer->length : vector->position + size;
127 __builtin_memcpy((void*)((uintptr_t)data + start - vector->position),
128 (void*)((uintptr_t)buffer->address + start - position),
132 position += buffer->length;
135 vector->position += size;
138 static inline void vector_write_scatter(vector_t *vector, const void *data, size_t size)
143 for (ptr = vector->buffer_list.next; ptr != &vector->buffer_list && position < vector->position + size; ptr = ptr->next)
145 vector_buffer_t *buffer = &CONTAINER_OF(ptr, vector_buffer_entry_t, link)->buf;
146 uintptr_t start = position > vector->position ? position : vector->position;
147 uintptr_t end = position + buffer->length < vector->position + size ? position + buffer->length : vector->position + size;
151 __builtin_memcpy((void*)((uintptr_t)buffer->address + start - position),
152 (void*)((uintptr_t)data + start - vector->position),
156 position += buffer->length;
159 vector->position += size;
162 static inline void vector_seek(vector_t *vector, size_t position)
164 vector->position = position < vector->size ? position : vector->size;