static _shm_queue_t* _shm_queue_create(const char* shm_path, size_t shm_size, int* shm_fd) { struct _shm_queue* q; struct stat st; off_t mmap_size = ROUND_TO_PAGESIZE(shm_size + sizeof(struct _shm_queue)); int fd = shm_open(shm_path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if(-1 == (*shm_fd = fd)) return NULL; fstat(fd, &st); if(0 == st.st_size) ftruncate(fd,mmap_size); else mmap_size = st.st_size; q = mmap(0,mmap_size,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0); if(MAP_FAILED == q) { printf("mmap failed: %s\n",strerror(errno)); return NULL; } q->allocated = mmap_size; return q; }
int eMPEGStreamInformation::loadCache(int index) { //eDebug("[eMPEGStreamInformation::loadCache] index=%d", index); if (m_structure_cache != NULL) { //eDebug("[eMPEGStreamInformation] munmap %p size %d index %d", m_structure_cache, m_structure_cache_entries * entry_size, m_cache_index); ::munmap(m_structure_cache, m_structure_cache_entries * entry_size); m_structure_cache = NULL; m_structure_cache_entries = 0; m_cache_index = -1; } off_t where = ROUND_TO_PAGESIZE(index * entry_size); off_t until = ::lseek(m_structure_read_fd, 0, SEEK_END); size_t bytes; if (where + MAPSIZE <= until) { bytes = MAPSIZE; } else { if (index * entry_size >= until) { eDebug("[eMPEGStreamInformation] index %d is past EOF", index); return 0; } bytes = (size_t)(until-where); if (bytes == 0) { eDebug("[eMPEGStreamInformation] mmap file is empty"); return 0; } } //eDebug("[eMPEGStreamInformation] mmap offset=%lld size %d", where, bytes); m_structure_cache = (unsigned long long*) ::mmap(NULL, bytes, PROT_READ, MAP_SHARED, m_structure_read_fd, where); if (m_structure_cache == NULL) { eDebug("[eMPEGStreamInformation] failed to mmap cache: %m"); m_cache_index = -1; m_structure_cache_entries = 0; return -1; } m_cache_index = (int)(where / entry_size); //eDebug("[eMPEGStreamInformation] cache index %d starts at %d (%lld) bytes: %d", index, m_cache_index, where, bytes); int num = (int)bytes / entry_size; m_structure_cache_entries = num; return num; }