O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
reserve.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* ____ _ _____ ____ _ _ _ _____ _ _ ____ ____ */
3/* / ___| / \ | ___| _ \ / \ | \ | | |_ _| || | | _ \/ ___| */
4/* \___ \ / _ \ | |_ | |_) | / _ \ | \| | | | | || |_| | | \___ \ */
5/* ___) / ___ \| _| | _ < / ___ \| |\ | | | |__ _| |_| |___) | */
6/* |____/_/ \_|_| |_| \_/_/ \_|_| \_| |_| |_| |____/|____/ */
7/* */
14/* ************************************************************************** */
15
16#include "o2s/deque.h"
17
18#include <stdlib.h> // reallocarray
19
25void deque_reserve(deque_t* self, size_t count)
26{
27 size_t room = deque_room(self);
28 if (room < count)
29 {
30 size_t new_capacity = self->capacity + count;
31 self->storage = reallocarray(self->storage, new_capacity, self->type_size);
32 if (self->storage == NULL)
33 new_capacity = 0;
34 self->capacity = new_capacity;
35 }
36}
Realtime double-ended queues.
size_t deque_room(const deque_t *self)
The remaining number of elements that can be added.
Definition internals.c:27
void deque_reserve(deque_t *self, size_t count)
Reallocates the deque if count elements cannot fit in the current allocation.
Definition reserve.c:25
Double-ended queue.
Definition deque.h:27
size_t type_size
Size in bytes of a single element.
Definition deque.h:33
size_t capacity
Maximum number of elements that can be stored.
Definition deque.h:32
void * storage
Memory area.
Definition deque.h:28