Re: What to do when malloc() returns 0?

On Thu, 21 Jul 1994, Daniel W. Connolly wrote:
> Currently, a 0 return from malloc() is considered a fatal
> error throughout wwwlib. The error is reported to stderr,
> and the process exits.
> 
> For some applications, this is unsuitable. A user may
> be editing some data, and exiting in this manner will
> cause the data to be lost. As long as we're writing
> read-only tools like browsers, this isn't such a big deal.
> But as we start building annotation and full editing
> applications, it will become more and more of an issue.
> 
>	....
> 
> In summary, here are some proposals for what the library
> code should do when malloc() returns 0:
> 
>	....

How C++ handles this is of worth noting here.  Of course, we won't use 
C++, just consider what it does for usage in libwww.

First, when the new (malloc) operator fails to allocate memory, it calls 
the new_handler function (pointer to a function that you design specific 
to the client; default is to just exit() the app).

The new_handler's purpose is to attempt to free off one allocated 
piece of memory that it can and return; if it can't, then exit() the app.
  
In this manner, successive allocation attempts inside of new will occur 
constantly calling new_handler to free up more memory either until 
allocation succeeds or the application is exited.

In DosLynx, as an example, the new_handler when called due to a failure of
new allocating more memory (all memory requests are sent through new), the
scenario is as follows in a simpler C equivalent (fill in the gaps).  It's
a bit of reading, but well worth it to understand what I'm talking about. 



/*	Allocation handler should be client specific.
 */
/*	Overriding
 *	void (*new_handler)(void) = NULL;
 */
void (*new_handler)(void) = my_handler;

/*	Allocate some memory.
 */
void *new(size_t st_bytes)	{
	void *vp_alloced = NULL;

	while(vp_alloced == NULL)	{
		vp_alloced = malloc(st_bytes);
		if(vp_alloced == NULL)	{
			if(new_handler == NULL)	{
				/*	No new handler defined.
				 *	Exit only, nothing else!
				 */
				exit(-1);
			}
			new_handler();
		}
		else	{
			return(vp_alloced);
		}
	}
}

/*	Handle failure to allocate some memory.
 */
void my_handler(void)	{
	/*	Call another funciton to free some memory that isn't needed.
	 */
	if(free_off_some_memory() == FALSE)	{
		/*	Unable to free stuff off.
		 *	Have the application due some final shutdown code and
		 *		then exit.
		 */
		cleanup_before_exit();
		exit(-1);
	}

	/*	Success!
	 *	Just return, if enough wasn't freed off, we'll be called
	 *		again.
	 */
}

/*	Function to release spare memory.
 *	This is really client specific stuff....
 *	This is only an example.
 */
void free_off_some_memory(void)	{
	HText *to_be_free;

	to_be_free = getOldCachedDoc();

	if(to_be_free != NULL)	{
		HText_free(to_be_free);
	}
	else	{
		/*	Free other memory if possible here.
		 */
	}
}



Garrett.

Trodden Soil

I am trodden soil.
Dust covers my face.
Soles crush my nature
Revealing a hard empty space.

Garrett Arch Blythe  (913)864-0436
User Services Student Programmer/Consultant
University of Kansas Computer Center
<doslynx@falcon.cc.ukans.edu>

Received on Thursday, 21 July 1994 03:16:56 UTC