malloc does not return null when out of memory

Steve O'Hara-Smith steve at sohara.org
Thu Jul 24 11:17:12 PDT 2003


On Thu, 24 Jul 2003 12:45:22 -0400
Barney Wolff <barney at databus.com> wrote:

BW> On Thu, Jul 24, 2003 at 11:15:43AM -0400, Chuck Swiger wrote:
BW> > 
BW> > +/* write a single byte per page to disable overcommit behavior */
BW> > +static int malloc_overcommit;
BW> 
BW> Won't this merely die in malloc, not return 0?

	Yes - it needs to trap the SEGV as well to produce the wanted
behaviour, something like:

+        else if (malloc_overcommit) {
+           ocsighandleron();
+           for (stride = 0; stride <= size; stride += malloc_pagesize) {
+             ((char *)result)[stride] = SOME_JUNK;
+             if (ocsignalled) {
+                ocsighandleroff();
+                return NULL;
+             }
+           }
+           ocsighandleroff();
+        }

	Where ocsighandleron() installs a SEGV handler that sets ocsignalled
when called and ocsighandleroff() installs the original one.

	BTW malloc_overcommit seems to be upside down, I left it that way.

-- 
C:>WIN                                      |     Directable Mirrors
The computer obeys and wins.                |A Better Way To Focus The Sun
You lose and Bill collects.                 |  licenses available - see:
                                            |   http://www.sohara.org/


More information about the freebsd-stable mailing list