This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Re: Patch: FYI: important memory-trashing fix
Mohan Embar writes:
>
> P.S. With my handy-dandy macro, here is an example of how
> gnu::awt::gtk::GtkButtonPeer::setLabel can be rewritten.
> (I'm typing this off the top of my head, so there may be
> slight errors):
>
> Current CVS Version:
>
> void
> gnu::awt::gtk::GtkButtonPeer::setLabel (java::lang::String *label)
> {
> _Jv_GdkThreadLock sync;
> jsize len = 0;
> if (label)
> len = JvGetStringUTFLength (label);
> char buf[len + 1];
> // FIXME: this can allocate an unbounded amount. Should use heap
> // even though it is slower.
> if (label)
> JvGetStringUTFRegion (label, 0, label->length(), buf);
> buf[len] = '\0';
> // The button child is a label.
> GtkBin *bin = GTK_BIN (ptr);
> gtk_label_set_text (GTK_LABEL (bin->child), buf);
> }
>
> Rewritten version:
>
> static const char*
> safe_cstr(const char* buf)
> {
> return buf ? buf : "";
> }
>
> void
> gnu::awt::gtk::GtkButtonPeer::setLabel (java::lang::String *label)
> {
> _Jv_GdkThreadLock sync;
> JV_TEMP_UTF_STRING(utflabel, label);
>
> // The button child is a label.
> GtkBin *bin = GTK_BIN (ptr);
> gtk_label_set_text (GTK_LABEL (bin->child), safe_cstr (utflabel));
> }
Why would you want to use a macro? Surely you could do
void
gnu::awt::gtk::GtkButtonPeer::setLabel (java::lang::String *label)
{
_Jv_GdkThreadLock sync;
Jv_temp_utf_string utflabel(label);
// The button child is a label.
GtkBin *bin = GTK_BIN (ptr);
gtk_label_set_text (GTK_LABEL (bin->child), safe_cstr (utflabel.data));
}
or somesuch.
Andrew.