Directly allocating memory from a memory pool that was reserved by the startup program is safe, secure, and efficient.
In QNX Neutrino 7.1 or later, you can use the SHMCTL_TYMEM flag for shm_ctl()
to directly allocate memory from these pools.
The steps are as follows:
- Use
posix_typed_mem_open()
to open the memory pool.
- Use
shm_open()
to create a shared memory object.
- Use
shm_ctl()
to populate the object with memory from the pool.
For example:
// Open the graphics pool
int tymem_fd =
posix_typed_mem_open( "/graphics", O_RDWR,
POSIX_TYPED_MEM_ALLOCATE);
// Create a shared memory object
int shm_fd = shm_open(SHM_ANON, O_RDWR, 0600);
// Populate with 1 MB of mostly-contiguous memory
shm_ctl(shm_fd, SHMCTL_ANON | SHMCTL_TYMEM, tymem_fd, 0x100000);
A few things to note:
- If you specify SHMCTL_TYMEM, you must also specify SHMCTL_ANON.
When used without SHMCTL_PHYS, SHMCTL_ANON requests mostly contiguous memory.
- If you need contiguous memory, specify POSIX_TYPED_MEM_ALLOCATE_CONTIG when you call
posix_typed_mem_open().
You must then specify SHMCTL_PHYS in addition to
SHMCTL_ANON and SHMCTL_TYMEM when you call shm_ctl().
- If you specify SHMCTL_TYMEM, the paddr argument for shm_ctl()
must be the the file descriptor that posix_typed_mem_open() gave you for the typed memory pool.
Once you've called shm_ctl(), it isn't necessary to keep this file descriptor open.