Browse Source

faux.buf: A bit of refactoring + faux_buf_will_be_overflow()

Serj Kalichev 3 years ago
parent
commit
4b831e73a6
3 changed files with 12 additions and 17 deletions
  1. 1 0
      faux/buf.h
  2. 10 17
      faux/buf/buf.c
  3. 1 0
      faux/faux.map

+ 1 - 0
faux/buf.h

@@ -20,6 +20,7 @@ void faux_buf_free(faux_buf_t *buf);
 ssize_t faux_buf_len(const faux_buf_t *buf);
 ssize_t faux_buf_limit(const faux_buf_t *buf);
 bool_t faux_buf_set_limit(faux_buf_t *buf, size_t limit);
+bool_t faux_buf_will_be_overflow(const faux_buf_t *buf, size_t add_len);
 size_t faux_buf_is_wlocked(const faux_buf_t *buf);
 size_t faux_buf_is_rlocked(const faux_buf_t *buf);
 ssize_t faux_buf_write(faux_buf_t *buf, const void *data, size_t len);

+ 10 - 17
faux/buf/buf.c

@@ -146,7 +146,7 @@ static ssize_t faux_buf_wavail(const faux_buf_t *buf)
 	if (!buf)
 		return -1;
 
-	if (faux_buf_chunk_num(buf) == 0)
+	if (!buf->wchunk)
 		return 0; // Empty list
 
 	return (buf->chunk_size - buf->wpos);
@@ -155,20 +155,19 @@ static ssize_t faux_buf_wavail(const faux_buf_t *buf)
 
 static ssize_t faux_buf_ravail(const faux_buf_t *buf)
 {
-	ssize_t num = 0;
-
 	assert(buf);
 	if (!buf)
 		return -1;
 
-	num = faux_buf_chunk_num(buf);
-	if (num == 0)
-		return 0; // Empty list
-	if (num > 1)
-		return (buf->chunk_size - buf->rpos);
+	// Empty list
+	if (buf->len == 0)
+		return 0;
+	// Read and write within the same chunk
+	if (faux_list_head(buf->list) == buf->wchunk)
+		return (buf->wpos - buf->rpos);
 
-	// Single chunk
-	return (buf->wpos - buf->rpos);
+	// Write pointer is far away from read pointer (more than chunk)
+	return (buf->chunk_size - buf->rpos);
 }
 
 
@@ -212,7 +211,7 @@ static faux_list_node_t *faux_buf_alloc_chunk(faux_buf_t *buf)
 }
 
 
-static bool_t faux_buf_will_be_overflow(const faux_buf_t *buf, size_t add_len)
+bool_t faux_buf_will_be_overflow(const faux_buf_t *buf, size_t add_len)
 {
 	assert(buf);
 	if (!buf)
@@ -228,12 +227,6 @@ static bool_t faux_buf_will_be_overflow(const faux_buf_t *buf, size_t add_len)
 }
 
 
-bool_t faux_buf_is_overflow(const faux_buf_t *buf)
-{
-	return faux_buf_will_be_overflow(buf, 0);
-}
-
-
 /** @brief buf data write.
  *
  * All given data will be stored to internal buffer (list of data chunks).

+ 1 - 0
faux/faux.map

@@ -324,6 +324,7 @@ FAUX_2.0 {
 		faux_buf_free;
 		faux_buf_len;
 		faux_buf_limit;
+		faux_buf_will_be_overflow;
 		faux_buf_set_limit;
 		faux_buf_is_wlocked;
 		faux_buf_is_rlocked;