-
Notifications
You must be signed in to change notification settings - Fork 1
/
tinypipe.h
136 lines (118 loc) · 3.88 KB
/
tinypipe.h
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
/**
* Copyright (c) 2018 Martin Roth ([email protected]).
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _TINYPIPE_H_
#define _TINYPIPE_H_
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* This pipe assumes that there is only one producer thread and one consumer
* thread. This data structure does not support any other configuration.
*/
typedef struct TinyPipe {
char *buffer;
char *writeHead;
char *readHead;
int32_t len;
int32_t remainingBytes; // total bytes from write head to end
} TinyPipe;
/**
* Initialise the pipe with a given length, in bytes.
*
* @param q The pipe.
*
* @return Returns the size of the pipe in bytes.
*/
int tpipe_init(TinyPipe *q, int numBytes);
/**
* Frees the internal buffer.
*
* @param q The light pipe.
*/
void tpipe_free(TinyPipe *q);
/**
* Indicates if data is available for reading.
*
* @param q The light pipe.
* @return Returns the number of bytes available for reading. Zero if no bytes are available.
*/
int tpipe_hasData(TinyPipe *q);
/**
* Returns a pointer to a location in the pipe where numBytes can be written.
*
* @param q The pipe.
* @param numBytes The number of bytes to be written.
* @return A pointer to a location where those bytes can be written. Returns
* NULL if no more space is available. Successive calls to this
* function may eventually return a valid pointer because the readhead
* has been advanced on another thread.
*/
char *tpipe_getWriteBuffer(TinyPipe *q, int numBytes);
/**
* Indicates to the pipe how many bytes have been written.
*
* @param q The pipe.
* @param numBytes The number of bytes written. In general this should be the
* same value as was passed to the preceeding call to
* tpipe_getWriteBuffer().
*/
void tpipe_produce(TinyPipe *q, int numBytes);
/**
* Returns the current read buffer, indicating the number of bytes available
* for reading.
*
* @param q The pipe.
* @param numBytes This value will be filled with the number of bytes available
* for reading.
*
* @return A pointer to the read buffer.
*/
char *tpipe_getReadBuffer(TinyPipe *q, int *numBytes);
/**
* Indicates that the next set of bytes have been read and are no longer needed.
*
* @param q The pipe.
*/
void tpipe_consume(TinyPipe *q);
/**
* Resets the queue to the initialised state. This should be done when only one thread is accessing the pipe.
*
* @param q The pipe.
*/
void tpipe_clear(TinyPipe *q);
/**
* Returns the total amount of data that is currently in the pipe.
*
* @param q The pipe.
*
* @return The number of bytes ready to be read from the pipe.
*/
int tpipe_getTotalData(TinyPipe *q);
/**
* A convenience function to write a number of bytes to the pipe;
*
* @param q The pipe.
* @param data The data pointer.
* @param numBytes The number of bytes to write.
*
* @return 1 if bytes were successfully written to the pipe. 0 otherwise.
*/
int tpipe_write(TinyPipe *q, char *data, int numBytes);
#ifdef __cplusplus
}
#endif
#endif // _TINYPIPE_H_