Re: [patch] calling g_malloc & co via a vtable



Hi Havoc,

> > 2) Implement an out-of-memory callback: What will happen? A
> > g_list_.. function will call g_malloc, no memory is available, the
> > callback is called, your code there decides to free a cache (being a
> > GList), but the main GList mutex will be locked. Bummer. In general
> > the internal state will not be consistent during a call to malloc,
> > so allowing other calls from there will be a nightmare.
> >
> 
> People can consider these issues and deal with them.

Yes, but what about the other problems:

1) allocating memory under linux doesn't guarantee, that you really can access
   it, the process might be killed, whenever memory is tight.
2) you can not use other memory allocating functions (which don't use g_malloc
   etc.) and that for example also means, you can't use XLib.
3) Your program might run out of stack as well.

> > BTW.: How do you implement realloc without knowing the previous allocated
> > size?
> >
> 
> Typically the malloc implementation stores the size in the allocated
> block.

Yeah, right, but if you implement a fallback for malloc for the out-of-mem
case, you have to save that information. Following scenario:

a = g_malloc (42);
/* here we run out of memory and start our own fallback implementation, that 
   uses some preallocated memory */
a = g_remalloc (a, 44); /* Here we have to know the old size (42), which we
  can't access (in general) as it is saved by the native implementation, so
we   have to save it ourselves for every allocation -> bloat */

So we can't use fallbacks sanely. What remains? 
* An out-of-mem callback, which is not good, because unreliable.
* Wait sometime for more memory to come. This might in fact be an option for
  important processes (gnome-session etc.), but there you would also need to
  make sure, that all XLib calls get the same thing. So you have to 'overload'
  malloc and free directly, as supported on the majority of platforms
already   (see electric-fence and friends)

Talking about X, I think, they didn't include malloc vtables for good reason
even back in the days of tight memory.

Bye,
Sebastian
-- 
Sebastian Wilhelmi                   |            här ovanför alla molnen
mailto:wilhelmi ira uka de           |     är himmlen så förunderligt blå
http://goethe.ira.uka.de/~wilhelmi   |




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