|
@@ -1,3 +1,27 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
|
|
3
|
+ *
|
|
4
|
+ * This program is free software; you can redistribute it and/or
|
|
5
|
+ * modify it under the terms of the GNU General Public License as
|
|
6
|
+ * published by the Free Software Foundation; either version 2 of the
|
|
7
|
+ * License, or any later version.
|
|
8
|
+ *
|
|
9
|
+ * This program is distributed in the hope that it will be useful, but
|
|
10
|
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+ * General Public License for more details.
|
|
13
|
+ *
|
|
14
|
+ * You should have received a copy of the GNU General Public License
|
|
15
|
+ * along with this program; if not, write to the Free Software
|
|
16
|
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+#include <stddef.h>
|
|
20
|
+#include <string.h>
|
|
21
|
+#include <errno.h>
|
|
22
|
+#include <assert.h>
|
|
23
|
+#include <io.h>
|
|
24
|
+#include <gpxe/buffer.h>
|
1
|
25
|
|
2
|
26
|
/** @file
|
3
|
27
|
*
|
|
@@ -7,27 +31,22 @@
|
7
|
31
|
* which is "filled" and the remainder of which is "free". The
|
8
|
32
|
* "filled" and "free" spaces are not necessarily contiguous.
|
9
|
33
|
*
|
10
|
|
- * When a buffer is initialised via init_buffer(), it consists of a
|
11
|
|
- * single free space. As data is added to the buffer via
|
12
|
|
- * fill_buffer(), this free space decreases and can become fragmented.
|
13
|
|
- *
|
14
|
|
- * Each free block within a buffer starts with a "tail byte". If the
|
15
|
|
- * tail byte is non-zero, this indicates that the free block is the
|
16
|
|
- * tail of the buffer, i.e. occupies all the remaining space up to the
|
17
|
|
- * end of the buffer. When the tail byte is non-zero, it indicates
|
18
|
|
- * that a descriptor (a @c struct @c buffer_free_block) follows the
|
19
|
|
- * tail byte. The descriptor describes the size of the free block and
|
20
|
|
- * the address of the next free block.
|
21
|
|
- *
|
22
|
|
- * We cannot simply always start a free block with a descriptor,
|
23
|
|
- * because it is conceivable that we will, at some point, encounter a
|
24
|
|
- * situation in which the final free block of a buffer is too small to
|
25
|
|
- * contain a descriptor. Consider a protocol with a blocksize of 512
|
26
|
|
- * downloading a 1025-byte file into a 1025-byte buffer. Suppose that
|
27
|
|
- * the first two blocks are received; we have now filled 1024 of the
|
28
|
|
- * 1025 bytes in the buffer, and our only free block consists of the
|
29
|
|
- * 1025th byte. Using a "tail byte" solves this problem.
|
30
|
|
- *
|
|
34
|
+ * At the start of a buffer's life, it consists of a single free
|
|
35
|
+ * space. As data is added to the buffer via fill_buffer(), this free
|
|
36
|
+ * space decreases and can become fragmented.
|
|
37
|
+ *
|
|
38
|
+ * Each free block within a buffer (except the last) starts with a @c
|
|
39
|
+ * struct @c buffer_free_block. This describes the size of the free
|
|
40
|
+ * block, and the offset to the next free block.
|
|
41
|
+ *
|
|
42
|
+ * We cannot simply start every free block (including the last) with a
|
|
43
|
+ * descriptor, because it is conceivable that we will, at some point,
|
|
44
|
+ * encounter a situation in which the final free block of a buffer is
|
|
45
|
+ * too small to contain a descriptor. Consider a protocol with a
|
|
46
|
+ * blocksize of 512 downloading a 1025-byte file into a 1025-byte
|
|
47
|
+ * buffer. Suppose that the first two blocks are received; we have
|
|
48
|
+ * now filled 1024 of the 1025 bytes in the buffer, and our only free
|
|
49
|
+ * block consists of the 1025th byte.
|
31
|
50
|
*
|
32
|
51
|
* Note that the rather convoluted way of manipulating the buffer
|
33
|
52
|
* descriptors (using copy_{to,from}_phys rather than straightforward
|
|
@@ -38,107 +57,76 @@
|
38
|
57
|
*
|
39
|
58
|
*/
|
40
|
59
|
|
41
|
|
-#include "stddef.h"
|
42
|
|
-#include "string.h"
|
43
|
|
-#include "io.h"
|
44
|
|
-#include "errno.h"
|
45
|
|
-#include <assert.h>
|
46
|
|
-#include <gpxe/buffer.h>
|
47
|
|
-
|
48
|
60
|
/**
|
49
|
|
- * Initialise a buffer.
|
|
61
|
+ * A free block descriptor
|
50
|
62
|
*
|
51
|
|
- * @v buffer The buffer to be initialised
|
52
|
|
- * @ret None -
|
53
|
|
- * @err None -
|
|
63
|
+ * This is the data structure that is found at the start of a free
|
|
64
|
+ * block within a data buffer.
|
|
65
|
+ */
|
|
66
|
+struct buffer_free_block {
|
|
67
|
+ /** Starting offset of the free block */
|
|
68
|
+ size_t start;
|
|
69
|
+ /** Ending offset of the free block */
|
|
70
|
+ size_t end;
|
|
71
|
+ /** Offset of next free block */
|
|
72
|
+ size_t next;
|
|
73
|
+};
|
|
74
|
+
|
|
75
|
+/**
|
|
76
|
+ * Get next free block within the buffer
|
54
|
77
|
*
|
55
|
|
- * Set @c buffer->start and @c buffer->end before calling init_buffer().
|
56
|
|
- * init_buffer() will initialise the buffer to the state of being
|
57
|
|
- * empty.
|
|
78
|
+ * @v buffer Data buffer
|
|
79
|
+ * @v block Previous free block descriptor
|
|
80
|
+ * @ret block Next free block descriptor
|
|
81
|
+ * @ret rc Return status code
|
58
|
82
|
*
|
|
83
|
+ * Set @c block->next=buffer->free before first call to
|
|
84
|
+ * get_next_free_block().
|
59
|
85
|
*/
|
60
|
|
-void init_buffer ( struct buffer *buffer ) {
|
61
|
|
- char tail = 1;
|
62
|
|
-
|
63
|
|
- buffer->fill = 0;
|
64
|
|
- if ( buffer->end != buffer->start )
|
65
|
|
- copy_to_phys ( buffer->start, &tail, sizeof ( tail ) );
|
|
86
|
+static int get_next_free_block ( struct buffer *buffer,
|
|
87
|
+ struct buffer_free_block *block ) {
|
66
|
88
|
|
67
|
|
- DBG ( "BUFFER [%x,%x) initialised\n", buffer->start, buffer->end );
|
68
|
|
-}
|
|
89
|
+ /* Check for end of buffer */
|
|
90
|
+ if ( block->end >= buffer->len )
|
|
91
|
+ return -ENOENT;
|
69
|
92
|
|
70
|
|
-/**
|
71
|
|
- * Move to the next block in the free list
|
72
|
|
- *
|
73
|
|
- * @v block The current free block
|
74
|
|
- * @v buffer The buffer
|
75
|
|
- * @ret True Successfully moved to the next free block
|
76
|
|
- * @ret False There are no more free blocks
|
77
|
|
- * @ret block The next free block
|
78
|
|
- * @err None -
|
79
|
|
- *
|
80
|
|
- * Move to the next block in the free block list, filling in @c block
|
81
|
|
- * with the descriptor for this next block. If the next block is the
|
82
|
|
- * tail block, @c block will be filled with the values calculated for
|
83
|
|
- * the tail block, otherwise the descriptor will be read from the free
|
84
|
|
- * block itself.
|
85
|
|
- *
|
86
|
|
- * If there are no more free blocks, next_free_block() returns False
|
87
|
|
- * and leaves @c block with invalid contents.
|
88
|
|
- *
|
89
|
|
- * Set <tt> block->next = buffer->start + buffer->fill </tt> for the
|
90
|
|
- * first call to next_free_block().
|
91
|
|
- */
|
92
|
|
-static inline int next_free_block ( struct buffer_free_block *block,
|
93
|
|
- struct buffer *buffer ) {
|
94
|
93
|
/* Move to next block */
|
95
|
94
|
block->start = block->next;
|
96
|
|
-
|
97
|
|
- /* If at end of buffer, return 0 */
|
98
|
|
- if ( block->start >= buffer->end )
|
99
|
|
- return 0;
|
100
|
|
-
|
101
|
|
- /* Set up ->next and ->end as for a tail block */
|
102
|
|
- block->next = block->end = buffer->end;
|
103
|
|
-
|
104
|
|
- /* Read tail marker from block */
|
105
|
|
- copy_from_phys ( &block->tail, block->start, sizeof ( block->tail ) );
|
106
|
|
-
|
107
|
|
- /* If not a tail block, read whole block descriptor from block */
|
108
|
|
- if ( ! block->tail ) {
|
109
|
|
- copy_from_phys ( block, block->start, sizeof ( *block ) );
|
|
95
|
+ if ( block->start >= buffer->free ) {
|
|
96
|
+ /* Final block; no in-band descriptor */
|
|
97
|
+ block->end = buffer->len;
|
|
98
|
+ } else {
|
|
99
|
+ /* Retrieve block descriptor */
|
|
100
|
+ copy_from_phys ( block, ( buffer->addr + block->start ),
|
|
101
|
+ sizeof ( *block ) );
|
110
|
102
|
}
|
111
|
103
|
|
112
|
|
- return 1;
|
|
104
|
+ return 0;
|
113
|
105
|
}
|
114
|
106
|
|
115
|
107
|
/**
|
116
|
|
- * Store a free block descriptor
|
|
108
|
+ * Write free block descriptor back to buffer
|
117
|
109
|
*
|
118
|
|
- * @v block The free block descriptor to store
|
119
|
|
- * @ret None -
|
120
|
|
- * @err None -
|
121
|
|
- *
|
122
|
|
- * Writes a free block descriptor back to a free block. If the block
|
123
|
|
- * is a tail block, only the tail marker will be written, otherwise
|
124
|
|
- * the whole block descriptor will be written.
|
|
110
|
+ * @v buffer Data buffer
|
|
111
|
+ * @v block Free block descriptor
|
125
|
112
|
*/
|
126
|
|
-static inline void store_free_block ( struct buffer_free_block *block ) {
|
127
|
|
- copy_to_phys ( block->start, block,
|
128
|
|
- ( block->tail ?
|
129
|
|
- sizeof ( block->tail ) : sizeof ( *block ) ) );
|
|
113
|
+static void store_free_block ( struct buffer *buffer,
|
|
114
|
+ struct buffer_free_block *block ) {
|
|
115
|
+ size_t free_block_size = ( block->end - block->start );
|
|
116
|
+
|
|
117
|
+ assert ( free_block_size >= sizeof ( *block ) );
|
|
118
|
+ copy_to_phys ( ( buffer->addr + block->start ), block,
|
|
119
|
+ sizeof ( *block ) );
|
130
|
120
|
}
|
131
|
121
|
|
132
|
122
|
/**
|
133
|
|
- * Write data into a buffer.
|
|
123
|
+ * Write data into a buffer
|
134
|
124
|
*
|
135
|
|
- * @v buffer The buffer into which to write the data
|
136
|
|
- * @v data The data to be written
|
|
125
|
+ * @v buffer Data buffer
|
|
126
|
+ * @v data Data to be written
|
137
|
127
|
* @v offset Offset within the buffer at which to write the data
|
138
|
128
|
* @v len Length of data to be written
|
139
|
|
- * @ret True Data was successfully written
|
140
|
|
- * @ret False Data was not written
|
141
|
|
- * @err ENOMEM Buffer is too small to contain the data
|
|
129
|
+ * @ret rc Return status code
|
142
|
130
|
*
|
143
|
131
|
* Writes a block of data into the buffer. The block need not be
|
144
|
132
|
* aligned to any particular boundary, or be of any particular size,
|
|
@@ -166,29 +154,37 @@ static inline void store_free_block ( struct buffer_free_block *block ) {
|
166
|
154
|
*
|
167
|
155
|
*/
|
168
|
156
|
int fill_buffer ( struct buffer *buffer, const void *data,
|
169
|
|
- off_t offset, size_t len ) {
|
|
157
|
+ size_t offset, size_t len ) {
|
170
|
158
|
struct buffer_free_block block, before, after;
|
171
|
|
- physaddr_t data_start, data_end;
|
172
|
|
-
|
173
|
|
- /* Calculate start and end addresses of data */
|
174
|
|
- data_start = buffer->start + offset;
|
175
|
|
- data_end = data_start + len;
|
176
|
|
- DBG ( "BUFFER [%x,%x) writing portion [%x,%x)\n",
|
177
|
|
- buffer->start, buffer->end, data_start, data_end );
|
178
|
|
-
|
179
|
|
- /* Check buffer bounds */
|
180
|
|
- if ( data_end > buffer->end ) {
|
181
|
|
- DBG ( "BUFFER [%x,%x) too small for data!\n",
|
182
|
|
- buffer->start, buffer->end );
|
183
|
|
- errno = ENOMEM;
|
184
|
|
- return 0;
|
|
159
|
+ size_t data_start = offset;
|
|
160
|
+ size_t data_end = ( data_start + len );
|
|
161
|
+ int rc;
|
|
162
|
+
|
|
163
|
+ DBGC ( buffer, "BUFFER %p [%lx,%lx) filling portion [%lx,%lx)\n",
|
|
164
|
+ buffer, buffer->addr, ( buffer->addr + buffer->len ),
|
|
165
|
+ ( buffer->addr + data_start ), ( buffer->addr + data_end ) );
|
|
166
|
+
|
|
167
|
+ /* Check that block fits within buffer, expand if necessary */
|
|
168
|
+ if ( data_end > buffer->len ) {
|
|
169
|
+ if ( ! buffer->expand ) {
|
|
170
|
+ DBGC ( buffer, "BUFFER %p not expandable\n", buffer );
|
|
171
|
+ return -ENOBUFS;
|
|
172
|
+ }
|
|
173
|
+ if ( ( rc = buffer->expand ( buffer, data_end ) ) != 0 ) {
|
|
174
|
+ DBGC ( buffer, "BUFFER %p could not expand :%s\n",
|
|
175
|
+ buffer, strerror ( rc ) );
|
|
176
|
+ return rc;
|
|
177
|
+ }
|
|
178
|
+ DBGC ( buffer, "BUFFER %p expanded to [%lx,%lx)\n", buffer,
|
|
179
|
+ buffer->addr, ( buffer->addr + buffer->len ) );
|
|
180
|
+ assert ( buffer->len >= data_end );
|
185
|
181
|
}
|
186
|
182
|
|
187
|
183
|
/* Find 'before' and 'after' blocks, if any */
|
188
|
184
|
before.start = before.end = 0;
|
189
|
|
- after.start = after.end = buffer->end;
|
190
|
|
- block.next = buffer->start + buffer->fill;
|
191
|
|
- while ( next_free_block ( &block, buffer ) ) {
|
|
185
|
+ after.start = after.end = buffer->len;
|
|
186
|
+ block.next = buffer->fill;
|
|
187
|
+ while ( get_next_free_block ( buffer, &block ) == 0 ) {
|
192
|
188
|
if ( ( block.start < data_start ) &&
|
193
|
189
|
( block.start >= before.start ) )
|
194
|
190
|
memcpy ( &before, &block, sizeof ( before ) );
|
|
@@ -206,33 +202,35 @@ int fill_buffer ( struct buffer *buffer, const void *data,
|
206
|
202
|
/* Link 'after' block to 'before' block */
|
207
|
203
|
before.next = after.start;
|
208
|
204
|
|
|
205
|
+ DBGC ( buffer, "BUFFER %p split before [%lx,%lx) after [%lx,%lx)\n",
|
|
206
|
+ buffer, ( buffer->addr + before.start ),
|
|
207
|
+ ( buffer->addr + before.end ), ( buffer->addr + after.start ),
|
|
208
|
+ ( buffer->addr + after.end ) );
|
|
209
|
+
|
209
|
210
|
/* Write back 'before' block, if any */
|
210
|
|
- if ( before.start ) {
|
211
|
|
- before.tail = 0;
|
212
|
|
- assert ( ( before.end - before.start ) >=
|
213
|
|
- sizeof ( struct buffer_free_block ) );
|
214
|
|
- store_free_block ( &before );
|
|
211
|
+ if ( before.end == 0 ) {
|
|
212
|
+ /* No 'before' block: update buffer->fill */
|
|
213
|
+ buffer->fill = after.start;
|
|
214
|
+ DBGC ( buffer, "BUFFER %p full up to %lx\n", buffer,
|
|
215
|
+ ( buffer->addr + buffer->fill ) );
|
215
|
216
|
} else {
|
216
|
|
- buffer->fill = before.next - buffer->start;
|
|
217
|
+ /* Write back 'before' block */
|
|
218
|
+ store_free_block ( buffer, &before );
|
217
|
219
|
}
|
218
|
220
|
|
219
|
|
- /* Write back 'after' block, if any */
|
220
|
|
- if ( after.start < buffer->end ) {
|
221
|
|
- assert ( after.tail ||
|
222
|
|
- ( ( after.end - after.start ) >=
|
223
|
|
- sizeof ( struct buffer_free_block ) ) );
|
224
|
|
- store_free_block ( &after );
|
|
221
|
+ /* Write back 'after' block */
|
|
222
|
+ if ( after.end == buffer->len ) {
|
|
223
|
+ /* 'After' block is the final block: update buffer->free */
|
|
224
|
+ buffer->free = after.start;
|
|
225
|
+ DBGC ( buffer, "BUFFER %p free from %lx onwards\n", buffer,
|
|
226
|
+ ( buffer->addr + buffer->free ) );
|
|
227
|
+ } else {
|
|
228
|
+ /* Write back 'after' block */
|
|
229
|
+ store_free_block ( buffer, &after );
|
225
|
230
|
}
|
226
|
|
-
|
227
|
|
- DBG ( "BUFFER [%x,%x) before [%x,%x) after [%x,%x)\n",
|
228
|
|
- buffer->start, buffer->end, before.start, before.end,
|
229
|
|
- after.start, after.end );
|
230
|
|
-
|
231
|
|
- /* Copy data into buffer */
|
232
|
|
- copy_to_phys ( data_start, data, len );
|
233
|
231
|
|
234
|
|
- DBG ( "BUFFER [%x,%x) full up to %x\n",
|
235
|
|
- buffer->start, buffer->end, buffer->start + buffer->fill );
|
|
232
|
+ /* Copy data into buffer */
|
|
233
|
+ copy_to_phys ( ( buffer->addr + data_start ), data, len );
|
236
|
234
|
|
237
|
|
- return 1;
|
|
235
|
+ return 0;
|
238
|
236
|
}
|