From 6292bdf074113dd51fdde2123d0016ed4522cdd9 Mon Sep 17 00:00:00 2001 From: Vsevolod Buzinov Date: Wed, 26 May 2010 10:29:48 +0300 Subject: Adds memory leak test, not run automatically --- tests/Makefile.am | 10 ++++- tests/memory-leak-test.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 tests/memory-leak-test.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 99d5deb..e7fe19d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,4 @@ -TESTS = resource-test +TESTS = resource-test resource_test_SOURCES = resource-test.c ../src/resource.c @@ -6,5 +6,11 @@ resource_test_CFLAGS = -I$(top_srcdir)/src @DBUS_CFLAGS@ resource_test_LDADD = -lcheck \ @DBUS_LIBS@ -noinst_PROGRAMS = resource_test +memory_leak_test_SOURCES = memory-leak-test.c +memory_leak_test_CFLAGS = -I$(top_srcdir)/src @DBUS_CFLAGS@ +memory_leak_test_LDADD = $(top_builddir)/src/libresource-glib.la \ + $(top_builddir)/src/libresource.la \ + @DBUS_LIBS@ + +noinst_PROGRAMS = resource_test memory_leak_test diff --git a/tests/memory-leak-test.c b/tests/memory-leak-test.c new file mode 100644 index 0000000..0d47044 --- /dev/null +++ b/tests/memory-leak-test.c @@ -0,0 +1,115 @@ +#include +#include +#include + +#include + +#include + +static GMainLoop *main_loop; + +static int iterations; + +static gboolean destructor (void *data); + +static void grant_callback (resource_set_t *resource_set, + uint32_t resources, + void *userdata) +{ + char buf[512]; + + (void)resource_set; + (void)userdata; + + printf("*** %s(): granted resources %s\n", __FUNCTION__, + resmsg_res_str (resources, buf, sizeof(buf))); + + + if (resources != 0) { + resource_set_release(resource_set); + + iterations--; + + if (iterations <= 0) { + destructor(0); + } + } else { + resource_set_acquire(resource_set); + } + + + +} + +static void advice_callback (resource_set_t *resource_set, + uint32_t resources, + void *userdata) +{ + char buf[512]; + + (void)resource_set; + (void)userdata; + + printf("*** %s(): adviced resources %s\n", __FUNCTION__, + resmsg_res_str (resources, buf, sizeof(buf))); +} + +static gboolean destructor (void *data) +{ + printf("destructor\n"); + resource_set_t *resource_set = data; + + g_main_loop_quit(main_loop); + + return FALSE; +} + +static void schedule_destruction (resource_set_t *resource_set, guint ms) +{ + printf("schedule_destruction\n"); + g_timeout_add (ms, destructor, resource_set); +} + +static void create_mainloop (void) +{ + printf("create_mainloop\n"); + + if ((main_loop = g_main_loop_new (NULL, FALSE)) == NULL) { + printf("Can't create G-MainLoop\n"); + } +} + +static void destroy_mainloop (void) +{ + printf("destroy_mainloop\n"); + + g_main_loop_unref (main_loop); +} + +static void run_mainloop (void) +{ + printf("run_mainloop\n"); + + g_main_loop_run (main_loop); +} + + + + +int main(int argc, char* argv[]) { + resource_set_t *rs; + create_mainloop (); + + rs = resource_set_create("player", RESOURCE_AUDIO_PLAYBACK, 0, 0, grant_callback, 0); + + resource_set_configure_advice_callback (rs, advice_callback, NULL); + resource_set_configure_audio (rs, "fmradio", 0, NULL); + + iterations = 10; + if (argc > 1) iterations = atoi(argv[1]); + + resource_set_acquire(rs); + run_mainloop(); + + destroy_mainloop (); +} -- cgit v1.2.3