Browse Source

Moved most buffer debug messages to DBG2.

Make expand_buffer() a non-inline function, so that we can add debug
messages.

Expandable buffers now don't *always* round up to the next power of two.
This allows e.g. loading a 137MB ISO image on a machine with only 256MB
RAM...
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
c42a384709
3 changed files with 64 additions and 45 deletions
  1. 51
    21
      src/core/buffer.c
  2. 12
    4
      src/core/ebuffer.c
  3. 1
    20
      src/include/gpxe/buffer.h

+ 51
- 21
src/core/buffer.c View File

159
 	size_t data_end = ( data_start + len );
159
 	size_t data_end = ( data_start + len );
160
 	int rc;
160
 	int rc;
161
 
161
 
162
-	DBGC ( buffer, "BUFFER %p [%lx,%lx) filling portion [%lx,%lx)\n",
163
-	       buffer, user_to_phys ( buffer->addr, 0 ),
164
-	       user_to_phys ( buffer->addr, buffer->len ),
165
-	       user_to_phys ( buffer->addr, data_start ),
166
-	       user_to_phys ( buffer->addr, data_end ) );
162
+	DBGC2 ( buffer, "BUFFER %p [%lx,%lx) filling portion [%lx,%lx)\n",
163
+		buffer, user_to_phys ( buffer->addr, 0 ),
164
+		user_to_phys ( buffer->addr, buffer->len ),
165
+		user_to_phys ( buffer->addr, data_start ),
166
+		user_to_phys ( buffer->addr, data_end ) );
167
 
167
 
168
 	/* Check that block fits within buffer, expand if necessary */
168
 	/* Check that block fits within buffer, expand if necessary */
169
 	if ( data_end > buffer->len ) {
169
 	if ( data_end > buffer->len ) {
170
-		if ( ( rc = expand_buffer ( buffer, data_end ) ) != 0 ) {
171
-			DBGC ( buffer, "BUFFER %p could not expand :%s\n",
172
-			       buffer, strerror ( rc ) );
170
+		if ( ( rc = expand_buffer ( buffer, data_end ) ) != 0 )
173
 			return rc;
171
 			return rc;
174
-		}
175
-		DBGC ( buffer, "BUFFER %p expanded to [%lx,%lx)\n", buffer,
176
-		       user_to_phys ( buffer->addr, 0 ),
177
-		       user_to_phys ( buffer->addr, buffer->len ) );
178
 		assert ( buffer->len >= data_end );
172
 		assert ( buffer->len >= data_end );
179
 	}
173
 	}
180
 
174
 
200
 	/* Link 'after' block to 'before' block */
194
 	/* Link 'after' block to 'before' block */
201
 	before.next = after.start;
195
 	before.next = after.start;
202
 
196
 
203
-	DBGC ( buffer, "BUFFER %p split before [%lx,%lx) after [%lx,%lx)\n",
204
-	       buffer, user_to_phys ( buffer->addr, before.start ),
205
-	       user_to_phys ( buffer->addr, before.end ),
206
-	       user_to_phys ( buffer->addr, after.start ),
207
-	       user_to_phys ( buffer->addr, after.end ) );
197
+	DBGC2 ( buffer, "BUFFER %p split before [%lx,%lx) after [%lx,%lx)\n",
198
+		buffer, user_to_phys ( buffer->addr, before.start ),
199
+		user_to_phys ( buffer->addr, before.end ),
200
+		user_to_phys ( buffer->addr, after.start ),
201
+		user_to_phys ( buffer->addr, after.end ) );
208
 
202
 
209
 	/* Write back 'before' block, if any */
203
 	/* Write back 'before' block, if any */
