Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 22 Feb 2001 22:12:36 -0800
From:      Farooq Mela <fmela0@sm.socccd.cc.ca.us>
To:        freebsd-hackers@FreeBSD.ORG
Subject:   Setting memory allocators for library functions.
Message-ID:  <3A95FF54.5D48E0A1@sm.socccd.cc.ca.us>

next in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3A95FF54.5D48E0A1>