mirror of
https://github.com/ipxe/ipxe
synced 2026-02-28 03:11:18 +03:00
Return -1 to indicate buffer overflow. Allow buffer fill level to be read
easily from struct buffer.
This commit is contained in:
@@ -35,7 +35,7 @@
|
|||||||
void init_buffer ( struct buffer *buffer, physaddr_t start, size_t len ) {
|
void init_buffer ( struct buffer *buffer, physaddr_t start, size_t len ) {
|
||||||
buffer->start = start;
|
buffer->start = start;
|
||||||
buffer->end = start + len;
|
buffer->end = start + len;
|
||||||
buffer->first_free = start;
|
buffer->fill = 0;
|
||||||
|
|
||||||
if ( len ) {
|
if ( len ) {
|
||||||
char tail = 1;
|
char tail = 1;
|
||||||
@@ -59,7 +59,7 @@ static void split_free_block ( struct buffer_free_block *desc,
|
|||||||
if ( split >= desc->end )
|
if ( split >= desc->end )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
DBG ( "BUFFER splitting [%x,%x) into [%x,%x) and [%x,%x)\n",
|
DBG ( "BUFFER splitting [%x,%x) -> [%x,%x) [%x,%x)\n",
|
||||||
block, desc->end, block, split, split, desc->end );
|
block, desc->end, block, split, split, desc->end );
|
||||||
|
|
||||||
/* Create descriptor for new free block */
|
/* Create descriptor for new free block */
|
||||||
@@ -83,11 +83,11 @@ static inline void unfree_block ( struct buffer *buffer,
|
|||||||
physaddr_t prev_block ) {
|
physaddr_t prev_block ) {
|
||||||
struct buffer_free_block prev_desc;
|
struct buffer_free_block prev_desc;
|
||||||
|
|
||||||
/* If this is the first block, just update first_free */
|
/* If this is the first block, just update buffer->fill */
|
||||||
if ( ! prev_block ) {
|
if ( ! prev_block ) {
|
||||||
DBG ( "BUFFER marking [%x,%x) as used\n",
|
DBG ( "BUFFER marking [%x,%x) as used\n",
|
||||||
buffer->first_free, desc->end );
|
buffer->start + buffer->fill, desc->end );
|
||||||
buffer->first_free = desc->next_free;
|
buffer->fill = desc->next_free - buffer->start;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,13 +110,10 @@ static inline void unfree_block ( struct buffer *buffer,
|
|||||||
* apart. If this condition is not satisfied, data corruption will
|
* apart. If this condition is not satisfied, data corruption will
|
||||||
* occur.
|
* occur.
|
||||||
*
|
*
|
||||||
* Returns the offset to the first gap in the buffer. (When the
|
* Returns 1 for success, 0 for failure (e.g. buffer too small).
|
||||||
* buffer is full, returns the offset to the byte past the end of the
|
|
||||||
* buffer.)
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
off_t fill_buffer ( struct buffer *buffer, void *data,
|
int fill_buffer ( struct buffer *buffer, void *data,
|
||||||
off_t offset, size_t len ) {
|
off_t offset, size_t len ) {
|
||||||
struct buffer_free_block desc;
|
struct buffer_free_block desc;
|
||||||
physaddr_t block, prev_block;
|
physaddr_t block, prev_block;
|
||||||
physaddr_t data_start, data_end;
|
physaddr_t data_start, data_end;
|
||||||
@@ -127,9 +124,16 @@ off_t fill_buffer ( struct buffer *buffer, void *data,
|
|||||||
DBG ( "BUFFER [%x,%x) writing portion [%x,%x)\n",
|
DBG ( "BUFFER [%x,%x) writing portion [%x,%x)\n",
|
||||||
buffer->start, buffer->end, data_start, data_end );
|
buffer->start, buffer->end, data_start, data_end );
|
||||||
|
|
||||||
|
/* Check buffer bounds */
|
||||||
|
if ( data_end > buffer->end ) {
|
||||||
|
DBG ( "BUFFER [%x,%x) too small for data!\n",
|
||||||
|
buffer->start, buffer->end );
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* Iterate through the buffer's free blocks */
|
/* Iterate through the buffer's free blocks */
|
||||||
prev_block = 0;
|
prev_block = 0;
|
||||||
block = buffer->first_free;
|
block = buffer->start + buffer->fill;
|
||||||
while ( block < buffer->end ) {
|
while ( block < buffer->end ) {
|
||||||
/* Read block descriptor */
|
/* Read block descriptor */
|
||||||
desc.next_free = buffer->end;
|
desc.next_free = buffer->end;
|
||||||
@@ -160,7 +164,7 @@ off_t fill_buffer ( struct buffer *buffer, void *data,
|
|||||||
}
|
}
|
||||||
|
|
||||||
DBG ( "BUFFER [%x,%x) full up to %x\n",
|
DBG ( "BUFFER [%x,%x) full up to %x\n",
|
||||||
buffer->start, buffer->end, buffer->first_free );
|
buffer->start, buffer->end, buffer->start + buffer->fill );
|
||||||
|
|
||||||
return ( buffer->first_free - buffer->start );
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,18 @@
|
|||||||
|
|
||||||
#include "stdint.h"
|
#include "stdint.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* "start" and "end" denote the real boundaries of the buffer. "fill"
|
||||||
|
* denotes the offset to the first free block in the buffer. (If the
|
||||||
|
* buffer is full, "fill" will equal ( end - start ) ).
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
struct buffer {
|
||||||
|
physaddr_t start;
|
||||||
|
physaddr_t end;
|
||||||
|
off_t fill;
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Free blocks in the buffer start with a "tail byte". If non-zero,
|
* Free blocks in the buffer start with a "tail byte". If non-zero,
|
||||||
* this byte indicates that the free block is the tail of the buffer,
|
* this byte indicates that the free block is the tail of the buffer,
|
||||||
@@ -15,24 +27,17 @@
|
|||||||
* smaller than a struct buffer_free_block.
|
* smaller than a struct buffer_free_block.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct buffer_free_block {
|
struct buffer_free_block {
|
||||||
char tail;
|
char tail;
|
||||||
physaddr_t next_free;
|
physaddr_t next_free;
|
||||||
physaddr_t end;
|
physaddr_t end;
|
||||||
} __attribute__ (( packed ));
|
} __attribute__ (( packed ));
|
||||||
|
|
||||||
struct buffer {
|
|
||||||
physaddr_t start;
|
|
||||||
physaddr_t end;
|
|
||||||
physaddr_t first_free;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Functions in buffer.c */
|
/* Functions in buffer.c */
|
||||||
|
|
||||||
extern void init_buffer ( struct buffer *buffer, physaddr_t start,
|
extern void init_buffer ( struct buffer *buffer, physaddr_t start,
|
||||||
size_t len );
|
size_t len );
|
||||||
extern off_t fill_buffer ( struct buffer *buffer, void *data,
|
extern int fill_buffer ( struct buffer *buffer, void *data,
|
||||||
off_t offset, size_t len );
|
off_t offset, size_t len );
|
||||||
|
|
||||||
#endif /* BUFFER_H */
|
#endif /* BUFFER_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user