Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Memory Allocation: (3 Items)
   
Memory Allocation  
Hello, 

I am currently developing driver for QMC-HDLC controller on freescale MPC8323E processor.

QMC-HDLC is controller, which grabs multi-channels Time Division Multiplexed data and stores them into memory buffers 
through buffer-descriptor for each channel. Buffer-descriptors contain status information, length of buffer and pointers
 to buffer into memory. With each buffer descriptor, one buffer of 2KB is associated.

As controller is directly accessing these memory buffers and buffer-descriptors, they have to be in physically 
contiguous space.
Therefore, my requirements for memory allocation

For Buffer-descriptors are to
1) Allocate physically contiguous memory of 128Kbytes for ALL buffer-descriptors. (256 * 64channels * 8Bytes = 128KB) 
2) Buffer-descriptor must be 64-bit aligned.
3) This allocated memory must not be shared or used by the any other process.

And for Buffers, to 
1) Allocate physically contiguous memory of 2KBytes for EACH buffer. (Total buffers = 256 * 64chan * 2KBytes = 32MBytes,
 here all 32MB need not to be in contiguous space only 2K is required)
2) This allocated memory must not be shared or used by the any other process.

Please help me if I want to allocate this kind of memory, which function should I use?

Application will also access these buffers to read the received data and vice versa.

Thank you very much
Re: Memory Allocation  
Hi,

you can do both allocations using mmap(). To allocate physically contiguous memory, you must specify flags MAP_PHYS and 
MAP_ANON and NOFD as the file descriptor, so for your buffer descriptors that'd be:

  descriptors = mmap( NULL, 256*64*8,
                                   PROT_READ|PROT_WRITE|PROT_NOCACHE, 
                                   MAP_PHYS|MAP_ANON, NOFD, 0 );

Since mmap allocations are done in full RAM pages of 4k each, you won't need to worry about the alignment of your 
descriptors or your buffers. Even better: Your buffers won't need to be allocated physically contiguous, as 2 buffers 
will just fit into one page. So here, a 'simple' allocation is sufficient:
  bufs = mmap( NULL, 256*64*2048, 
                         PROT_READ|PROT_WRITE|PROT_NOCACHE, 
                         MAP_ANON, NOFD, 0 );

Hope this helps,
- Thomas
Re: Memory Allocation  
> Hi,
> 
> you can do both allocations using mmap(). To allocate physically contiguous 
> memory, you must specify flags MAP_PHYS and MAP_ANON and NOFD as the file 
> descriptor, so for your buffer descriptors that'd be:
> 
>   descriptors = mmap( NULL, 256*64*8,
>                                    PROT_READ|PROT_WRITE|PROT_NOCACHE, 
>                                    MAP_PHYS|MAP_ANON, NOFD, 0 );
> 
> Since mmap allocations are done in full RAM pages of 4k each, you won't need 
> to worry about the alignment of your descriptors or your buffers. Even better:
>  Your buffers won't need to be allocated physically contiguous, as 2 buffers 
> will just fit into one page. So here, a 'simple' allocation is sufficient:
>   bufs = mmap( NULL, 256*64*2048, 
>                          PROT_READ|PROT_WRITE|PROT_NOCACHE, 
>                          MAP_ANON, NOFD, 0 );
> 
> Hope this helps,
> - Thomas


Hi,

Thank you for fast reply

I looked at the mmap() library function reference. Three are two flags MAP_PRIVATE and MAP_SHARED.

MAP_SHARED
The mapping may be shared by many processes.

MAP_PRIVATE
The mapping is private to the calling process. It allocates system RAM and copies the current object.


1) As I don't want memory space to be shared so definitely I will not use MAP_SHARED.
2) But should I use MAP_PRIVATE? Cause it says, it allocates system RAM and "copies" the current object. 

Actually Buffer and buffer-descriptors can be modified by hardware any-time.

Thank You