Date: Fri, 23 Feb 2001 06:14:42 -0500 (EST) From: Thomas David Rivers <rivers@dignus.com> To: fmela0@sm.socccd.cc.ca.us, freebsd-hackers@freebsd.org Subject: Re: Setting memory allocators for library functions. Message-ID: <200102231114.GAA03225@lakes.dignus.com> In-Reply-To: <3A95FF54.5D48E0A1@sm.socccd.cc.ca.us>
next in thread | previous in thread | raw e-mail | index | archive | help
Farooq Mela <fmela0@sm.socccd.cc.ca.us> wrote:
>
> Hi,
>
> Usually when I write programs, I have functions such as the following:
>
> void *
> xmalloc(size_t size)
> {
> <nice code>
> }
>
> void *
> xrealloc(void *ptr, size_t size)
> {
> <nice code>
> }
>
> And then I use these instead of malloc and realloc throughout the
> program, and never have to worry about return values. Sometimes these
> functions also have additional cleanup they perform. Anyway, there are
> certain libc functions which also use malloc, such as getaddrinfo,
> vasprintf, etc. These may fail with errno = ENOMEM. In general, it is
> annoying to have to check the return values for these functions too.
> Would it not be convenient to set the memory allocator used by certain
> functions inside libc? I.E, be able to write:
>
> set_allocator(xmalloc);
> set_reallocator(xrealloc);
>
> >From then on, one would never have to worry about the functions running
> out of memory. I know that wrappers for functions which allocate memory
> can also be written, but I feel that my proposal would is in general a
> more convenient solution.
>
> How do you guys feel about this?
>
> -Farooq
This would have probably been an outstanding idea when the
C standard was being put together... (and, who knows, somethine
similar may very well have been proposed.)
But, let me point out that adding such a feature to the FreeBSD
library doesn't mean you can dispense with your checks and/or
wrapping functions. As soon as your code needs to run on another
platform, you'll need those checks...
Such is the way of the world - when you have a standard, that's
all you can expect from other systems...
- Dave Rivers -
--
rivers@dignus.com Work: (919) 676-0847
Get your mainframe (370) `C' compiler at http://www.dignus.com
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200102231114.GAA03225>
