THRIFT-2980 Accept external buffer in thrift_memory_buffer constructor
This closes #821
diff --git a/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.c b/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.c
index 8d66c3d..b96db35 100644
--- a/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.c
+++ b/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.c
@@ -32,7 +32,9 @@
enum _ThriftMemoryBufferProperties
{
PROP_0,
- PROP_THRIFT_MEMORY_BUFFER_BUFFER_SIZE
+ PROP_THRIFT_MEMORY_BUFFER_BUFFER_SIZE,
+ PROP_THRIFT_MEMORY_BUFFER_BUFFER,
+ PROP_THRIFT_MEMORY_BUFFER_OWNER
};
G_DEFINE_TYPE(ThriftMemoryBuffer, thrift_memory_buffer, THRIFT_TYPE_TRANSPORT)
@@ -141,24 +143,24 @@
return TRUE;
}
-/* initializes the instance */
+/* initializes class before constructor properties are set */
static void
-thrift_memory_buffer_init (ThriftMemoryBuffer *transport)
+thrift_memory_buffer_init (ThriftMemoryBuffer *t)
{
- transport->buf = g_byte_array_new ();
+ THRIFT_UNUSED_VAR (t);
}
/* destructor */
static void
thrift_memory_buffer_finalize (GObject *object)
{
- ThriftMemoryBuffer *transport = THRIFT_MEMORY_BUFFER (object);
+ ThriftMemoryBuffer *t = THRIFT_MEMORY_BUFFER (object);
- if (transport->buf != NULL)
+ if (t->owner && t->buf != NULL)
{
- g_byte_array_free (transport->buf, TRUE);
+ g_byte_array_unref (t->buf);
}
- transport->buf = NULL;
+ t->buf = NULL;
}
/* property accessor */
@@ -166,14 +168,23 @@
thrift_memory_buffer_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec)
{
- ThriftMemoryBuffer *transport = THRIFT_MEMORY_BUFFER (object);
+ ThriftMemoryBuffer *t = THRIFT_MEMORY_BUFFER (object);
THRIFT_UNUSED_VAR (pspec);
switch (property_id)
{
case PROP_THRIFT_MEMORY_BUFFER_BUFFER_SIZE:
- g_value_set_uint (value, transport->buf_size);
+ g_value_set_uint (value, t->buf_size);
+ break;
+ case PROP_THRIFT_MEMORY_BUFFER_BUFFER:
+ g_value_set_pointer (value, (gpointer) (t->buf));
+ break;
+ case PROP_THRIFT_MEMORY_BUFFER_OWNER:
+ g_value_set_boolean (value, t->owner);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
@@ -183,18 +194,40 @@
thrift_memory_buffer_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec)
{
- ThriftMemoryBuffer *transport = THRIFT_MEMORY_BUFFER (object);
+ ThriftMemoryBuffer *t = THRIFT_MEMORY_BUFFER (object);
THRIFT_UNUSED_VAR (pspec);
switch (property_id)
{
case PROP_THRIFT_MEMORY_BUFFER_BUFFER_SIZE:
- transport->buf_size = g_value_get_uint (value);
+ t->buf_size = g_value_get_uint (value);
+ break;
+ case PROP_THRIFT_MEMORY_BUFFER_BUFFER:
+ t->buf = (GByteArray*) g_value_get_pointer (value);
+ break;
+ case PROP_THRIFT_MEMORY_BUFFER_OWNER:
+ t->owner = g_value_get_boolean (value);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
+/* initializes class after constructor properties are set */
+static void
+thrift_memory_buffer_constructed (GObject *object)
+{
+ ThriftMemoryBuffer *t = THRIFT_MEMORY_BUFFER (object);
+
+ if (t->buf == NULL) {
+ t->buf = g_byte_array_new ();
+ }
+
+ G_OBJECT_CLASS (thrift_memory_buffer_parent_class)->constructed (object);
+}
+
/* initializes the class */
static void
thrift_memory_buffer_class_init (ThriftMemoryBufferClass *cls)
@@ -209,16 +242,38 @@
param_spec = g_param_spec_uint ("buf_size",
"buffer size (construct)",
- "Set the read buffer size",
+ "Set the read/write buffer size limit",
0, /* min */
- 1048576, /* max, 1024*1024 */
- 512, /* default value */
+ G_MAXUINT32, /* max */
+ G_MAXUINT32, /* default */
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_READWRITE);
g_object_class_install_property (gobject_class,
PROP_THRIFT_MEMORY_BUFFER_BUFFER_SIZE,
param_spec);
+ param_spec = g_param_spec_pointer ("buf",
+ "internal buffer (GByteArray)",
+ "Set the internal buffer (GByteArray)",
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_READWRITE);
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_MEMORY_BUFFER_BUFFER,
+ param_spec);
+
+ param_spec = g_param_spec_boolean ("owner",
+ "internal buffer memory management policy",
+ "Set whether internal buffer should be"
+ " unreferenced when thrift_memory_buffer"
+ " is finalized",
+ TRUE,
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_READWRITE);
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_MEMORY_BUFFER_OWNER,
+ param_spec);
+
+ gobject_class->constructed = thrift_memory_buffer_constructed;
gobject_class->finalize = thrift_memory_buffer_finalize;
ttc->is_open = thrift_memory_buffer_is_open;
ttc->open = thrift_memory_buffer_open;
diff --git a/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.h b/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.h
index 4ebe98f..d5d47b3 100644
--- a/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.h
+++ b/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.h
@@ -51,6 +51,7 @@
/* private */
GByteArray *buf;
guint32 buf_size;
+ gboolean owner;
};
typedef struct _ThriftMemoryBufferClass ThriftMemoryBufferClass;
diff --git a/lib/c_glib/test/testmemorybuffer.c b/lib/c_glib/test/testmemorybuffer.c
index 5c75273..2ad8c0f 100755
--- a/lib/c_glib/test/testmemorybuffer.c
+++ b/lib/c_glib/test/testmemorybuffer.c
@@ -25,13 +25,35 @@
#include <thrift/c_glib/transport/thrift_server_transport.h>
#include <thrift/c_glib/transport/thrift_server_socket.h>
-#define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
+static const gchar TEST_DATA[11] = "abcdefghij";
#include "../src/thrift/c_glib/transport/thrift_memory_buffer.c"
/* test object creation and destruction */
static void
-test_create_and_destroy(void)
+test_create_and_destroy (void)
+{
+ GObject *object = NULL;
+ object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
+ "buf_size", 10,
+ NULL);
+ assert (object != NULL);
+ g_object_unref (object);
+}
+
+static void
+test_create_and_destroy_large (void)
+{
+ GObject *object = NULL;
+ object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
+ "buf_size", 10 * 1024 * 1024,
+ NULL);
+ assert (object != NULL);
+ g_object_unref (object);
+}
+
+static void
+test_create_and_destroy_default (void)
{
GObject *object = NULL;
object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
@@ -40,31 +62,68 @@
}
static void
-test_open_and_close(void)
+test_create_and_destroy_external (void)
+{
+ GObject *object = NULL;
+ GByteArray *buf = g_byte_array_new ();
+ assert (buf != NULL);
+ object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
+ "buf", buf,
+ NULL);
+ assert (object != NULL);
+ g_object_unref (object);
+}
+
+static void
+test_create_and_destroy_unowned (void)
+{
+ GObject *object = NULL;
+ GValue val = G_VALUE_INIT;
+ GByteArray *buf;
+
+ object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
+ "owner", FALSE,
+ NULL);
+ assert (object != NULL);
+
+ g_value_init (&val, G_TYPE_POINTER);
+ g_object_get_property (object, "buf", &val);
+ buf = (GByteArray*) g_value_get_pointer (&val);
+ assert (buf != NULL);
+
+ g_byte_array_unref (buf);
+ g_value_unset (&val);
+ g_object_unref (object);
+}
+
+static void
+test_open_and_close (void)
{
ThriftMemoryBuffer *tbuffer = NULL;
/* create a ThriftMemoryBuffer */
tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
- /* this shouldn't work */
+ /* no-ops */
assert (thrift_memory_buffer_open (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
assert (thrift_memory_buffer_is_open (THRIFT_TRANSPORT (tbuffer)) == TRUE);
assert (thrift_memory_buffer_close (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
+
g_object_unref (tbuffer);
}
static void
-test_read_and_write(void)
+test_read_and_write (void)
{
ThriftMemoryBuffer *tbuffer = NULL;
- guchar buf[10] = TEST_DATA;
- guchar read[10];
+ gint got, want;
+ gchar read[10];
+ gchar *b;
GError *error = NULL;
tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 5, NULL);
assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
- (gpointer) buf,
+ (gpointer) TEST_DATA,
10, &error) == FALSE);
assert (error != NULL);
g_error_free (error);
@@ -73,26 +132,93 @@
tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 15, NULL);
assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
- (gpointer) buf, 10, &error) == TRUE);
+ (gpointer) TEST_DATA, 10, &error) == TRUE);
assert (error == NULL);
- assert (thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
- &read, 10, &error) > 0);
+ memset(read, 0, 10);
+ b = read;
+ want = 10;
+ while (want > 0) {
+ got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
+ (gpointer) b, want, &error);
+ assert (got > 0 && got <= want);
+ assert (error == NULL);
+ b += got;
+ want -= got;
+ }
+ assert (memcmp (read, TEST_DATA, 10) == 0);
+ g_object_unref (tbuffer);
+}
+
+static void
+test_read_and_write_default (void)
+{
+ ThriftMemoryBuffer *tbuffer = NULL;
+ gint got, want, i;
+ gchar read[10];
+ gchar *b;
+ GError *error = NULL;
+
+ tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
+ for (i = 0; i < 100; ++i) {
+ assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
+ (gpointer) TEST_DATA, 10, &error) == TRUE);
+ assert (error == NULL);
+ }
+
+ for (i = 0; i < 100; ++i) {
+ memset(read, 0, 10);
+ b = read;
+ want = 10;
+ while (want > 0) {
+ got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
+ (gpointer) b, want, &error);
+ assert (got > 0 && got <= want);
+ assert (error == NULL);
+ b += got;
+ want -= got;
+ }
+ assert (memcmp (read, TEST_DATA, 10) == 0);
+ }
+ g_object_unref (tbuffer);
+}
+
+static void
+test_read_and_write_external (void)
+{
+ ThriftMemoryBuffer *tbuffer = NULL;
+ gchar *b;
+ GError *error = NULL;
+ GByteArray *buf = g_byte_array_new ();
+ assert (buf != NULL);
+
+ tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf", buf, NULL);
+ assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
+ (gpointer) TEST_DATA, 10, &error) == TRUE);
assert (error == NULL);
+
+ assert (memcmp (buf->data, TEST_DATA, 10) == 0);
+ g_object_unref (tbuffer);
}
int
main(int argc, char *argv[])
{
#if (!GLIB_CHECK_VERSION (2, 36, 0))
- g_type_init();
+ g_type_init ();
#endif
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/testmemorybuffer/CreateAndDestroy", test_create_and_destroy);
+ g_test_add_func ("/testmemorybuffer/CreateAndDestroyLarge", test_create_and_destroy_large);
+ g_test_add_func ("/testmemorybuffer/CreateAndDestroyUnlimited", test_create_and_destroy_default);
+ g_test_add_func ("/testmemorybuffer/CreateAndDestroyExternal", test_create_and_destroy_external);
+ g_test_add_func ("/testmemorybuffer/CreateAndDestroyUnowned", test_create_and_destroy_unowned);
g_test_add_func ("/testmemorybuffer/OpenAndClose", test_open_and_close);
g_test_add_func ("/testmemorybuffer/ReadAndWrite", test_read_and_write);
+ g_test_add_func ("/testmemorybuffer/ReadAndWriteUnlimited", test_read_and_write_default);
+ g_test_add_func ("/testmemorybuffer/ReadAndWriteExternal", test_read_and_write_external);
return g_test_run ();
}