/soc/2013/ankitkv/gobjectification: 68cf25486001: GObjectify Pur...
Ankit Vani
a at nevitus.org
Wed Jun 12 09:28:40 EDT 2013
Changeset: 68cf25486001fbcee3d0d2d31fa45dc12856a7bf
Author: Ankit Vani <a at nevitus.org>
Date: 2013-06-12 18:58 +0530
Branch: soc.2013.gobjectification
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/68cf25486001
Description:
GObjectify PurpleCircBuffer as PurpleCircularBuffer (from gobjectification branch)
diffstat:
libpurple/circbuffer.c | 433 +++++++++++++++++++++++----
libpurple/circbuffer.h | 77 ++--
libpurple/protocols/bonjour/jabber.c | 16 +-
libpurple/protocols/bonjour/jabber.h | 2 +-
libpurple/protocols/irc/irc.c | 15 +-
libpurple/protocols/irc/irc.h | 2 +-
libpurple/protocols/jabber/bosh.c | 44 +-
libpurple/protocols/jabber/jabber.c | 15 +-
libpurple/protocols/jabber/jabber.h | 2 +-
libpurple/protocols/jabber/si.c | 20 +-
libpurple/protocols/msn/httpconn.c | 14 +-
libpurple/protocols/msn/httpconn.h | 2 +-
libpurple/protocols/msn/servconn.c | 14 +-
libpurple/protocols/msn/servconn.h | 2 +-
libpurple/protocols/oscar/flap_connection.c | 17 +-
libpurple/protocols/oscar/oft.c | 4 +-
libpurple/protocols/oscar/oscar.h | 2 +-
libpurple/protocols/oscar/peer.c | 22 +-
libpurple/protocols/oscar/peer.h | 2 +-
libpurple/protocols/sametime/sametime.c | 20 +-
libpurple/protocols/simple/simple.c | 27 +-
libpurple/protocols/simple/simple.h | 2 +-
libpurple/protocols/yahoo/libymsg.c | 4 +-
libpurple/protocols/yahoo/libymsg.h | 2 +-
libpurple/protocols/yahoo/yahoo_packet.c | 11 +-
libpurple/protocols/yahoo/ycht.c | 13 +-
libpurple/protocols/yahoo/ycht.h | 2 +-
27 files changed, 559 insertions(+), 227 deletions(-)
diffs (truncated from 1724 to 300 lines):
diff --git a/libpurple/circbuffer.c b/libpurple/circbuffer.c
--- a/libpurple/circbuffer.c
+++ b/libpurple/circbuffer.c
@@ -27,127 +27,418 @@
#define DEFAULT_BUF_SIZE 256
-PurpleCircBuffer *
-purple_circ_buffer_new(gsize growsize) {
- PurpleCircBuffer *buf = g_new0(PurpleCircBuffer, 1);
- buf->growsize = growsize ? growsize : DEFAULT_BUF_SIZE;
- return buf;
-}
+#define PURPLE_CIRCULAR_BUFFER_GET_PRIVATE(obj) \
+ (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_CIRCULAR_BUFFER, PurpleCircularBufferPrivate))
-void purple_circ_buffer_destroy(PurpleCircBuffer *buf) {
- g_return_if_fail(buf != NULL);
+/******************************************************************************
+ * Structs
+ *****************************************************************************/
+typedef struct {
+ gchar *buffer;
+ gsize growsize;
+ gsize buflen;
+ gsize bufused;
+ gchar *input;
+ gchar *output;
+} PurpleCircularBufferPrivate;
- g_free(buf->buffer);
- g_free(buf);
-}
+/******************************************************************************
+ * Enums
+ *****************************************************************************/
+enum {
+ PROP_ZERO,
+ PROP_GROW_SIZE,
+ PROP_BUFFER_USED,
+ PROP_INPUT,
+ PROP_OUTPUT,
+ PROP_LAST,
+};
-static void grow_circ_buffer(PurpleCircBuffer *buf, gsize len) {
- int in_offset = 0, out_offset = 0;
- int start_buflen;
+/******************************************************************************
+ * Globals
+ *****************************************************************************/
+static GObjectClass *parent_class = NULL;
- g_return_if_fail(buf != NULL);
+/******************************************************************************
+ * Circular Buffer Implementation
+ *****************************************************************************/
+static void
+purple_circular_buffer_real_grow(PurpleCircularBuffer *buffer, gsize len) {
+ PurpleCircularBufferPrivate *priv = NULL;
+ gint in_offset = 0, out_offset = 0;
+ gint start_buflen;
- start_buflen = buf->buflen;
+ priv = PURPLE_CIRCULAR_BUFFER_GET_PRIVATE(buffer);
- while ((buf->buflen - buf->bufused) < len)
- buf->buflen += buf->growsize;
+ start_buflen = priv->buflen;
- if (buf->inptr != NULL) {
- in_offset = buf->inptr - buf->buffer;
- out_offset = buf->outptr - buf->buffer;
+ while((priv->buflen - priv->bufused) < len)
+ priv->buflen += priv->growsize;
+
+ if(priv->input != NULL) {
+ in_offset = priv->input - priv->buffer;
+ out_offset = priv->output - priv->buffer;
}
- buf->buffer = g_realloc(buf->buffer, buf->buflen);
+
+ priv->buffer = g_realloc(priv->buffer, priv->buflen);
/* adjust the fill and remove pointer locations */
- if (buf->inptr == NULL) {
- buf->inptr = buf->outptr = buf->buffer;
+ if(priv->input == NULL) {
+ priv->input = priv->output = priv->buffer;
} else {
- buf->inptr = buf->buffer + in_offset;
- buf->outptr = buf->buffer + out_offset;
+ priv->input = priv->buffer + in_offset;
+ priv->output = priv->buffer + out_offset;
}
/* If the fill pointer is wrapped to before the remove
* pointer, we need to shift the data */
- if (in_offset < out_offset
- || (in_offset == out_offset && buf->bufused > 0)) {
- int shift_n = MIN(buf->buflen - start_buflen,
- in_offset);
- memcpy(buf->buffer + start_buflen, buf->buffer,
- shift_n);
+ if(in_offset < out_offset
+ || (in_offset == out_offset && priv->bufused > 0))
+ {
+ gint shift_n = MIN(priv->buflen - start_buflen, in_offset);
+ memcpy(priv->buffer + start_buflen, priv->buffer, shift_n);
- /* If we couldn't fit the wrapped read buffer
- * at the end */
+ /* If we couldn't fit the wrapped read buffer at the end */
if (shift_n < in_offset) {
- memmove(buf->buffer,
- buf->buffer + shift_n,
- in_offset - shift_n);
- buf->inptr = buf->buffer +
- (in_offset - shift_n);
+ memmove(priv->buffer, priv->buffer + shift_n, in_offset - shift_n);
+ priv->input = priv->buffer + (in_offset - shift_n);
} else {
- buf->inptr = buf->buffer +
- start_buflen + in_offset;
+ priv->input = priv->buffer + start_buflen + in_offset;
}
}
}
-void purple_circ_buffer_append(PurpleCircBuffer *buf, gconstpointer src, gsize len) {
+static void
+purple_circular_buffer_real_append(PurpleCircularBuffer *buffer,
+ gconstpointer src, gsize len)
+{
+ PurpleCircularBufferPrivate *priv = NULL;
+ gint len_stored;
- int len_stored;
-
- g_return_if_fail(buf != NULL);
+ priv = PURPLE_CIRCULAR_BUFFER_GET_PRIVATE(buffer);
/* Grow the buffer, if necessary */
- if ((buf->buflen - buf->bufused) < len)
- grow_circ_buffer(buf, len);
+ if((priv->buflen - priv->bufused) < len)
+ purple_circular_buffer_grow(buffer, len);
/* If there is not enough room to copy all of src before hitting
* the end of the buffer then we will need to do two copies.
- * One copy from inptr to the end of the buffer, and the
+ * One copy from input to the end of the buffer, and the
* second copy from the start of the buffer to the end of src. */
- if (buf->inptr >= buf->outptr)
- len_stored = MIN(len, buf->buflen
- - (buf->inptr - buf->buffer));
+ if(priv->input >= priv->output)
+ len_stored = MIN(len, priv->buflen - (priv->input - priv->buffer));
else
len_stored = len;
- if (len_stored > 0)
- memcpy(buf->inptr, src, len_stored);
+ if(len_stored > 0)
+ memcpy(priv->input, src, len_stored);
- if (len_stored < len) {
- memcpy(buf->buffer, (char*)src + len_stored, len - len_stored);
- buf->inptr = buf->buffer + (len - len_stored);
+ if(len_stored < len) {
+ memcpy(priv->buffer, (char*)src + len_stored, len - len_stored);
+ priv->input = priv->buffer + (len - len_stored);
} else {
- buf->inptr += len_stored;
+ priv->input += len_stored;
}
- buf->bufused += len;
+ priv->bufused += len;
}
-gsize purple_circ_buffer_get_max_read(const PurpleCircBuffer *buf) {
+static gsize
+purple_circular_buffer_real_max_read_size(const PurpleCircularBuffer *buffer) {
+ PurpleCircularBufferPrivate *priv = NULL;
gsize max_read;
- g_return_val_if_fail(buf != NULL, 0);
+ priv = PURPLE_CIRCULAR_BUFFER_GET_PRIVATE(buffer);
- if (buf->bufused == 0)
+ if(priv->bufused == 0)
max_read = 0;
- else if ((buf->outptr - buf->inptr) >= 0)
- max_read = buf->buflen - (buf->outptr - buf->buffer);
+ else if((priv->output - priv->input) >= 0)
+ max_read = priv->buflen - (priv->output - priv->buffer);
else
- max_read = buf->inptr - buf->outptr;
+ max_read = priv->input - priv->output;
return max_read;
}
-gboolean purple_circ_buffer_mark_read(PurpleCircBuffer *buf, gsize len) {
- g_return_val_if_fail(buf != NULL, FALSE);
- g_return_val_if_fail(purple_circ_buffer_get_max_read(buf) >= len, FALSE);
+static gboolean
+purple_circular_buffer_real_mark_read(PurpleCircularBuffer *buffer,
+ gsize len)
+{
+ PurpleCircularBufferPrivate *priv = NULL;
- buf->outptr += len;
- buf->bufused -= len;
+ g_return_val_if_fail(purple_circular_buffer_get_max_read(buffer) >= len, FALSE);
+
+ priv = PURPLE_CIRCULAR_BUFFER_GET_PRIVATE(buffer);
+
+ priv->output += len;
+ priv->bufused -= len;
+
/* wrap to the start if we're at the end */
- if ((buf->outptr - buf->buffer) == buf->buflen)
- buf->outptr = buf->buffer;
+ if((priv->output - priv->buffer) == priv->buflen)
+ priv->output = priv->buffer;
return TRUE;
}
+/******************************************************************************
+ * Private API
+ *****************************************************************************/
+static void
+purple_circular_buffer_set_grow_size(PurpleCircularBuffer *buffer,
+ gsize grow_size)
+{
+ PurpleCircularBufferPrivate *priv =
+ PURPLE_CIRCULAR_BUFFER_GET_PRIVATE(buffer);
+
+ priv->growsize = (grow_size != 0) ? grow_size : DEFAULT_BUF_SIZE;
+
+ g_object_notify(G_OBJECT(buffer), "grow-size");
+}
+
+/******************************************************************************
+ * Object Stuff
+ *****************************************************************************/
+static void
+purple_circular_buffer_finalize(GObject *obj) {
+ PurpleCircularBufferPrivate *priv =
+ PURPLE_CIRCULAR_BUFFER_GET_PRIVATE(obj);
+
+ g_free(priv->buffer);
+
+ G_OBJECT_CLASS(parent_class)->finalize(obj);
+}
+
+static void
+purple_circular_buffer_get_property(GObject *obj, guint param_id,
+ GValue *value, GParamSpec *pspec)
+{
+ PurpleCircularBuffer *buffer = PURPLE_CIRCULAR_BUFFER(obj);
+
+ switch(param_id) {
+ case PROP_GROW_SIZE:
+ g_value_set_ulong(value,
+ purple_circular_buffer_get_grow_size(buffer));
+ break;
+ case PROP_BUFFER_USED:
+ g_value_set_ulong(value,
+ purple_circular_buffer_get_used(buffer));
+ break;
+ case PROP_INPUT:
+ g_value_set_pointer(value,
+ (void*) purple_circular_buffer_get_input(buffer));
+ break;
+ case PROP_OUTPUT:
+ g_value_set_pointer(value,
+ (void*) purple_circular_buffer_get_output(buffer));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
+ break;
+ }
+}
+
+static void
+purple_circular_buffer_set_property(GObject *obj, guint param_id,
+ const GValue *value, GParamSpec *pspec)
+{
+ PurpleCircularBuffer *buffer = PURPLE_CIRCULAR_BUFFER(obj);
+
+ switch(param_id) {
+ case PROP_GROW_SIZE:
+ purple_circular_buffer_set_grow_size(buffer,
+ g_value_get_uint(value));
+ break;
+ default:
More information about the Commits
mailing list