next up previous contents
Next: Reference of C stdio Up: Reference of Dynamic Memory Previous: Creating Heaps

Standard C/C++ Memory Management

UCR includes in the core library the C standard memory management routines malloc and free. The heap functions described above, together with a default segment of heap memory, implement these functions. uCR also protects them (using thread_splhi) from interrupts and threads, so the C routines may be used in interrupt handlers.

The ucr_start function creates the default heap using the end and end_heap linker symbols. The whole default heap is passed to heap_init and the HEAP_SPACE created is saved in a private variable that malloc and free can access. This initialization happens before any static initializers are called, so the default heap is available at all times.

The C++ allocation operators also take memory from the default heap and are also protected from interrupts and threads. The <ucr/heap.h> header file adds the following C++ operators:

void* operator new (size_t size, HEAP_SPACE*space);
void* operator new[] (size_t size, HEAP_SPACE*space);

that can be used to allocate memory from specific heaps.


 
Figure 3: Basic Heap Allocation with C++
\begin{figure}
\begin{center}
\begin{verbatim}
 ...

The previous example of heap_alloc can be rewritten as in Figure . The delete and delete[] operators work on memory allocated using these special operators. All the forms of new and delete provided by uCR are thread-safe.


next up previous contents
Next: Reference of C stdio Up: Reference of Dynamic Memory Previous: Creating Heaps
Stephen Williams
9/2/1997