-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First implementation of a ring buffer (FIFO, queue-like) DS
- Loading branch information
1 parent
6f8d62e
commit 73df131
Showing
5 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* GNU Sparky --- A 5v5 character-based libre tactical shooter | ||
* Copyright (C) 2024 Wasym A. Alonso | ||
* | ||
* This file is part of Sparky. | ||
* | ||
* Sparky is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Sparky is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Sparky. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
|
||
#pragma once | ||
|
||
#include <sk_defines.h> | ||
|
||
typedef struct { | ||
u32 capacity; | ||
u32 element_size; | ||
u32 curr_len; | ||
i32 head; | ||
i32 tail; | ||
void *data; | ||
} sk_rngbuf; | ||
|
||
sk_rngbuf sk_rngbuf_create(u32 capacity, u32 element_size); | ||
|
||
void sk_rngbuf_destroy(sk_rngbuf *rb); | ||
|
||
u8 sk_rngbuf_push(sk_rngbuf *rb, void *value); | ||
|
||
u8 sk_rngbuf_pop(sk_rngbuf *rb, void *out_value); | ||
|
||
u8 sk_rngbuf_peek(const sk_rngbuf *rb, void *out_value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* GNU Sparky --- A 5v5 character-based libre tactical shooter | ||
* Copyright (C) 2024 Wasym A. Alonso | ||
* | ||
* This file is part of Sparky. | ||
* | ||
* Sparky is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Sparky is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Sparky. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
|
||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sk_log.h> | ||
#include <sk_rngbuf.h> | ||
|
||
sk_rngbuf sk_rngbuf_create(u32 capacity, u32 element_size) { | ||
return (sk_rngbuf) { | ||
.capacity = capacity, | ||
.element_size = element_size, | ||
.curr_len = 0, | ||
.head = 0, | ||
.tail = -1, | ||
.data = malloc(capacity * element_size) | ||
}; | ||
} | ||
|
||
void sk_rngbuf_destroy(sk_rngbuf *rb) { | ||
if (!rb) { | ||
SK_LOG_WARN("sk_rngbuf_destroy :: `rb` is not a valid pointer, skipping destruction"); | ||
return; | ||
} | ||
*rb = (sk_rngbuf) {0}; | ||
rb = 0; | ||
} | ||
|
||
u8 sk_rngbuf_push(sk_rngbuf *rb, void *value) { | ||
if (!rb || !value) { | ||
SK_LOG_ERROR("sk_rngbuf_push :: `rb` and `value` need to be valid pointers"); | ||
return 0; | ||
} | ||
if (rb->curr_len == rb->capacity) { | ||
SK_LOG_ERROR("sk_rngbuf_push :: ring buffer is full (%p)", rb); | ||
return 0; | ||
} | ||
rb->tail = (rb->tail + 1) % rb->capacity; | ||
memcpy((void *) ((i32 *) rb->data + (rb->tail * rb->element_size)), value, rb->element_size); | ||
++rb->curr_len; | ||
return 1; | ||
} | ||
|
||
u8 sk_rngbuf_pop(sk_rngbuf *rb, void *out_value) { | ||
if (!rb || !out_value) { | ||
SK_LOG_ERROR("sk_rngbuf_pop :: `rb` and `out_value` need to be valid pointers"); | ||
return 0; | ||
} | ||
if (rb->curr_len == 0) { | ||
SK_LOG_ERROR("sk_rngbuf_pop :: ring buffer is empty (%p)", rb); | ||
return 0; | ||
} | ||
memcpy(out_value, (void *) ((i32 *) rb->data + (rb->head * rb->element_size)), rb->element_size); | ||
rb->head = (rb->head + 1) % rb->capacity; | ||
--rb->curr_len; | ||
return 1; | ||
} | ||
|
||
u8 sk_rngbuf_peek(const sk_rngbuf *rb, void *out_value) { | ||
if (!rb || !out_value) { | ||
SK_LOG_ERROR("sk_rngbuf_peek :: `rb` and `out_value` need to be valid pointers"); | ||
return 0; | ||
} | ||
if (rb->curr_len == 0) { | ||
SK_LOG_ERROR("sk_rngbuf_peek :: ring buffer is empty (%p)", rb); | ||
return 0; | ||
} | ||
memcpy(out_value, (void *) ((i32 *) rb->data + (rb->head * rb->element_size)), rb->element_size); | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters