//DESCRIPTION: Initalize the slab object. This is an abstraction for a contiguous block of objects that can be index into using //the id of the object. The slab works exclusively with mmap'd regions. Thus it takes a file descriptor and does an ftruncate if //it needs to resize the region. Also, the slab is a homogenous storage device, so it only stores the same type (size) of objects ObjectSlab * initObjectSlab(char * segmentName, void * initAddress, int sizeOfType, int slabIncrementSize){ ObjectSlab * slab = malloc(sizeof(ObjectSlab)); if (slab) { int sizeInBytes=0; openSharedMemory(slab,segmentName,&sizeInBytes); if (slab->shm_fd == -1){ perror("creating shared memory segment failed"); return NULL; } if (sizeInBytes == 0){ sizeInBytes=slabIncrementSize * sizeOfType + sizeof(ObjectSlabHeader); int result = ftruncate(slab->shm_fd, sizeInBytes); //make the file bigger if (result==-1){ perror("An error occured when trying to truncate a file"); return NULL; } } slab->objects = mapSharedMemory(slab, sizeInBytes, initAddress); if (!slab->objects){ perror("failed to map the segment into the address space"); return NULL; } slab->slabIncrementSize = slabIncrementSize; slab->sizeInObjects = (sizeInBytes - sizeof(ObjectSlabHeader))/sizeOfType; slab->sizeOfType = sizeOfType; } return slab; }
int SharedMemoryTracker::init() { OutputDebugString("Socket Tracker Init\n"); openSharedMemory(); return 0; }
/** * Creates/open a shared memory region * * The rootname will uniquely identify the shared memory region, * and is valid across different JVM instance. * * The shared memory region should persist across process, until OS reboots * or destroy call is being made. * * @param[in] portLibrary The port Library * @param[out] handle This handle is required for further attach/destroy of the memory region * @param[in] rootname Shared name for the region, which used to identify the region. * @param[in] size Size of the region in bytes * @param[in] perm permission for the region. * * @return * \arg HYPORT_ERROR_SHMEM_OPFAILED Failure - Cannot open the shared memory region * \arg HYPORT_INFO_SHMEM_OPENED Success - Existing memory region has been opened * \arg HYPORT_INFO_SHMEM_CREATED Success - A new shared memory region has been created * */ IDATA VMCALL hyshmem_open (HyPortLibrary * portLibrary, struct hyshmem_handle **handle, const char *rootname, I_32 size, I_32 perm) { /*TODO: Do we need the length to be longer? */ char controlFile[HYSH_MAXPATH]; IDATA retryCount, exist; key_t fkey; void *region; int retry = RETRY_COUNT; Trc_PRT_shmem_hyshmem_open_Entry (rootname, size, perm); if (ensureDirectory (portLibrary) == FAILED) { portLibrary->error_set_last_error (portLibrary, errno, HYPORT_ERROR_SHMEM_DATA_DIRECTORY_FAILED); Trc_PRT_shmem_hyshmem_open_Exit3 (); return HYPORT_ERROR_SHSEM_OPFAILED; } getControlFilePath (portLibrary, controlFile, HYSH_MAXPATH, rootname); while (retry) { I_32 rc; rc = portLibrary->file_attr (portLibrary, controlFile); if (HyIsFile != rc) { Trc_PRT_shmem_hyshmem_open_Event1 (controlFile); rc = createSharedMemory (portLibrary, handle, controlFile, size, perm); } else { Trc_PRT_shmem_hyshmem_open_Event2 (controlFile); rc = openSharedMemory (portLibrary, handle, controlFile); } switch (rc) { case RETRY: Trc_PRT_shmem_hyshmem_open_Event3 (retry); retry--; usleep (100); continue; case FAILED: Trc_PRT_shmem_hyshmem_open_Exit1 (); return HYPORT_ERROR_SHMEM_OPFAILED; default: Trc_PRT_shmem_hyshmem_open_Exit (rc, *handle); return rc; } } /* max number of retry count reach, return failure */ portLibrary->file_unlink (portLibrary, controlFile); Trc_PRT_shmem_hyshmem_open_Exit2 (); return HYPORT_ERROR_SHMEM_OPFAILED; }