|
@@ -35,7 +35,7 @@
|
35
|
35
|
void init_buffer ( struct buffer *buffer, physaddr_t start, size_t len ) {
|
36
|
36
|
buffer->start = start;
|
37
|
37
|
buffer->end = start + len;
|
38
|
|
- buffer->first_free = start;
|
|
38
|
+ buffer->fill = 0;
|
39
|
39
|
|
40
|
40
|
if ( len ) {
|
41
|
41
|
char tail = 1;
|
|
@@ -59,7 +59,7 @@ static void split_free_block ( struct buffer_free_block *desc,
|
59
|
59
|
if ( split >= desc->end )
|
60
|
60
|
return;
|
61
|
61
|
|
62
|
|
- DBG ( "BUFFER splitting [%x,%x) into [%x,%x) and [%x,%x)\n",
|
|
62
|
+ DBG ( "BUFFER splitting [%x,%x) -> [%x,%x) [%x,%x)\n",
|
63
|
63
|
block, desc->end, block, split, split, desc->end );
|
64
|
64
|
|
65
|
65
|
/* Create descriptor for new free block */
|
|
@@ -83,11 +83,11 @@ static inline void unfree_block ( struct buffer *buffer,
|
83
|
83
|
physaddr_t prev_block ) {
|
84
|
84
|
struct buffer_free_block prev_desc;
|
85
|
85
|
|
86
|
|
- /* If this is the first block, just update first_free */
|
|
86
|
+ /* If this is the first block, just update buffer->fill */
|
87
|
87
|
if ( ! prev_block ) {
|
88
|
88
|
DBG ( "BUFFER marking [%x,%x) as used\n",
|
89
|
|
- buffer->first_free, desc->end );
|
90
|
|
- buffer->first_free = desc->next_free;
|
|
89
|
+ buffer->start + buffer->fill, desc->end );
|
|
90
|
+ buffer->fill = desc->next_free - buffer->start;
|
91
|
91
|
return;
|
92
|
92
|
}
|
93
|
93
|
|
|
@@ -110,13 +110,10 @@ static inline void unfree_block ( struct buffer *buffer,
|
110
|
110
|
* apart. If this condition is not satisfied, data corruption will
|
111
|
111
|
* occur.
|
112
|
112
|
*
|
113
|
|
- * Returns the offset to the first gap in the buffer. (When the
|
114
|
|
- * buffer is full, returns the offset to the byte past the end of the
|
115
|
|
- * buffer.)
|
116
|
|
- *
|
|
113
|
+ * Returns 1 for success, 0 for failure (e.g. buffer too small).
|
117
|
114
|
*/
|
118
|
|
-off_t fill_buffer ( struct buffer *buffer, void *data,
|
119
|
|
- off_t offset, size_t len ) {
|
|
115
|
+int fill_buffer ( struct buffer *buffer, void *data,
|
|
116
|
+ off_t offset, size_t len ) {
|
120
|
117
|
struct buffer_free_block desc;
|
121
|
118
|
physaddr_t block, prev_block;
|
122
|
119
|
physaddr_t data_start, data_end;
|
|
@@ -127,9 +124,16 @@ off_t fill_buffer ( struct buffer *buffer, void *data,
|
127
|
124
|
DBG ( "BUFFER [%x,%x) writing portion [%x,%x)\n",
|
128
|
125
|
buffer->start, buffer->end, data_start, data_end );
|
129
|
126
|
|
|
127
|
+ /* Check buffer bounds */
|
|
128
|
+ if ( data_end > buffer->end ) {
|
|
129
|
+ DBG ( "BUFFER [%x,%x) too small for data!\n",
|
|
130
|
+ buffer->start, buffer->end );
|
|
131
|
+ return 0;
|
|
132
|
+ }
|
|
133
|
+
|
130
|
134
|
/* Iterate through the buffer's free blocks */
|
131
|
135
|
prev_block = 0;
|
132
|
|
- block = buffer->first_free;
|
|
136
|
+ block = buffer->start + buffer->fill;
|
133
|
137
|
while ( block < buffer->end ) {
|
134
|
138
|
/* Read block descriptor */
|
135
|
139
|
desc.next_free = buffer->end;
|
|
@@ -160,7 +164,7 @@ off_t fill_buffer ( struct buffer *buffer, void *data,
|
160
|
164
|
}
|
161
|
165
|
|
162
|
166
|
DBG ( "BUFFER [%x,%x) full up to %x\n",
|
163
|
|
- buffer->start, buffer->end, buffer->first_free );
|
|
167
|
+ buffer->start, buffer->end, buffer->start + buffer->fill );
|
164
|
168
|
|
165
|
|
- return ( buffer->first_free - buffer->start );
|
|
169
|
+ return 1;
|
166
|
170
|
}
|