mirror of
https://github.com/xcat2/xNBA.git
synced 2024-12-24 12:11:33 +00:00
Return -1 to indicate buffer overflow. Allow buffer fill level to be read
easily from struct buffer.
This commit is contained in:
parent
a89651f3bb
commit
bab2924e89
@ -35,7 +35,7 @@
|
||||
void init_buffer ( struct buffer *buffer, physaddr_t start, size_t len ) {
|
||||
buffer->start = start;
|
||||
buffer->end = start + len;
|
||||
buffer->first_free = start;
|
||||
buffer->fill = 0;
|
||||
|
||||
if ( len ) {
|
||||
char tail = 1;
|
||||
@ -59,7 +59,7 @@ static void split_free_block ( struct buffer_free_block *desc,
|
||||
if ( split >= desc->end )
|
||||
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 );
|
||||
|
||||
/* Create descriptor for new free block */
|
||||
@ -83,11 +83,11 @@ static inline void unfree_block ( struct buffer *buffer,
|
||||
physaddr_t prev_block ) {
|
||||
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 ) {
|
||||
DBG ( "BUFFER marking [%x,%x) as used\n",
|
||||
buffer->first_free, desc->end );
|
||||
buffer->first_free = desc->next_free;
|
||||
buffer->start + buffer->fill, desc->end );
|
||||
buffer->fill = desc->next_free - buffer->start;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -110,13 +110,10 @@ static inline void unfree_block ( struct buffer *buffer,
|
||||
* apart. If this condition is not satisfied, data corruption will
|
||||
* occur.
|
||||
*
|
||||
* Returns the offset to the first gap in the buffer. (When the
|
||||
* buffer is full, returns the offset to the byte past the end of the
|
||||
* buffer.)
|
||||
*
|
||||
* Returns 1 for success, 0 for failure (e.g. buffer too small).
|
||||
*/
|
||||
off_t fill_buffer ( struct buffer *buffer, void *data,
|
||||
off_t offset, size_t len ) {
|
||||
int fill_buffer ( struct buffer *buffer, void *data,
|
||||
off_t offset, size_t len ) {
|
||||
struct buffer_free_block desc;
|
||||
physaddr_t block, prev_block;
|
||||
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",
|
||||
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 */
|
||||
prev_block = 0;
|
||||
block = buffer->first_free;
|
||||
block = buffer->start + buffer->fill;
|
||||
while ( block < buffer->end ) {
|
||||
/* Read block descriptor */
|
||||
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",
|
||||
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"
|
||||
|
||||
/*
|
||||
* "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,
|
||||
* this byte indicates that the free block is the tail of the buffer,
|
||||
@ -15,24 +27,17 @@
|
||||
* smaller than a struct buffer_free_block.
|
||||
*
|
||||
*/
|
||||
|
||||
struct buffer_free_block {
|
||||
char tail;
|
||||
physaddr_t next_free;
|
||||
physaddr_t end;
|
||||
} __attribute__ (( packed ));
|
||||
|
||||
struct buffer {
|
||||
physaddr_t start;
|
||||
physaddr_t end;
|
||||
physaddr_t first_free;
|
||||
};
|
||||
|
||||
/* Functions in buffer.c */
|
||||
|
||||
extern void init_buffer ( struct buffer *buffer, physaddr_t start,
|
||||
size_t len );
|
||||
extern off_t fill_buffer ( struct buffer *buffer, void *data,
|
||||
off_t offset, size_t len );
|
||||
extern int fill_buffer ( struct buffer *buffer, void *data,
|
||||
off_t offset, size_t len );
|
||||
|
||||
#endif /* BUFFER_H */
|
||||
|
Loading…
Reference in New Issue
Block a user