aboutsummaryrefslogtreecommitdiffstats
path: root/src/ring.h
blob: 58258107f8dccab7a1973ba43754a10af2847e27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * coopgammad -- Cooperative gamma server
 * Copyright (C) 2016  Mattias Andrée (maandree@kth.se)
 * 
 * This program 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.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stddef.h>



#ifndef GCC_ONLY
# if defined(__GNUC__) && !defined(__clang__)
#  define GCC_ONLY(...)  __VA_ARGS__
# else
#  define GCC_ONLY(...)  /* nothing */
# endif
#endif



/**
 * Ring buffer
 */
struct ring
{
  /**
   * Buffer for the data
   */
  char* restrict buffer;
  
  /**
   * The first set byte in `.buffer`
   */
  size_t start;
  
  /**
   * The last set byte in `.buffer`, plus 1
   */
  size_t end;
  
  /**
   * The size of `.buffer`
   */
  size_t size;
};



/**
 * Initialise a ring buffer
 * 
 * @param  this  The ring buffer
 */
GCC_ONLY(__attribute__((nonnull)))
void ring_initialise(struct ring* restrict this);

/**
 * Release resource allocated to a ring buffer
 * 
 * @param  this  The ring buffer
 */
GCC_ONLY(__attribute__((nonnull)))
void ring_destroy(struct ring* restrict this);

/**
 * Marshal a ring buffer
 * 
 * @param   this  The ring buffer
 * @param   buf   Output buffer for the marshalled data,
 *                `NULL` to only measure how large buffer
 *                is needed
 * @return        The number of marshalled bytes
 */
GCC_ONLY(__attribute__((nonnull(1))))
size_t ring_marshal(const struct ring* restrict this, void* restrict buf);

/**
 * Unmarshal a ring buffer
 * 
 * @param   this  Output parameter for the ring buffer
 * @param   buf   Buffer with the marshalled data
 * @return        The number of unmarshalled bytes, 0 on error
 */
GCC_ONLY(__attribute__((nonnull)))
size_t ring_unmarshal(struct ring* restrict this, const void* restrict buf);

/**
 * Append data to a ring buffer
 * 
 * @param   this  The ring buffer
 * @param   data  The new data
 * @param   n     The number of bytes in `data`
 * @return        Zero on success, -1 on error
 */
GCC_ONLY(__attribute__((nonnull(1))))
int ring_push(struct ring* restrict this, void* restrict data, size_t n);

/**
 * Get queued data from a ring buffer
 * 
 * It can take up to two calls (with `ring_pop` between)
 * to get all queued data
 * 
 * @param   this  The ring buffer
 * @param   n     Output parameter for the length
 *                of the returned segment
 * @return        The beginning of the queued data,
 *                `NULL` if there is nothing more
 */
GCC_ONLY(__attribute__((nonnull)))
void* ring_peek(struct ring* restrict this, size_t* restrict n);

/**
 * Dequeue data from a ring bubber
 * 
 * @param  this  The ring buffer
 * @param  n     The number of bytes to dequeue
 */
GCC_ONLY(__attribute__((nonnull)))
void ring_pop(struct ring* restrict this, size_t n);

/**
 * Check whether there is more data waiting
 * in a ring buffer
 * 
 * @param   this  The ring buffer
 * @return        1 if there is more data, 0 otherwise
 */
GCC_ONLY(__attribute__((nonnull)))
static inline int ring_have_more(struct ring* restrict this)
{
  return this->buffer != NULL;
}