Initialize a segment of persistant shared memory. Note that as shared memory is a system wide rather than an application wide resource, the rules concerning program Unix User ID and access permissions apply.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | hzShmem::Init | (const char*,uint32_t,uint32_t,) |
Declared in file: hzProcess.h
Defined in file : hzProcess.cpp
Function Logic:
Function body:
hzEcode hzShmem::Init (const char* name)uint32_t size, uint32_t mask,
{
// Category: System
//
// Initialize a segment of persistant shared memory.
//
// Arguments: 1) name Name of segment
// 2) size No of bytes
// 3) mask Access permissions
//
// Returns: E_INITDUP If shared memory segment is already initialized
// E_ARGUMENT If no name is supplied
// E_INITFAIL If the shared memory segment could not be set to the requested size
//
// Note that as shared memory is a system wide rather than an application wide resource, the rules concerning program Unix User ID and access permissions apply.
_hzfunc("hzShmem::Init") ;
// Test the instance is not already initialized
if (m_name)
{
hzerr(E_INITDUP, "Shared memory segment %s already initialized", *m_name) ;
return E_INITDUP ;
}
if (!name || !name[0])
{
hzerr(E_ARGUMENT, "No name supplied") ;
return E_ARGUMENT ;
}
m_name = name ;
m_size = ((size/4096)+1)*4096;
m_perm = mask ;
// Open shared memory
m_fd = shm_open(name, O_RDWR | O_CREAT, 0666);
threadLog("Set fd to %d\n", m_fd) ;
// Set to requested size
if (ftruncate(m_fd, m_size) != 0)
{
hzerr(E_INITFAIL, "Shared memory could not be sized to %d bytes", m_size) ;
return E_INITFAIL ;
}
threadLog("Set size to %d\n", m_size) ;
// Obtain pointer to start of segment
m_pStart = mmap(0,m_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
if (!m_pStart || m_pStart == (void*)-1)
{
hzerr(E_INITFAIL, "Shared memory address at %p errno is %d\n", m_pStart, errno) ;
return E_INITFAIL ;
}
threadLog("mem at %p\n", m_pStart) ;
return E_OK ;
}