Revision 77903 of Infallible memory allocation

  • Revision slug: Infallible_memory_allocation
  • Revision title: Infallible memory allocation
  • Revision id: 77903
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment new page; page created, 212 words added

Revision Content

{{ gecko_minversion_header("1.9.3") }}

There's ongoing work to implement infallible memory allocators. These are memory allocation routines that never return null; that is, they always successfully return the requested block of memory. This is in contrast to a traditional, fallible, memory allocator that can return null indicating that the request failed.

Currently, while code is transitioned to be compatible with infallible memory allocators, you have to explicitly decide whether to use infallible allocation or not. Eventually, however, the generic memory allocators (malloc() and NS_Alloc())will become infallible.

Explicitly infallible memory allocators

The following memory allocators are explicitly infallible, and will remain so; they are guaranteed to return a valid memory pointer.

p = moz_xmalloc();
f = new Foo();
fArray = new Foo[];
n = ::operator new(...);
nArray = ::operator new[](...);

Explicitly fallible memory allocators

These memory allocators are explicitly fallible, and will remain so. When using these, you must check to ensure the resulting value isn't null before attempting to use the returned memory.

p = moz_malloc();
namespace mozilla {
  f = new (fallible_t()) Foo();
  fArray = new (fallible_t()) Foo[];
}
namespace mozilla {
  n = ::operator new(fallible_t(), ...);
  nArray = ::operator new[](fallible_t(), ...);
}

Eventually infallible memory allocators

The following memory allocators are currently fallible, but will become infallible in the relatively near future:

p = malloc();
p = NS_Alloc(); 

See also

Revision Source

<p>{{ gecko_minversion_header("1.9.3") }}</p>
<p>There's ongoing work to implement <strong>infallible memory allocators</strong>. These are memory allocation routines that never return <code>null</code>; that is, they always successfully return the requested block of memory. This is in contrast to a traditional, fallible, memory allocator that can return <code>null</code> indicating that the request failed.</p>
<p>Currently, while code is transitioned to be compatible with infallible memory allocators, you have to explicitly decide whether to use infallible allocation or not. Eventually, however, the generic memory allocators (<code>malloc()</code> and <code>NS_Alloc()</code>)will become infallible.</p>
<h2>Explicitly infallible memory allocators</h2>
<p>The following memory allocators are explicitly infallible, and will remain so; they are guaranteed to return a valid memory pointer.</p>
<pre>p = moz_xmalloc();
</pre>
<pre>f = new Foo();
fArray = new Foo[];
</pre>
<pre>n = ::operator new(...);
nArray = ::operator new[](...);
</pre>
<h2>Explicitly fallible memory allocators</h2>
<p>These memory allocators are explicitly fallible, and will remain so. When using these, you must check to ensure the resulting value isn't <code>null</code> before attempting to use the returned memory.</p>
<pre>p = moz_malloc();
</pre>
<pre>namespace mozilla {
  f = new (fallible_t()) Foo();
  fArray = new (fallible_t()) Foo[];
}
</pre>
<pre>namespace mozilla {
  n = ::operator new(fallible_t(), ...);
  nArray = ::operator new[](fallible_t(), ...);
}
</pre>
<h2>Eventually infallible memory allocators</h2>
<p>The following memory allocators are currently fallible, but will become infallible in the relatively near future:</p>
<pre>p = malloc();
p = NS_Alloc(); 
</pre>
<h2>See also</h2>
<ul> <li><a href="/en/Choosing_the_right_memory_allocator" title="en/Choosing the right memory allocator">Choosing the right memory allocator</a></li>
</ul>
Revert to this revision