Hi all
I wrote a small patch to enable glib's memory statistics support for
vala programs.
It is trivial, but gives the programmer the possibility to use the
mem_profile() call. This function needs an initialization done by the
commandline option from this patch first.
There seems to be some interest to use this function:
http://mail.gnome.org/archives/vala-list/2009-December/msg00128.html
g_mem_profile is described like this in documentation:
...
It outputs the frequency of allocations of different sizes, the total
number of bytes which have been allocated, the total number of bytes
which have been freed, and the difference between the previous two
values, i.e. the number of bytes still in use.
... and it prints a nice table to the terminal.
So, if anybody is interested feel free to add this patch to vala.
I added a testcase ("memory_int32.vala"). If you compile it with a
patched vala and the -C switch, you can see the call to
"g_mem_set_vtable (glib_mem_profiler_table);"
before anything else in the program in the main function. This is
necessary to use the memory statistics functionality.
Regards
Jörn Magens
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index ef8c41d..880e128 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -756,7 +756,14 @@ internal class Vala.CCodeMethodModule : CCodeStructModule {
var thread_init_call = new CCodeFunctionCall (new CCodeIdentifier ("g_thread_init"));
thread_init_call.line = cmain.line;
thread_init_call.add_argument (new CCodeConstant ("NULL"));
- main_block.add_statement (new CCodeExpressionStatement (thread_init_call));
+ main_block.add_statement (new CCodeExpressionStatement (thread_init_call));
+ }
+
+ if (context.mem_profile) {
+ var mem_profile_init_call = new CCodeFunctionCall (new CCodeIdentifier ("g_mem_set_vtable"));
+ mem_profile_init_call.line = cmain.line;
+ mem_profile_init_call.add_argument (new CCodeConstant ("glib_mem_profiler_table"));
+ main_block.add_statement (new CCodeExpressionStatement (mem_profile_init_call));
}
var type_init_call = new CCodeExpressionStatement (new CCodeFunctionCall (new CCodeIdentifier ("g_type_init")));
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index bddaa1f..6b9f81f 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -55,6 +55,7 @@ class Vala.Compiler {
static string output;
static bool debug;
static bool thread;
+ static bool mem_profile;
static bool disable_assert;
static bool enable_checking;
static bool deprecated;
@@ -100,6 +101,7 @@ class Vala.Compiler {
{ "output", 'o', 0, OptionArg.FILENAME, ref output, "Place output in file FILE", "FILE" },
{ "debug", 'g', 0, OptionArg.NONE, ref debug, "Produce debug information", null },
{ "thread", 0, 0, OptionArg.NONE, ref thread, "Enable multithreading support", null },
+ { "mem_profile", 0, 0, OptionArg.NONE, ref mem_profile, "Enable GLib Memory statistics support", null },
{ "define", 'D', 0, OptionArg.STRING_ARRAY, ref defines, "Define SYMBOL", "SYMBOL..." },
{ "main", 0, 0, OptionArg.STRING, ref entry_point, "Use SYMBOL as entry point", "SYMBOL..." },
{ "disable-assert", 0, 0, OptionArg.NONE, ref disable_assert, "Disable assertions", null },
@@ -235,6 +237,7 @@ class Vala.Compiler {
}
context.debug = debug;
context.thread = thread;
+ context.mem_profile = mem_profile;
context.save_temps = save_temps;
if (profile == "posix") {
context.profile = Profile.POSIX;
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index e9e694d..9812ab4 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -119,6 +119,11 @@ public class Vala.CodeContext {
public bool thread { get; set; }
/**
+ * Enable g_mem_profile support.
+ */
+ public bool mem_profile { get; set; }
+
+ /**
* Specifies the optional module initialization method.
*/
public Method module_init_method { get; set; }
// compile with valac --mem_profile memory_int32.vala
public class DataObj {
public int32 _val;
public DataObj(int32 val) {
this._val = val;
}
}
public class Test : Object {
private DataObj[] n_array;
public uint timeout;
public Test() {
n_array = {};
print("constructed\n");
}
public void run() {
print("running\n");
for(int i = 0; i < 10000000; i++) {
n_array += new DataObj((int32)(1923912737 + i));
}
print("filled array\n");
mem_profile();
n_array = null;
Idle.add(quit);
}
private bool quit() {
print("quitting...\n");
loop.quit();
return false;
}
public static MainLoop loop;
public static int main() {
loop = new MainLoop(null, false);
//call g_mem_profile() when the application exits
Environment.atexit(mem_profile);
var t = new Test();
t.run();
loop.run();
return 0;
}
}
_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list