world leader in high performance signal processing
Trace: » user_space_memory_allocation

User Space Memory Usage

Blackin's MMU provides limited memory protection, while it does not support virtual addressing. User application running on Blackfin uClinux uses memory almost the same way as other Linux system with virtual memory addressing. However, there some noticable differeces.

Memory protection on Blackfin

Allocate Memory

An application may dynamically request a block of memory, then free it.

Allocate memory in user space on NOMMU system

Here we are talking about implementation in Blackfin uClinux.

malloc

For most applications, memory is allocated using malloc() provided in uClibc.

uClibc provides several implementations of malloc(). The implementation can be chosen by configuring uClibc.

  • “toolchain/uClibc/libc/stdlib/malloc/”

This is currently the defautl malloc implementation. uClibc manages a memory heap. And the memory managed in the heap is allocated from kernel using mmap() (if not configured to use brk()):

 
toolchain/uClibc/libc/stdlib/malloc/malloc.c: malloc_from_heap():

- snip -
block = mmap ((void *)0, block_size, PROT_READ | PROT_WRITE,
                    MAP_SHARED | MAP_ANONYMOUS | MAP_UNINITIALIZE, 0, 0);
- snip -
  • “toolchain/uClibc/libc/stdlib/malloc-simple”

This implementation simply invokes mmap().

brk

In uClinux, brk() can only change mm→brk within the range of data segment which is fixed when program loading.

Memory allocation on MMU System

The system with MMU supports “demand paging”. When the process calls brk() system call or malloc() (which calls brk()), the kernel update the size of the heap memory region of the process. A page frame is assigned to the process only when it generates an exception by trying to refer this virtual address. The heap is the virtual address after the task's code and data space.

The user space application uses brk or sbrk to adjust the processes data segment size:

Extract from the brk man page

       **brk**  sets  the  end  of  the  data  segment  to the value specified by
       end_data_segment, when that value is reasonable, the system does  have
       enough  memory  and the process does not exceed its max data size (see
       setrlimit(2)).

       **sbrk** increments the program's data space  by  increment  bytes.   sbrk
       isn't  a  system  call,  it is just a C library wrapper.  Calling sbrk
       with an increment of 0 can be used to find the current location of the
       program break.

You can also get and release memory using malloc and free calls

#include <stdio.h>
#include <unistd.h>
 
 
// simple brk / sbrk / malloc test
int main ( int argc, char * argv[] )
{
    char * edata1;
    char * edata2;
    char * nedata;
    char * mdata;
 
    // find the end of the data segment
    edata1 = sbrk(0);
 
    // move the end of the data segment
    nedata = sbrk(1024);
 
    // find the new end of the data segment
    edata2 = sbrk(0);
 
    // reset the data segment end  back to where it was
    brk(edata1);
 
    printf(" brk/sbrk tests \n"
           " edata1 %p old end of data\n"
           " nedata %p new data area \n"
           " edata2 %p reset end of data\n",
           edata1,
           nedata,
           edata2 );
 
    // malloc free tests
 
    mdata = (char *)malloc(0x1000);
    printf(" malloc tests \n"
           " mdata %p malloc data\n"
           mdata);
 
    // free the data area
    free(mdata);
    return 0;
}

What the Blackfin uClinux kernel does

How to use memory in kernel space? How Blackfin uClinux kernel manages memory? Please refer to memory_allocation.

What a user space application needs to care about

Memory Fragmentation

Stack Overflow

Access hardware directly in user space?

Null pointer

vfork()

More info

Complete Table of Contents/Topics