Re: xmalloc string functions



[snips]

On Sun, 27 Jan 2008 10:24:59 -0500, Eric Sosman wrote:

The specifications are too weak to drive implementations.
What does xmalloc() do when unable to allocate memory?

Calls an optionally-user-specified function to figure out how to free up
some memory (eg opening a window to say "Please close another
application", which, of course, assumes there's enough memory to do so),
or, failing that, aborts the application.

Yes, that's right, it's a trap door function: you can enter, but you can
never leave, unless and until the memory is available. No error
reporting, no recovery, no options at all. That is the intent of the
code, as Malcolm has expressed more than once, that you *cannot* recover
from any allocation failure. You get the memory, or you exit. Period.

Does replace()
replace all occurrences, or just the first, or just the last, or what?
What does getquote() do with unbalanced quotes?

And does it do single quotes? Double quotes? Both?

I've think we've got something quite powerful here, purely because none
of these functions can ever return null for out of memory conditions.
It massively simplifies string handling.

... and massively complicates error handling.

No, no, it makes error handling *easier*, because you don't ever need to
*do* error handling. xmalloc does that for you - by ensuring you get the
memory, or the app aborts. See how wonderful it is? No need for all
that error handling code at the calling point. Also no way to ever do
anything like, oh, reduce the allocation request and try again, or
reschedule the operation for later, etc, etc, etc, but hey, it's not like
any application would ever actually do things like that.

If xmalloc()
can unilaterally terminate the program

Not "can", by design it is *required to*, unless the allocation
succeeds. One or the other: you get the memory, or the app dies.

the whole suite is unusable
except in toy programs.

Except it's of little use in toy apps. Such an app probably doesn't do a
hell of a lot of complex allocations in the first place, so wouldn't
benefit significantly from "easy malloc". A complex app where there may
be thousands or millions of allocations done from hundreds or thousands
of places in the code, now this would benefit from "easy malloc" - if it
were actually useful.

The xmalloc notion presented is of little interest in a toy app, and is
unusable in a real app. One wonders where, exactly, it would actually be
used. One place it won't be used: my code. I actually like to be able
to choose a course of action on allocation failure, not be stuck with
some asinine notion that allocation will succeed or there's no point in
continuing.

.



Relevant Pages

  • Re: When to check the return value of malloc
    ... code to handle the failure of every single allocation. ... most of the time the program will fail to get memory again ... meanwhile the app may be able to do other useful things. ... In other cases - such as the one I mentioned - an allocation failure is ...
    (comp.lang.c)
  • Re: How to release heap memory that is marked as free
    ... As I said, fragmentation is a very serious problem, and one of the most serious problems ... my allocator was accused of using massive amounts of memory. ... I'm going to have to re-think the memory allocation that I'm ... process's 'working set'. ...
    (microsoft.public.vc.mfc)
  • Re: [PATCH 00/28] Swap over NFS -v16
    ... memory they can consume. ... So we need the extra (skb) ... included in the reserve? ... if the allocation had to dip into emergency reserves, ...
    (Linux-Kernel)
  • Re: Memory leak with CAsyncSocket::Create
    ... read my essay on how storage allocators work. ... Create method is consuming system memory that is not released back to ... The memory consumption is either shown as "Mem Usage" on the Task ... many levels of allocation going ...
    (microsoft.public.vc.mfc)
  • Re: xmalloc string functions
    ... recover from any allocation failure. ... You get the memory, or you exit. ... A complex app where there may ... Or database apps where there isn't a robust memory recovery strategy in place - e.g. anything which uses an X server. ...
    (comp.lang.c)