Branch data MC/DC data Line data Source code
1 : : : #include <stdint.h>
2 : : : #include <string.h>
3 : : :
4 : : : #include "common.h"
5 : : :
6 : : : #include "safe_ring_buffer.h"
7 : : :
8 : : 211 : static size_t get_offset_bytes(const srb_ctx_t *ctx, size_t index) {
9 [ + - ]: [ T f ]: 211 : w_assert(ctx);
10 : : :
11 [ + - ]: [ t F ]: 211 : if (index >= ctx->max_elements) {
12 : : : return 0;
13 : : : }
14 : : 211 : return index * (ctx->element_size);
15 : : : }
16 : : :
17 : : 4 : void srb_init(srb_ctx_t *ctx, void *pool, size_t pool_size, size_t element_size) {
18 [ + + ]: [ T F ]: 4 : w_assert(ctx);
19 [ + + ]: [ T F ]: 2 : w_assert(pool);
20 : : :
21 : : 1 : ctx->memory_pool = pool;
22 : : 1 : ctx->element_size = element_size;
23 : : 1 : ctx->max_elements = (pool_size / (element_size));
24 : : 1 : ctx->rd_idx = 0;
25 : : 1 : ctx->wr_idx = 0;
26 : : 1 : }
27 : : :
28 : : 110 : bool srb_push(srb_ctx_t *ctx, const void *element) {
29 [ + + ]: [ T F ]: 110 : w_assert(ctx);
30 [ + + ]: [ T F ]: 108 : w_assert(element);
31 : : :
32 [ + - ]: [ t F ]: 107 : if (srb_is_full(ctx)) {
33 : : : return false;
34 : : : }
35 : : 107 : size_t offset = get_offset_bytes(ctx, ctx->wr_idx);
36 : : 107 : memcpy(((uint8_t *)ctx->memory_pool) + offset, element, ctx->element_size);
37 [ + + ]: [ T F ]: 107 : if (++(ctx->wr_idx) >= ctx->max_elements) {
38 : : 1 : ctx->wr_idx = 0;
39 : : : }
40 : : : return true;
41 : : : }
42 : : :
43 : : 215 : bool srb_is_full(const srb_ctx_t *ctx) {
44 [ + + ]: [ T F ]: 215 : w_assert(ctx);
45 : : :
46 [ + - ]:[ t F t F : 214 : if ((ctx->wr_idx + 1 == ctx->rd_idx) ||
t F ]
47 [ + + + - ]: : 214 : (ctx->wr_idx + 1 == ctx->max_elements && ctx->rd_idx == 0)) {
48 : : : return true;
49 : : : } else {
50 : : 214 : return false;
51 : : : }
52 : : : }
53 : : :
54 : : 1105 : bool srb_is_empty(const srb_ctx_t *ctx) {
55 [ + + ]: [ T F ]: 1105 : w_assert(ctx);
56 : : :
57 [ + + ]: [ T F ]: 1104 : if (ctx->wr_idx == ctx->rd_idx) {
58 : : : return true;
59 : : : } else {
60 : : 554 : return false;
61 : : : }
62 : : : }
63 : : :
64 : : 107 : bool srb_pop(srb_ctx_t *ctx, void *element) {
65 [ + + ]: [ T F ]: 107 : w_assert(ctx);
66 [ + + ]: [ T F ]: 105 : w_assert(element);
67 : : :
68 [ + - ]: [ t F ]: 104 : if (srb_is_empty(ctx)) {
69 : : : return false;
70 : : : }
71 : : 104 : size_t offset = get_offset_bytes(ctx, ctx->rd_idx);
72 : : 104 : memcpy(element, ((uint8_t *)ctx->memory_pool) + offset, ctx->element_size);
73 [ + + ]: [ T F ]: 104 : if (++(ctx->rd_idx) >= ctx->max_elements) {
74 : : 1 : ctx->rd_idx = 0;
75 : : : }
76 : : : return true;
77 : : : }
78 : : :
79 : : 3 : bool srb_peek(const srb_ctx_t *ctx, void *element) {
80 [ + + ]: [ T F ]: 3 : w_assert(ctx);
81 [ - + ]: [ t F ]: 1 : w_assert(element);
82 : : :
83 [ # # ]: [ t f ]: 0 : if (srb_is_empty(ctx)) {
84 : : : return false;
85 : : : }
86 : : 0 : size_t offset = get_offset_bytes(ctx, ctx->rd_idx);
87 : : 0 : memcpy(element, ((uint8_t *)ctx->memory_pool) + offset, ctx->element_size);
88 : : 0 : return true;
89 : : : }
|