O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
constructors_destructors.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* ____ _ _____ ____ _ _ _ _____ _ _ ____ ____ */
3/* / ___| / \ | ___| _ \ / \ | \ | | |_ _| || | | _ \/ ___| */
4/* \___ \ / _ \ | |_ | |_) | / _ \ | \| | | | | || |_| | | \___ \ */
5/* ___) / ___ \| _| | _ < / ___ \| |\ | | | |__ _| |_| |___) | */
6/* |____/_/ \_|_| |_| \_/_/ \_|_| \_| |_| |_| |____/|____/ */
7/* */
15/* ************************************************************************** */
16
17#include "o2s/deque.h"
18
19#include <stdlib.h> //free
20
22deque_t deque_new(void* storage, size_t capacity, size_t type_size)
23{
24 return (deque_t){.storage = storage,
25 .front = storage,
26 .back = storage,
27 .count = 0,
28 .type_size = type_size,
29 .capacity = capacity};
30}
31
36deque_t deque_allocate(size_t capacity, size_t type_size)
37{
38 void* storage = calloc(capacity, type_size);
39
40 if (storage == NULL)
41 capacity = 0;
42 return deque_new(storage, capacity, type_size);
43}
44
47{
48 self->front = self->storage;
49 self->back = self->storage;
50 self->count = 0;
51}
52
55{
56 if (self->storage != NULL)
57 free(self->storage);
58 self->storage = NULL;
59 deque_clear(self);
60 self->capacity = 0;
61}
void deque_free(deque_t *self)
Frees properly the deque.
void deque_clear(deque_t *self)
Clears properly the deque.
deque_t deque_new(void *storage, size_t capacity, size_t type_size)
Contructs an deque, using externally owned memory.
deque_t deque_allocate(size_t capacity, size_t type_size)
Constructs a deque, allocating the needed memory.
Realtime double-ended queues.
Double-ended queue.
Definition deque.h:27
void * back
Pointer past-the-end of the queue.
Definition deque.h:30
void * front
Pointer to the front of the queue.
Definition deque.h:29
size_t capacity
Maximum number of elements that can be stored.
Definition deque.h:32
size_t count
Number of elements currently stored.
Definition deque.h:31
void * storage
Memory area.
Definition deque.h:28