From owner-freebsd-hackers Thu Feb 22 23:20:39 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from harrier.prod.itd.earthlink.net (harrier.prod.itd.earthlink.net [207.217.121.12]) by hub.freebsd.org (Postfix) with ESMTP id C053C37B401 for ; Thu, 22 Feb 2001 23:20:36 -0800 (PST) (envelope-from fmela0@sm.socccd.cc.ca.us) Received: from sm.socccd.cc.ca.us (pool0451.cvx15-bradley.dialup.earthlink.net [209.179.45.196]) by harrier.prod.itd.earthlink.net (EL-8_9_3_3/8.9.3) with ESMTP id XAA13406; Thu, 22 Feb 2001 23:20:32 -0800 (PST) Message-ID: <3A961004.723691C9@sm.socccd.cc.ca.us> Date: Thu, 22 Feb 2001 23:23:48 -0800 From: Farooq Mela Reply-To: fmela0@sm.socccd.cc.ca.us X-Mailer: Mozilla 4.75 [en] (X11; U; FreeBSD 4.2-STABLE i386) X-Accept-Language: en MIME-Version: 1.0 To: Peter Seebach Cc: freebsd-hackers@freebsd.org Subject: Re: Setting memory allocators for library functions. References: <200102230627.f1N6Rk618467@guild.plethora.net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Peter Seebach wrote: > It is a mistake to believe that you "don't have to worry about running > out of memory". You should always check, every time, and exit as gracefully > as you can. > > -s Of course I realize that allocating memory can fail. That is why I use xmalloc and friends - so that I can avoid having to check for failure each time I want to allocate memory. I don't believe you understand what I am proposing. Consider the following inside libc: void *(*libc_malloc)(size_t)=malloc; void *(*libc_calloc)(size_t, size_t)=calloc; void *(*libc_realloc)(void *, size_t)=realloc; void set_allocator(void *(*func)(size_t)) { libc_malloc=func; } /* Same type of functions for calloc and realloc.. */ Now, consider some other function that is part of libc, such as getaddrinfo. Say it does the following: /* code ... */ foo=malloc(128); if (foo==NULL) { /* do clean-up.. */ errno=ENOMEM; return(NULL); } This would be replaced by: foo=(*libc_malloc)(128); if (foo=NULL) { /* do clean-up */ errno=ENOMEM; return(NULL); } It will still have to check for the allocation failing. But if this were to be done, an application which installs its own allocators will not have to worry about anything inside libc running out of memory, and will not have to handle that error condition. Furthermore, inside the user-defined allocator (xmalloc etc), other types of cleanup can be handled if the real malloc() returns NULL. (Atexit can only do so much.) Hope this clears it up. -Farooq To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message