[asterisk-commits] twilson: branch twilson/sip_binarification r314472 - in /team/twilson/sip_bin...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Apr 20 15:08:05 CDT 2011


Author: twilson
Date: Wed Apr 20 15:08:03 2011
New Revision: 314472

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=314472
Log:
Add ast_buf_cmp, some more docs, and shorten buf struct member names

Modified:
    team/twilson/sip_binarification/include/asterisk/buffers.h
    team/twilson/sip_binarification/main/buffers.c
    team/twilson/sip_binarification/tests/test_buffers.c

Modified: team/twilson/sip_binarification/include/asterisk/buffers.h
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sip_binarification/include/asterisk/buffers.h?view=diff&rev=314472&r1=314471&r2=314472
==============================================================================
--- team/twilson/sip_binarification/include/asterisk/buffers.h (original)
+++ team/twilson/sip_binarification/include/asterisk/buffers.h Wed Apr 20 15:08:03 2011
@@ -118,7 +118,17 @@
 int ast_buf_make_space(struct ast_buf **buf, size_t new_len);
 #endif
 
+/*!\brief Copy the contents of one buffer to another
+ * \param dst The address of a pointer to an ast_buf created with ast_buf_create() to copy to
+ * \param src The buffer to copy
+ */
 int ast_buf_copy_buffer(struct ast_buf **dst, struct ast_buf *src);
+
+/*!\brief Compare two ast_bufs
+ * \retval -1 Not equal
+ * \retval 0 Eual
+ */
+int ast_buf_cmp(struct ast_buf *one, struct ast_buf *two);
 
 /*!
  * \brief Retrieve a thread locally stored dynamic buffer

Modified: team/twilson/sip_binarification/main/buffers.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sip_binarification/main/buffers.c?view=diff&rev=314472&r1=314471&r2=314472
==============================================================================
--- team/twilson/sip_binarification/main/buffers.c (original)
+++ team/twilson/sip_binarification/main/buffers.c Wed Apr 20 15:08:03 2011
@@ -35,19 +35,19 @@
  * struct ast_threadstorage pointer.
  */
 struct ast_buf {
-	size_t __AST_BUF_SIZE;			/*!< The current maximum length of the buffer */
-	size_t __AST_BUF_USED;			/*!< Amount of space used */
+	size_t size;			/*!< The current maximum length of the buffer */
+	size_t used;			/*!< Amount of space used */
 	struct ast_threadstorage *__AST_BUF_TS;	/*!< What kind of storage is this ? */
 #define DS_MALLOC	((struct ast_threadstorage *)1)
 #define DS_ALLOCA	((struct ast_threadstorage *)2)
 #define DS_STATIC	((struct ast_threadstorage *)3)	/* not supported yet */
-	unsigned char __AST_BUF_BUFFER[0];			/*!< The buffer buffer */
+	unsigned char buffer[0];			/*!< The buffer buffer */
 };
 
 static int __ast_buf_helper(struct ast_buf **buf, ssize_t maxlen, const unsigned char *src, size_t srclen, int append)
 {
 	int dynamic = 0;
-	size_t left = (*buf)->__AST_BUF_SIZE - (*buf)->__AST_BUF_USED;
+	size_t left = (*buf)->size - (*buf)->used;
 	unsigned char *ptr;
 
 	switch (maxlen) {
@@ -62,14 +62,14 @@
 	}
 
 	if (left < srclen && dynamic) {
-		if (ast_buf_make_space(buf, MAX((*buf)->__AST_BUF_SIZE * 2, srclen - left + (*buf)->__AST_BUF_SIZE))) {
+		if (ast_buf_make_space(buf, MAX((*buf)->size * 2, srclen - left + (*buf)->size))) {
 			return -1;
 		}
 	}
 
-	ptr = append ? &((*buf)->__AST_BUF_BUFFER[(*buf)->__AST_BUF_USED]) : (*buf)->__AST_BUF_BUFFER;
+	ptr = append ? &((*buf)->buffer[(*buf)->used]) : (*buf)->buffer;
 	memcpy(ptr, src, srclen);
-	(*buf)->__AST_BUF_USED += srclen;
+	(*buf)->used += srclen;
 
 	return 0;
 }
@@ -84,10 +84,10 @@
 	if (buf == NULL)
 		return NULL;
 
-	buf->__AST_BUF_SIZE = init_len;
-	buf->__AST_BUF_USED = 0;
+	buf->size = init_len;
+	buf->used = 0;
 	buf->__AST_BUF_TS = DS_MALLOC;
-	buf->__AST_BUF_BUFFER[init_len] = '\0';
+	buf->buffer[init_len] = '\0';
 
 	return buf;
 }
@@ -100,10 +100,10 @@
 	if (buf == NULL)
 		return NULL;
 
-	buf->__AST_BUF_SIZE = init_len;
-	buf->__AST_BUF_USED = 0;
+	buf->size = init_len;
+	buf->used = 0;
 	buf->__AST_BUF_TS = DS_MALLOC;
-	buf->__AST_BUF_BUFFER[init_len] = '\0';
+	buf->buffer[init_len] = '\0';
 
 	return buf;
 }
@@ -112,7 +112,7 @@
 void ast_buf_reset(struct ast_buf *buf)
 {
 	if (buf) {
-		buf->__AST_BUF_USED = 0;
+		buf->used = 0;
 	}
 }
 
