[ds6-devel] nc6/src circ_buf.c,1.8,1.9 circ_buf.h,1.7,1.8 readwrite.c,1.17,1.18

chris@deepspace6.net chris@deepspace6.net
Tue Dec 24 23:47:26 2002


Update of /cvs/nc6/src

Modified Files:
	circ_buf.c circ_buf.h readwrite.c 
Log Message:
Reworked circ_buf to work the same way as io_stream, etc (normalized function
names, changed construction/destruction to init/destroy).

BTW - Merry Christmas all!


Index: circ_buf.c
===================================================================
RCS file: /cvs/nc6/src/circ_buf.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- circ_buf.c	24 Dec 2002 19:50:56 -0000	1.8
+++ circ_buf.c	24 Dec 2002 23:47:24 -0000	1.9
@@ -32,53 +32,60 @@
 
 
 
-bool is_empty(const circ_buf *cb)
+#ifndef NDEBUG
+static void cb_assert(const circ_buf *cb)
 {
-	assert(cb != NULL);
-	assert(cb->data_size >= 0);
-	assert(cb->data_size <= cb->buf_size);
-
-	return (cb->data_size == 0 ? TRUE : FALSE);
+	if (cb == NULL ||
+	    cb->buf == NULL ||
+	    cb->ptr == NULL ||
+	    cb->buf_size < cb->data_size) 
+		fatal("internal error with circular buffers: please "
+		      "contact the authors of nc6 for bugfixing ;-)");
 }
+#else
+#define cb_assert(CB)	do {} while(0)
+#endif
 
 
 
-bool is_full(const circ_buf *cb)
+void cb_init(circ_buf *cb, size_t size)
 {
-	assert(cb != NULL);
-	assert(cb->data_size >= 0);
-	assert(cb->data_size <= cb->buf_size);
+	memset(cb, 0, sizeof(circ_buf));
+	
+	/* normalization: size must be a multiple of 16 */
+	if (size & 0xF) size = (size & ~0xF) + 0x10;
+	
+	cb->buf = (uint8_t *)xmalloc(size);
+	cb->ptr = cb->buf;
+	cb->data_size = 0;
+	cb->buf_size  = size;
 
-	return (cb->data_size == cb->buf_size ? TRUE : FALSE);
+	cb_assert(cb);
 }
 
 
 
-#ifndef NDEBUG
-void check_cb(const circ_buf *cb)
+void cb_destroy(circ_buf *cb)
 {
-	if (cb == NULL ||
-	    cb->buf == NULL ||
-	    cb->ptr == NULL ||
-	    cb->buf_size  < 0 ||
-	    cb->data_size < 0 ||
-	    cb->buf_size < cb->data_size) 
-		fatal("internal error with circular buffers: please "
-		      "contact the authors of nc6 for bugfixing ;-)");
+	assert(cb != NULL);
+	assert(cb->buf != NULL);
+
+	free(cb->buf);
+	cb->buf = NULL;
 }
-#endif
 
 
 
