idea: g_alloca_printf



hi,

a little brainer for everyone who fells like having a look at it.
At pattern that can be found in many glib based app is:

gchar *status=g_strdup_printf(_("Loading file \"%s\""),file_name);
g_object_set(G_OBJECT(self),"status",status,NULL);
g_free(status);

This has the advantage that it framents memory with small strings. When e.g. serializing xml it can matter. A solution is:

gchar *status=g_alloca(strlen(_("Loading file \"%s\""))+strlen(file_name)-1);
g_sprintf(status,_("Loading file \"%s\""),file_name);
g_object_set(G_OBJECT(self),"status",status,NULL);

This is using g_alloca(). A good side-effect is, that one can forget to free it ;) Unfortunately it not nice to write it this way.

The idea now is to wrap it all in a macro (needs to be a macro, as alloca only work localy). In the end I'd like to do:

g_object_set(G_OBJECT(self),
    "status",g_alloca_printf(_("Loading file \"%s\""),file_name),
    NULL);

First try:

#define g_alloca_printf(str,format,...) \
(sprintf((str=g_printf_upper_bound(format,args)),format,...),str)

* how to get from '...' to 'args'
* passing 'str' as a param is not nice

Any ideas folks?

Stefan



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]