@@ -120,34 +120,34 @@
 {
 	size_t x;
 
-	for (x = 0; x < buf->__AST_BUF_SIZE; x++) {
-		buf->__AST_BUF_BUFFER[x] = '\0';
+	for (x = 0; x < buf->size; x++) {
+		buf->buffer[x] = '\0';
 	}
 }
 
 size_t attribute_pure ast_buf_len(const struct ast_buf *buf)
 {
-	return buf->__AST_BUF_USED;
+	return buf->used;
 }
 
 size_t attribute_pure ast_buf_size(const struct ast_buf *buf)
 {
-	return buf->__AST_BUF_SIZE;
+	return buf->size;
 }
 
 const unsigned char * attribute_pure ast_buf_buffer(const struct ast_buf *buf)
 {
-	return buf->__AST_BUF_BUFFER;
+	return buf->buffer;
 }
 
 unsigned char *ast_buf_truncate(struct ast_buf *buf, ssize_t len)
 {
 	if (len < 0) {
-		buf->__AST_BUF_USED += ((ssize_t) abs(len)) > (ssize_t) buf->__AST_BUF_USED ? -buf->__AST_BUF_USED : len;
+		buf->used += ((ssize_t) abs(len)) > (ssize_t) buf->used ? -buf->used : len;
 	} else {
-		buf->__AST_BUF_USED = len;
-	}
-	return buf->__AST_BUF_BUFFER;
+		buf->used = len;
+	}
+	return buf->buffer;
 }
 
 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
@@ -155,7 +155,7 @@
 {
 	struct ast_buf *old_buf = *buf;
 
-	if (new_len <= (*buf)->__AST_BUF_SIZE) 
+	if (new_len <= (*buf)->size) 
 		return 0;	/* success */
 	if ((*buf)->__AST_BUF_TS == DS_ALLOCA || (*buf)->__AST_BUF_TS == DS_STATIC)
 		return -1;	/* cannot extend */
@@ -171,8 +171,8 @@
 #endif /* defined(DEBUG_THREADLOCALS) */
 	}
 
-	(*buf)->__AST_BUF_SIZE = new_len;
-	(*buf)->__AST_BUF_BUFFER[new_len] = '\0';
+	(*buf)->size = new_len;
+	(*buf)->buffer[new_len] = '\0';
 	return 0;
 }
 #define ast_buf_make_space(a,b)	_ast_buf_make_space(a,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
@@ -181,7 +181,7 @@
 {
 	struct ast_buf *old_buf = *buf;
 
-	if (new_len <= (*buf)->__AST_BUF_SIZE) 
+	if (new_len <= (*buf)->size) 
 		return 0;	/* success */
 	if ((*buf)->__AST_BUF_TS == DS_ALLOCA || (*buf)->__AST_BUF_TS == DS_STATIC)
 		return -1;	/* cannot extend */
@@ -197,8 +197,8 @@
 #endif /* defined(DEBUG_THREADLOCALS) */
 	}
 
-	(*buf)->__AST_BUF_SIZE = new_len;
-	(*buf)->__AST_BUF_BUFFER[new_len] = '\0';
+	(*buf)->size = new_len;
+	(*buf)->buffer[new_len] = '\0';
 	return 0;
 }
 #endif
@@ -207,15 +207,20 @@
 {
 
 	/* make sure our destination is large enough */
-	if (src->__AST_BUF_USED > (*dst)->__AST_BUF_SIZE) {
-		if (ast_buf_make_space(dst, src->__AST_BUF_USED)) {
+	if (src->used > (*dst)->size) {
+		if (ast_buf_make_space(dst, src->used)) {
 			return -1;
 		}
 	}
 
-	memcpy((*dst)->__AST_BUF_BUFFER, src->__AST_BUF_BUFFER, src->__AST_BUF_USED);
-	(*dst)->__AST_BUF_USED = src->__AST_BUF_USED;
-	return 0;
+	memcpy((*dst)->buffer, src->buffer, src->used);
+	(*dst)->used = src->used;
+	return 0;
+}
+
+int ast_buf_cmp(struct ast_buf *one, struct ast_buf *two)
+{
+	return one->used == two->used ? memcmp(one->buffer, two->buffer, one->used) : -1;
 }
 
 #if !defined(DEBUG_THREADLOCALS)
@@ -227,11 +232,11 @@
 	if (buf == NULL)
 		return NULL;
 
-	if (!buf->__AST_BUF_SIZE) {
-		buf->__AST_BUF_SIZE = init_len;
-		buf->__AST_BUF_USED = 0;
+	if (!buf->size) {
+		buf->size = init_len;
+		buf->used = 0;
 		buf->__AST_BUF_TS = ts;
-		buf->__AST_BUF_BUFFER[init_len] = '\0';
+		buf->buffer[init_len] = '\0';
 	}
 
 	return buf;
@@ -246,9 +251,9 @@
 	if (buf == NULL)
 		return NULL;
 
-	if (!buf->__AST_BUF_SIZE) {
-		buf->__AST_BUF_SIZE = init_len;
-		buf->__AST_BUF_USED = 0;
+	if (!buf->size) {
+		buf->size = init_len;
+		buf->used = 0;
 		buf->__AST_BUF_TS = ts;
 		buf->AST_BUF_BUFFER[init_len] = '\0';
 	}

Modified: team/twilson/sip_binarification/tests/test_buffers.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sip_binarification/tests/test_buffers.c?view=diff&rev=314472&r1=314471&r2=314472
==============================================================================
--- team/twilson/sip_binarification/tests/test_buffers.c (original)
+++ team/twilson/sip_binarification/tests/test_buffers.c Wed Apr 20 15:08:03 2011
@@ -217,6 +217,12 @@
 		goto cleanup;
 	}
 
+	if (ast_buf_cmp(buf, bufcopy)) {
+		ast_test_status_update(test, "ast_buf_cmp returned different result from memcmp\n");
+		res = AST_TEST_FAIL;
+		goto cleanup;
+	}
+
 	ast_buf_reset(buf);
 	ast_buf_zero(buf);
 




More information about the asterisk-commits mailing list