From owner-freebsd-hackers Fri Feb 23 3:13:14 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from fjord.dignus.com (sdsl-64-244-29-38.dsl.rdu.megapath.net [64.244.29.38]) by hub.freebsd.org (Postfix) with ESMTP id 46CA437B491 for ; Fri, 23 Feb 2001 03:13:11 -0800 (PST) (envelope-from rivers@dignus.com) Received: from lakes.dignus.com (lakes.dignus.com [10.0.0.3]) by fjord.dignus.com (8.11.1/8.11.1) with ESMTP id f1NBEZn41491; Fri, 23 Feb 2001 06:14:35 -0500 (EST) (envelope-from rivers@dignus.com) Received: (from rivers@localhost) by lakes.dignus.com (8.9.3/8.6.9) id GAA03225; Fri, 23 Feb 2001 06:14:42 -0500 (EST) Date: Fri, 23 Feb 2001 06:14:42 -0500 (EST) From: Thomas David Rivers Message-Id: <200102231114.GAA03225@lakes.dignus.com> To: fmela0@sm.socccd.cc.ca.us, freebsd-hackers@freebsd.org Subject: Re: Setting memory allocators for library functions. In-Reply-To: <3A95FF54.5D48E0A1@sm.socccd.cc.ca.us> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Farooq Mela wrote: > > Hi, > > Usually when I write programs, I have functions such as the following: > > void * > xmalloc(size_t size) > { > > } > > void * > xrealloc(void *ptr, size_t size) > { > > } > > 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