210
 	if ( before.end == 0 ) {
204
 	if ( before.end == 0 ) {
211
 		/* No 'before' block: update buffer->fill */
205
 		/* No 'before' block: update buffer->fill */
212
 		buffer->fill = after.start;
206
 		buffer->fill = after.start;
213
-		DBGC ( buffer, "BUFFER %p full up to %lx\n", buffer,
214
-		       user_to_phys ( buffer->addr, buffer->fill ) );
207
+		DBGC2 ( buffer, "BUFFER %p full up to %lx\n", buffer,
208
+			user_to_phys ( buffer->addr, buffer->fill ) );
215
 	} else {
209
 	} else {
216
 		/* Write back 'before' block */
210
 		/* Write back 'before' block */
217
 		store_free_block ( buffer, &before );
211
 		store_free_block ( buffer, &before );
221
 	if ( after.end == buffer->len ) {
215
 	if ( after.end == buffer->len ) {
222
 		/* 'After' block is the final block: update buffer->free */
216
 		/* 'After' block is the final block: update buffer->free */
223
 		buffer->free = after.start;
217
 		buffer->free = after.start;
224
-		DBGC ( buffer, "BUFFER %p free from %lx onwards\n", buffer,
225
-		       user_to_phys ( buffer->addr, buffer->free ) );
218
+		DBGC2 ( buffer, "BUFFER %p free from %lx onwards\n", buffer,
219
+			user_to_phys ( buffer->addr, buffer->free ) );
226
 	} else {
220
 	} else {
227
 		/* Write back 'after' block */
221
 		/* Write back 'after' block */
228
 		store_free_block ( buffer, &after );
222
 		store_free_block ( buffer, &after );
233
 
227
 
234
 	return 0;
228
 	return 0;
235
 }
229
 }
230
+
231
+/** Expand data buffer
232
+ *
233
+ * @v buffer		Data buffer
234
+ * @v new_len		New length
235
+ * @ret rc		Return status code
236
+ *
237
+ * Expand the data buffer to accommodate more data.  Some buffers may
238
+ * not support being expanded.
239
+ */
240
+int expand_buffer ( struct buffer *buffer, size_t new_len ) {
241
+	int rc;
242
+
243
+	if ( new_len <= buffer->len )
244
+		return 0;
245
+
246
+	DBGC ( buffer, "BUFFER %p attempting to expand from length %zx to "
247
+	       "length %zx\n", buffer, buffer->len, new_len );
248
+
249
+	if ( ! buffer->expand ) {
250
+		DBGC ( buffer, "BUFFER %p is not expandable\n", buffer );
251
+		return -ENOBUFS;
252
+	}
253
+
254
+	if ( ( rc = buffer->expand ( buffer, new_len ) ) != 0 ) {
255
+		DBGC ( buffer, "BUFFER %p could not expand: %s\n",
256
+		       buffer, strerror ( rc ) );
257
+		return rc;
258
+	}
259
+
260
+	DBGC ( buffer, "BUFFER %p expanded to [%lx,%lx)\n", buffer,
261
+	       user_to_phys ( buffer->addr, 0 ),
262
+	       user_to_phys ( buffer->addr, buffer->len ) );
263
+
264
+	return 0;
265
+}

+ 12
- 4
src/core/ebuffer.c View File

40
 	userptr_t new_addr;
40
 	userptr_t new_addr;
41
 
41
 
42
 	/* Round new_len up to the nearest power of two, to reduce
42
 	/* Round new_len up to the nearest power of two, to reduce
43
-	 * total number of reallocations required.
43
+	 * total number of reallocations required.  Don't do this for
44
+	 * the first expansion; this allows for protocols that do
45
+	 * actually know the exact length in advance.
44
 	 */
46
 	 */
45
-	while ( actual_len < new_len )
46
-		actual_len <<= 1;
47
+	if ( buffer->len ) {
48
+		while ( actual_len < new_len )
49
+			actual_len <<= 1;
50
+	} else {
51
+		actual_len = new_len;
52
+	}
47
 
53
 
48
 	/* Reallocate buffer */
54
 	/* Reallocate buffer */
49
-	new_addr = urealloc ( buffer->addr, actual_len );
55
+#warning "urealloc() has issues with length zero"
56
+	new_addr = urealloc ( buffer->addr, // actual_len );
57
+			      actual_len ? actual_len : 1 );
50
 	if ( ! new_addr )
58
 	if ( ! new_addr )
51
 		return -ENOMEM;
59
 		return -ENOMEM;
52
 
60
 

+ 1
- 20
src/include/gpxe/buffer.h View File

101
 
101
 
102
 extern int fill_buffer ( struct buffer *buffer, const void *data,
102
 extern int fill_buffer ( struct buffer *buffer, const void *data,
103
 			 size_t offset, size_t len );
103
 			 size_t offset, size_t len );
104
-
105
-/** Expand data buffer
106
- *
107
- * @v buffer		Data buffer
108
- * @v new_len		New length
109
- * @ret rc		Return status code
110
- *
111
- * Expand the data buffer to accommodate more data.  Some buffers may
112
- * not support being expanded.
113
- */
114
-static inline int expand_buffer ( struct buffer *buffer, size_t new_len ) {
115
-
116
-	if ( new_len <= buffer->len )
117
-		return 0;
118
-
119
-	if ( ! buffer->expand )
120
-		return -ENOBUFS;
121
-
122
-	return buffer->expand ( buffer, new_len );
123
-}
104
+extern int expand_buffer ( struct buffer *buffer, size_t new_len );
124
 
105
 
125
 #endif /* _GPXE_BUFFER_H */
106
 #endif /* _GPXE_BUFFER_H */

Loading…
Cancel
Save