xref: /rk3399_rockchip-uboot/include/membuff.h (revision b7b65090b29e6bc133c7922f8290c1e2f75b064a)
1*b7b65090SSimon Glass /*
2*b7b65090SSimon Glass  * Copyright (c) 2015 Google, Inc
3*b7b65090SSimon Glass  * Written by Simon Glass <sjg@chromium.org>
4*b7b65090SSimon Glass  *
5*b7b65090SSimon Glass  * Copyright (c) 1992 Simon Glass
6*b7b65090SSimon Glass  *
7*b7b65090SSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
8*b7b65090SSimon Glass  */
9*b7b65090SSimon Glass 
10*b7b65090SSimon Glass #ifndef _MEMBUFF_H
11*b7b65090SSimon Glass #define _MEMBUFF_H
12*b7b65090SSimon Glass 
13*b7b65090SSimon Glass /**
14*b7b65090SSimon Glass  * @struct membuff: holds the state of a membuff - it is used for input and
15*b7b65090SSimon Glass  * output buffers. The buffer extends from @start to (@start + @size - 1).
16*b7b65090SSimon Glass  * Data in the buffer extends from @tail to @head: it is written in at
17*b7b65090SSimon Glass  * @head and read out from @tail. The membuff is empty when @head == @tail
18*b7b65090SSimon Glass  * and full when adding another character would make @head == @tail. We
19*b7b65090SSimon Glass  * therefore waste one character in the membuff to avoid having an extra flag
20*b7b65090SSimon Glass  * to determine whether (when @head == @tail) the membuff is empty or full.
21*b7b65090SSimon Glass  *
22*b7b65090SSimon Glass  * xxxxxx  data
23*b7b65090SSimon Glass  * ......  empty
24*b7b65090SSimon Glass  *
25*b7b65090SSimon Glass  * .............xxxxxxxxxxxxxxxx.........................
26*b7b65090SSimon Glass  *		^		^
27*b7b65090SSimon Glass  *		tail		head
28*b7b65090SSimon Glass  *
29*b7b65090SSimon Glass  * xxxxxxxxxxxxx................xxxxxxxxxxxxxxxxxxxxxxxxx
30*b7b65090SSimon Glass  *		^		^
31*b7b65090SSimon Glass  *		head		tail
32*b7b65090SSimon Glass  */
33*b7b65090SSimon Glass struct membuff {
34*b7b65090SSimon Glass 	char *start;		/** the start of the buffer */
35*b7b65090SSimon Glass 	char *end;		/** the end of the buffer (start + length) */
36*b7b65090SSimon Glass 	char *head;		/** current buffer head */
37*b7b65090SSimon Glass 	char *tail;		/** current buffer tail */
38*b7b65090SSimon Glass };
39*b7b65090SSimon Glass 
40*b7b65090SSimon Glass /**
41*b7b65090SSimon Glass  * membuff_purge() - reset a membuff to the empty state
42*b7b65090SSimon Glass  *
43*b7b65090SSimon Glass  * Initialise head and tail pointers so that the membuff becomes empty.
44*b7b65090SSimon Glass  *
45*b7b65090SSimon Glass  * @mb: membuff to purge
46*b7b65090SSimon Glass  */
47*b7b65090SSimon Glass void membuff_purge(struct membuff *mb);
48*b7b65090SSimon Glass 
49*b7b65090SSimon Glass /**
50*b7b65090SSimon Glass  * membuff_putraw() - find out where bytes can be written
51*b7b65090SSimon Glass  *
52*b7b65090SSimon Glass  * Work out where in the membuff some data could be written. Return a pointer
53*b7b65090SSimon Glass  * to the address and the number of bytes which can be written there. If
54*b7b65090SSimon Glass  * @update is true, the caller must then write the data immediately, since
55*b7b65090SSimon Glass  * the membuff is updated as if the write has been done,
56*b7b65090SSimon Glass  *
57*b7b65090SSimon Glass  * Note that because the spare space in a membuff may not be contiguous, this
58*b7b65090SSimon Glass  * function may not return @maxlen even if there is enough space in the
59*b7b65090SSimon Glass  * membuff. However, by calling this function twice (with @update == true),
60*b7b65090SSimon Glass  * you will get access to all the spare space.
61*b7b65090SSimon Glass  *
62*b7b65090SSimon Glass  * @mb: membuff to adjust
63*b7b65090SSimon Glass  * @maxlen: the number of bytes we want to write
64*b7b65090SSimon Glass  * @update: true to update the membuff as if the write happened, false to not
65*b7b65090SSimon Glass  * @data: the address data can be written to
66*b7b65090SSimon Glass  * @return number of bytes which can be written
67*b7b65090SSimon Glass  */
68*b7b65090SSimon Glass int membuff_putraw(struct membuff *mb, int maxlen, bool update, char **data);
69*b7b65090SSimon Glass 
70*b7b65090SSimon Glass /**
71*b7b65090SSimon Glass  * membuff_getraw() - find and return a pointer to available bytes
72*b7b65090SSimon Glass  *
73*b7b65090SSimon Glass  * Returns a pointer to any valid input data in the given membuff and
74*b7b65090SSimon Glass  * optionally marks it as read. Note that not all input data may not be
75*b7b65090SSimon Glass  * returned, since data is not necessarily contiguous in the membuff. However,
76*b7b65090SSimon Glass  * if you call this function twice (with @update == true) you are guaranteed
77*b7b65090SSimon Glass  * to get all available data, in at most two installments.
78*b7b65090SSimon Glass  *
79*b7b65090SSimon Glass  * @mb: membuff to adjust
80*b7b65090SSimon Glass  * @maxlen: maximum number of bytes to get
81*b7b65090SSimon Glass  * @update: true to update the membuff as if the bytes have been read (use
82*b7b65090SSimon Glass  * false to check bytes without reading them)
83*b7b65090SSimon Glass  * @data: returns address of data in input membuff
84*b7b65090SSimon Glass  * @return the number of bytes available at *@data
85*b7b65090SSimon Glass  */
86*b7b65090SSimon Glass int membuff_getraw(struct membuff *mb, int maxlen, bool update, char **data);
87*b7b65090SSimon Glass 
88*b7b65090SSimon Glass /**
89*b7b65090SSimon Glass  * membuff_putbyte() - Writes a byte to a membuff
90*b7b65090SSimon Glass  *
91*b7b65090SSimon Glass  * @mb: membuff to adjust
92*b7b65090SSimon Glass  * @ch: byte to write
93*b7b65090SSimon Glass  * @return true on success, false if membuff is full
94*b7b65090SSimon Glass  */
95*b7b65090SSimon Glass bool membuff_putbyte(struct membuff *mb, int ch);
96*b7b65090SSimon Glass 
97*b7b65090SSimon Glass /**
98*b7b65090SSimon Glass  * @mb: membuff to adjust
99*b7b65090SSimon Glass  * membuff_getbyte() - Read a byte from the membuff
100*b7b65090SSimon Glass  * @return the byte read, or -1 if the membuff is empty
101*b7b65090SSimon Glass  */
102*b7b65090SSimon Glass int membuff_getbyte(struct membuff *mb);
103*b7b65090SSimon Glass 
104*b7b65090SSimon Glass /**
105*b7b65090SSimon Glass  * membuff_peekbyte() - check the next available byte
106*b7b65090SSimon Glass  *
107*b7b65090SSimon Glass  * Return the next byte which membuff_getbyte() would return, without
108*b7b65090SSimon Glass  * removing it from the membuff.
109*b7b65090SSimon Glass  *
110*b7b65090SSimon Glass  * @mb: membuff to adjust
111*b7b65090SSimon Glass  * @return the byte peeked, or -1 if the membuff is empty
112*b7b65090SSimon Glass  */
113*b7b65090SSimon Glass int membuff_peekbyte(struct membuff *mb);
114*b7b65090SSimon Glass 
115*b7b65090SSimon Glass /**
116*b7b65090SSimon Glass  * membuff_get() - get data from a membuff
117*b7b65090SSimon Glass  *
118*b7b65090SSimon Glass  * Copies any available data (up to @maxlen bytes) to @buff and removes it
119*b7b65090SSimon Glass  * from the membuff.
120*b7b65090SSimon Glass  *
121*b7b65090SSimon Glass  * @mb: membuff to adjust
122*b7b65090SSimon Glass  * @Buff: address of membuff to transfer bytes to
123*b7b65090SSimon Glass  * @maxlen: maximum number of bytes to read
124*b7b65090SSimon Glass  * @return the number of bytes read
125*b7b65090SSimon Glass  */
126*b7b65090SSimon Glass int membuff_get(struct membuff *mb, char *buff, int maxlen);
127*b7b65090SSimon Glass 
128*b7b65090SSimon Glass /**
129*b7b65090SSimon Glass  * membuff_put() - write data to a membuff
130*b7b65090SSimon Glass  *
131*b7b65090SSimon Glass  * Writes some data to a membuff. Returns the number of bytes added. If this
132*b7b65090SSimon Glass  * is less than @lnehgt, then the membuff got full
133*b7b65090SSimon Glass  *
134*b7b65090SSimon Glass  * @mb: membuff to adjust
135*b7b65090SSimon Glass  * @data: the data to write
136*b7b65090SSimon Glass  * @length: number of bytes to write from 'data'
137*b7b65090SSimon Glass  * @return the number of bytes added
138*b7b65090SSimon Glass  */
139*b7b65090SSimon Glass int membuff_put(struct membuff *mb, const char *buff, int length);
140*b7b65090SSimon Glass 
141*b7b65090SSimon Glass /**
142*b7b65090SSimon Glass  * membuff_isempty() - check if a membuff is empty
143*b7b65090SSimon Glass  *
144*b7b65090SSimon Glass  * @mb: membuff to check
145*b7b65090SSimon Glass  * @return true if empty, else false
146*b7b65090SSimon Glass  */
147*b7b65090SSimon Glass bool membuff_isempty(struct membuff *mb);
148*b7b65090SSimon Glass 
149*b7b65090SSimon Glass /**
150*b7b65090SSimon Glass  * membuff_avail() - check available data in a membuff
151*b7b65090SSimon Glass  *
152*b7b65090SSimon Glass  * @mb: membuff to check
153*b7b65090SSimon Glass  * @return number of bytes of data available
154*b7b65090SSimon Glass  */
155*b7b65090SSimon Glass int membuff_avail(struct membuff *mb);
156*b7b65090SSimon Glass 
157*b7b65090SSimon Glass /**
158*b7b65090SSimon Glass  * membuff_size() - get the size of a membuff
159*b7b65090SSimon Glass  *
160*b7b65090SSimon Glass  * Note that a membuff can only old data up to one byte less than its size.
161*b7b65090SSimon Glass  *
162*b7b65090SSimon Glass  * @mb: membuff to check
163*b7b65090SSimon Glass  * @return total size
164*b7b65090SSimon Glass  */
165*b7b65090SSimon Glass int membuff_size(struct membuff *mb);
166*b7b65090SSimon Glass 
167*b7b65090SSimon Glass /**
168*b7b65090SSimon Glass  * membuff_makecontig() - adjust all membuff data to be contiguous
169*b7b65090SSimon Glass  *
170*b7b65090SSimon Glass  * This places all data in a membuff into a single contiguous lump, if
171*b7b65090SSimon Glass  * possible
172*b7b65090SSimon Glass  *
173*b7b65090SSimon Glass  * @mb: membuff to adjust
174*b7b65090SSimon Glass  * @return true on success
175*b7b65090SSimon Glass  */
176*b7b65090SSimon Glass bool membuff_makecontig(struct membuff *mb);
177*b7b65090SSimon Glass 
178*b7b65090SSimon Glass /**
179*b7b65090SSimon Glass  * membuff_free() - find the number of bytes that can be written to a membuff
180*b7b65090SSimon Glass  *
181*b7b65090SSimon Glass  * @mb: membuff to check
182*b7b65090SSimon Glass  * @return returns the number of bytes free in a membuff
183*b7b65090SSimon Glass  */
184*b7b65090SSimon Glass int membuff_free(struct membuff *mb);
185*b7b65090SSimon Glass 
186*b7b65090SSimon Glass /**
187*b7b65090SSimon Glass  * membuff_readline() - read a line of text from a membuff
188*b7b65090SSimon Glass  *
189*b7b65090SSimon Glass  * Reads a line of text of up to 'maxlen' characters from a membuff and puts
190*b7b65090SSimon Glass  * it in @str. Any character less than @minch is assumed to be the end of
191*b7b65090SSimon Glass  * line character
192*b7b65090SSimon Glass  *
193*b7b65090SSimon Glass  * @mb: membuff to adjust
194*b7b65090SSimon Glass  * @str: Place to put the line
195*b7b65090SSimon Glass  * @maxlen: Maximum line length (excluding terminator)
196*b7b65090SSimon Glass  * @return number of bytes read (including terminator) if a line has been
197*b7b65090SSimon Glass  *	   read, 0 if nothing was there
198*b7b65090SSimon Glass  */
199*b7b65090SSimon Glass int membuff_readline(struct membuff *mb, char *str, int maxlen, int minch);
200*b7b65090SSimon Glass 
201*b7b65090SSimon Glass /**
202*b7b65090SSimon Glass  * membuff_extend_by() - expand a membuff
203*b7b65090SSimon Glass  *
204*b7b65090SSimon Glass  * Extends a membuff by the given number of bytes
205*b7b65090SSimon Glass  *
206*b7b65090SSimon Glass  * @mb: membuff to adjust
207*b7b65090SSimon Glass  * @by: Number of bytes to increase the size by
208*b7b65090SSimon Glass  * @max: Maximum size to allow
209*b7b65090SSimon Glass  * @return 0 if the expand succeeded, -ENOMEM if not enough memory, -E2BIG
210*b7b65090SSimon Glass  * if the the size would exceed @max
211*b7b65090SSimon Glass  */
212*b7b65090SSimon Glass int membuff_extend_by(struct membuff *mb, int by, int max);
213*b7b65090SSimon Glass 
214*b7b65090SSimon Glass /**
215*b7b65090SSimon Glass  * membuff_init() - set up a new membuff using an existing membuff
216*b7b65090SSimon Glass  *
217*b7b65090SSimon Glass  * @mb: membuff to set up
218*b7b65090SSimon Glass  * @buff: Address of buffer
219*b7b65090SSimon Glass  * @size: Size of buffer
220*b7b65090SSimon Glass  */
221*b7b65090SSimon Glass void membuff_init(struct membuff *mb, char *buff, int size);
222*b7b65090SSimon Glass 
223*b7b65090SSimon Glass /**
224*b7b65090SSimon Glass  * membuff_uninit() - clear a membuff so it can no longer be used
225*b7b65090SSimon Glass  *
226*b7b65090SSimon Glass  * @mb: membuff to uninit
227*b7b65090SSimon Glass  */
228*b7b65090SSimon Glass void membuff_uninit(struct membuff *mb);
229*b7b65090SSimon Glass 
230*b7b65090SSimon Glass /**
231*b7b65090SSimon Glass  * membuff_new() - create a new membuff
232*b7b65090SSimon Glass  *
233*b7b65090SSimon Glass  * @mb: membuff to init
234*b7b65090SSimon Glass  * @size: size of membuff to create
235*b7b65090SSimon Glass  * @return 0 if OK, -ENOMEM if out of memory
236*b7b65090SSimon Glass  */
237*b7b65090SSimon Glass int membuff_new(struct membuff *mb, int size);
238*b7b65090SSimon Glass 
239*b7b65090SSimon Glass /**
240*b7b65090SSimon Glass  * membuff_dispose() - free memory allocated to a membuff and uninit it
241*b7b65090SSimon Glass  *
242*b7b65090SSimon Glass  * @mb: membuff to dispose
243*b7b65090SSimon Glass  */
244*b7b65090SSimon Glass void membuff_dispose(struct membuff *mb);
245*b7b65090SSimon Glass 
246*b7b65090SSimon Glass #endif
247