Browse Source

Return -1 to indicate buffer overflow. Allow buffer fill level to be read

easily from struct buffer.
tags/v0.9.3
Michael Brown 20 years ago
parent
commit
bab2924e89
2 changed files with 32 additions and 23 deletions
  1. 18
    14
      src/core/buffer.c
  2. 14
    9
      src/include/buffer.h

+ 18
- 14
src/core/buffer.c View File

35
 void init_buffer ( struct buffer *buffer, physaddr_t start, size_t len ) {
35
 void init_buffer ( struct buffer *buffer, physaddr_t start, size_t len ) {
36
 	buffer->start = start;
36
 	buffer->start = start;
37
 	buffer->end = start + len;
37
 	buffer->end = start + len;
38
-	buffer->first_free = start;
38
+	buffer->fill = 0;
39
 
39
 
40
 	if ( len ) {
40
 	if ( len ) {
41
 		char tail = 1;
41
 		char tail = 1;
59
 	if ( split >= desc->end )
59
 	if ( split >= desc->end )
60
 		return;
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
 	      block, desc->end, block, split, split, desc->end );
63
 	      block, desc->end, block, split, split, desc->end );
64
 
64
 
65
 	/* Create descriptor for new free block */
65
 	/* Create descriptor for new free block */
83
 				  physaddr_t prev_block ) {
83
 				  physaddr_t prev_block ) {
84
 	struct buffer_free_block prev_desc;
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
 	if ( ! prev_block ) {
87
 	if ( ! prev_block ) {
88
 		DBG ( "BUFFER marking [%x,%x) as used\n",
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
 		return;
91
 		return;
92
 	}
92
 	}
93
 
93
 
110
  * apart.  If this condition is not satisfied, data corruption will
110
  * apart.  If this condition is not satisfied, data corruption will
111
  * occur.
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
 	struct buffer_free_block desc;
117
 	struct buffer_free_block desc;
121
 	physaddr_t block, prev_block;
118
 	physaddr_t block, prev_block;
122
 	physaddr_t data_start, data_end;
119
 	physaddr_t data_start, data_end;
127
 	DBG ( "BUFFER [%x,%x) writing portion [%x,%x)\n",
124
 	DBG ( "BUFFER [%x,%x) writing portion [%x,%x)\n",
128
 	      buffer->start, buffer->end, data_start, data_end );
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
 	/* Iterate through the buffer's free blocks */
134
 	/* Iterate through the buffer's free blocks */
131
 	prev_block = 0;
135
 	prev_block = 0;
132
-	block = buffer->first_free;
136
+	block = buffer->start + buffer->fill;
133
 	while ( block < buffer->end ) {
137
 	while ( block < buffer->end ) {
134
 		/* Read block descriptor */
138
 		/* Read block descriptor */
135
 		desc.next_free = buffer->end;
139
 		desc.next_free = buffer->end;
160
 	}
164
 	}
161
 
165
 
162
 	DBG ( "BUFFER [%x,%x) full up to %x\n",
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
 }

+ 14
- 9
src/include/buffer.h View File

3
 
3
 
4
 #include "stdint.h"
4
 #include "stdint.h"
5
 
5
 
6
+/*
7
+ * "start" and "end" denote the real boundaries of the buffer.  "fill"
8
+ * denotes the offset to the first free block in the buffer.  (If the
9
+ * buffer is full, "fill" will equal ( end - start ) ).
10
+ *
11
+ */
12
+struct buffer {
13
+	physaddr_t	start;
14
+	physaddr_t	end;
15
+	off_t		fill;
16
+};
17
+
6
 /*
18
 /*
7
  * Free blocks in the buffer start with a "tail byte".  If non-zero,
19
  * Free blocks in the buffer start with a "tail byte".  If non-zero,
8
  * this byte indicates that the free block is the tail of the buffer,
20
  * this byte indicates that the free block is the tail of the buffer,
15
  * smaller than a struct buffer_free_block.
27
  * smaller than a struct buffer_free_block.
16
  *
28
  *
17
  */
29
  */
18
-
19
 struct buffer_free_block {
30
 struct buffer_free_block {
20
 	char		tail;
31
 	char		tail;
21
 	physaddr_t	next_free;
32
 	physaddr_t	next_free;
22
 	physaddr_t	end;
33
 	physaddr_t	end;
23
 } __attribute__ (( packed ));
34
 } __attribute__ (( packed ));
24
 
35
 
25
-struct buffer {
26
-	physaddr_t	start;
27
-	physaddr_t	end;
28
-       	physaddr_t	first_free;
29
-};
30
-
31
 /* Functions in buffer.c */
36
 /* Functions in buffer.c */
32
 
37
 
33
 extern void init_buffer ( struct buffer *buffer, physaddr_t start,
38
 extern void init_buffer ( struct buffer *buffer, physaddr_t start,
34
 			  size_t len );
39
 			  size_t len );
35
-extern off_t fill_buffer ( struct buffer *buffer, void *data,
36
-			   off_t offset, size_t len );
40
+extern int fill_buffer ( struct buffer *buffer, void *data,
41
+			 off_t offset, size_t len );
37
 
42
 
38
 #endif /* BUFFER_H */
43
 #endif /* BUFFER_H */

Loading…
Cancel
Save