Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is glib usable in an unobtrusive way?

I was looking for a good general-purpose library for C on top of the standard C library, and have seen several suggestions to use glib. How 'obtrusive' is it in your code? To explain what I mean by obtrusiveness, the first thing I noticed in the reference manual is the basic types section, thinking to myself, "what, am I going to start using gint, gchar, and gprefixing geverything gin gmy gcode gnow?"

More generally, can you use it only locally without other functions or files in your code having to be aware of its use? Does it force certain assumptions on your code, or constraints on your compilation/linking process? Does it take up a lot of memory in runtime for global data structures? etc.

like image 326
einpoklum Avatar asked Jul 03 '13 12:07

einpoklum


3 Answers

The most obtrustive thing about glib is that any program or library using it is non-robust against resource exhaustion. It unconditionally calls abort when malloc fails and there's nothing you can do to fix this, as the entire library is designed around the concept that their internal allocation function g_malloc "can't fail"

As for the ugly "g" types, you definitely don't need any casts. The types are 100% equivalent to the standard types, and are basically just cruft from the early (mis)design of glib. Unfortunately the glib developers lack much understanding of C, as evidenced by this FAQ:

Why use g_print, g_malloc, g_strdup and fellow glib functions?

"Regarding g_malloc(), g_free() and siblings, these functions are much safer than their libc equivalents. For example, g_free() just returns if called with NULL.

(Source: https://developer.gnome.org/gtk-faq/stable/x908.html)

FYI, free(NULL) is perfectly valid C, and does the exact same thing: it just returns.

like image 149
R.. GitHub STOP HELPING ICE Avatar answered Nov 08 '22 20:11

R.. GitHub STOP HELPING ICE


I have used GLib professionally for over 6 years, and have nothing but praise for it. It is very light-weight, with lots of great utilities like lists, hashtables, rand-functions, io-libraries, threads/mutexes/conditionals and even GObject. All done in a portable way. In fact, we have compiled the same GLib-code on Windows, OSX, Linux, Solaris, iOS, Android and Arm-Linux without any hiccups on the GLib side.

In terms of obtrusiveness, I have definitely "bought into the g", and there is no doubt in my mind that this has been extremely beneficial in producing stable, portable code at great speed. Maybe specially when it comes to writing advanced tests.

And if g_malloc don't suit your purpose, simply use malloc instead, which of course goes for all of it.

like image 33
Havard Graff Avatar answered Nov 08 '22 21:11

Havard Graff


Of course you can "forget about it elsewhere", unless of course those other places somehow interact with glib code, then there's a connection (and, arguable, you're not really "elsewhere").

You don't have to use the types that are just regular types with a prepended g (gchar, gint and so on); they're guaranteed to be the same as char, int and so on. You never need to cast to/from gint for instance.

I think the intention is that application code should never use gint, it's just included so that the glib code can be more consistent.

like image 1
unwind Avatar answered Nov 08 '22 19:11

unwind