Просмотр исходного кода

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

easily from struct buffer.
tags/v0.9.3
Michael Brown 20 лет назад
Родитель
Сommit
bab2924e89
2 измененных файлов: 32 добавлений и 23 удалений
  1. 18
    14
      src/core/buffer.c
  2. 14
    9
      src/include/buffer.h

+ 18
- 14
src/core/buffer.c Просмотреть файл

@@ -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
 }

+ 14
- 9
src/include/buffer.h Просмотреть файл

@@ -3,6 +3,18 @@
3 3
 
4 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 19
  * Free blocks in the buffer start with a "tail byte".  If non-zero,
8 20
  * this byte indicates that the free block is the tail of the buffer,
@@ -15,24 +27,17 @@
15 27
  * smaller than a struct buffer_free_block.
16 28
  *
17 29
  */
18
-
19 30
 struct buffer_free_block {
20 31
 	char		tail;
21 32
 	physaddr_t	next_free;
22 33
 	physaddr_t	end;
23 34
 } __attribute__ (( packed ));
24 35
 
25
-struct buffer {
26
-	physaddr_t	start;
27
-	physaddr_t	end;
28
-       	physaddr_t	first_free;
29
-};
30
-
31 36
 /* Functions in buffer.c */
32 37
 
33 38
 extern void init_buffer ( struct buffer *buffer, physaddr_t start,
34 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 43
 #endif /* BUFFER_H */

Загрузка…
Отмена
Сохранить