J
jacob navia
Le 29/07/12 20:58, Keith Thompson a écrit :
We have discussed this to death but here we go again.
http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/
Examples – libraries
In this section, I present the OOM policies of a couple of well-known
libraries.
Glib
Glib is a cross platform utility library in C, used most notably for
GTK+. At first sight, Glib’s approach to memory allocation is flexible.
It provides two functions (with several variations):
g_malloc: attempts to allocate memory and exits with an error if the
allocation fails, using g_error [1]. This is the abort policy.
g_try_malloc: attempts to allocate memory and just returns NULL if that
fails, without aborting.
This way, Glib leaves the programmer the choice – you can choose the
policy. However, the story doesn’t end here. What does Glib use for its
own utilities? Let’s check g_array for instance. Allocation of a new
array is done by means of calling g_array_maybe_expand that uses
g_realloc, which is implemented with the same abort policy as g_malloc –
it aborts when the memory can’t be allocated.
Curiously, Glib isn’t consistent with this policy. Many modules use
g_malloc, but a couple (such as the gfileutils module) use g_try_malloc
and notify the caller on memory allocation errors.
So what do we have here? It seems that one of the most popular C
libraries out there uses the abort policy of memory allocations. Take
that into account when writing applications that make use of Glib – if
you’re planning some kind of graceful OOM recovery, you’re out of luck.
And as far as I can tell, glib has no function called "xmalloc".
Perhaps it was in an earlier version?
We have discussed this to death but here we go again.
http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/
Examples – libraries
In this section, I present the OOM policies of a couple of well-known
libraries.
Glib
Glib is a cross platform utility library in C, used most notably for
GTK+. At first sight, Glib’s approach to memory allocation is flexible.
It provides two functions (with several variations):
g_malloc: attempts to allocate memory and exits with an error if the
allocation fails, using g_error [1]. This is the abort policy.
g_try_malloc: attempts to allocate memory and just returns NULL if that
fails, without aborting.
This way, Glib leaves the programmer the choice – you can choose the
policy. However, the story doesn’t end here. What does Glib use for its
own utilities? Let’s check g_array for instance. Allocation of a new
array is done by means of calling g_array_maybe_expand that uses
g_realloc, which is implemented with the same abort policy as g_malloc –
it aborts when the memory can’t be allocated.
Curiously, Glib isn’t consistent with this policy. Many modules use
g_malloc, but a couple (such as the gfileutils module) use g_try_malloc
and notify the caller on memory allocation errors.
So what do we have here? It seems that one of the most popular C
libraries out there uses the abort policy of memory allocations. Take
that into account when writing applications that make use of Glib – if
you’re planning some kind of graceful OOM recovery, you’re out of luck.