sc/queue/sc_queue.c

152 lines
4.0 KiB
C
Raw Normal View History

2020-11-11 01:19:49 +03:00
/*
* MIT License
*
* Copyright (c) 2020 Ozan Tezcan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "sc_queue.h"
#ifndef SC_SIZE_MAX
2021-01-24 17:56:18 +03:00
#define SC_SIZE_MAX SIZE_MAX
2020-11-11 01:19:49 +03:00
#endif
#define SC_MAX_CAP ((SC_SIZE_MAX - sizeof(struct sc_queue)) / 2ul)
static const struct sc_queue sc_empty = {.cap = 1, .first = 0, .last = 0};
static void *queue_alloc(void *prev, size_t elem_size, size_t *cap)
{
2020-11-29 23:21:25 +03:00
size_t alloc, v = *cap;
2020-11-11 01:19:49 +03:00
if (*cap > SC_MAX_CAP) {
return NULL;
}
// Find next power of two.
v = v < 4 ? 4 : v;
v--;
for (size_t i = 1; i < sizeof(v) * 8; i *= 2) {
v |= v >> i;
}
v++;
*cap = v;
2020-11-29 23:21:25 +03:00
alloc = sizeof(struct sc_queue) + (elem_size * v);
2020-11-11 01:19:49 +03:00
2020-11-29 23:21:25 +03:00
return sc_queue_realloc(prev, alloc);
2020-11-11 01:19:49 +03:00
}
2021-02-01 03:50:16 +03:00
bool sc_queue_init(void *q, size_t elem_size, size_t cap)
2020-11-11 01:19:49 +03:00
{
size_t p = cap;
2021-02-01 03:50:16 +03:00
void **ptr = q;
2020-11-11 01:19:49 +03:00
struct sc_queue *meta;
if (cap == 0) {
2021-02-01 03:50:16 +03:00
*ptr = (void *) sc_empty.elems;
2020-11-11 01:19:49 +03:00
return true;
}
meta = queue_alloc(NULL, elem_size, &p);
if (meta == NULL) {
2021-02-01 03:50:16 +03:00
*ptr = NULL;
2020-11-11 01:19:49 +03:00
return false;
}
meta->cap = p;
meta->first = 0;
meta->last = 0;
2021-02-01 03:50:16 +03:00
*ptr = meta->elems;
2020-11-11 01:19:49 +03:00
return true;
}
2021-02-01 03:50:16 +03:00
void sc_queue_term(void *q)
2020-11-11 01:19:49 +03:00
{
2020-12-28 02:52:22 +03:00
struct sc_queue *meta;
2021-02-01 03:50:16 +03:00
void **ptr = q;
2020-12-28 02:52:22 +03:00
2021-02-01 03:50:16 +03:00
if (*ptr == NULL) {
2020-12-28 02:52:22 +03:00
return;
}
2021-02-01 03:50:16 +03:00
meta = sc_queue_meta(*ptr);
2020-11-11 01:19:49 +03:00
if (meta != &sc_empty) {
sc_queue_free(meta);
}
2021-02-01 03:50:16 +03:00
*ptr = NULL;
2020-11-11 01:19:49 +03:00
}
2021-02-01 03:50:16 +03:00
bool sc_queue_expand(void *q, size_t elem_size)
2020-11-11 01:19:49 +03:00
{
2021-02-01 03:50:16 +03:00
void **ptr = q;
2020-11-11 01:19:49 +03:00
struct sc_queue *tmp;
2021-02-01 03:50:16 +03:00
struct sc_queue *meta = sc_queue_meta(*ptr);
2020-11-11 01:19:49 +03:00
size_t cap, count, size;
size_t pos = (meta->last + 1) & (meta->cap - 1);
uint8_t *e;
if (pos == meta->first) {
if (meta == &sc_empty) {
2021-02-01 03:50:16 +03:00
return sc_queue_init(ptr, elem_size, 4);
2020-11-11 01:19:49 +03:00
}
cap = meta->cap * 2;
tmp = queue_alloc(meta, elem_size, &cap);
if (tmp == NULL) {
return false;
}
/**
* Move items to make empty slots at the end.
* e.g :
* last first
* | |
* Step 0 : | 2 | 3 | - | 1 | // tmp->cap : 4
* Step 1 : | 2 | 3 | - | 1 | - | - | - | - | // realloc
2021-01-24 17:56:18 +03:00
* Step 2 : | 2 | 3 | - | 1 | 1 | - | - | - | // memcpy
2020-11-11 01:19:49 +03:00
* Step 3 : | 2 | 2 | 3 | 1 | 1 | - | - | - | // memmove
2021-01-24 17:56:18 +03:00
* Step 4 : | 1 | 2 | 3 | 1 | 1 | - | - | - | // memcpy
2020-11-11 01:19:49 +03:00
* Step 5 : | 1 | 2 | 3 | - | - | - | - | - | // tmp->last = cap - 1;
* | |
* first last
*
*/
e = tmp->elems;
count = tmp->cap - tmp->first;
size = elem_size;
memcpy(e + (size * tmp->cap), e + (size * tmp->first), count * size);
memmove(e + (count * size), e, tmp->first * size);
memcpy(e, e + (size * tmp->cap), count * size);
tmp->last = tmp->cap - 1;
tmp->first = 0;
tmp->cap = cap;
2021-02-01 03:50:16 +03:00
*ptr = tmp->elems;
2020-11-11 01:19:49 +03:00
}
return true;
}