Browse Source

faux.async: read_cb() callback now gets faux_buf_t object instead allocated linear buffer

Serj Kalichev 2 years ago
parent
commit
0c5a7d2cf3
3 changed files with 11 additions and 11 deletions
  1. 2 1
      faux/async.h
  2. 4 9
      faux/async/async.c
  3. 5 1
      faux/async/testc_async.c

+ 2 - 1
faux/async.h

@@ -6,6 +6,7 @@
 #define _faux_async_h
 
 #include <faux/faux.h>
+#include <faux/buf.h>
 #include <faux/sched.h>
 
 #define FAUX_ASYNC_UNLIMITED 0
@@ -21,7 +22,7 @@ typedef struct faux_async_s faux_async_t;
 
 // Callback function prototypes
 typedef bool_t (*faux_async_read_cb_fn)(faux_async_t *async,
-	void *data, size_t len, void *user_data);
+	faux_buf_t *buf, size_t len, void *user_data);
 typedef bool_t (*faux_async_stall_cb_fn)(faux_async_t *async,
 	size_t len, void *user_data);
 

+ 4 - 9
faux/async/async.c

@@ -385,10 +385,8 @@ ssize_t faux_async_out(faux_async_t *async)
  *
  * Reads fd and puts data to internal buffer. It can't be blocked. If length of
  * data stored within internal buffer is greater or equal than "min" limit then
- * function will execute "read" callback. It allocates linear buffer, copies
- * data to it and give it to callback. Note this function will never free
- * allocated buffer. So callback must do it or it must be done later. Function
- * will not allocate buffer larger than "max" read limit. If "max" limit is "0"
+ * function will execute "read" callback. It gives faux_buf_t object to callback.
+ * If "max" limit is "0"
  * (it means indefinite) then function will pass all available data to callback.
  *
  * @param [in] async Allocated and initialized async I/O object.
@@ -431,20 +429,17 @@ ssize_t faux_async_in(faux_async_t *async)
 		// Check for amount of stored data
 		while ((bytes_stored = faux_buf_len(async->ibuf)) >= async->min) {
 			size_t copy_len = 0;
-			char *buf = NULL;
 
+			// Calculate length of user-requested block
 			if (FAUX_ASYNC_UNLIMITED == async->max) { // Indefinite
 				copy_len = bytes_stored; // Take all data
 			} else {
 				copy_len = (bytes_stored < async->max) ?
 					bytes_stored : async->max;
 			}
-			buf = faux_malloc(copy_len);
-			assert(buf);
-			faux_buf_read(async->ibuf, buf, copy_len);
 
 			// Execute callback
-			async->read_cb(async, buf,
+			async->read_cb(async, async->ibuf,
 				copy_len, async->read_udata);
 		}
 	} while (bytes_readed == locked_len);

+ 5 - 1
faux/async/testc_async.c

@@ -105,10 +105,14 @@ parse_error:
 }
 
 
-static bool_t read_cb(faux_async_t *async, void *data, size_t len, void *user_data)
+static bool_t read_cb(faux_async_t *async, faux_buf_t *buf, size_t len,
+	void *user_data)
 {
 	int fd = *((int *)user_data);
+	char *data = NULL;
 
+	data = malloc(len);
+	faux_buf_read(buf, data, len);
 	faux_write_block(fd, data, len);
 	faux_free(data);