From owner-freebsd-hackers Thu Feb 22 22: 9:29 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from scaup.prod.itd.earthlink.net (scaup.prod.itd.earthlink.net [207.217.121.49]) by hub.freebsd.org (Postfix) with ESMTP id 76ABF37B401 for ; Thu, 22 Feb 2001 22:09:26 -0800 (PST) (envelope-from fmela0@sm.socccd.cc.ca.us) Received: from sm.socccd.cc.ca.us (pool0482.cvx4-bradley.dialup.earthlink.net [209.178.147.227]) by scaup.prod.itd.earthlink.net (EL-8_9_3_3/8.9.3) with ESMTP id WAA07244 for ; Thu, 22 Feb 2001 22:09:21 -0800 (PST) Message-ID: <3A95FF54.5D48E0A1@sm.socccd.cc.ca.us> Date: Thu, 22 Feb 2001 22:12:36 -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: freebsd-hackers@FreeBSD.ORG Subject: Setting memory allocators for library functions. Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Hi, Usually when I write programs, I have functions such as the following: void * xmalloc(size_t size) { void *p; if ((p=malloc(size))==NULL) { fprintf(stderr, "out of memory\n"); exit(1); } return(p); } void * xrealloc(void *ptr, size_t size) { void *p; if ((p=realloc(ptr,size))==NULL) { fprintf(stderr, "out of memory\n"); exit(1); } return(p); } 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 To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message