-int read_to_cb(int fd, circ_buf *cb)
+ssize_t cb_read(circ_buf *cb, int fd)
 {
-	int rr, count;
+	ssize_t rr;
+	int count;
 	struct iovec iov[2];
 
-	check_cb(cb);
+	cb_assert(cb);
 	
 	/* buffer is full, return an error condition */
-	if (cb->data_size == cb->buf_size) return -1;
+	if (cb_is_full(cb)) return -1;
 	
 	/* prepare for writing to buffer */
 	if (cb->ptr == cb->buf) {
@@ -114,7 +121,7 @@
 		cb->data_size += rr;
 		
 		/* sanity check */
-		check_cb(cb);
[...170 lines suppressed...]
Index: circ_buf.h
===================================================================
RCS file: /cvs/nc6/src/circ_buf.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- circ_buf.h	15 Dec 2002 17:27:08 -0000	1.7
+++ circ_buf.h	24 Dec 2002 23:47:24 -0000	1.8
@@ -30,28 +30,30 @@
 #endif
 
 typedef struct circ_buf_t {
-	uint8_t *buf;  /* pointer to the buffer */
-	uint8_t *ptr;  /* pointer to the beginning of written data */
-	int data_size; /* number of bytes that have been written 
-			* into the buffer */
-	int buf_size;  /* size of the buffer */
+	uint8_t *buf;      /* pointer to the buffer */
+	uint8_t *ptr;      /* pointer to the beginning of written data */
+	size_t data_size;  /* number of bytes that have been written 
+	                    * into the buffer */
+	size_t buf_size;   /* size of the buffer */
 } circ_buf;
 
-bool is_empty(const circ_buf *cb);
-bool is_full(const circ_buf *cb);
-int read_to_cb(int fd, circ_buf *cb);
-int copy_to_cb(const uint8_t *buf, size_t len, circ_buf *cb);
-int write_from_cb(int fd, circ_buf *cb);
-int send_from_cb(int fd, circ_buf *cb, struct sockaddr *dest, size_t destlen);
-void clear_cb(circ_buf *cb);
-circ_buf *alloc_cb(size_t size);
-void free_cb(circ_buf **cb);
 
-#ifdef NDEBUG
-#define check_cb(_x_)	do { } while(0)
-#else
-void check_cb(const circ_buf *cb);
-#endif
+void cb_init(circ_buf *cb, size_t size);
+void cb_destroy(circ_buf *cb);
+
+#define cb_size(CB)	((CB)->data_size)
+#define cb_space(CB)	((CB)->buf_size - (CB)->data_size)
+
+#define cb_is_empty(CB)	(cb_size(CB) == 0)
+#define cb_is_full(CB)	(cb_space(CB) == 0)
+
+ssize_t cb_read(circ_buf *cb, int fd);
+
+ssize_t cb_write(circ_buf *cb, int fd);
+ssize_t cb_send(circ_buf *cb, int fd, struct sockaddr *dest, size_t destlen);
 
+ssize_t cb_append(circ_buf *cb, const uint8_t *buf, size_t len);
+
+void cb_clear(circ_buf *cb);
 
 #endif /* CIRC_BUF_H */
Index: readwrite.c
===================================================================
RCS file: /cvs/nc6/src/readwrite.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- readwrite.c	24 Dec 2002 21:05:37 -0000	1.17
+++ readwrite.c	24 Dec 2002 23:47:24 -0000	1.18
@@ -52,10 +52,10 @@
 {
 	int rr, max_fd = -1;
 	fd_set read_fdset, tmp_rd_fdset, write_fdset, tmp_wr_fdset;
-	circ_buf *buf1 = NULL;
+	circ_buf buf1;
 	uint8_t *tbuf1 = NULL;
 	int tbuf1_bytes = 0, tbuf1_offset = 0;
-	circ_buf *buf2 = NULL;
+	circ_buf buf2;
 	struct timeval tv1, tv2;
 	struct timeval *tvp1, *tvp2, *tvp;
 	int retval = 0;
@@ -69,13 +69,13 @@
 	 * a temporary buffer to read into, then move the data gradually into
 	 * the circular buffer.  Further reads are suspended until all data
 	 * is moved */
-	buf1 = alloc_cb(BUFFER_SIZE);
+	cb_init(&buf1, BUFFER_SIZE);
 	if (ios1->socktype == SOCK_DGRAM)
 		tbuf1 = (uint8_t *)xmalloc(TEMP_BUFFER_SIZE);
 
 	/* since we know ios2 is local, it will be a stream - hence no need for
 	 * the temporary buffer */
-	buf2 = alloc_cb(BUFFER_SIZE);
+	cb_init(&buf2, BUFFER_SIZE);
 	assert(ios2->socktype != SOCK_DGRAM);
 
 	/* reset status */
@@ -126,25 +126,25 @@
 	 * side as well.
 	 */
 	while (is_read_open(ios1) || is_read_open(ios2) ||
-	       is_empty(buf1) == FALSE || is_empty(buf2) == FALSE)
+	       cb_is_empty(&buf1) == FALSE || cb_is_empty(&buf2) == FALSE)
 	{
 
 		/* sanity checks */
 		/* writefd should be set iff the buffer contains data */
-		assert(XOR(is_empty(buf1) == TRUE,
+		assert(XOR(cb_is_empty(&buf1) == TRUE,
 		       is_write_open(ios2) &&
 		         FD_ISSET(ios_writefd(ios2), &write_fdset)));
-		assert(XOR((is_empty(buf2) == TRUE),
+		assert(XOR((cb_is_empty(&buf2) == TRUE),
 		       is_write_open(ios1) &&
 		         FD_ISSET(ios_writefd(ios1), &write_fdset)));
 		/* readfd should be set (or closed) iff the buffer is not full
 		 * or tbuf1 is in use and it still contains data */
 		assert(XOR(
 		       (tbuf1 && tbuf1_bytes > 0) ||
-		         (!tbuf1 && is_full(buf1) == TRUE),
+		         (!tbuf1 && cb_is_full(&buf1) == TRUE),
 		       !is_read_open(ios1) ||
 		         FD_ISSET(ios_readfd(ios1), &read_fdset)));
-		assert(XOR(is_full(buf2) == TRUE,
+		assert(XOR(cb_is_full(&buf2) == TRUE,
 		       !is_read_open(ios2) ||
 		         FD_ISSET(ios_readfd(ios2), &read_fdset)));
 		/* if tbuf1 is not being used, tbuf1_bytes must be 0 */
@@ -200,7 +200,7 @@
 		    FD_ISSET(ios_readfd(ios1), &tmp_rd_fdset))
 		{
 			assert((tbuf1 && tbuf1_bytes == 0) ||
-			       (!tbuf1 && is_full(buf1) == FALSE));
+			       (!tbuf1 && cb_is_full(&buf1) == FALSE));
 
 			/* something is ready to read on ios1 (remote) */
 			if (ios1->socktype == SOCK_DGRAM) {
@@ -208,7 +208,7 @@
 				rr = recv(ios_readfd(ios1),
 				        (void *)tbuf1, TEMP_BUFFER_SIZE, 0);
 			} else {
-				rr = read_to_cb(ios_readfd(ios1), buf1);
+				rr = cb_read(&buf1, ios_readfd(ios1));
 			}
 			
 			if (rr > 0) {
@@ -222,7 +222,7 @@
 				/* forward any data in tbuf1 to buf1 */
 				if (tbuf1) {
 					int copied =
-					        copy_to_cb(tbuf1, rr, buf1);
+					        cb_append(&buf1, tbuf1, rr);
 					tbuf1_bytes = rr - copied;
 					tbuf1_offset = copied;
 					/* some MUST have gone into buf1 */
@@ -233,7 +233,7 @@
 				 * data OR if reading straight to buf1 and
 				 * it's full, then suspend further reading */
 				if ((tbuf1 && tbuf1_bytes > 0) ||
-				    (!tbuf1 && is_full(buf1) == TRUE))
[...118 lines suppressed...]



More information about the ds6-devel mailing list