[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 4/8] mbuffers: Add mbuffer_linearize.
From: |
Jonathan Bastien-Filiatrault |
Subject: |
[PATCH 4/8] mbuffers: Add mbuffer_linearize. |
Date: |
Wed, 8 Sep 2010 18:34:43 -0400 |
Signed-off-by: Jonathan Bastien-Filiatrault <address@hidden>
diff --git a/lib/gnutls_mbuffers.c b/lib/gnutls_mbuffers.c
index 45c4b97..c9af21d 100644
--- a/lib/gnutls_mbuffers.c
+++ b/lib/gnutls_mbuffers.c
@@ -272,3 +272,42 @@ _mbuffer_append_data (mbuffer_st *bufel, void* newdata,
size_t newdata_size)
return 0;
}
+
+/* Takes a buffer in multiple chunks and puts all the data in a single
+ * contiguous segment.
+ *
+ * Returns 0 on success or an error code otherwise.
+ *
+ * Cost: O(n)
+ * n: number of segments initially in the buffer
+ */
+int
+_mbuffer_linearize (mbuffer_head_st *buf)
+{
+ mbuffer_st *bufel, *cur;
+ gnutls_datum_t msg;
+ size_t pos=0;
+
+ if (buf->length <= 1)
+ /* Nothing to do */
+ return 0;
+
+ bufel = _mbuffer_alloc (buf->byte_length, buf->byte_length);
+ if (!bufel) {
+ gnutls_assert ();
+ return GNUTLS_E_MEMORY_ERROR;
+ }
+
+ for (cur = _mbuffer_get_first(buf, &msg);
+ msg.data != NULL;
+ cur = _mbuffer_get_next(cur, &msg))
+ {
+ memcpy (&bufel->msg.data[pos], msg.data, cur->msg.size);
+ pos += cur->msg.size;
+ }
+
+ _mbuffer_clear (buf);
+ _mbuffer_enqueue (buf, bufel);
+
+ return 0;
+}
diff --git a/lib/gnutls_mbuffers.h b/lib/gnutls_mbuffers.h
index ed94824..8f08a96 100644
--- a/lib/gnutls_mbuffers.h
+++ b/lib/gnutls_mbuffers.h
@@ -40,6 +40,7 @@ mbuffer_st* _mbuffer_get_next (mbuffer_st * cur,
gnutls_datum_t *msg);
* one.
*/
int _mbuffer_append_data (mbuffer_st *bufel, void* newdata, size_t
newdata_size);
+int _mbuffer_linearize (mbuffer_head_st *buf);
/* For "user" use. One can have buffer data and header.
--
1.7.1
- [PATCH] Read-side mbuffers, Jonathan Bastien-Filiatrault, 2010/09/08
- [PATCH 1/8] mbuffers: Document the internal mbuffer API., Jonathan Bastien-Filiatrault, 2010/09/08
- [PATCH 2/8] mbuffers: Make _mbuffer_remove_bytes return a meaningful error code., Jonathan Bastien-Filiatrault, 2010/09/08
- [PATCH 3/8] mbuffers: fix wrong size calculation., Jonathan Bastien-Filiatrault, 2010/09/08
- [PATCH 5/8] Parenthesize size calculations., Jonathan Bastien-Filiatrault, 2010/09/08
- [PATCH 4/8] mbuffers: Add mbuffer_linearize.,
Jonathan Bastien-Filiatrault <=
- [PATCH 6/8] mbuffers: make _gnutls_io_read_buffered use mbuffers., Jonathan Bastien-Filiatrault, 2010/09/08
- [PATCH 8/8] Fully mbufferize _gnutls_read and _gnutls_read_buffered., Jonathan Bastien-Filiatrault, 2010/09/08
- [PATCH 7/8] mbuffers: Add _mbuffer_xfree operation., Jonathan Bastien-Filiatrault, 2010/09/08
- Re: [PATCH] Read-side mbuffers, Nikos Mavrogiannopoulos, 2010/09/09